({
rootNode,
initialState,
stateReducer,
}: ColumnViewOptions)
| 78 | type ColumnDefinitionCache = Map<string, ColumnDefinition>; |
| 79 | |
| 80 | export function useColumnView({ |
| 81 | rootNode, |
| 82 | initialState, |
| 83 | stateReducer, |
| 84 | }: ColumnViewOptions): ColumnViewInstance { |
| 85 | const columnCache = React.useRef<ColumnDefinitionCache>(new Map()); |
| 86 | |
| 87 | useEffect(() => { |
| 88 | columnCache.current = new Map(); |
| 89 | }, [rootNode]); |
| 90 | |
| 91 | const nodeTable = useMemo<NodeTable>( |
| 92 | () => generateNodeTable(rootNode), |
| 93 | [rootNode] |
| 94 | ); |
| 95 | |
| 96 | const enhancedReducer: Reducer<ColumnViewState, ColumnViewAction> = |
| 97 | useCallback( |
| 98 | (state: ColumnViewState, action: ColumnViewAction): ColumnViewState => { |
| 99 | let changes = columnViewReducer(state, action); |
| 100 | |
| 101 | // Don't allow the client modify history actions |
| 102 | if (action.type === "GO") { |
| 103 | return changes; |
| 104 | } |
| 105 | |
| 106 | if (stateReducer) { |
| 107 | changes = stateReducer(state, action, changes); |
| 108 | } |
| 109 | |
| 110 | //we need to get rid of any history items further forward because the user has forked by navigating |
| 111 | let updatedHistory = changes.history.slice( |
| 112 | 0, |
| 113 | changes.historyCurrentIndex + 1 |
| 114 | ); |
| 115 | let historyIndex = changes.historyCurrentIndex; |
| 116 | |
| 117 | //add new entry |
| 118 | const newHistoryEntry = omit(changes, "history", "historyCurrentIndex"); |
| 119 | updatedHistory.push(newHistoryEntry); |
| 120 | historyIndex = updatedHistory.length - 1; |
| 121 | |
| 122 | return { |
| 123 | ...changes, |
| 124 | history: updatedHistory, |
| 125 | historyCurrentIndex: historyIndex, |
| 126 | }; |
| 127 | }, |
| 128 | [stateReducer] |
| 129 | ); |
| 130 | |
| 131 | const [state, dispatch] = useReducer< |
| 132 | Reducer<ColumnViewState, ColumnViewAction> |
| 133 | >( |
| 134 | enhancedReducer, |
| 135 | typeof initialState === "string" |
| 136 | ? { |
| 137 | selectedNodeId: initialState, |
no test coverage detected