Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 6s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 4m35s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m22s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (push) Has been skipped
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (push) Successful in 4m2s
Build ErsatzTV Image / Build & test (.NET) (push) Successful in 6m50s
Build ErsatzTV Image / Build & push image (amd64) (push) Successful in 4m13s
Last SPA pre-work before deleting Blazor Libraries.razor (#91 phase b): wire the shipped scanLibrary(id, deep) + scanCollections(family, id, deep) clients (F9 API, #235) into LibrariesScreen so the SPA reaches parity with Libraries.razor's four scan actions. - Deep Scan Library button on each remote (Plex/Jellyfin/Emby) library row, threading `deep` through the existing optimistic-pending/poll hook (quick + deep share the per-library lock). - External Collections section (quick + deep per remote source). Rows derive client-side from getMediaSources(): the media-sources API handler already filters each source's `libraries` to sync-enabled entries, so a remote source with a non-empty libraries list is exactly GetExternalCollections's Libraries.Any(ShouldSyncItems) filter — no new endpoint. - useCollectionsScan hook: collections scans have no scan-status poll surface (the endpoint is library-keyed; Blazor observed collections locks via in-process IEntityLocker events), so pending is optimistic + timeout-bounded (409 benign, 404/network surfaces the error). Follow-up #271 for a proper collections status surface. Pure SPA change (no backend/OpenAPI). Docs: blazor-route-parity.md §5 (SPA affordance DONE), decisions.md (derive-vs-endpoint + optimistic-timeout). Refs #91 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
480 lines
17 KiB
TypeScript
480 lines
17 KiB
TypeScript
import { useEffect, useRef, useState, type ReactNode } from 'react';
|
|
import {
|
|
Clapperboard,
|
|
Clock,
|
|
FileImage,
|
|
Film,
|
|
Folder,
|
|
HardDrive,
|
|
Library,
|
|
MonitorPlay,
|
|
Music,
|
|
Plus,
|
|
Radio,
|
|
RefreshCw,
|
|
ScanSearch,
|
|
Server,
|
|
Settings,
|
|
TriangleAlert
|
|
} from 'lucide-react';
|
|
import {
|
|
Badge,
|
|
Button,
|
|
Card,
|
|
IconButton,
|
|
ProgressBar,
|
|
Spinner,
|
|
StatusDot
|
|
} from '../components';
|
|
import {
|
|
collectionsScanKey,
|
|
useCollectionsScan,
|
|
useLibrariesScreenQuery,
|
|
type CollectionsScanSource,
|
|
type LibraryScanStatus,
|
|
type MediaSource,
|
|
type MediaSourceLibrary
|
|
} from '../api';
|
|
import { navigateToPath } from '../routing';
|
|
import {
|
|
familyRoute,
|
|
localLibraryEditRoute,
|
|
localLibraryNewRoute,
|
|
parseRemoteFamily,
|
|
REMOTE_FAMILIES
|
|
} from '../mediaSources/familyMeta';
|
|
|
|
export function LibrariesScreen() {
|
|
const query = useLibrariesScreenQuery();
|
|
const collectionsScan = useCollectionsScan();
|
|
|
|
if (query.status === 'loading') {
|
|
return <LibrariesLoadingState />;
|
|
}
|
|
|
|
if (query.status === 'error') {
|
|
return <LibrariesErrorState error={query.error} refresh={query.refresh} />;
|
|
}
|
|
|
|
if (query.data.sources.length === 0) {
|
|
return <LibrariesEmptyState />;
|
|
}
|
|
|
|
const totalItems = query.data.sources.reduce(
|
|
(sum, source) => sum + source.libraries.reduce((librarySum, library) => librarySum + library.itemCount, 0),
|
|
0
|
|
);
|
|
const scanStatusesByLibraryId = new Map(query.data.scanStatuses.map((status) => [status.libraryId, status]));
|
|
|
|
// External Collections mirrors Blazor Libraries.razor's second table: one row per Plex/Jellyfin/Emby
|
|
// source that has at least one sync-enabled library. GetAllMediaSourcesForApiHandler already filters
|
|
// each source's `libraries` to sync-enabled entries (ShouldIncludeLibrary), so a remote source with a
|
|
// non-empty `libraries` list is exactly the `Libraries.Any(ShouldSyncItems)` filter GetExternalCollections
|
|
// applies - no separate endpoint needed.
|
|
const collectionsRows = query.data.sources
|
|
.map((source) => {
|
|
const family = parseRemoteFamily(source.kind);
|
|
if (family === null || source.libraries.length === 0) {
|
|
return null;
|
|
}
|
|
return { family, name: source.name, sourceId: source.id };
|
|
})
|
|
.filter((row): row is { family: CollectionsScanSource; name: string; sourceId: number } => row !== null);
|
|
|
|
return (
|
|
<div className="ctv-libraries-screen">
|
|
<section className="ctv-libraries-header">
|
|
<div>
|
|
<span className="ctv-kicker">Sources</span>
|
|
<h2>Media Libraries</h2>
|
|
<p>Monitor connected media sources and trigger per-library scans.</p>
|
|
</div>
|
|
<div className="ctv-libraries-header-actions">
|
|
<AddSourceMenu />
|
|
</div>
|
|
</section>
|
|
|
|
{query.error && (
|
|
<div className="ctv-channels-error" role="alert">
|
|
<TriangleAlert aria-hidden="true" size={15} />
|
|
<span>{query.error}</span>
|
|
</div>
|
|
)}
|
|
|
|
<div className="ctv-libraries-summary">
|
|
<span>{query.data.sources.length} source{query.data.sources.length === 1 ? '' : 's'}</span>
|
|
<span>{totalItems.toLocaleString()} items</span>
|
|
<span><StatusDot status={query.data.scanStatuses.length > 0 ? 'live' : 'ok'} size={7} />{query.data.scanStatuses.length} active scan{query.data.scanStatuses.length === 1 ? '' : 's'}</span>
|
|
</div>
|
|
|
|
<div className="ctv-libraries-grid">
|
|
{query.data.sources.map((source) => (
|
|
<MediaSourceCard
|
|
key={source.id}
|
|
onScanLibrary={query.scanLibrary}
|
|
scanStatusesByLibraryId={scanStatusesByLibraryId}
|
|
scanningLibraryIds={query.scanningLibraryIds}
|
|
source={source}
|
|
/>
|
|
))}
|
|
</div>
|
|
|
|
{collectionsRows.length > 0 && (
|
|
<ExternalCollectionsSection
|
|
error={collectionsScan.error}
|
|
onScanCollections={collectionsScan.scan}
|
|
rows={collectionsRows}
|
|
scanningKeys={collectionsScan.scanningKeys}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// External Collections table (parity with Blazor Libraries.razor's second table). Each row is a
|
|
// remote source with sync-enabled libraries; the quick/deep buttons queue that source's collections
|
|
// scan. There is no scan-status poll surface for collections (see useCollectionsScan), so a row's
|
|
// "scanning" state is optimistic and bounded by a timeout rather than reconciled against a live set.
|
|
function ExternalCollectionsSection({
|
|
error,
|
|
onScanCollections,
|
|
rows,
|
|
scanningKeys
|
|
}: {
|
|
error: string | null;
|
|
onScanCollections: (source: CollectionsScanSource, sourceId: number, deep: boolean) => Promise<void>;
|
|
rows: Array<{ family: CollectionsScanSource; name: string; sourceId: number }>;
|
|
scanningKeys: Set<string>;
|
|
}) {
|
|
return (
|
|
<section className="ctv-library-source-card" aria-label="External collections">
|
|
<div className="ctv-library-source-head">
|
|
<span className="ctv-library-kind-icon"><Library aria-hidden="true" size={18} /></span>
|
|
<div>
|
|
<div className="ctv-library-source-title">
|
|
<h3>External Collections</h3>
|
|
</div>
|
|
<span>Sync Plex, Jellyfin, and Emby collections into ErsatzTV.</span>
|
|
</div>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="ctv-channels-error" role="alert">
|
|
<TriangleAlert aria-hidden="true" size={15} />
|
|
<span>{error}</span>
|
|
</div>
|
|
)}
|
|
|
|
<div className="ctv-library-list" role="list" aria-label="External collections list">
|
|
{rows.map((row) => {
|
|
const scanning = scanningKeys.has(collectionsScanKey(row.family, row.sourceId));
|
|
|
|
return (
|
|
<div className="ctv-library-row" key={collectionsScanKey(row.family, row.sourceId)} role="listitem">
|
|
<span className={`ctv-library-kind-icon ctv-library-kind-${row.family}`}><Server aria-hidden="true" size={16} /></span>
|
|
<div className="ctv-library-row-main">
|
|
<strong>{row.name}</strong>
|
|
<span>{familyLabel(row.family)} collections</span>
|
|
</div>
|
|
<div className="ctv-library-row-status">
|
|
{scanning ? <Badge tone="accent" dot>Scanning</Badge> : <Badge tone="ok" dot>Idle</Badge>}
|
|
<IconButton disabled={scanning} onClick={() => void onScanCollections(row.family, row.sourceId, true)} size="sm" title={`Deep scan ${row.name} collections`}>
|
|
<ScanSearch aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
<IconButton disabled={scanning} onClick={() => void onScanCollections(row.family, row.sourceId, false)} size="sm" title={`Scan ${row.name} collections`}>
|
|
<RefreshCw aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
// "Add Source" dropdown: Local library opens the create editor; each remote family opens its screen.
|
|
// Mirrors the AddToMenu popover pattern (click-outside + Escape close). Navigation goes through
|
|
// navigateToPath, which App's popstate owner turns into the right libraries sub-path (design §D.2).
|
|
function AddSourceMenu() {
|
|
const [open, setOpen] = useState(false);
|
|
const rootRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (!open) {
|
|
return undefined;
|
|
}
|
|
|
|
const onPointerDown = (event: MouseEvent) => {
|
|
if (rootRef.current && !rootRef.current.contains(event.target as Node)) {
|
|
setOpen(false);
|
|
}
|
|
};
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
if (event.key === 'Escape') {
|
|
setOpen(false);
|
|
rootRef.current?.querySelector<HTMLButtonElement>(':scope > button')?.focus();
|
|
}
|
|
};
|
|
document.addEventListener('mousedown', onPointerDown);
|
|
document.addEventListener('keydown', onKeyDown);
|
|
|
|
return () => {
|
|
document.removeEventListener('mousedown', onPointerDown);
|
|
document.removeEventListener('keydown', onKeyDown);
|
|
};
|
|
}, [open]);
|
|
|
|
const go = (path: string) => {
|
|
setOpen(false);
|
|
navigateToPath(path);
|
|
};
|
|
|
|
return (
|
|
<div className="ctv-addto-menu" ref={rootRef}>
|
|
<Button aria-expanded={open} aria-haspopup="menu" onClick={() => setOpen((v) => !v)} startIcon={<Plus aria-hidden="true" size={15} />}>
|
|
Add Source
|
|
</Button>
|
|
{open && (
|
|
<div className="ctv-addto-menu-popover" role="menu">
|
|
<button className="ctv-addto-menu-item" onClick={() => go(localLibraryNewRoute)} role="menuitem" type="button">
|
|
<HardDrive aria-hidden="true" size={14} />Local library
|
|
</button>
|
|
{REMOTE_FAMILIES.map((family) => (
|
|
<button className="ctv-addto-menu-item" key={family} onClick={() => go(familyRoute(family))} role="menuitem" type="button">
|
|
<Server aria-hidden="true" size={14} />{familyLabel(family)}
|
|
</button>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function familyLabel(family: string): string {
|
|
return family.charAt(0).toUpperCase() + family.slice(1);
|
|
}
|
|
|
|
function LibrariesLoadingState() {
|
|
return (
|
|
<Card title={<h2>Loading libraries</h2>} subtitle="Fetching media sources and active scan status.">
|
|
<div className="ctv-dashboard-state">
|
|
<Spinner tone="muted" />
|
|
<span>Loading libraries</span>
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
function LibrariesErrorState({ error, refresh }: { error: string; refresh: () => void }) {
|
|
return (
|
|
<Card
|
|
title={<h2>Libraries unavailable</h2>}
|
|
subtitle="The API returned an error while loading media sources."
|
|
actions={<Button onClick={refresh} startIcon={<RefreshCw aria-hidden="true" size={15} />} variant="secondary">Retry</Button>}
|
|
>
|
|
<div className="ctv-channels-error" role="alert">
|
|
<TriangleAlert aria-hidden="true" size={15} />
|
|
<span>{error}</span>
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
function LibrariesEmptyState() {
|
|
return (
|
|
<Card title={<h2>No media sources</h2>} subtitle="Configure a source in the server setup screens before monitoring scans here.">
|
|
<div className="ctv-schedule-empty">No media sources returned</div>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
function MediaSourceCard({
|
|
onScanLibrary,
|
|
scanStatusesByLibraryId,
|
|
scanningLibraryIds,
|
|
source
|
|
}: {
|
|
onScanLibrary: (libraryId: number, deep?: boolean) => Promise<void>;
|
|
scanStatusesByLibraryId: Map<number, LibraryScanStatus>;
|
|
scanningLibraryIds: Set<number>;
|
|
source: MediaSource;
|
|
}) {
|
|
const totalItems = source.libraries.reduce((sum, library) => sum + library.itemCount, 0);
|
|
const scanning = source.libraries.some((library) => scanningLibraryIds.has(library.id));
|
|
// Remote families (Plex/Jellyfin/Emby) manage the whole source from its family screen; Local
|
|
// libraries are edited per-row (each row is a library) via localLibraryEditRoute below.
|
|
const family = parseRemoteFamily(source.kind);
|
|
const isLocal = family === null;
|
|
|
|
const scanSource = () => {
|
|
source.libraries.forEach((library) => {
|
|
void onScanLibrary(library.id);
|
|
});
|
|
};
|
|
|
|
return (
|
|
<section className="ctv-library-source-card" aria-label={`${source.name} media source`}>
|
|
<div className="ctv-library-source-head">
|
|
<span className={`ctv-library-kind-icon ctv-library-kind-${source.kind.toLowerCase()}`}>{sourceKindIcon(source.kind)}</span>
|
|
<div>
|
|
<div className="ctv-library-source-title">
|
|
<h3>{source.name}</h3>
|
|
<StatusDot status={scanning ? 'live' : 'ok'} label={scanning ? 'Scanning' : 'Synced'} size={7} />
|
|
</div>
|
|
<span>{source.connectionAddress ?? 'Local connection'}</span>
|
|
</div>
|
|
<div className="ctv-library-source-actions">
|
|
<IconButton disabled={scanning} onClick={scanSource} size="sm" title={`Scan all libraries in ${source.name}`}>
|
|
<RefreshCw aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
{family !== null && (
|
|
<IconButton onClick={() => navigateToPath(familyRoute(family))} size="sm" title={`Manage ${source.name}`}>
|
|
<Settings aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="ctv-library-list" role="list" aria-label={`${source.name} libraries`}>
|
|
{source.libraries.length === 0 ? (
|
|
<div className="ctv-library-empty">No synced libraries returned</div>
|
|
) : source.libraries.map((library) => (
|
|
<LibraryRow
|
|
key={library.id}
|
|
allowDeepScan={!isLocal}
|
|
editable={isLocal}
|
|
library={library}
|
|
onScanLibrary={onScanLibrary}
|
|
scanStatus={scanStatusesByLibraryId.get(library.id) ?? null}
|
|
scanning={scanningLibraryIds.has(library.id)}
|
|
/>
|
|
))}
|
|
</div>
|
|
|
|
<div className="ctv-library-source-foot">
|
|
<span><Clock aria-hidden="true" size={13} />{scanning ? 'Scanning now' : sourceLastScanLabel(source)}</span>
|
|
<span><code>{totalItems.toLocaleString()}</code> items · {source.libraries.length} librar{source.libraries.length === 1 ? 'y' : 'ies'}</span>
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function LibraryRow({
|
|
allowDeepScan,
|
|
editable,
|
|
library,
|
|
onScanLibrary,
|
|
scanStatus,
|
|
scanning
|
|
}: {
|
|
allowDeepScan: boolean;
|
|
editable: boolean;
|
|
library: MediaSourceLibrary;
|
|
onScanLibrary: (libraryId: number, deep?: boolean) => Promise<void>;
|
|
scanStatus: LibraryScanStatus | null;
|
|
scanning: boolean;
|
|
}) {
|
|
const scanActive = scanStatus !== null;
|
|
const disabled = scanning || scanActive;
|
|
|
|
return (
|
|
<div className="ctv-library-row" role="listitem">
|
|
<span className="ctv-library-media-icon">{libraryMediaIcon(library.mediaKind)}</span>
|
|
<div className="ctv-library-row-main">
|
|
<strong>{library.name}</strong>
|
|
<span><span>{formatLibraryMediaKind(library.mediaKind)}</span> · <code>{library.itemCount.toLocaleString()}</code> items</span>
|
|
<small>{library.lastScan ? `Last scan ${formatDateTime(library.lastScan)}` : 'Never scanned'}</small>
|
|
</div>
|
|
<div className="ctv-library-row-status">
|
|
{scanStatus ? (
|
|
<div className="ctv-library-progress">
|
|
<ProgressBar value={scanStatus.percent} showLabel />
|
|
<Badge tone="accent" dot>Scanning</Badge>
|
|
</div>
|
|
) : (
|
|
<Badge tone="ok" dot>Synced</Badge>
|
|
)}
|
|
{allowDeepScan && (
|
|
<IconButton disabled={disabled} onClick={() => void onScanLibrary(library.id, true)} size="sm" title={`Deep scan ${library.name}`}>
|
|
<ScanSearch aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
)}
|
|
<IconButton disabled={disabled} onClick={() => void onScanLibrary(library.id)} size="sm" title={`Scan ${library.name}`}>
|
|
<RefreshCw aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
{editable && (
|
|
<IconButton onClick={() => navigateToPath(localLibraryEditRoute(library.id))} size="sm" title={`Edit ${library.name}`}>
|
|
<Settings aria-hidden="true" size={15} />
|
|
</IconButton>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function sourceKindIcon(kind: string): ReactNode {
|
|
return kind === 'Local'
|
|
? <HardDrive aria-hidden="true" size={18} />
|
|
: <Server aria-hidden="true" size={18} />;
|
|
}
|
|
|
|
function libraryMediaIcon(kind: string): ReactNode {
|
|
switch (kind) {
|
|
case 'Movies':
|
|
return <Clapperboard aria-hidden="true" size={14} />;
|
|
case 'Shows':
|
|
return <MonitorPlay aria-hidden="true" size={14} />;
|
|
case 'MusicVideos':
|
|
case 'Songs':
|
|
return <Music aria-hidden="true" size={14} />;
|
|
case 'Images':
|
|
return <FileImage aria-hidden="true" size={14} />;
|
|
case 'RemoteStreams':
|
|
return <Radio aria-hidden="true" size={14} />;
|
|
case 'OtherVideos':
|
|
return <Film aria-hidden="true" size={14} />;
|
|
default:
|
|
return <Folder aria-hidden="true" size={14} />;
|
|
}
|
|
}
|
|
|
|
function formatLibraryMediaKind(kind: string): string {
|
|
switch (kind) {
|
|
case 'MusicVideos':
|
|
return 'Music Videos';
|
|
case 'OtherVideos':
|
|
return 'Other Videos';
|
|
case 'RemoteStreams':
|
|
return 'Remote Streams';
|
|
default:
|
|
return kind;
|
|
}
|
|
}
|
|
|
|
function sourceLastScanLabel(source: MediaSource): string {
|
|
const scans = source.libraries
|
|
.map((library) => library.lastScan)
|
|
.filter((scan): scan is string => Boolean(scan))
|
|
.sort();
|
|
|
|
return scans.length > 0 ? `Last scan ${formatDateTime(scans[scans.length - 1])}` : 'Never scanned';
|
|
}
|
|
|
|
// Inlined rather than imported back from App.tsx (mirrors the ChannelsScreen #244 extraction):
|
|
// App.tsx's formatDateTime is still used by other App.tsx-owned screens (Playouts, Guide), so
|
|
// keeping this screen self-sufficient means duplicating the tiny formatter here.
|
|
function formatDateTime(value: string | null | undefined): string {
|
|
if (!value) {
|
|
return 'unknown';
|
|
}
|
|
|
|
const parsed = new Date(value);
|
|
|
|
if (Number.isNaN(parsed.getTime())) {
|
|
return value.slice(0, 5);
|
|
}
|
|
|
|
return parsed.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
|
}
|