29 lines
893 B
TypeScript
29 lines
893 B
TypeScript
import { useQuery, type UseQueryResult } from "@tanstack/react-query";
|
|
import type { SidebarInitialData } from "@/components/sidebar/types";
|
|
|
|
async function requestSidebarData(workspaceId: string): Promise<SidebarInitialData> {
|
|
const response = await fetch(`/api/sidebar?workspaceId=${workspaceId}`, {
|
|
method: "GET",
|
|
credentials: "include",
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const payload = await response.json().catch(() => ({}));
|
|
const message = payload?.error ?? "获取侧边栏数据失败";
|
|
throw new Error(message);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export function useSidebarData(initialData: SidebarInitialData): UseQueryResult<SidebarInitialData> {
|
|
const workspaceId = initialData.activeWorkspaceId;
|
|
|
|
return useQuery({
|
|
queryKey: ["sidebar", workspaceId],
|
|
queryFn: () => requestSidebarData(workspaceId),
|
|
initialData,
|
|
staleTime: 1000 * 60,
|
|
});
|
|
}
|