|
| 1 | +import type { QueryClient } from '@tanstack/react-query' |
| 2 | +import type { FolderApi } from '@/lib/api/contracts' |
| 3 | +import type { ListWorkspaceFilesResponse } from '@/lib/api/contracts/workspace-files' |
| 4 | +import { prefetchInternalJson } from '@/app/workspace/[workspaceId]/lib/prefetch-internal-fetch' |
| 5 | +import { FOLDER_LIST_STALE_TIME, mapFolder } from '@/hooks/queries/folders' |
| 6 | +import { folderKeys } from '@/hooks/queries/utils/folder-keys' |
| 7 | +import { workspaceFilesKeys } from '@/hooks/queries/workspace-files' |
| 8 | + |
| 9 | +/** |
| 10 | + * Prefetches the home page's secondary lists — folders and workspace files — |
| 11 | + * under the same query keys their client hooks (`useFolders`, |
| 12 | + * `useWorkspaceFiles`) use, so the home view paints populated on first render. |
| 13 | + * |
| 14 | + * The workflow list (`workflowKeys.list(ws, 'active')`) is already hydrated by |
| 15 | + * the workspace sidebar prefetch and is intentionally not repeated here. |
| 16 | + * |
| 17 | + * Folders are fetched through the route and mapped with the same `mapFolder` |
| 18 | + * the hook applies, matching its cached shape (string dates → `Date`). Files |
| 19 | + * carry `Date` fields, so they go through the route and cache the serialized |
| 20 | + * wire shape — see {@link prefetchInternalJson}. |
| 21 | + */ |
| 22 | +export async function prefetchHomeLists( |
| 23 | + queryClient: QueryClient, |
| 24 | + workspaceId: string |
| 25 | +): Promise<void> { |
| 26 | + await Promise.all([ |
| 27 | + queryClient.prefetchQuery({ |
| 28 | + queryKey: folderKeys.list(workspaceId, 'active'), |
| 29 | + queryFn: async () => { |
| 30 | + const { folders } = await prefetchInternalJson<{ folders?: FolderApi[] }>( |
| 31 | + `/api/folders?workspaceId=${workspaceId}&scope=active` |
| 32 | + ) |
| 33 | + return (folders ?? []).map(mapFolder) |
| 34 | + }, |
| 35 | + staleTime: FOLDER_LIST_STALE_TIME, |
| 36 | + }), |
| 37 | + queryClient.prefetchQuery({ |
| 38 | + queryKey: workspaceFilesKeys.list(workspaceId, 'active'), |
| 39 | + queryFn: async () => { |
| 40 | + const data = await prefetchInternalJson<ListWorkspaceFilesResponse>( |
| 41 | + `/api/workspaces/${workspaceId}/files?scope=active` |
| 42 | + ) |
| 43 | + return data.success ? data.files : [] |
| 44 | + }, |
| 45 | + staleTime: 30 * 1000, |
| 46 | + }), |
| 47 | + ]) |
| 48 | +} |
0 commit comments