import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; import { getUserViewApi } from "@jellyfin/sdk/lib/utils/api/user-view-api"; import { useQuery } from "@tanstack/react-query"; import { useMemo } from "react"; import { useApiInContext } from "@/utils/store/api"; import { useCentralStore } from "@/utils/store/central"; export type AppNavItem = | { kind: "home"; key: "home"; label: string; } | { kind: "library"; key: "movies" | "tvshows" | "music"; label: string; libraryId: string; }; const LIBRARY_NAV = [ { key: "movies", collectionType: "movies", label: "Фильмы" }, { key: "tvshows", collectionType: "tvshows", label: "Сериалы" }, { key: "music", collectionType: "music", label: "Музыка" }, ] as const; /** * The four permanent sections of the app titlebar. Library items are resolved * from the same cached `getUserViews` query the home page already loads. */ export const useAppNavigation = (enabled: boolean) => { const api = useApiInContext((s) => s.api); const user = useCentralStore((s) => s.currentUser); const libraries = useQuery({ queryKey: ["libraries"], queryFn: async () => { if (!api?.accessToken || !user?.Id) return undefined; const response = await getUserViewApi(api).getUserViews({ userId: user.Id, }); return response.data; }, enabled: enabled && !!api?.accessToken && !!user?.Id, staleTime: 5 * 60 * 1000, networkMode: "always", }); const items = useMemo(() => { const list: AppNavItem[] = [ { kind: "home", key: "home", label: "Главная" }, ]; const views: BaseItemDto[] = libraries.data?.Items ?? []; for (const entry of LIBRARY_NAV) { const library = views.find( (view) => view.Id && view.CollectionType === entry.collectionType, ); if (!library?.Id) continue; list.push({ kind: "library", key: entry.key, label: entry.label, libraryId: library.Id, }); } return list; }, [libraries.data]); return items; };