fix(sidebar): 让文件树即时应用页面和资源变更
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { buildSidebarDataSyncKey } from "@/components/sidebar/sidebar-sync";
|
||||
import {
|
||||
buildSidebarDataSyncKey,
|
||||
getSidebarDataFreshness,
|
||||
} from "@/components/sidebar/sidebar-sync";
|
||||
import type { SidebarInitialData } from "@/components/sidebar/types";
|
||||
import { useConvexSidebarData } from "@/hooks/use-convex-sidebar-data";
|
||||
|
||||
@@ -48,6 +51,10 @@ async function requestSidebarData(workspaceId: string): Promise<SidebarInitialDa
|
||||
export function useSidebarData(initialData: SidebarInitialData): SidebarDataResult {
|
||||
const workspaceId = initialData.activeWorkspaceId;
|
||||
const convexSidebar = useConvexSidebarData(workspaceId);
|
||||
const [manualSnapshot, setManualSnapshot] = useState<{
|
||||
workspaceId: string;
|
||||
data: SidebarInitialData;
|
||||
} | null>(null);
|
||||
const shouldUseHttpFallback =
|
||||
!convexSidebar.hasLiveSubscription && convexSidebar.canUseHttpFallback;
|
||||
|
||||
@@ -59,7 +66,28 @@ export function useSidebarData(initialData: SidebarInitialData): SidebarDataResu
|
||||
enabled: shouldUseHttpFallback,
|
||||
});
|
||||
|
||||
const baseLiveData = convexSidebar.data ?? httpQuery.data ?? initialData;
|
||||
const baseLiveData = useMemo(() => {
|
||||
const currentManualSnapshot = manualSnapshot?.workspaceId === workspaceId
|
||||
? manualSnapshot.data
|
||||
: null;
|
||||
const candidates = [
|
||||
initialData,
|
||||
httpQuery.data,
|
||||
convexSidebar.data,
|
||||
currentManualSnapshot,
|
||||
].filter((item): item is SidebarInitialData => Boolean(item));
|
||||
return candidates.reduce((selected, candidate) => {
|
||||
const selectedFreshness = getSidebarDataFreshness(selected);
|
||||
const candidateFreshness = getSidebarDataFreshness(candidate);
|
||||
return candidateFreshness >= selectedFreshness ? candidate : selected;
|
||||
}, initialData);
|
||||
}, [
|
||||
convexSidebar.data,
|
||||
httpQuery.data,
|
||||
initialData,
|
||||
manualSnapshot,
|
||||
workspaceId,
|
||||
]);
|
||||
const liveData = baseLiveData;
|
||||
const isLoading =
|
||||
convexSidebar.hasLiveSubscription
|
||||
@@ -93,7 +121,9 @@ export function useSidebarData(initialData: SidebarInitialData): SidebarDataResu
|
||||
const refetch = useCallback(async () => {
|
||||
if (convexSidebar.hasLiveSubscription) {
|
||||
await convexRefetchRef.current();
|
||||
return liveDataRef.current;
|
||||
const snapshot = await requestSidebarData(workspaceId);
|
||||
setManualSnapshot({ workspaceId, data: snapshot });
|
||||
return snapshot;
|
||||
}
|
||||
if (shouldUseHttpFallback) {
|
||||
return httpRefetchRef.current();
|
||||
|
||||
Reference in New Issue
Block a user