- components/overlay.tsx: first Dialog/ConfirmDialog (portal, esc/backdrop close, scroll lock, aria wiring, reduced-motion aware) + 7 tests - api: libraryBrowse, channelTemplates (default 404->null), artwork upload (multipart /api/artwork/uploads), pickers (client-sorted lists incl. /api/ffmpeg/profiles), createChannelFromLineup in channels.ts - schedules.ts now sources filler/watermark fetchers from pickers.ts (avoids duplicate barrel exports) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
294 lines
8.5 KiB
TypeScript
294 lines
8.5 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { ApiError, request } from './client';
|
|
import type { components } from './generated/v1';
|
|
import { getFillerPresets, getWatermarks } from './pickers';
|
|
import type { FillerPreset, Watermark } from './pickers';
|
|
|
|
export type ProgramSchedule = components['schemas']['ProgramScheduleViewModel'];
|
|
// These fields are real on the wire (the backend serializes ProgramScheduleItemViewModel
|
|
// subtypes with Newtonsoft using the runtime type), but the OpenAPI schema doesn't declare
|
|
// them: ProgramScheduleItemViewModel is an abstract base with no polymorphism annotation, so
|
|
// the generator only sees the base shape. Tracked in Gitea issue #126 — remove this widening
|
|
// once the schema is fixed to describe the concrete subtypes.
|
|
export type ProgramScheduleItem = components['schemas']['ProgramScheduleItemViewModel'] & {
|
|
count?: null | string;
|
|
discardToFillAttempts?: null | number;
|
|
multipleCount?: null | string;
|
|
multipleMode?: components['schemas']['MultipleMode'];
|
|
playoutDuration?: null | string;
|
|
tailMode?: components['schemas']['TailMode'];
|
|
};
|
|
export type ProgramScheduleItemsWithDuration = components['schemas']['ProgramScheduleItemsWithDurationViewModel'];
|
|
export type ScheduleItemRequest = components['schemas']['ScheduleItemRequest'];
|
|
export type ReplaceScheduleItemsRequest = components['schemas']['ReplaceScheduleItemsRequest'];
|
|
export type MediaCollection = components['schemas']['MediaCollectionViewModel'];
|
|
export type SmartCollection = components['schemas']['SmartCollectionViewModel'];
|
|
export type { FillerPreset, Watermark };
|
|
|
|
export interface SchedulePickerData {
|
|
collections: MediaCollection[];
|
|
fillerPresets: FillerPreset[];
|
|
smartCollections: SmartCollection[];
|
|
watermarks: Watermark[];
|
|
}
|
|
|
|
export interface ScheduleScreenData {
|
|
activeSchedule: ProgramSchedule | null;
|
|
items: ProgramScheduleItem[];
|
|
pickers: SchedulePickerData;
|
|
schedules: ProgramSchedule[];
|
|
totalDurationEstimate: string | null;
|
|
}
|
|
|
|
export type ScheduleScreenQueryState =
|
|
| {
|
|
data: ScheduleScreenData;
|
|
error: null;
|
|
itemsLoading: boolean;
|
|
refresh: () => void;
|
|
setActiveSchedule: (scheduleId: number) => void;
|
|
setItems: (items: ProgramScheduleItem[]) => void;
|
|
status: 'success';
|
|
}
|
|
| { data: null; error: string; refresh: () => void; status: 'error' }
|
|
| { data: null; error: null; refresh: () => void; status: 'loading' };
|
|
|
|
type ScheduleScreenState =
|
|
| { data: ScheduleScreenData; error: null; itemsLoading: boolean; status: 'success' }
|
|
| { data: null; error: string; status: 'error' }
|
|
| { data: null; error: null; status: 'loading' };
|
|
|
|
export function getSchedules(): Promise<ProgramSchedule[]> {
|
|
return request<ProgramSchedule[]>('/api/schedules');
|
|
}
|
|
|
|
export function getScheduleItems(scheduleId: number): Promise<ProgramScheduleItemsWithDuration> {
|
|
return request<ProgramScheduleItemsWithDuration>(`/api/schedules/${scheduleId}/items`);
|
|
}
|
|
|
|
export function getCollections(): Promise<MediaCollection[]> {
|
|
return request<MediaCollection[]>('/api/collections');
|
|
}
|
|
|
|
export function getSmartCollections(): Promise<SmartCollection[]> {
|
|
return request<SmartCollection[]>('/api/smart-collections');
|
|
}
|
|
|
|
export function addScheduleItem(scheduleId: number, body: ScheduleItemRequest): Promise<ProgramScheduleItem> {
|
|
return request<ProgramScheduleItem>(`/api/schedules/${scheduleId}/items`, {
|
|
body,
|
|
method: 'POST'
|
|
});
|
|
}
|
|
|
|
export function replaceScheduleItems(
|
|
scheduleId: number,
|
|
body: ReplaceScheduleItemsRequest
|
|
): Promise<ProgramScheduleItem[]> {
|
|
return request<ProgramScheduleItem[]>(`/api/schedules/${scheduleId}/items`, {
|
|
body,
|
|
method: 'PUT'
|
|
});
|
|
}
|
|
|
|
export function deleteScheduleItem(scheduleId: number, itemId: number): Promise<void> {
|
|
return request<void>(`/api/schedules/${scheduleId}/items/${itemId}`, {
|
|
method: 'DELETE'
|
|
});
|
|
}
|
|
|
|
export async function getScheduleScreenData(): Promise<ScheduleScreenData> {
|
|
const schedules = await getSchedules();
|
|
const activeSchedule = schedules[0] ?? null;
|
|
|
|
if (!activeSchedule) {
|
|
return {
|
|
activeSchedule: null,
|
|
items: [],
|
|
pickers: emptyPickerData(),
|
|
schedules,
|
|
totalDurationEstimate: null
|
|
};
|
|
}
|
|
|
|
const [itemsEnvelope, pickers] = await Promise.all([
|
|
getScheduleItems(activeSchedule.id),
|
|
getSchedulePickerData()
|
|
]);
|
|
|
|
return {
|
|
activeSchedule,
|
|
items: (itemsEnvelope.items ?? []) as ProgramScheduleItem[],
|
|
pickers,
|
|
schedules,
|
|
totalDurationEstimate: itemsEnvelope.totalDurationEstimate
|
|
};
|
|
}
|
|
|
|
export function useScheduleScreenQuery(): ScheduleScreenQueryState {
|
|
const [state, setState] = useState<ScheduleScreenState>({
|
|
data: null,
|
|
error: null,
|
|
status: 'loading'
|
|
});
|
|
const activeRef = useRef(true);
|
|
const activeScheduleIdRef = useRef<number | null>(null);
|
|
|
|
useEffect(() => {
|
|
activeRef.current = true;
|
|
|
|
return () => {
|
|
activeRef.current = false;
|
|
};
|
|
}, []);
|
|
|
|
const load = useCallback(() => {
|
|
getScheduleScreenData()
|
|
.then((data) => {
|
|
if (activeRef.current) {
|
|
activeScheduleIdRef.current = data.activeSchedule?.id ?? null;
|
|
setState({ data, error: null, itemsLoading: false, status: 'success' });
|
|
}
|
|
})
|
|
.catch((error: unknown) => {
|
|
if (activeRef.current) {
|
|
setState({ data: null, error: messageFromScheduleError(error, 'Unable to load schedules'), status: 'error' });
|
|
}
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
}, [load]);
|
|
|
|
const refresh = useCallback(() => {
|
|
setState({ data: null, error: null, status: 'loading' });
|
|
load();
|
|
}, [load]);
|
|
|
|
const setActiveSchedule = useCallback((scheduleId: number) => {
|
|
activeScheduleIdRef.current = scheduleId;
|
|
|
|
setState((current) => {
|
|
if (current.status !== 'success') {
|
|
return current;
|
|
}
|
|
|
|
const nextActiveSchedule = current.data.schedules.find((schedule) => schedule.id === scheduleId)
|
|
?? current.data.activeSchedule;
|
|
|
|
return {
|
|
data: { ...current.data, activeSchedule: nextActiveSchedule },
|
|
error: null,
|
|
itemsLoading: true,
|
|
status: 'success'
|
|
};
|
|
});
|
|
|
|
getScheduleItems(scheduleId)
|
|
.then((itemsEnvelope) => {
|
|
if (!activeRef.current || activeScheduleIdRef.current !== scheduleId) {
|
|
return;
|
|
}
|
|
|
|
setState((current) => {
|
|
if (current.status !== 'success') {
|
|
return current;
|
|
}
|
|
|
|
return {
|
|
data: {
|
|
...current.data,
|
|
items: (itemsEnvelope.items ?? []) as ProgramScheduleItem[],
|
|
totalDurationEstimate: itemsEnvelope.totalDurationEstimate
|
|
},
|
|
error: null,
|
|
itemsLoading: false,
|
|
status: 'success'
|
|
};
|
|
});
|
|
})
|
|
.catch((error: unknown) => {
|
|
if (!activeRef.current || activeScheduleIdRef.current !== scheduleId) {
|
|
return;
|
|
}
|
|
|
|
setState({
|
|
data: null,
|
|
error: messageFromScheduleError(error, 'Unable to load schedule items'),
|
|
status: 'error'
|
|
});
|
|
});
|
|
}, []);
|
|
|
|
const setItems = useCallback((items: ProgramScheduleItem[]) => {
|
|
setState((current) => {
|
|
if (current.status !== 'success') {
|
|
return current;
|
|
}
|
|
|
|
return {
|
|
data: { ...current.data, items },
|
|
error: null,
|
|
itemsLoading: false,
|
|
status: 'success'
|
|
};
|
|
});
|
|
}, []);
|
|
|
|
if (state.status === 'success') {
|
|
return {
|
|
data: state.data,
|
|
error: null,
|
|
itemsLoading: state.itemsLoading,
|
|
refresh,
|
|
setActiveSchedule,
|
|
setItems,
|
|
status: 'success'
|
|
};
|
|
}
|
|
|
|
if (state.status === 'error') {
|
|
return { data: null, error: state.error, refresh, status: 'error' };
|
|
}
|
|
|
|
return { data: null, error: null, refresh, status: 'loading' };
|
|
}
|
|
|
|
async function getSchedulePickerData(): Promise<SchedulePickerData> {
|
|
const [collections, smartCollections, fillerPresets, watermarks] = await Promise.all([
|
|
getCollections(),
|
|
getSmartCollections(),
|
|
getFillerPresets(),
|
|
getWatermarks()
|
|
]);
|
|
|
|
return {
|
|
collections,
|
|
fillerPresets,
|
|
smartCollections,
|
|
watermarks
|
|
};
|
|
}
|
|
|
|
function emptyPickerData(): SchedulePickerData {
|
|
return {
|
|
collections: [],
|
|
fillerPresets: [],
|
|
smartCollections: [],
|
|
watermarks: []
|
|
};
|
|
}
|
|
|
|
function messageFromScheduleError(error: unknown, fallback = 'Unable to load schedules'): string {
|
|
if (error instanceof ApiError) {
|
|
return error.detail ?? error.message;
|
|
}
|
|
|
|
if (error instanceof Error) {
|
|
return error.message;
|
|
}
|
|
|
|
return fallback;
|
|
}
|