- Display all seen dates inline (comma-separated) on episode cards in SeasonPage; remove unused Tooltip imports - Propagate episode_count in normalizeCredit so TV series cast shows X ep. - Add entriesApi.getCredits(id) API method and useEntryCredits(id) hook - Load all credits in EntryCastsPage via useEntryCredits (no more 10-cast limit) - Replace full-list render with IntersectionObserver lazy loader (24 per chunk) - Fix Sort/Filter Select trigger to show human-readable label instead of raw key
176 lines
4.8 KiB
TypeScript
176 lines
4.8 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { toast } from 'sonner';
|
|
import { entriesApi } from '@/api/entries';
|
|
import { useLibraryStore } from '@/stores/useLibraryStore';
|
|
import type { Entry } from '@/types';
|
|
|
|
export const entryKeys = {
|
|
all: ['entries'] as const,
|
|
detail: (id: number) => ['entries', id] as const,
|
|
seasons: (id: number) => ['entries', id, 'seasons'] as const,
|
|
credits: (id: number) => ['entries', id, 'credits'] as const,
|
|
tmdbPreview: (tmdbId: number, type: string) => ['entries', 'tmdb-preview', type, tmdbId] as const,
|
|
};
|
|
|
|
export function useLibrary() {
|
|
const setEntries = useLibraryStore((s) => s.setEntries);
|
|
|
|
return useQuery({
|
|
queryKey: entryKeys.all,
|
|
queryFn: async () => {
|
|
const data = await entriesApi.getAll();
|
|
setEntries(data);
|
|
return data;
|
|
},
|
|
staleTime: 5 * 60 * 1000,
|
|
});
|
|
}
|
|
|
|
export function useEntry(id: number) {
|
|
return useQuery({
|
|
queryKey: entryKeys.detail(id),
|
|
queryFn: () => entriesApi.getById(id),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useEntrySeasons(id: number) {
|
|
return useQuery({
|
|
queryKey: entryKeys.seasons(id),
|
|
queryFn: () => entriesApi.getSeasons(id),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useEntryCredits(id: number) {
|
|
return useQuery({
|
|
queryKey: entryKeys.credits(id),
|
|
queryFn: () => entriesApi.getCredits(id),
|
|
enabled: !!id,
|
|
staleTime: 5 * 60 * 1000,
|
|
});
|
|
}
|
|
|
|
export function useCreateEntry() {
|
|
const queryClient = useQueryClient();
|
|
const addEntry = useLibraryStore((s) => s.addEntry);
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
tmdb_id,
|
|
is_tvserie,
|
|
}: {
|
|
tmdb_id: number;
|
|
is_tvserie: boolean;
|
|
}) => entriesApi.create(tmdb_id, is_tvserie),
|
|
onSuccess: (entry) => {
|
|
addEntry(entry);
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.all });
|
|
toast.success(`"${entry.title}" added to library`);
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to add entry to library');
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateEntry() {
|
|
const queryClient = useQueryClient();
|
|
const updateEntry = useLibraryStore((s) => s.updateEntry);
|
|
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: number; data: Partial<Entry> }) =>
|
|
entriesApi.update(id, data),
|
|
onSuccess: (entry) => {
|
|
updateEntry(entry.id, entry);
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.detail(entry.id) });
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.all });
|
|
toast.success('Entry updated');
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to update entry');
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteEntry() {
|
|
const queryClient = useQueryClient();
|
|
const removeEntry = useLibraryStore((s) => s.removeEntry);
|
|
|
|
return useMutation({
|
|
mutationFn: (id: number) => entriesApi.delete(id),
|
|
onSuccess: (_data, id) => {
|
|
removeEntry(id);
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.all });
|
|
toast.success('Entry removed from library');
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to remove entry');
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useRefreshEntry() {
|
|
const queryClient = useQueryClient();
|
|
const updateEntry = useLibraryStore((s) => s.updateEntry);
|
|
|
|
return useMutation({
|
|
mutationFn: (id: number) => entriesApi.refresh(id),
|
|
onSuccess: (entry) => {
|
|
updateEntry(entry.id, entry);
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.detail(entry.id) });
|
|
toast.success('Entry details refreshed');
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to refresh entry');
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateEntryRatings() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
ratings,
|
|
}: {
|
|
id: number;
|
|
ratings: Parameters<typeof entriesApi.updateRatings>[1];
|
|
}) => entriesApi.updateRatings(id, ratings),
|
|
onSuccess: (_data, { id }) => {
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.detail(id) });
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.all });
|
|
toast.success('Rating updated');
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to update rating');
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUploadMedia() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ id, formData }: { id: number; formData: FormData }) =>
|
|
entriesApi.uploadMedia(id, formData),
|
|
onSuccess: (_data, { id }) => {
|
|
queryClient.invalidateQueries({ queryKey: entryKeys.detail(id) });
|
|
toast.success('Media uploaded successfully');
|
|
},
|
|
onError: () => {
|
|
toast.error('Failed to upload media');
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTmdbPreview(tmdbId: number, type: 'movie' | 'tv') {
|
|
return useQuery({
|
|
queryKey: entryKeys.tmdbPreview(tmdbId, type),
|
|
queryFn: () => entriesApi.previewTmdb(tmdbId, type),
|
|
enabled: !!tmdbId,
|
|
staleTime: 10 * 60 * 1000,
|
|
});
|
|
}
|