MCPcopy Create free account
hub / github.com/triggerdotdev/jsonhero-web / useColumnView

Function useColumnView

app/useColumnView/index.ts:80–230  ·  view source on GitHub ↗
({
  rootNode,
  initialState,
  stateReducer,
}: ColumnViewOptions)

Source from the content-addressed store, hash-verified

78type ColumnDefinitionCache = Map<string, ColumnDefinition>;
79
80export 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,

Callers 1

JsonColumnViewProviderFunction · 0.90

Calls 13

useMemoCompareFunction · 0.90
generateNodeTableFunction · 0.85
columnViewReducerFunction · 0.85
goBackActionFunction · 0.85
goForwardActionFunction · 0.85
goToNodeIdActionFunction · 0.85
goToParentActionFunction · 0.85
goToChildrenActionFunction · 0.85
goToPreviousSiblingFunction · 0.85
goToNextSiblingFunction · 0.85
resetSelectionActionFunction · 0.85
getPathToNodeFunction · 0.85

Tested by

no test coverage detected