2026-04-17 23:36:24 +08:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { useQuery } from "@tanstack/react-query";
|
2025-11-23 10:55:04 +08:00
|
|
|
import type { SidebarInitialData } from "@/components/sidebar/types";
|
2026-04-17 23:36:24 +08:00
|
|
|
import { useConvexSidebarData } from "@/hooks/use-convex-sidebar-data";
|
|
|
|
|
|
|
|
|
|
export interface SidebarDataResult {
|
|
|
|
|
data: SidebarInitialData;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
error: Error | null;
|
|
|
|
|
refetch: () => Promise<unknown>;
|
|
|
|
|
source: "convex-live" | "http-fallback" | "initial";
|
|
|
|
|
}
|
2025-11-23 10:55:04 +08:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 23:36:24 +08:00
|
|
|
export function useSidebarData(initialData: SidebarInitialData): SidebarDataResult {
|
2025-11-23 10:55:04 +08:00
|
|
|
const workspaceId = initialData.activeWorkspaceId;
|
2026-04-17 23:36:24 +08:00
|
|
|
const convexSidebar = useConvexSidebarData(workspaceId);
|
|
|
|
|
const shouldUseHttpFallback =
|
|
|
|
|
!convexSidebar.hasLiveSubscription && convexSidebar.canUseHttpFallback;
|
2025-11-23 10:55:04 +08:00
|
|
|
|
2026-04-17 23:36:24 +08:00
|
|
|
const httpQuery = useQuery({
|
2025-11-23 10:55:04 +08:00
|
|
|
queryKey: ["sidebar", workspaceId],
|
|
|
|
|
queryFn: () => requestSidebarData(workspaceId),
|
|
|
|
|
initialData,
|
|
|
|
|
staleTime: 1000 * 60,
|
2026-04-17 23:36:24 +08:00
|
|
|
enabled: shouldUseHttpFallback,
|
2025-11-23 10:55:04 +08:00
|
|
|
});
|
2026-04-17 23:36:24 +08:00
|
|
|
|
|
|
|
|
return useMemo<SidebarDataResult>(() => {
|
|
|
|
|
const liveData = convexSidebar.data ?? httpQuery.data ?? initialData;
|
|
|
|
|
const isLoading =
|
|
|
|
|
convexSidebar.hasLiveSubscription
|
|
|
|
|
? convexSidebar.isLoading
|
|
|
|
|
: shouldUseHttpFallback
|
|
|
|
|
? httpQuery.isLoading
|
|
|
|
|
: false;
|
|
|
|
|
const source: SidebarDataResult["source"] = convexSidebar.data
|
|
|
|
|
? "convex-live"
|
|
|
|
|
: shouldUseHttpFallback && httpQuery.data
|
|
|
|
|
? "http-fallback"
|
|
|
|
|
: "initial";
|
|
|
|
|
const refetch = async () => {
|
|
|
|
|
if (convexSidebar.hasLiveSubscription) {
|
|
|
|
|
await convexSidebar.refetch();
|
|
|
|
|
return liveData;
|
|
|
|
|
}
|
|
|
|
|
if (shouldUseHttpFallback) {
|
|
|
|
|
return httpQuery.refetch();
|
|
|
|
|
}
|
|
|
|
|
return liveData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data: liveData,
|
|
|
|
|
isLoading,
|
|
|
|
|
error: convexSidebar.error ?? httpQuery.error ?? null,
|
|
|
|
|
refetch,
|
|
|
|
|
source,
|
|
|
|
|
};
|
|
|
|
|
}, [
|
|
|
|
|
convexSidebar,
|
|
|
|
|
httpQuery,
|
|
|
|
|
initialData,
|
|
|
|
|
shouldUseHttpFallback,
|
|
|
|
|
]);
|
2025-11-23 10:55:04 +08:00
|
|
|
}
|