import { getLibraries, getRecentItems } from '../embyApi.js'; import { listActiveParties, createParty, joinParty } from '../party.js'; import { navigate } from '../router.js'; import { tileHtml, wireTileButtons, escapeHtml } from '../tiles.js'; export function renderHome(root) { root.innerHTML = `

`; root.querySelector('#joinBtn').addEventListener('click', async () => { const code = root.querySelector('#joinCode').value.trim(); if (!code) return; try { const party = await joinParty(code); navigate(`/party/${party.partyId}`); } catch (err) { root.querySelector('#joinError').textContent = err.message; } }); loadParties(root); loadRecentSections(root); } async function loadParties(root) { let parties; try { parties = await listActiveParties(); } catch (err) { return; // non-critical - the rest of the home page still works } if (!parties.length) return; root.querySelector('#partiesSection').hidden = false; root.querySelector('#partiesCarousel').innerHTML = parties.map((p) => `
${escapeHtml(p.itemName || '')} ${escapeHtml(p.hostUserName || '')} · ${p.memberCount} participant${p.memberCount > 1 ? 's' : ''}
`).join(''); root.querySelectorAll('.joinPartyBtn').forEach((btn) => { btn.addEventListener('click', () => navigate(`/party/${btn.dataset.partyId}`)); }); } async function loadRecentSections(root) { const container = root.querySelector('#recentSections'); let libraries; try { libraries = await getLibraries(); } catch (err) { container.innerHTML = '

Impossible de charger les bibliothèques Emby.

'; return; } for (const lib of libraries) { if (lib.collectionType === 'music') continue; // music gets its own experience later, not a poster row const section = document.createElement('section'); section.className = 'section'; section.innerHTML = `

Ajouts récents - ${escapeHtml(lib.name)}

`; container.appendChild(section); getRecentItems(lib.id) .then((items) => { if (!items.length) { section.remove(); return; } const carousel = section.querySelector('.carousel'); carousel.innerHTML = items.map(tileHtml).join(''); wireTileButtons(carousel, async (itemId) => { const party = await createParty(itemId); navigate(`/party/${party.partyId}`); }); }) .catch(() => section.remove()); } }