11import { useQuery , useQueryClient } from '@tanstack/react-query' ;
22import { getBaseNodeChannel } from '@teable/core' ;
3- import type { IBaseNodeVo } from '@teable/openapi' ;
3+ import type { IBaseNodeTreeVo , IBaseNodeVo } from '@teable/openapi' ;
44import { getBaseNodeTree } from '@teable/openapi' ;
55import { ReactQueryKeys } from '@teable/sdk/config' ;
66import { useConnection } from '@teable/sdk/hooks' ;
@@ -16,10 +16,21 @@ export const useBaseNode = (baseId: string) => {
1616 const channel = getBaseNodeChannel ( baseId ) ;
1717 const presence = connection ?. getPresence ( channel ) ;
1818 const [ nodes , setNodes ] = useState < IBaseNodeVo [ ] > ( [ ] ) ;
19- const [ treeItems , setTreeItems ] = useState < Record < string , TreeItemData > > ( { } ) ;
2019 const [ shouldInvalidate , setShouldInvalidate ] = useState ( 0 ) ;
2120
2221 const queryClient = useQueryClient ( ) ;
22+
23+ // Initialize treeItems from cache to avoid flash of empty state on remount
24+ const [ treeItems , setTreeItems ] = useState < Record < string , TreeItemData > > ( ( ) => {
25+ const cachedData = queryClient . getQueryData < IBaseNodeTreeVo > (
26+ ReactQueryKeys . baseNodeTree ( baseId )
27+ ) ;
28+ if ( cachedData ?. nodes && cachedData . nodes . length > 0 ) {
29+ return buildTreeItems ( cachedData . nodes ) ;
30+ }
31+ return { } ;
32+ } ) ;
33+
2334 const { data : queryData , isLoading } = useQuery ( {
2435 queryKey : ReactQueryKeys . baseNodeTree ( baseId ) ,
2536 queryFn : ( { queryKey } ) => getBaseNodeTree ( queryKey [ 1 ] ) . then ( ( res ) => res . data ) ,
0 commit comments