1- import { NavigationTreeState } from './types' ;
1+ import {
2+ NavigationTreeNodeState ,
3+ NavigationTreeNodePartialState ,
4+ NavigationTreeState ,
5+ } from './types' ;
26
37export enum NavigationTreeActionType {
48 ToggleCollapsed = 'toggle-collapsed' ,
@@ -21,6 +25,16 @@ export function getDefaultNodeState() {
2125 } ;
2226}
2327
28+ export function getNodeState (
29+ partialState : NavigationTreeNodePartialState ,
30+ ) : NavigationTreeNodeState {
31+ return {
32+ ...getDefaultNodeState ( ) ,
33+ expandable : partialState . type === 'database' || partialState . type === 'directory' ,
34+ ...partialState ,
35+ } ;
36+ }
37+
2438export function reducer ( state : NavigationTreeState = { } , action : NavigationTreeAction ) {
2539 switch ( action . type ) {
2640 case NavigationTreeActionType . ToggleCollapsed :
@@ -60,21 +74,17 @@ export function reducer(state: NavigationTreeState = {}, action: NavigationTreeA
6074
6175 for ( const item of action . payload . data ) {
6276 const path = `${ action . payload . path } /${ item . name } ` ;
63- const name = item . name ;
64- const type = item . type ;
6577
6678 // expand the tree according to the active path
6779 // prioritize the existing state to expand the tree only on first load
6880 const { activePath = '' } = action . payload ;
6981 const collapsed = state [ path ] ?. collapsed ?? ! activePath . startsWith ( `${ path } /` ) ;
7082
71- newState [ path ] = {
72- ...getDefaultNodeState ( ) ,
83+ newState [ path ] = getNodeState ( {
84+ ...item ,
7385 collapsed,
7486 path,
75- name,
76- type,
77- } ;
87+ } ) ;
7888 }
7989 }
8090
0 commit comments