All checks were successful
Validate Kaleido Desktop / validate (push) Successful in 1m24s
- Automatic intro/credits skip waits a configurable delay (default 5 s) with a filling "Skipping intro" button; pausing, seeking or pressing a key cancels it, and seeking into an intro never auto-skips it. - Watch-state queries refetch on window focus, and Continue Watching / next episode poll every minute while visible, so progress from other Plex apps shows up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
3.1 KiB
TypeScript
65 lines
3.1 KiB
TypeScript
import { QueryClient, useQuery, type UseQueryOptions } from '@tanstack/react-query';
|
|
import * as api from '../plex/api';
|
|
import type { Item, Section } from '../plex/types';
|
|
import { useServer } from './session';
|
|
import { useSettings } from './settings';
|
|
|
|
export const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
staleTime: 60_000,
|
|
gcTime: 30 * 60_000,
|
|
retry: 1,
|
|
refetchOnWindowFocus: false,
|
|
},
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Watch state changes from other Plex apps too (a TV, a phone), so the queries that show
|
|
* it refresh when Kaleido comes back into view and, for the progress rows, every minute
|
|
* while it is on screen. Polling pauses while the window is hidden or minimised.
|
|
*/
|
|
export const WATCH_STATE = { refetchOnWindowFocus: true } as const;
|
|
export const PROGRESS = { ...WATCH_STATE, staleTime: 15_000, refetchInterval: 60_000 } as const;
|
|
|
|
// React Query treats a visibility change as focus. Switching back to Kaleido from another
|
|
// app doesn't change visibility, so a window focus counts as well.
|
|
window.addEventListener('focus', () => queryClient.getQueryCache().onFocus());
|
|
|
|
/** Every key starts with the server so switching servers never mixes caches. */
|
|
export function useKey(...parts: unknown[]) {
|
|
const server = useServer();
|
|
return [server.machineId, ...parts];
|
|
}
|
|
|
|
export function useServerQuery<T>(parts: unknown[], fn: (s: ReturnType<typeof useServer>) => Promise<T>, opts: Partial<UseQueryOptions<T>> = {}) {
|
|
const server = useServer();
|
|
return useQuery<T>({ queryKey: [server.machineId, ...parts], queryFn: () => fn(server), ...opts } as UseQueryOptions<T>);
|
|
}
|
|
|
|
export const useSections = () => useServerQuery(['sections'], (s) => api.sections(s), { staleTime: 5 * 60_000 });
|
|
|
|
/** The library a Movies or TV destination shows: the user's choice, else the first. */
|
|
export function useSectionFor(kind: 'movie' | 'show'): { section: Section | null; all: Section[]; loading: boolean } {
|
|
const { data, isLoading } = useSections();
|
|
const chosen = useSettings((s) => (kind === 'movie' ? s.movieSection : s.showSection));
|
|
const all = (data ?? []).filter((s) => s.type === kind && !s.hidden);
|
|
return { section: all.find((s) => s.id === chosen) ?? all[0] ?? null, all, loading: isLoading };
|
|
}
|
|
|
|
export const useItem = (id: string | undefined, opts: Partial<UseQueryOptions<Item>> = {}) =>
|
|
useServerQuery(['item', id], (s) => api.metadata(s, id!), { enabled: !!id, staleTime: 30_000, ...WATCH_STATE, ...opts });
|
|
|
|
export const useChildren = (id: string | undefined) => useServerQuery(['children', id], (s) => api.children(s, id!), { enabled: !!id, staleTime: 30_000, ...WATCH_STATE });
|
|
|
|
export const useRelated = (id: string | undefined) => useServerQuery(['related', id], (s) => api.related(s, id!), { enabled: !!id, staleTime: 10 * 60_000 });
|
|
|
|
/** After a watched-state or metadata change, refresh everything that may show it. */
|
|
export function invalidateLibrary() {
|
|
queryClient.invalidateQueries({ predicate: (q) => !['sections', 'libraryMeta', 'filterOptions', 'serverPrefs'].includes(String(q.queryKey[1])) });
|
|
}
|
|
|
|
export function invalidateItem(id: string) {
|
|
queryClient.invalidateQueries({ predicate: (q) => q.queryKey.includes(id) });
|
|
}
|