Skip to content

Commit 3b14a76

Browse files
committed
fixed state management
1 parent 4ae4028 commit 3b14a76

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

packages/column-views/src/data-provider/base.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** Data provider for information that needs to be loaded in bulk for frontend views */
22
import baseFetch from "cross-fetch";
3-
import { createContext, useContext, useEffect, useState } from "react";
3+
import { createContext, useContext, useEffect, useMemo, useState } from "react";
44
import h from "@macrostrat/hyper";
55
import { create, useStore } from "zustand";
66
import {
@@ -279,19 +279,24 @@ export function useMacrostratColumns(
279279
const key = projectID ?? -1;
280280
const colData = columnsMap?.get(key);
281281
useEffect(() => {
282-
if (colData != null && (!inProcess || (inProcess && !colData.inProcess))) {
283-
return;
282+
// Refetch if the columns are not available, or if we have requested inProcess columns where we didn't before
283+
if (colData == null || (inProcess && !colData.inProcess)) {
284+
getColumns(projectID, inProcess);
284285
}
285-
getColumns(projectID, inProcess);
286+
// If we've already fetched the columns there's nothing to do...
286287
}, [colData, inProcess, getColumns]);
287-
if (colData == null) return null;
288-
let columns = colData.columns;
289-
if (!inProcess && colData.inProcess) {
290-
columns.features = columns.features?.filter(
291-
(d) => d.properties.status != "in process"
292-
);
293-
}
294-
return columns;
288+
289+
return useMemo(() => {
290+
if (colData == null) return null;
291+
let columns = colData.columns;
292+
if (!inProcess && colData.inProcess) {
293+
// Our available set of columns includes 'in process' columns, but we don't want them
294+
columns.features = columns.features?.filter(
295+
(d) => d.properties.status != "in process"
296+
);
297+
}
298+
return columns;
299+
}, [colData, inProcess]);
295300
}
296301

297302
export function useMacrostratData(dataType: string, ...args: any[]) {

packages/column-views/src/maps/column-navigation/mapbox/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export function ColumnNavigationProvider({
8686

8787
// Kind of an awkward way to do this but we need to allow the selector to run
8888
useEffect(() => {
89+
console.log("Selecting column", selectedColumn);
8990
const { selectColumn, selectedColumn: _internalSelectedColumn } =
9091
store.getState();
9192
if (selectedColumn == _internalSelectedColumn) {

packages/column-views/src/units/selection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ export function UnitSelectionProvider<T extends BaseUnit>(props: {
109109
const { units, selectedUnit } = props;
110110

111111
useEffect(() => {
112+
// Synchronize store with provided props
112113
if (selectedUnit != null) {
113114
const unitData = units?.find((u) => u.unit_id === selectedUnit);
114-
store.setState({ selectedUnitData: unitData });
115+
store.setState({ selectedUnit, selectedUnitData: unitData });
116+
} else {
117+
store.setState({ selectedUnit: null, selectedUnitData: null });
115118
}
116119
}, [selectedUnit, units]);
117120

0 commit comments

Comments
 (0)