diff --git a/hooks/useLibraryData.ts b/hooks/useLibraryData.ts index 4944eaf5..7633f08e 100644 --- a/hooks/useLibraryData.ts +++ b/hooks/useLibraryData.ts @@ -20,8 +20,9 @@ export function useLibraryData(initialFilters?: Partial) { const service = useMemo(() => libraryService, []); // Create a stable query key based on filter values (excluding pagination.skip) + // Use queryKeys.library.books() as base to ensure consistency with refresh/invalidation const queryKey = useMemo(() => [ - 'library-books', + ...queryKeys.library.books(), filters.status, filters.search, filters.tags, diff --git a/hooks/usePullToRefreshLogic.ts b/hooks/usePullToRefreshLogic.ts index 1c8f690e..d0860772 100644 --- a/hooks/usePullToRefreshLogic.ts +++ b/hooks/usePullToRefreshLogic.ts @@ -50,6 +50,12 @@ export function usePullToRefreshLogic() { if (queryKey) { await queryClient.invalidateQueries({ queryKey }); + } else if (pathname === "/journal") { + // Special case: Invalidate both journal entries and archive using prefix matching + await Promise.all([ + queryClient.invalidateQueries({ queryKey: queryKeys.journal.entriesBase() }), + queryClient.invalidateQueries({ queryKey: queryKeys.journal.archiveBase() }), + ]); } else { // Fallback: invalidate all queries if we don't have specific keys await queryClient.invalidateQueries(); diff --git a/hooks/useShelfBooks.ts b/hooks/useShelfBooks.ts index b7171585..96616433 100644 --- a/hooks/useShelfBooks.ts +++ b/hooks/useShelfBooks.ts @@ -26,7 +26,7 @@ export function useShelfBooks( ? queryKeys.shelf.detail(shelfId, { orderBy, direction }) : ['shelf-empty'], queryFn: async () => { - if (!shelfId) return null; + if (shelfId === null) return null; const shelf = await shelfApi.get(shelfId, { withBooks: true, @@ -53,7 +53,7 @@ export function useShelfBooks( */ const addBooksMutation = useMutation({ mutationFn: async (bookIds: number[]) => { - if (!shelfId || bookIds.length === 0) { + if (shelfId === null || bookIds.length === 0) { throw new Error("No shelf ID or book IDs provided"); } return shelfApi.addBooks(shelfId, { bookIds }); @@ -73,7 +73,7 @@ export function useShelfBooks( */ const removeBookMutation = useMutation({ mutationFn: async (bookId: number) => { - if (!shelfId) { + if (shelfId === null) { throw new Error("No shelf ID provided"); } return shelfApi.removeBook(shelfId, bookId); @@ -127,7 +127,7 @@ export function useShelfBooks( */ const removeBooksMutation = useMutation({ mutationFn: async (bookIds: number[]) => { - if (!shelfId || bookIds.length === 0) { + if (shelfId === null || bookIds.length === 0) { throw new Error("No shelf ID or book IDs provided"); } return shelfApi.removeBooks(shelfId, { bookIds }); @@ -182,7 +182,7 @@ export function useShelfBooks( */ const reorderBooksMutation = useMutation({ mutationFn: async (bookIds: number[]) => { - if (!shelfId) { + if (shelfId === null) { throw new Error("No shelf ID provided"); } return shelfApi.reorderBooks(shelfId, { bookIds }); @@ -245,7 +245,7 @@ export function useShelfBooks( bookIds: number[]; targetShelfName?: string; }) => { - if (!shelfId || bookIds.length === 0) { + if (shelfId === null || bookIds.length === 0) { throw new Error("No shelf ID or book IDs provided"); } return shelfApi.moveBooks(shelfId, targetShelfId, { bookIds }); @@ -329,7 +329,7 @@ export function useShelfBooks( */ const moveToTopMutation = useMutation({ mutationFn: async (bookId: number) => { - if (!shelfId) { + if (shelfId === null) { throw new Error("No shelf ID provided"); } @@ -399,7 +399,7 @@ export function useShelfBooks( const moveToBottomMutation = useMutation({ mutationFn: async (bookId: number) => { - if (!shelfId) { + if (shelfId === null) { throw new Error("No shelf ID provided"); } diff --git a/lib/hooks/usePageTitle.ts b/lib/hooks/usePageTitle.ts index 463dd394..7c66a0fb 100644 --- a/lib/hooks/usePageTitle.ts +++ b/lib/hooks/usePageTitle.ts @@ -1,4 +1,4 @@ -import { useLayoutEffect, useRef, useEffect } from "react"; +import { useLayoutEffect, useEffect } from "react"; /** * Custom hook to set the page title diff --git a/lib/query-keys.ts b/lib/query-keys.ts index e74e8332..658ae618 100644 --- a/lib/query-keys.ts +++ b/lib/query-keys.ts @@ -197,6 +197,12 @@ export const queryKeys = { // JOURNAL // ============================================================================ journal: { + /** Base key for journal entries (prefix matching): ['journal-entries'] */ + entriesBase: () => ['journal-entries'] as const, + + /** Base key for journal archive (prefix matching): ['journal-archive'] */ + archiveBase: () => ['journal-archive'] as const, + /** Journal entries for timezone: ['journal-entries', timezone] */ entries: (timezone: string) => ['journal-entries', timezone] as const,