Files
OmniArr/src/App.tsx
T

1862 lines
65 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
type CSSProperties,
type FormEvent,
type ReactNode,
startTransition,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react'
import { api } from './api'
import {
defaultConfig,
type AppConfig,
type AppRoute,
type ConnectionStatus,
type DownloadItem,
type HubPageResponse,
type MediaCard,
type SelectOption,
type ServiceOptionsResponse,
type TitleDetailsResponse,
} from '../shared/contracts'
type AuthState = 'loading' | 'setup' | 'login' | 'app'
type EntryStage = 'onboarding' | 'profiles' | 'dashboard'
const MAX_CACHED_HUB_PAGES = 3
type ModalOriginRect = { left: number; top: number; width: number; height: number }
type SelectedCardState = { card: MediaCard; originRect: ModalOriginRect | null }
const sidebarItems: Array<{ route: AppRoute; label: string; short: string }> = [
{ route: 'home', label: 'Inicio', short: 'H' },
{ route: 'dorama', label: 'K-Drama', short: 'K' },
{ route: 'anime', label: 'Anime', short: 'A' },
{ route: 'library', label: 'Library', short: 'L' },
{ route: 'downloads', label: 'Downloads', short: 'D' },
]
const networkSidebarItems: Array<{ route: AppRoute; label: string; icon: string; iconClassName?: string }> = [
{ route: 'netflix', label: 'Netflix', icon: '/networks/netflix.png' },
{ route: 'primevideo', label: 'Prime Video', icon: '/networks/prime.svg' },
{ route: 'hbo', label: 'HBO', icon: '/networks/hbo.svg', iconClassName: 'sidebar-network-icon-image white-network-icon' },
{ route: 'viki', label: 'Viki', icon: '/networks/viki.png' },
{ route: 'disney', label: 'Disney+', icon: '/networks/disney.webp' },
]
function App() {
const [authState, setAuthState] = useState<AuthState>('loading')
const [entryStage, setEntryStage] = useState<EntryStage>('profiles')
const [route, setRoute] = useState<AppRoute>('home')
const [busy, setBusy] = useState(false)
const [jellyfinUsersBusy, setJellyfinUsersBusy] = useState(false)
const [error, setError] = useState('')
const [notice, setNotice] = useState('')
const [password, setPassword] = useState('')
const [config, setConfig] = useState<AppConfig>(defaultConfig)
const [options, setOptions] = useState<ServiceOptionsResponse>({
jellyfinUsers: [],
sonarrRootFolders: [],
sonarrQualityProfiles: [],
sonarrLanguageProfiles: [],
radarrRootFolders: [],
radarrQualityProfiles: [],
})
const [downloads, setDownloads] = useState<DownloadItem[]>([])
const [connections, setConnections] = useState<ConnectionStatus[]>([])
const [hubPages, setHubPages] = useState<Partial<Record<HubPageResponse['page'], HubPageResponse>>>({})
const [hubLoading, setHubLoading] = useState(false)
const [selectedCard, setSelectedCard] = useState<SelectedCardState | null>(null)
const [downloadDeleteId, setDownloadDeleteId] = useState('')
async function refreshBaseData() {
const [settings, serviceOptions, dashboard] = await Promise.all([
api.getSettings(),
api.getOptions(),
api.getDashboard(),
])
setConfig(settings)
setOptions(serviceOptions)
setDownloads(dashboard.downloads)
setConnections(dashboard.connections)
setEntryStage(resolveEntryStage(settings, serviceOptions))
}
async function loadHub(page: HubPageResponse['page'], options?: { silent?: boolean }) {
if (!options?.silent) {
setHubLoading(true)
}
try {
const hub = await api.getHub(page)
setHubPages((current) => cacheHubPage(current, page, hub))
} catch (loadError) {
setError(loadError instanceof Error ? loadError.message : 'Failed to load this section.')
} finally {
if (!options?.silent) {
setHubLoading(false)
}
}
}
useEffect(() => {
let cancelled = false
async function boot() {
try {
const session = await api.getSession()
if (cancelled) return
if (session.needsSetup) {
setAuthState('setup')
return
}
if (!session.authenticated) {
setAuthState('login')
return
}
await refreshBaseData()
if (!cancelled) {
setAuthState('app')
}
} catch (bootError) {
if (!cancelled) {
setError(bootError instanceof Error ? bootError.message : 'Failed to start OmniArr.')
setAuthState('login')
}
}
}
void boot()
return () => {
cancelled = true
}
}, [])
useEffect(() => {
if (authState !== 'app' || entryStage !== 'dashboard') {
return
}
if (route === 'downloads' || route === 'settings') {
return
}
const page = route as HubPageResponse['page']
let cancelled = false
setHubLoading(true)
void api.getHub(page)
.then((hub) => {
if (cancelled) return
setHubPages((current) => cacheHubPage(current, page, hub))
})
.catch((loadError) => {
if (cancelled) return
setError(loadError instanceof Error ? loadError.message : 'Failed to load this section.')
})
.finally(() => {
if (cancelled) return
setHubLoading(false)
})
return () => {
cancelled = true
}
}, [authState, entryStage, route])
useEffect(() => {
if (authState !== 'app' || entryStage !== 'dashboard') {
return
}
const interval = window.setInterval(() => {
void refreshBaseData()
if (route !== 'downloads' && route !== 'settings') {
void loadHub(route as HubPageResponse['page'], { silent: true })
}
}, 30000)
return () => window.clearInterval(interval)
}, [authState, entryStage, route])
async function handleAuthSubmit() {
setBusy(true)
setError('')
try {
if (authState === 'setup') {
await api.register({ password })
} else {
await api.login({ password })
}
await refreshBaseData()
setPassword('')
setNotice(authState === 'setup' ? 'Instance initialized.' : 'Signed in.')
setAuthState('app')
} catch (authError) {
setError(authError instanceof Error ? authError.message : 'Authentication failed.')
} finally {
setBusy(false)
}
}
async function handleLoadJellyfinUsers() {
setJellyfinUsersBusy(true)
setError('')
setNotice('')
try {
const response = await api.loadJellyfinUsers({
baseUrl: config.jellyfin.baseUrl,
apiKey: config.jellyfin.apiKey,
})
setOptions((current) => ({ ...current, jellyfinUsers: response.jellyfinUsers }))
setEntryStage(response.jellyfinUsers.length ? 'profiles' : 'onboarding')
setNotice(response.jellyfinUsers.length ? 'Jellyfin users loaded.' : 'No Jellyfin users found.')
} catch (loadError) {
setError(loadError instanceof Error ? loadError.message : 'Failed to load Jellyfin users.')
} finally {
setJellyfinUsersBusy(false)
}
}
async function handleSelectJellyfinUser(userId: string) {
setBusy(true)
setError('')
setNotice('')
try {
const nextConfig = {
...config,
jellyfin: {
...config.jellyfin,
userId,
},
}
const response = await api.saveSettings(nextConfig)
setConfig(response.config)
await refreshBaseData()
setEntryStage('dashboard')
setNotice('Profile selected.')
setRoute('home')
} catch (selectError) {
setError(selectError instanceof Error ? selectError.message : 'Failed to select Jellyfin profile.')
} finally {
setBusy(false)
}
}
async function handleSaveSettings(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
setBusy(true)
setError('')
setNotice('')
try {
const response = await api.saveSettings(config)
setConfig(response.config)
await refreshBaseData()
setNotice('Settings saved and services refreshed.')
} catch (saveError) {
setError(saveError instanceof Error ? saveError.message : 'Failed to save settings.')
} finally {
setBusy(false)
}
}
async function handleRequest(card: MediaCard) {
if (!card.tmdbId) return
setBusy(true)
setError('')
setNotice('')
try {
if (card.mediaType === 'movie') {
await api.requestMovie(card.tmdbId)
} else {
await api.requestSeries(card.tmdbId)
}
if (route !== 'downloads' && route !== 'settings') {
await loadHub(route as HubPageResponse['page'], { silent: true })
}
setNotice(`${card.title} sent to ${card.mediaType === 'movie' ? 'Radarr' : 'Sonarr'}.`)
} catch (requestError) {
setError(requestError instanceof Error ? requestError.message : 'Failed to send request.')
} finally {
setBusy(false)
}
}
async function handleDeleteDownload(id: string) {
setDownloadDeleteId(id)
setError('')
setNotice('')
try {
await api.deleteDownload({ id })
const nextDownloads = await api.getDownloads()
setDownloads(nextDownloads)
setNotice('Download removed from the queue.')
} catch (deleteError) {
setError(deleteError instanceof Error ? deleteError.message : 'Failed to remove the download.')
} finally {
setDownloadDeleteId('')
}
}
function updateConfig<K extends keyof AppConfig>(key: K, value: AppConfig[K]) {
setConfig((current) => ({
...current,
[key]: value,
}))
}
if (authState === 'loading') {
return <LoadingScreen />
}
if (authState === 'setup' || authState === 'login') {
return (
<AuthScreen
busy={busy}
error={error}
mode={authState}
onSubmit={handleAuthSubmit}
password={password}
setPassword={setPassword}
/>
)
}
if (entryStage === 'onboarding') {
return (
<JellyfinOnboardingScreen
apiKey={config.jellyfin.apiKey}
baseUrl={config.jellyfin.baseUrl}
busy={jellyfinUsersBusy}
error={error}
notice={notice}
onApiKeyChange={(apiKey) =>
updateConfig('jellyfin', { ...config.jellyfin, apiKey })
}
onBaseUrlChange={(baseUrl) =>
updateConfig('jellyfin', { ...config.jellyfin, baseUrl })
}
onLoadUsers={handleLoadJellyfinUsers}
users={options.jellyfinUsers}
/>
)
}
if (entryStage === 'profiles') {
return (
<ProfileSelectionScreen
busy={busy}
currentUserId={config.jellyfin.userId}
error={error}
notice={notice}
onBackToOnboarding={() => setEntryStage('onboarding')}
onSelectUser={handleSelectJellyfinUser}
users={options.jellyfinUsers}
/>
)
}
const currentJellyfinUser =
options.jellyfinUsers.find((option) => option.id === config.jellyfin.userId)?.label || 'Media cockpit'
const currentHub = route !== 'downloads' && route !== 'settings' ? hubPages[route as HubPageResponse['page']] : undefined
return (
<div className="omniarr-app">
<Sidebar
busy={busy}
connections={connections}
currentUser={currentJellyfinUser}
onExitProfile={() => setEntryStage('profiles')}
route={route}
setRoute={setRoute}
/>
<div className="content-shell">
<main className="content-panel">
<div className="content-scroll">
<div className="content-status">
{notice ? <p className="notice">{notice}</p> : null}
{error ? <p className="error-banner">{error}</p> : null}
</div>
{route === 'downloads' ? <DownloadsPage busyId={downloadDeleteId} downloads={downloads} onDelete={handleDeleteDownload} /> : null}
{route === 'settings' ? (
<SettingsView
busy={busy}
config={config}
connections={connections}
jellyfinUsersBusy={jellyfinUsersBusy}
onLoadJellyfinUsers={handleLoadJellyfinUsers}
onSave={handleSaveSettings}
options={options}
updateConfig={updateConfig}
/>
) : null}
{route !== 'downloads' && route !== 'settings' ? (
<HubPageView
busy={busy}
hub={currentHub}
loading={hubLoading}
onOpenCard={(card, originRect) => setSelectedCard({ card, originRect })}
onRequest={handleRequest}
route={route}
tmdbToken={config.tmdb.bearerToken}
/>
) : null}
</div>
</main>
</div>
{selectedCard ? (
<TitleDetailsModal
card={selectedCard.card}
config={config}
onClose={() => setSelectedCard(null)}
originRect={selectedCard.originRect}
onRequest={handleRequest}
/>
) : null}
</div>
)
}
function Sidebar(props: {
route: AppRoute
setRoute: (route: AppRoute) => void
currentUser: string
connections: ConnectionStatus[]
busy: boolean
onExitProfile: () => void
}) {
const hasServiceFailure = props.connections.some((connection) => !connection.ok)
return (
<aside className="omniarr-sidebar">
<div className="sidebar-wallpaper" />
<div className="sidebar-inner">
<button className="sidebar-profile" onClick={() => startTransition(() => props.setRoute('home'))} type="button">
<div className="sidebar-avatar">{props.currentUser.slice(0, 1).toUpperCase()}</div>
<div className="sidebar-profile-copy">
<strong>{props.currentUser}</strong>
</div>
</button>
<div className="sidebar-links">
{sidebarItems.map((item) => (
<button
className={item.route === props.route ? 'sidebar-link active' : 'sidebar-link'}
key={item.route}
onClick={() => startTransition(() => props.setRoute(item.route))}
type="button"
>
<span className="sidebar-link-short" aria-hidden="true">{item.short}</span>
<span>{item.label}</span>
</button>
))}
</div>
<div className="sidebar-links">
{networkSidebarItems.map((item) => (
<button
className={item.route === props.route ? 'sidebar-link active' : 'sidebar-link'}
key={item.route}
onClick={() => startTransition(() => props.setRoute(item.route))}
type="button"
>
<span className="sidebar-link-short sidebar-network-icon" aria-hidden="true">
<img alt="" className={item.iconClassName || 'sidebar-network-icon-image'} src={item.icon} />
</span>
<span>{item.label}</span>
</button>
))}
</div>
<div className="sidebar-footer">
<button className="ghost-button sidebar-settings" onClick={() => startTransition(() => props.setRoute('settings'))} type="button">
<span className="door-icon sidebar-action-icon" aria-hidden="true">S</span>
<span
className={hasServiceFailure ? 'status-dot down sidebar-settings-status' : 'status-dot ok sidebar-settings-status'}
title={hasServiceFailure ? 'One or more services are failing.' : 'All services are healthy.'}
/>
<span>Settings</span>
</button>
<button className="ghost-button sidebar-exit" disabled={props.busy} onClick={props.onExitProfile} type="button">
<span className="door-icon sidebar-action-icon" aria-hidden="true">[]</span>
<span>Switch user</span>
</button>
</div>
</div>
</aside>
)
}
function LoadingScreen() {
return (
<div className="loading-screen">
<div className="loading-orb" />
<p>Preparing OmniArr…</p>
</div>
)
}
function AuthScreen({
mode,
password,
setPassword,
onSubmit,
busy,
error,
}: {
mode: 'setup' | 'login'
password: string
setPassword: (value: string) => void
onSubmit: () => Promise<void>
busy: boolean
error: string
}) {
const isPasswordValid = password.trim().length >= 8
return (
<div className="auth-layout">
<section className="auth-panel">
<p className="eyebrow">{mode === 'setup' ? 'First boot' : 'Secure access'}</p>
<h1>{mode === 'setup' ? 'Create your master password' : 'Sign in to OmniArr'}</h1>
<p className="auth-copy">
{mode === 'setup'
? 'This password protects the session and encrypts service settings on the server.'
: 'The full backend remains protected behind the current session.'}
</p>
<div className="auth-form">
<label>
Password
<input
autoComplete={mode === 'setup' ? 'new-password' : 'current-password'}
className="input"
minLength={8}
onChange={(event) => setPassword(event.target.value)}
placeholder="At least 8 characters"
type="password"
value={password}
/>
</label>
{!isPasswordValid && password.length > 0 ? <p className="helper-text">Password must be at least 8 non-space characters.</p> : null}
{error ? <p className="error-banner">{error}</p> : null}
{busy ? <p className="notice">Contacting backend…</p> : null}
<button className="primary-button" disabled={busy || !isPasswordValid} onClick={() => void onSubmit()} type="button">
{busy ? 'Processing…' : mode === 'setup' ? 'Initialize' : 'Sign in'}
</button>
</div>
</section>
<section className="auth-visual">
<div className="auth-wallpaper" />
<div className="glass-card">
<p className="eyebrow">Migration</p>
<ul className="feature-list">
<li>Server-side session auth</li>
<li>Encrypted persisted settings</li>
<li>Jellyfin, Sonarr and Radarr</li>
<li>Rebuilt React UI with shared layout</li>
</ul>
</div>
</section>
</div>
)
}
function JellyfinOnboardingScreen(props: {
baseUrl: string
apiKey: string
users: SelectOption[]
busy: boolean
error: string
notice: string
onBaseUrlChange: (value: string) => void
onApiKeyChange: (value: string) => void
onLoadUsers: () => Promise<void>
}) {
return (
<div className="auth-layout">
<section className="auth-panel">
<p className="eyebrow">Jellyfin setup</p>
<h1>Connect your server</h1>
<p className="auth-copy">
Enter your Jellyfin URL and API key to load the available profiles before entering the dashboard.
</p>
<div className="auth-form">
<label>
Jellyfin URL
<input className="input" onChange={(event) => props.onBaseUrlChange(event.target.value)} value={props.baseUrl} />
</label>
<label>
API key
<input className="input" onChange={(event) => props.onApiKeyChange(event.target.value)} value={props.apiKey} />
</label>
{props.error ? <p className="error-banner">{props.error}</p> : null}
{props.notice ? <p className="notice">{props.notice}</p> : null}
<button className="primary-button" disabled={props.busy || !props.baseUrl.trim() || !props.apiKey.trim()} onClick={() => void props.onLoadUsers()} type="button">
{props.busy ? 'Loading users…' : 'Load Jellyfin users'}
</button>
</div>
</section>
<section className="auth-visual">
<div className="auth-wallpaper" />
<div className="glass-card">
<p className="eyebrow">Profiles</p>
<div className="profile-grid compact">
{props.users.map((user) => (
<div className="profile-card static" key={user.id}>
<div className="profile-avatar">{user.label.slice(0, 1).toUpperCase()}</div>
<strong>{user.label}</strong>
</div>
))}
</div>
</div>
</section>
</div>
)
}
function ProfileSelectionScreen(props: {
users: SelectOption[]
currentUserId: string
busy: boolean
error: string
notice: string
onSelectUser: (userId: string) => Promise<void>
onBackToOnboarding: () => void
}) {
return (
<div className="profiles-layout">
<div className="profiles-header">
<p className="eyebrow">Profiles</p>
<h1>Who&apos;s watching?</h1>
{props.error ? <p className="error-banner">{props.error}</p> : null}
{props.notice ? <p className="notice">{props.notice}</p> : null}
</div>
<div className="profile-grid">
{props.users.map((user) => (
<button
className={user.id === props.currentUserId ? 'profile-card active' : 'profile-card'}
disabled={props.busy}
key={user.id}
onClick={() => void props.onSelectUser(user.id)}
type="button"
>
<div className="profile-avatar">{user.label.slice(0, 1).toUpperCase()}</div>
<strong>{user.label}</strong>
</button>
))}
</div>
<div className="profiles-actions">
<button className="ghost-button" onClick={props.onBackToOnboarding} type="button">
Edit Jellyfin connection
</button>
</div>
</div>
)
}
function HubPageView(props: {
route: AppRoute
hub?: HubPageResponse
loading: boolean
onRequest: (card: MediaCard) => Promise<void>
onOpenCard: (card: MediaCard, originRect?: ModalOriginRect | null) => void
busy: boolean
tmdbToken: string
}) {
if (props.loading) {
return <LoadingHub />
}
if (!props.hub) {
return (
<EmptyState
copy="This section could not be loaded. Check the service URLs and the current Jellyfin profile."
title="Section unavailable"
/>
)
}
const hasHero = props.hub.hero.length > 0
const hasSections = props.hub.sections.some((section) => section.items.length > 0)
const hasContinueWatching = Boolean(props.hub.continueWatching?.length)
if (!hasHero && !hasSections && !hasContinueWatching) {
return (
<EmptyState
copy="Check Jellyfin, TMDb and the selected profile. This page is connected, but it has no media to display yet."
title="No media available"
/>
)
}
return (
<div className="hub-page">
<HeroShowcase hero={props.hub.hero} onOpenCard={props.onOpenCard} tmdbToken={props.tmdbToken} />
{props.hub.continueWatching?.length ? (
<SectionCarousel busy={props.busy} onOpenCard={props.onOpenCard} onRequest={props.onRequest} section={{ id: 'continue', title: 'Continue Watching', style: 'episode', items: props.hub.continueWatching }} />
) : null}
{props.hub.sections.map((section) => (
<SectionCarousel busy={props.busy} key={section.id} onOpenCard={props.onOpenCard} onRequest={props.onRequest} section={section} />
))}
</div>
)
}
function HeroShowcase({ hero, onOpenCard, tmdbToken }: { hero: MediaCard[]; onOpenCard: (card: MediaCard, originRect?: ModalOriginRect | null) => void; tmdbToken: string }) {
const [activeIndex, setActiveIndex] = useState(0)
const [activeLogoUrl, setActiveLogoUrl] = useState('')
const logoCacheRef = useRef(new Map<string, string>())
useEffect(() => {
setActiveIndex((current) => (hero.length ? Math.min(current, hero.length - 1) : 0))
}, [hero])
useEffect(() => {
if (hero.length <= 1) {
return
}
const interval = window.setInterval(() => {
setActiveIndex((current) => (current + 1) % hero.length)
}, 9000)
return () => window.clearInterval(interval)
}, [hero])
const featured = hero[activeIndex]
useEffect(() => {
if (!featured?.tmdbId) {
setActiveLogoUrl('')
return
}
const mediaType = featured.mediaType === 'movie' ? 'movie' : featured.mediaType === 'tv' || featured.mediaType === 'series' ? 'tv' : ''
if (!mediaType) {
setActiveLogoUrl('')
return
}
let cancelled = false
const cacheKey = `${mediaType}:${featured.tmdbId}`
const cachedLogoUrl = logoCacheRef.current.get(cacheKey)
if (cachedLogoUrl !== undefined) {
setActiveLogoUrl(cachedLogoUrl)
return
}
const fetchImages = tmdbToken
? fetch(`https://api.themoviedb.org/3/${mediaType}/${featured.tmdbId}/images`, {
headers: {
Authorization: `Bearer ${tmdbToken}`,
},
})
: fetch(`/api/tmdb/logo?tmdbId=${featured.tmdbId}&mediaType=${mediaType}`)
void fetchImages
.then(async (response) => {
if (!response.ok) {
throw new Error('Failed to load logo')
}
return response.json() as Promise<{
logos?: Array<{ file_path: string; iso_639_1: string | null }>
backdrops?: Array<{ file_path: string; iso_639_1: string | null }>
}>
})
.then((payload) => {
if (!cancelled) {
const englishLogos = payload.logos?.filter((logo) => logo.iso_639_1 === 'en') || []
const logo = englishLogos[0] || payload.logos?.[0]
const nextLogoUrl = logo?.file_path ? `https://image.tmdb.org/t/p/original${logo.file_path}` : ''
logoCacheRef.current.set(cacheKey, nextLogoUrl)
setActiveLogoUrl(nextLogoUrl)
}
})
.catch(() => {
if (!cancelled) {
logoCacheRef.current.set(cacheKey, '')
setActiveLogoUrl('')
}
})
return () => {
cancelled = true
}
}, [featured])
if (!featured) {
return (
<div className="hero-showcase empty">
<div className="hero-copy">
<span className="badge">Library</span>
<h1>No featured media yet</h1>
<p>Once Jellyfin and TMDb return content, featured titles will appear here.</p>
</div>
</div>
)
}
return (
<section
className="hero-showcase"
onClick={() => onOpenCard(featured)}
>
<div className="hero-media" aria-hidden="true">
{hero.slice(0, 6).map((card, index) => (
<div
className={index === activeIndex ? 'hero-slide active' : 'hero-slide'}
key={card.id}
style={resolveHeroSlideStyle(card)}
/>
))}
</div>
<div className="hero-overlay" />
<div className="hero-content">
<div className="hero-copy">
{activeLogoUrl ? (
<img
alt={featured.title}
className="hero-logo-image"
loading="eager"
src={activeLogoUrl}
/>
) : (
<div className="hero-logo" aria-label={featured.title}>{featured.title}</div>
)}
</div>
</div>
<div className="hero-dots">
{hero.slice(0, 6).map((card, index) => (
<button
aria-label={`Show ${card.title}`}
className={index === activeIndex ? 'hero-dot active' : 'hero-dot'}
key={card.id}
onClick={(event) => {
event.stopPropagation()
setActiveIndex(index)
}}
type="button"
/>
))}
</div>
</section>
)
}
function resolveHeroSlideStyle(card: MediaCard | undefined) {
if (!card) {
return undefined
}
const slideImage = card.backdropUrl || card.posterUrl
return slideImage ? { backgroundImage: `url("${slideImage}")` } : undefined
}
function cacheHubPage(
current: Partial<Record<HubPageResponse['page'], HubPageResponse>>,
page: HubPageResponse['page'],
hub: HubPageResponse
): Partial<Record<HubPageResponse['page'], HubPageResponse>> {
const next = { ...current, [page]: hub }
const pages = Object.keys(next) as HubPageResponse['page'][]
if (pages.length <= MAX_CACHED_HUB_PAGES) {
return next
}
const trimmed: Partial<Record<HubPageResponse['page'], HubPageResponse>> = {}
pages.slice(-MAX_CACHED_HUB_PAGES).forEach((cachedPage) => {
trimmed[cachedPage] = next[cachedPage]
})
return trimmed
}
function SectionCarousel(props: {
section: HubPageResponse['sections'][number]
onRequest: (card: MediaCard) => Promise<void>
onOpenCard: (card: MediaCard, originRect?: ModalOriginRect | null) => void
busy: boolean
}) {
if (!props.section.items.length) return null
return (
<section className={props.section.id === 'continue' ? 'carousel-row continue-row' : 'carousel-row'}>
<div className="carousel-title">{props.section.title}</div>
<div className="carousel-section">
<div className="carousel-track">
{props.section.items.map((card) => (
<MediaTile busy={props.busy} card={card} isContinueWatching={props.section.id === 'continue'} key={card.id} onOpenCard={props.onOpenCard} onRequest={props.onRequest} styleType={props.section.style || 'landscape'} />
))}
</div>
</div>
</section>
)
}
function MediaTile(props: {
card: MediaCard
isContinueWatching: boolean
styleType: 'poster' | 'landscape' | 'profile' | 'genre' | 'network' | 'episode'
busy: boolean
onOpenCard: (card: MediaCard, originRect?: ModalOriginRect | null) => void
onRequest: (card: MediaCard) => Promise<void>
}) {
const imageUrl = props.styleType === 'landscape' || props.styleType === 'episode'
? (props.card.backdropUrl || props.card.posterUrl)
: props.card.posterUrl
return (
<article className={props.isContinueWatching ? `media-tile ${props.styleType} continue-tile` : `media-tile ${props.styleType}`}>
<button
className="media-art media-trigger"
onClick={(event) => {
const tile = event.currentTarget.closest('.media-tile') as HTMLElement | null
props.onOpenCard(props.card, tile ? captureOriginRect(tile) : captureOriginRect(event.currentTarget))
}}
type="button"
>
{imageUrl ? <img alt={props.card.title} loading="lazy" src={imageUrl} /> : <div className="media-fallback" />}
{props.card.hoverLabel ? <div className="media-hover-label">{props.card.hoverLabel}</div> : null}
{props.card.progress ? <div className="tile-progress"><div className="tile-progress-fill" style={{ width: `${props.card.progress}%` }} /></div> : null}
</button>
</article>
)
}
function TitleDetailsModal(props: {
card: MediaCard
config: AppConfig
onClose: () => void
originRect: ModalOriginRect | null
onRequest: (card: MediaCard) => Promise<void>
}) {
const [details, setDetails] = useState<TitleDetailsResponse | null>(null)
const [detailsLoading, setDetailsLoading] = useState(true)
const [detailsError, setDetailsError] = useState('')
const [requestedSeasonId, setRequestedSeasonId] = useState<string | undefined>(undefined)
const [activeSeasonId, setActiveSeasonId] = useState<string | undefined>(undefined)
const [seasonCacheVersion, setSeasonCacheVersion] = useState(0)
const [loadingSeasonIds, setLoadingSeasonIds] = useState<string[]>([])
const [playerTarget, setPlayerTarget] = useState<{ id: string; title: string } | null>(null)
const [isOverviewExpanded, setIsOverviewExpanded] = useState(false)
const [episodeScrollState, setEpisodeScrollState] = useState({ canScrollLeft: false, canScrollRight: false })
const [transitionState, setTransitionState] = useState<'opening' | 'open' | 'closing'>(
props.originRect ? 'opening' : 'open'
)
const [transitionStyle, setTransitionStyle] = useState<CSSProperties | undefined>(undefined)
const closeTimeoutRef = useRef<number | null>(null)
const modalRef = useRef<HTMLDivElement | null>(null)
const episodeListRef = useRef<HTMLDivElement | null>(null)
const seasonDetailsCacheRef = useRef(new Map<string, TitleDetailsResponse>())
const seasonRequestsRef = useRef(new Map<string, Promise<TitleDetailsResponse>>())
const pendingSeasonScrollRef = useRef<string | null>(null)
const previousRenderedSeasonIdsRef = useRef<string[]>([])
useLayoutEffect(() => {
if (!props.originRect || !modalRef.current) {
setTransitionState('open')
setTransitionStyle(undefined)
return
}
const modalRect = modalRef.current.getBoundingClientRect()
const scaleX = props.originRect.width / modalRect.width
const scaleY = props.originRect.height / modalRect.height
const translateX = props.originRect.left - modalRect.left
const translateY = props.originRect.top - modalRect.top
setTransitionStyle({
['--title-modal-from-x' as '--title-modal-from-x']: `${translateX}px`,
['--title-modal-from-y' as '--title-modal-from-y']: `${translateY}px`,
['--title-modal-from-scale-x' as '--title-modal-from-scale-x']: `${scaleX}`,
['--title-modal-from-scale-y' as '--title-modal-from-scale-y']: `${scaleY}`,
})
let frameA = 0
let frameB = 0
frameA = window.requestAnimationFrame(() => {
frameB = window.requestAnimationFrame(() => {
setTransitionState('open')
})
})
return () => {
window.cancelAnimationFrame(frameA)
window.cancelAnimationFrame(frameB)
}
}, [props.card.id, props.originRect])
useEffect(() => () => {
if (closeTimeoutRef.current) {
window.clearTimeout(closeTimeoutRef.current)
}
}, [])
useEffect(() => {
setDetails(null)
setDetailsLoading(true)
setDetailsError('')
setRequestedSeasonId(undefined)
setActiveSeasonId(undefined)
setSeasonCacheVersion(0)
setLoadingSeasonIds([])
setPlayerTarget(null)
setIsOverviewExpanded(false)
seasonDetailsCacheRef.current.clear()
seasonRequestsRef.current.clear()
pendingSeasonScrollRef.current = null
previousRenderedSeasonIdsRef.current = []
}, [props.card.id])
function cacheSeasonDetails(nextDetails: TitleDetailsResponse, requestedSeason?: string) {
const resolvedSeasonId = nextDetails.selectedSeasonId || requestedSeason
if (requestedSeason) {
seasonDetailsCacheRef.current.set(requestedSeason, nextDetails)
}
if (resolvedSeasonId) {
seasonDetailsCacheRef.current.set(resolvedSeasonId, nextDetails)
}
setSeasonCacheVersion((current) => current + 1)
return resolvedSeasonId
}
function ensureSeasonLoaded(seasonId: string) {
const cached = seasonDetailsCacheRef.current.get(seasonId)
if (cached) {
return Promise.resolve(cached)
}
const existingRequest = seasonRequestsRef.current.get(seasonId)
if (existingRequest) {
return existingRequest
}
setLoadingSeasonIds((current) => current.includes(seasonId) ? current : [...current, seasonId])
const request = api.getTitleDetails(props.card, seasonId)
.then((nextDetails) => {
cacheSeasonDetails(nextDetails, seasonId)
return nextDetails
})
.finally(() => {
seasonRequestsRef.current.delete(seasonId)
setLoadingSeasonIds((current) => current.filter((id) => id !== seasonId))
})
seasonRequestsRef.current.set(seasonId, request)
return request
}
useEffect(() => {
let cancelled = false
const cacheKey = requestedSeasonId || '__default__'
const cachedDetails = requestedSeasonId
? seasonDetailsCacheRef.current.get(requestedSeasonId)
: seasonDetailsCacheRef.current.get(cacheKey)
if (cachedDetails) {
setDetails(cachedDetails)
setDetailsLoading(false)
setDetailsError('')
const resolvedSeasonId = cachedDetails.selectedSeasonId || requestedSeasonId
if (resolvedSeasonId) {
setActiveSeasonId(resolvedSeasonId)
}
return () => {
cancelled = true
}
}
setDetailsLoading(true)
setDetailsError('')
void api.getTitleDetails(props.card, requestedSeasonId)
.then((nextDetails) => {
if (cancelled) return
seasonDetailsCacheRef.current.set(cacheKey, nextDetails)
const resolvedSeasonId = cacheSeasonDetails(nextDetails, requestedSeasonId)
if (resolvedSeasonId) {
setActiveSeasonId(resolvedSeasonId)
}
setDetails(nextDetails)
})
.catch((error) => {
if (cancelled) return
setDetailsError(error instanceof Error ? error.message : 'Failed to load title details.')
})
.finally(() => {
if (cancelled) return
setDetailsLoading(false)
})
return () => {
cancelled = true
}
}, [props.card, requestedSeasonId])
const seasonValue = activeSeasonId ?? requestedSeasonId ?? details?.selectedSeasonId ?? ''
useEffect(() => {
if (!details || details.mediaType !== 'series' || !details.seasons.length) {
return
}
const currentIndex = details.seasons.findIndex((season) => season.id === seasonValue)
if (currentIndex < 0) {
return
}
const keepSeasonIds = new Set(
details.seasons
.slice(Math.max(0, currentIndex - 2), currentIndex + 3)
.map((season) => season.id)
)
for (const seasonId of Array.from(seasonDetailsCacheRef.current.keys())) {
if (seasonId !== '__default__' && !keepSeasonIds.has(seasonId)) {
seasonDetailsCacheRef.current.delete(seasonId)
}
}
}, [details, seasonCacheVersion, seasonValue])
useEffect(() => {
if (!details || details.mediaType !== 'series' || !details.seasons.length) {
return
}
const activeIndex = details.seasons.findIndex((season) => season.id === seasonValue)
if (activeIndex < 0) {
return
}
const prioritySeasonIds = [
details.seasons[activeIndex + 1]?.id,
details.seasons[activeIndex - 1]?.id,
details.seasons[activeIndex + 2]?.id,
details.seasons[activeIndex - 2]?.id,
].filter(Boolean) as string[]
for (const seasonId of prioritySeasonIds) {
if (!seasonDetailsCacheRef.current.has(seasonId)) {
void ensureSeasonLoaded(seasonId)
}
}
}, [details, seasonValue])
useEffect(() => {
const list = episodeListRef.current
const targetSeasonId = pendingSeasonScrollRef.current
if (!list || !targetSeasonId) {
return
}
const targetEpisode = list.querySelector<HTMLElement>(`[data-season-id="${CSS.escape(targetSeasonId)}"]`)
if (!targetEpisode) {
return
}
const listRect = list.getBoundingClientRect()
const targetRect = targetEpisode.getBoundingClientRect()
list.scrollTo({
left: list.scrollLeft + (targetRect.left - listRect.left),
behavior: 'smooth',
})
pendingSeasonScrollRef.current = null
}, [details, seasonCacheVersion, seasonValue])
useLayoutEffect(() => {
const list = episodeListRef.current
if (!list || pendingSeasonScrollRef.current) {
return
}
const previousRenderedSeasonIds = previousRenderedSeasonIdsRef.current
const currentRenderedSeasonIds = Array.from(new Set(
Array.from(list.querySelectorAll<HTMLElement>('[data-season-id]'))
.map((card) => card.dataset.seasonId)
.filter((seasonId): seasonId is string => Boolean(seasonId))
))
if (!previousRenderedSeasonIds.length) {
previousRenderedSeasonIdsRef.current = currentRenderedSeasonIds
return
}
const previousFirstSeasonId = previousRenderedSeasonIds[0]
const currentFirstSeasonId = currentRenderedSeasonIds[0]
if (previousFirstSeasonId && currentFirstSeasonId && previousFirstSeasonId !== currentFirstSeasonId) {
if (currentRenderedSeasonIds.includes(previousFirstSeasonId)) {
const anchorCard = list.querySelector<HTMLElement>(`[data-season-id="${CSS.escape(previousFirstSeasonId)}"]`)
if (anchorCard) {
list.scrollLeft += anchorCard.offsetLeft
}
} else if (previousRenderedSeasonIds.includes(currentFirstSeasonId)) {
const currentFirstCard = list.querySelector<HTMLElement>(`[data-season-id="${CSS.escape(currentFirstSeasonId)}"]`)
if (currentFirstCard) {
list.scrollLeft = Math.max(0, list.scrollLeft - currentFirstCard.offsetLeft)
}
}
}
previousRenderedSeasonIdsRef.current = currentRenderedSeasonIds
}, [details, seasonCacheVersion, seasonValue])
useEffect(() => {
const list = episodeListRef.current
if (!list || detailsLoading || details?.mediaType !== 'series') {
setEpisodeScrollState({ canScrollLeft: false, canScrollRight: false })
return
}
const syncScrollState = () => {
const maxScrollLeft = list.scrollWidth - list.clientWidth
setEpisodeScrollState({
canScrollLeft: list.scrollLeft > 8,
canScrollRight: maxScrollLeft - list.scrollLeft > 8,
})
const viewportLeft = list.scrollLeft
const viewportRight = viewportLeft + list.clientWidth
const coverageBySeason = new Map<string, number>()
const boundsBySeason = new Map<string, { left: number; right: number }>()
const cards = list.querySelectorAll<HTMLElement>('[data-season-id]')
for (const card of cards) {
const seasonId = card.dataset.seasonId
if (!seasonId) continue
const cardLeft = card.offsetLeft
const cardRight = cardLeft + card.offsetWidth
const currentBounds = boundsBySeason.get(seasonId)
boundsBySeason.set(seasonId, {
left: currentBounds ? Math.min(currentBounds.left, cardLeft) : cardLeft,
right: currentBounds ? Math.max(currentBounds.right, cardRight) : cardRight,
})
const visibleWidth = Math.min(cardRight, viewportRight) - Math.max(cardLeft, viewportLeft)
if (visibleWidth <= 0) continue
coverageBySeason.set(seasonId, (coverageBySeason.get(seasonId) || 0) + visibleWidth)
}
let dominantSeasonId = seasonValue
let dominantCoverage = -1
for (const [seasonId, coverage] of coverageBySeason.entries()) {
if (coverage > dominantCoverage) {
dominantCoverage = coverage
dominantSeasonId = seasonId
}
}
if (dominantSeasonId && dominantSeasonId !== activeSeasonId) {
setActiveSeasonId(dominantSeasonId)
}
const activeBounds = boundsBySeason.get(dominantSeasonId || seasonValue)
const activeIndex = details.seasons.findIndex((season) => season.id === (dominantSeasonId || seasonValue))
if (!activeBounds || activeIndex < 0) {
return
}
const preloadThreshold = list.clientWidth * 0.45
const previousSeasonId = details.seasons[activeIndex - 1]?.id
const nextSeasonId = details.seasons[activeIndex + 1]?.id
const nextNextSeasonId = details.seasons[activeIndex + 2]?.id
const previousPreviousSeasonId = details.seasons[activeIndex - 2]?.id
if (previousSeasonId && viewportLeft - activeBounds.left < preloadThreshold && !seasonDetailsCacheRef.current.has(previousSeasonId)) {
void ensureSeasonLoaded(previousSeasonId)
}
if (nextSeasonId && activeBounds.right - viewportRight < preloadThreshold && !seasonDetailsCacheRef.current.has(nextSeasonId)) {
void ensureSeasonLoaded(nextSeasonId)
}
if (nextNextSeasonId && activeBounds.right - viewportRight < preloadThreshold * 0.5 && !seasonDetailsCacheRef.current.has(nextNextSeasonId)) {
void ensureSeasonLoaded(nextNextSeasonId)
}
if (previousPreviousSeasonId && viewportLeft - activeBounds.left < preloadThreshold * 0.5 && !seasonDetailsCacheRef.current.has(previousPreviousSeasonId)) {
void ensureSeasonLoaded(previousPreviousSeasonId)
}
}
syncScrollState()
list.addEventListener('scroll', syncScrollState, { passive: true })
window.addEventListener('resize', syncScrollState)
return () => {
list.removeEventListener('scroll', syncScrollState)
window.removeEventListener('resize', syncScrollState)
}
}, [activeSeasonId, details, detailsLoading, seasonCacheVersion, seasonValue])
const canRequest = Boolean(props.card.tmdbId && props.card.mediaType !== 'mixed')
const playId = details?.playTargetId || details?.providerId || props.card.providerId
const isAvailableInJellyfin = details?.availableInJellyfin ?? Boolean(props.card.providerId)
const canPlay = Boolean(isAvailableInJellyfin && playId && props.config.jellyfin.baseUrl)
const backgroundImage = details?.backdropUrl || props.card.backdropUrl || props.card.posterUrl
const logoUrl = details?.logoUrl || props.card.logoUrl
const displayTitle = details?.title || props.card.title
const displaySubtitle = details?.subtitle || props.card.subtitle
const displayOverview = details?.overview || props.card.overview || 'No details available yet.'
const displayYear = props.card.releaseDate?.slice(0, 4)
|| props.card.firstAirDate?.slice(0, 4)
|| displaySubtitle.match(/\b(19|20)\d{2}\b/)?.[0]
|| ''
const truncatedOverview = truncateOverview(displayOverview, 260)
const shouldShowOverviewToggle = truncatedOverview !== displayOverview
const jellyfinPlayerUrl = playerTarget && props.config.jellyfin.baseUrl
? buildJellyfinPlayerUrl(props.config.jellyfin.baseUrl, playerTarget.id)
: ''
const activeSeasonMeta = details?.seasons.find((season) => season.id === seasonValue)
const activeSeasonDetails = seasonValue ? seasonDetailsCacheRef.current.get(seasonValue) : null
const currentSeasonIndex = details?.seasons.findIndex((season) => season.id === seasonValue) ?? -1
const resolvedSeasonIndex = currentSeasonIndex >= 0 ? currentSeasonIndex : 0
const hasPreviousSeason = Boolean(details?.seasons.length && resolvedSeasonIndex > 0)
const hasNextSeason = Boolean(details?.seasons.length && resolvedSeasonIndex < details.seasons.length - 1)
const previousSeasonId = hasPreviousSeason ? details?.seasons[resolvedSeasonIndex - 1]?.id : undefined
const nextSeasonId = hasNextSeason ? details?.seasons[resolvedSeasonIndex + 1]?.id : undefined
const isPreviousSeasonLoading = Boolean(previousSeasonId && loadingSeasonIds.includes(previousSeasonId))
const isNextSeasonLoading = Boolean(nextSeasonId && loadingSeasonIds.includes(nextSeasonId))
const visibleEpisodes = details?.seasons
.filter((season, index) => Math.abs(index - resolvedSeasonIndex) <= 2)
.flatMap((season) =>
(seasonDetailsCacheRef.current.get(season.id)?.episodes || []).map((episode) => ({
...episode,
seasonId: season.id,
}))
) ?? []
function handleRequestClose() {
if (!props.originRect || transitionState === 'closing') {
props.onClose()
return
}
setTransitionState('closing')
closeTimeoutRef.current = window.setTimeout(() => {
props.onClose()
}, 680)
}
function scrollEpisodes(direction: 'left' | 'right') {
const list = episodeListRef.current
if (!list) return
const distance = Math.max(list.clientWidth * 0.72, 320)
list.scrollBy({
left: direction === 'left' ? -distance : distance,
behavior: 'smooth',
})
}
function changeSeason(offset: -1 | 1) {
if (!details?.seasons.length) {
return
}
const nextSeason = details.seasons[resolvedSeasonIndex + offset]
if (!nextSeason) {
return
}
pendingSeasonScrollRef.current = nextSeason.id
setActiveSeasonId(nextSeason.id)
startTransition(() => {
setRequestedSeasonId(nextSeason.id)
})
}
function handleSeasonSelectChange(nextSeasonId: string) {
pendingSeasonScrollRef.current = nextSeasonId || null
setActiveSeasonId(nextSeasonId || undefined)
startTransition(() => {
setRequestedSeasonId(nextSeasonId || undefined)
})
}
if (playerTarget && jellyfinPlayerUrl) {
return (
<div className="modal-backdrop player-backdrop" role="presentation">
<div className="title-player-view" role="dialog" aria-modal="true">
<div className="title-player-topbar">
<button className="secondary-button" onClick={() => setPlayerTarget(null)} type="button">Back</button>
<button className="modal-close omniarr-modal-close" onClick={props.onClose} type="button">Close</button>
</div>
<iframe
allow="autoplay; fullscreen; encrypted-media"
className="title-player-frame"
src={jellyfinPlayerUrl}
title={`${playerTarget.title} player`}
/>
</div>
</div>
)
}
return (
<div className={transitionState === 'closing' ? 'modal-backdrop is-closing' : 'modal-backdrop'} onClick={handleRequestClose} role="presentation">
<div
className={
transitionStyle
? `title-modal title-details-modal title-details-modal-transition is-${transitionState}`
: 'title-modal title-details-modal'
}
onClick={(event) => event.stopPropagation()}
ref={modalRef}
role="dialog"
aria-modal="true"
style={transitionStyle}
>
{detailsLoading && !details ? (
<div className="title-details-loading-screen" role="status" aria-live="polite">
<div className="title-details-spinner" aria-hidden="true" />
<p>Loading details…</p>
</div>
) : null}
<div className="title-details-modal-content">
<div className="title-details-hero" style={backgroundImage ? { backgroundImage: `url("${backgroundImage}")` } : undefined}>
<div className="title-details-darken" />
<button className="modal-close omniarr-modal-close" onClick={handleRequestClose} type="button">Close</button>
</div>
<div className="title-details-body">
<div className="title-details-shell">
{detailsError ? <p className="error-banner">{detailsError}</p> : null}
<div className="title-details-summary">
<div className="title-details-hero-heading">
{logoUrl ? (
<img alt={displayTitle} className="title-details-logo" src={logoUrl} />
) : (
<h2 className="title-details-title">{displayTitle}</h2>
)}
</div>
<div className="title-details-summary-content">
<p className="title-details-overview">
{isOverviewExpanded ? displayOverview : truncatedOverview}
{shouldShowOverviewToggle ? (
<>
{' '}
<button
className="title-details-overview-toggle"
onClick={() => setIsOverviewExpanded((current) => !current)}
type="button"
>
{isOverviewExpanded ? 'less' : 'more'}
</button>
</>
) : null}
</p>
<div className="title-details-summary-header">
{canPlay ? (
<button className="secondary-button title-action-button title-action-button-play" onClick={() => setPlayerTarget({ id: playId!, title: displayTitle })} type="button">
<PlayGlyph />
<span>Play</span>
</button>
) : canRequest ? (
<button className="secondary-button title-action-button" onClick={() => void props.onRequest(props.card)} type="button">
<DownloadGlyph />
<span>Request</span>
</button>
) : null}
</div>
</div>
{displayYear ? <p className="title-details-year">{displayYear}</p> : null}
</div>
{details?.mediaType === 'series' ? (
<div className="title-seasons-panel">
<div className="title-seasons-header">
<div
className="title-season-strip"
>
<button
aria-label="Previous season"
className="title-season-nav"
disabled={!hasPreviousSeason}
onClick={() => changeSeason(-1)}
type="button"
/>
{details.seasons.length ? (
<label className="title-season-selector">
<span className="title-season-kicker">Season</span>
<select
className="title-season-select"
onChange={(event) => handleSeasonSelectChange(event.target.value)}
value={seasonValue}
>
{details.seasons.map((season) => (
<option key={season.id} value={season.id}>
{season.title}
</option>
))}
</select>
</label>
) : null}
<div className="title-season-divider" aria-hidden="true" />
<div className="title-season-badge">
<strong>{activeSeasonDetails?.episodes.length ?? activeSeasonMeta?.episodeCount ?? details.episodes.length}</strong>
<span>{(activeSeasonDetails?.episodes.length ?? activeSeasonMeta?.episodeCount ?? details.episodes.length) === 1 ? 'episode' : 'episodes'}</span>
</div>
<button
aria-label="Next season"
className="title-season-nav next"
disabled={!hasNextSeason}
onClick={() => changeSeason(1)}
type="button"
/>
</div>
</div>
<div
className={[
'episode-dock',
episodeScrollState.canScrollLeft ? 'show-left-fade' : '',
episodeScrollState.canScrollRight ? 'show-right-fade' : '',
].filter(Boolean).join(' ')}
>
{episodeScrollState.canScrollLeft ? (
<button
aria-label="Scroll episodes left"
className="episode-dock-arrow left"
onClick={() => scrollEpisodes('left')}
type="button"
/>
) : null}
{episodeScrollState.canScrollRight ? (
<button
aria-label="Scroll episodes right"
className="episode-dock-arrow right"
onClick={() => scrollEpisodes('right')}
type="button"
/>
) : null}
{isPreviousSeasonLoading ? <div className="episode-dock-spinner left" aria-hidden="true" /> : null}
{isNextSeasonLoading ? <div className="episode-dock-spinner right" aria-hidden="true" /> : null}
<div
className="episode-list"
ref={episodeListRef}
>
{visibleEpisodes.map((episode) => (
<button
className="episode-card"
data-season-id={episode.seasonId}
key={`${episode.seasonId}-${episode.id}`}
onClick={() => setPlayerTarget({ id: episode.id, title: episode.title })}
type="button"
>
<div className="episode-card-art">
{episode.imageUrl ? <img alt={episode.title} loading="lazy" src={episode.imageUrl} /> : <div className="media-fallback" />}
</div>
<div className="episode-card-copy">
<strong>{episode.title}</strong>
<span>{formatEpisodeMeta(episode.subtitle)}</span>
</div>
</button>
))}
</div>
</div>
</div>
) : null}
</div>
</div>
</div>
</div>
</div>
)
}
function buildJellyfinPlayerUrl(baseUrl: string, itemId: string) {
return `${baseUrl.replace(/\/$/, '')}/web/#/details?id=${encodeURIComponent(itemId)}&autoplay=true`
}
function captureOriginRect(element: HTMLElement): ModalOriginRect {
const rect = element.getBoundingClientRect()
return {
left: rect.left,
top: rect.top,
width: rect.width,
height: rect.height,
}
}
function PlayGlyph() {
return (
<svg aria-hidden="true" className="title-action-icon" viewBox="0 0 16 16">
<path d="M5 3.2v9.6L12.6 8 5 3.2Z" fill="currentColor" />
</svg>
)
}
function DownloadGlyph() {
return (
<svg aria-hidden="true" className="title-action-icon" viewBox="0 0 16 16">
<path d="M8.75 2.5h-1.5v6.1L5.1 6.45 4 7.55 8 11.55l4-4-1.1-1.1-2.15 2.15V2.5Z" fill="currentColor" />
<path d="M3 12h10v1.5H3z" fill="currentColor" />
</svg>
)
}
function formatEpisodeMeta(subtitle: string) {
return subtitle.replace(/^Season\s+\d+\s*,\s*/i, '')
}
function truncateOverview(text: string, maxLength: number) {
if (text.length <= maxLength) {
return text
}
const clipped = text.slice(0, maxLength)
const safeBreak = clipped.lastIndexOf(' ')
return `${(safeBreak > maxLength * 0.7 ? clipped.slice(0, safeBreak) : clipped).trimEnd()}...`
}
function DownloadsPage(props: {
downloads: DownloadItem[]
busyId: string
onDelete: (id: string) => Promise<void>
}) {
return (
<div className="downloads-page">
<div className="downloads-header">
<h2>Downloads</h2>
</div>
<section className="downloads-section">
<div className="downloads-grid">
{props.downloads.length
? props.downloads.map((item) => (
<DownloadRow
busy={props.busyId === item.id}
item={item}
key={item.id}
onDelete={props.onDelete}
/>
))
: <EmptyState copy="The queue will appear here as soon as your services start working." title="No active downloads" />}
</div>
</section>
</div>
)
}
function DownloadRow(props: {
item: DownloadItem
busy: boolean
onDelete: (id: string) => Promise<void>
}) {
const item = props.item
const progressMeta = [item.size, item.speed, item.eta].filter(Boolean).join(' · ')
const artwork = item.backdropUrl || item.posterUrl
const isCompleted = item.progress >= 100
function confirmDelete() {
if (!window.confirm(`Remove "${item.title}" from the queue?`)) {
return
}
void props.onDelete(item.id)
}
return (
<article className="download-row">
<div className="download-card">
<div className="download-artwork">
{artwork ? <img alt={item.title} loading="lazy" src={artwork} /> : <div className="media-fallback download-poster-fallback">{item.title.slice(0, 1).toUpperCase()}</div>}
</div>
<div className="download-copy">
<h4>{item.title}</h4>
<p className="download-subtitle">{item.subtitle || item.detail}</p>
<p className="download-detail">{progressMeta || humanizeDownloadState(item.state)}</p>
</div>
</div>
<div className="download-side">
<div className="download-progress-panel">
<div className="download-progress-header">
<strong>{item.progress}%</strong>
<span>{progressMeta || humanizeDownloadState(item.state)}</span>
</div>
<div className="download-progress-row">
<div className={isCompleted ? 'download-progress-bar download-progress-complete' : 'download-progress-bar'}>
<div className="download-progress-fill" style={{ width: `${item.progress}%` }} />
</div>
<button
aria-label={`Delete ${item.title}`}
className="download-delete-button"
disabled={props.busy}
onClick={confirmDelete}
type="button"
>
{props.busy ? '…' : '×'}
</button>
</div>
</div>
</div>
</article>
)
}
function humanizeDownloadState(state: string) {
return state
.replace(/[_-]+/g, ' ')
.replace(/\b\w/g, (match) => match.toUpperCase())
}
function SettingsView(props: {
config: AppConfig
connections: ConnectionStatus[]
jellyfinUsersBusy: boolean
onLoadJellyfinUsers: () => Promise<void>
updateConfig: <K extends keyof AppConfig>(key: K, value: AppConfig[K]) => void
options: ServiceOptionsResponse
onSave: (event: FormEvent<HTMLFormElement>) => Promise<void>
busy: boolean
}) {
return (
<form className="settings-page" onSubmit={(event) => void props.onSave(event)}>
<SettingsCard title="Connections">
<div className="settings-connections">
{props.connections.map((connection) => (
<div className="settings-connection" key={connection.key} title={connection.message}>
<div className="settings-connection-label">
<span className={connection.ok ? 'status-dot ok' : 'status-dot down'} />
<strong>{connection.label}</strong>
</div>
<span className="settings-connection-message">{connection.message}</span>
</div>
))}
</div>
</SettingsCard>
<SettingsCard title="Preferences">
<label>
Language
<input className="input" onChange={(event) => props.updateConfig('preferences', { ...props.config.preferences, language: event.target.value })} value={props.config.preferences.language} />
</label>
<label>
Region
<input className="input" maxLength={2} onChange={(event) => props.updateConfig('preferences', { ...props.config.preferences, region: event.target.value })} value={props.config.preferences.region} />
</label>
</SettingsCard>
<SettingsCard title="Jellyfin">
<ServiceFields
apiKey={props.config.jellyfin.apiKey}
baseUrl={props.config.jellyfin.baseUrl}
onApiKeyChange={(apiKey) => props.updateConfig('jellyfin', { ...props.config.jellyfin, apiKey })}
onBaseUrlChange={(baseUrl) => props.updateConfig('jellyfin', { ...props.config.jellyfin, baseUrl })}
/>
<div className="inline-actions">
<button className="secondary-button" disabled={props.jellyfinUsersBusy || !props.config.jellyfin.baseUrl.trim() || !props.config.jellyfin.apiKey.trim()} onClick={() => void props.onLoadJellyfinUsers()} type="button">
{props.jellyfinUsersBusy ? 'Loading users…' : 'Load users'}
</button>
</div>
<label>
User
<select className="input" onChange={(event) => props.updateConfig('jellyfin', { ...props.config.jellyfin, userId: event.target.value })} value={props.config.jellyfin.userId}>
<option value="">Select user</option>
{props.options.jellyfinUsers.map((option) => <option key={option.id} value={option.id}>{option.label}</option>)}
</select>
</label>
</SettingsCard>
<SettingsCard title="Sonarr">
<ServiceFields
apiKey={props.config.sonarr.apiKey}
baseUrl={props.config.sonarr.baseUrl}
onApiKeyChange={(apiKey) => props.updateConfig('sonarr', { ...props.config.sonarr, apiKey })}
onBaseUrlChange={(baseUrl) => props.updateConfig('sonarr', { ...props.config.sonarr, baseUrl })}
/>
</SettingsCard>
<SettingsCard title="Radarr">
<ServiceFields
apiKey={props.config.radarr.apiKey}
baseUrl={props.config.radarr.baseUrl}
onApiKeyChange={(apiKey) => props.updateConfig('radarr', { ...props.config.radarr, apiKey })}
onBaseUrlChange={(baseUrl) => props.updateConfig('radarr', { ...props.config.radarr, baseUrl })}
/>
</SettingsCard>
<SettingsCard title="TMDb">
<label>
Bearer token
<input className="input" onChange={(event) => props.updateConfig('tmdb', { bearerToken: event.target.value })} placeholder="Optional if already provided via TMDB_BEARER_TOKEN" value={props.config.tmdb.bearerToken} />
</label>
</SettingsCard>
<div className="actions-row">
<button className="primary-button" disabled={props.busy} type="submit">
{props.busy ? 'Saving…' : 'Save settings'}
</button>
</div>
</form>
)
}
function SettingsCard({ title, children }: { title: string; children: ReactNode }) {
return (
<section className="settings-card">
<h2>{title}</h2>
<div className="settings-fields">{children}</div>
</section>
)
}
function ServiceFields(props: {
baseUrl: string
apiKey: string
onBaseUrlChange: (value: string) => void
onApiKeyChange: (value: string) => void
}) {
return (
<>
<label>
Base URL
<input className="input" onChange={(event) => props.onBaseUrlChange(event.target.value)} value={props.baseUrl} />
</label>
<label>
API key
<input className="input" onChange={(event) => props.onApiKeyChange(event.target.value)} value={props.apiKey} />
</label>
</>
)
}
function EmptyState({ title, copy }: { title: string; copy: string }) {
return (
<div className="empty-state">
<h4>{title}</h4>
<p>{copy}</p>
</div>
)
}
function LoadingHub() {
return (
<div className="hub-loading">
<div className="loading-placeholder" />
<p>Loading media…</p>
</div>
)
}
function resolveEntryStage(config: AppConfig, options: ServiceOptionsResponse): EntryStage {
if (!config.jellyfin.baseUrl.trim() || !config.jellyfin.apiKey.trim()) {
return 'onboarding'
}
if (!options.jellyfinUsers.length) {
return 'profiles'
}
if (!config.jellyfin.userId.trim()) {
return 'profiles'
}
return 'dashboard'
}
export default App