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(parts: unknown[], fn: (s: ReturnType) => Promise, opts: Partial> = {}) { const server = useServer(); return useQuery({ queryKey: [server.machineId, ...parts], queryFn: () => fn(server), ...opts } as UseQueryOptions); } 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> = {}) => 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) }); }