|
1 | 1 | import React from 'react'; |
| 2 | +import ReactList from 'react-list'; |
| 3 | + |
| 4 | +import type { |
| 5 | + NavigationTreeNodeState, |
| 6 | + NavigationTreeProps, |
| 7 | + NavigationTreeServiceNode, |
| 8 | +} from './types'; |
| 9 | +import {reducer, getNodeState, selectTreeAsList} from './state'; |
| 10 | +import {isServiceNode} from './utils'; |
2 | 11 |
|
3 | | -import {NavigationTreeProps} from './types'; |
4 | | -import {reducer, getNodeState} from './state'; |
5 | 12 | import {NavigationTreeNode} from './NavigationTreeNode'; |
6 | | -import {NavigationTreeDirectory} from './NavigationTreeDirectory'; |
| 13 | +import {LoaderView} from './LoaderView/LoaderView'; |
| 14 | +import {ErrorView} from './ErrorView/ErrorView'; |
| 15 | +import {EmptyView} from './EmptyView/EmptyView'; |
7 | 16 |
|
8 | 17 | export type {NavigationTreeProps}; |
9 | 18 |
|
| 19 | +const renderServiceNode = (node: NavigationTreeServiceNode) => { |
| 20 | + const key = `${node.path}|${node.status}`; |
| 21 | + |
| 22 | + if (node.status === 'loading') { |
| 23 | + return <LoaderView key={key} level={node.level} />; |
| 24 | + } |
| 25 | + |
| 26 | + if (node.status === 'error') { |
| 27 | + return <ErrorView key={key} level={node.level} />; |
| 28 | + } |
| 29 | + |
| 30 | + return <EmptyView key={key} level={node.level} />; |
| 31 | +}; |
| 32 | + |
10 | 33 | export function NavigationTree({ |
11 | 34 | rootState: partialRootState, |
12 | 35 | fetchPath, |
13 | 36 | getActions, |
14 | 37 | activePath, |
15 | 38 | onActivePathUpdate, |
16 | 39 | cache = true, |
| 40 | + virtualize = false, |
17 | 41 | }: NavigationTreeProps) { |
18 | 42 | const [state, dispatch] = React.useReducer(reducer, { |
19 | 43 | [partialRootState.path]: getNodeState(partialRootState), |
20 | 44 | }); |
21 | | - const rootState = state[partialRootState.path]; |
22 | | - |
23 | | - return ( |
24 | | - <NavigationTreeNode |
25 | | - path={rootState.path} |
26 | | - state={state} |
27 | | - dispatch={dispatch} |
28 | | - active={rootState.path === activePath} |
29 | | - onActivate={onActivePathUpdate} |
30 | | - getActions={getActions} |
31 | | - > |
32 | | - <NavigationTreeDirectory |
| 45 | + const nodesList = React.useMemo(() => selectTreeAsList(state, partialRootState.path), [state]); |
| 46 | + |
| 47 | + const renderNode = (node: NavigationTreeNodeState) => { |
| 48 | + return ( |
| 49 | + <NavigationTreeNode |
| 50 | + key={node.path} |
33 | 51 | state={state} |
34 | | - dispatch={dispatch} |
35 | | - path={rootState.path} |
36 | | - fetchPath={fetchPath} |
| 52 | + path={node.path} |
37 | 53 | activePath={activePath} |
38 | | - onItemActivate={onActivePathUpdate} |
| 54 | + fetchPath={fetchPath} |
| 55 | + dispatch={dispatch} |
| 56 | + onActivate={onActivePathUpdate} |
39 | 57 | getActions={getActions} |
40 | 58 | cache={cache} |
| 59 | + level={node.level} |
41 | 60 | /> |
42 | | - </NavigationTreeNode> |
| 61 | + ); |
| 62 | + }; |
| 63 | + |
| 64 | + const renderVirtualizedTree = () => ( |
| 65 | + <ReactList |
| 66 | + type="uniform" |
| 67 | + length={nodesList.length} |
| 68 | + useStaticSize |
| 69 | + itemRenderer={(index) => { |
| 70 | + const node = nodesList[index]; |
| 71 | + return isServiceNode(node) ? renderServiceNode(node) : renderNode(node); |
| 72 | + }} |
| 73 | + /> |
43 | 74 | ); |
| 75 | + |
| 76 | + const renderSimpleTree = () => ( |
| 77 | + <> |
| 78 | + {nodesList.map((node) => |
| 79 | + isServiceNode(node) ? renderServiceNode(node) : renderNode(node), |
| 80 | + )} |
| 81 | + </> |
| 82 | + ); |
| 83 | + |
| 84 | + return virtualize ? renderVirtualizedTree() : renderSimpleTree(); |
44 | 85 | } |
0 commit comments