11import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
22import { LoadableArray , RootTree , TreeNode , TreeSource , TreeSourceNode , TreeState } from './types' ;
3+ import { objectFromEntries } from './util' ;
34
45interface StringMap < V > {
56 [ k : string ] : V ;
@@ -63,13 +64,15 @@ export function useTreeLoader<T>(
6364 // Get active trail IDs from active ID.
6465 const activeTrailIds = useMemo (
6566 ( ) => ( activeId && trails [ activeId ] ) ? trails [ activeId ] . map ( ( node ) => node . id ) : [ ]
66- , [ activeId , trails ] ) ;
67+ , [ activeId , trails ] ) ;
6768
6869 // Add new trails and their sub trails.
6970 const addTrails = useCallback ( ( newTrails : Array < Array < TreeSourceNode < T > > > ) => {
7071 setTrails ( ( currentTrails ) => {
71- const newEntries = newTrails . map ( ( trail ) => [ trail [ 0 ] . id , trail ] ) ;
72- return newEntries . length > 0 ? { ...currentTrails , ...Object . fromEntries ( newEntries ) } : currentTrails ;
72+ const newEntries : Array < [ string , Array < TreeSourceNode < T > > ] > = newTrails . map (
73+ ( trail ) => [ trail [ 0 ] . id , trail ] ,
74+ ) ;
75+ return newEntries . length > 0 ? { ...currentTrails , ...objectFromEntries ( newEntries ) } : currentTrails ;
7376 } ) ;
7477 } , [ setTrails ] ) ;
7578
@@ -110,8 +113,8 @@ export function useTreeLoader<T>(
110113 }
111114
112115 const enableChildrenLoadingState = ( ) => {
113- setChildren ( ( currentChildren ) => ( {
114- ...currentChildren , ...Object . fromEntries ( idsToLoad . map ( ( id ) => [ id , { isLoading : true , items : [ ] } ] ) ) ,
116+ setChildren ( ( currentChildren ) => ( {
117+ ...currentChildren , ...objectFromEntries ( idsToLoad . map ( ( id ) => [ id , { isLoading : true , items : [ ] } ] ) ) ,
115118 } ) ) ;
116119 } ;
117120 let loadingTransitionTimeout : unknown | null = null ;
@@ -125,8 +128,10 @@ export function useTreeLoader<T>(
125128
126129 // Load them from the source.
127130 Promise . all (
128- idsToLoad . map ( ( id ) => source . children ( id ) . then ( ( items ) => [ id , { loading : false , items } ] ) ) ,
129- ) . then ( ( results ) => {
131+ idsToLoad . map (
132+ ( id ) => source . children ( id ) . then ( ( items ) => [ id , { isLoading : false , items } ] ) ,
133+ ) as Array < Promise < [ string , LoadableArray < TreeSourceNode < T > > ] > > ,
134+ ) . then ( ( results : Array < [ string , LoadableArray < TreeSourceNode < T > > ] > ) => {
130135 if ( loadingTransitionTimeout !== null ) {
131136 clearTimeout ( loadingTransitionTimeout as any ) ;
132137 }
@@ -137,7 +142,7 @@ export function useTreeLoader<T>(
137142 }
138143
139144 // Add the children to state.
140- const loadedChildren : StringMap < LoadableArray < TreeSourceNode < T > > > = Object . fromEntries ( results ) ;
145+ const loadedChildren : StringMap < LoadableArray < TreeSourceNode < T > > > = objectFromEntries ( results ) ;
141146 setChildren ( ( currentChildren ) => ( { ...currentChildren , ...loadedChildren } ) ) ;
142147
143148 // Add trails for the new children so we can make them active.
@@ -148,7 +153,7 @@ export function useTreeLoader<T>(
148153 } , [ expandedIds , children , trails , activeTrailIds , source , addTrails , setChildren , loadingTransitionMs ] ) ;
149154
150155 return useMemo ( ( ) => {
151- const activeTrailIdsIndex = Object . fromEntries ( activeTrailIds . map ( ( id ) => [ id , true ] ) ) ;
156+ const activeTrailIdsIndex = objectFromEntries ( activeTrailIds . map ( ( id ) => [ id , true ] ) ) ;
152157 const expandedIdsIndex = expandedIds || { } ;
153158
154159 function buildOutputNode ( node : TreeSourceNode < T > , depth : number ) : TreeNode < T > {
0 commit comments