import { auth, logout, getLibraries, getLibraryItems } from '../embyApi.js'; import { listActiveParties, createParty, joinParty } from '../party.js'; import { navigate } from '../router.js'; export function renderBrowse(root) { root.innerHTML = `

Watch Party

Watch parties en cours

Chargement...

`; root.querySelector('#logout').addEventListener('click', async () => { await logout(); navigate('/login'); }); 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); loadLibraries(root); } async function loadParties(root) { let parties; try { parties = await listActiveParties(); } catch (err) { return; // non-critical for the page as a whole - just skip the section } if (!parties.length) return; const section = root.querySelector('#partiesSection'); section.classList.remove('hide'); root.querySelector('#partiesList').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 loadLibraries(root) { const tabsEl = root.querySelector('#tabs'); const contentEl = root.querySelector('#tabContent'); let libraries; try { libraries = await getLibraries(); } catch (err) { contentEl.innerHTML = '

Impossible de charger les bibliothèques Emby.

'; return; } if (!libraries.length) { contentEl.innerHTML = '

Aucune bibliothèque disponible sur ce compte.

'; return; } tabsEl.innerHTML = libraries .map((lib, i) => ``) .join(''); tabsEl.querySelectorAll('.tab').forEach((tab) => { tab.addEventListener('click', () => { if (tab.classList.contains('active')) return; tabsEl.querySelectorAll('.tab').forEach((t) => t.classList.remove('active')); tab.classList.add('active'); loadLibraryItems(contentEl, tab.dataset.libraryId); }); }); loadLibraryItems(contentEl, libraries[0].id); } async function loadLibraryItems(contentEl, libraryId) { contentEl.innerHTML = '

Chargement...

'; let items; try { items = await getLibraryItems(libraryId); } catch (err) { contentEl.innerHTML = '

Impossible de charger cette bibliothèque.

'; return; } if (!items.length) { contentEl.innerHTML = '

Bibliothèque vide.

'; return; } contentEl.innerHTML = items.map((item) => `
${item.canWatchParty ? `` : ''} Voir sur Emby
${escapeHtml(item.name)}${item.year ? ` (${item.year})` : ''}
`).join(''); contentEl.querySelectorAll('.startPartyBtn').forEach((btn) => { btn.addEventListener('click', async () => { btn.disabled = true; btn.textContent = 'Démarrage...'; try { const party = await createParty(btn.dataset.itemId); navigate(`/party/${party.partyId}`); } catch (err) { alert(err.message); btn.disabled = false; btn.textContent = 'Lancer une watch party'; } }); }); } function escapeHtml(str) { const div = document.createElement('div'); div.textContent = str; return div.innerHTML; }