Skip to content

Commit a9011b2

Browse files
authored
open by default custom store variables (#649)
* open by default custom store variables * just current * it opens correctly * only local store url check * suggestions
1 parent 12293df commit a9011b2

3 files changed

Lines changed: 57 additions & 26 deletions

File tree

src/GlobalStates/GlobalStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type StoreState = {
3131
plotDim: number;
3232
flipY:boolean;
3333
initStore:string;
34+
storeFromURL: boolean;
3435
variable: string;
3536
variables: string[];
3637
openVariables: boolean;
@@ -65,6 +66,7 @@ type StoreState = {
6566
setPlotDim: (plotDim: number) => void;
6667
setFlipY: (flipY:boolean) => void;
6768
setInitStore: (initStore:string) => void;
69+
setStoreFromURL: (storeFromURL: boolean) => void;
6870
setVariable: (variable: string) => void;
6971
setVariables: (variables: string[]) => void;
7072
setOpenVariables: (openVariables: boolean) => void;
@@ -98,6 +100,7 @@ export const useGlobalStore = create<StoreState>((set, get) => ({
98100
plotDim: 0,
99101
flipY: false,
100102
initStore: ESDC,
103+
storeFromURL: false,
101104
variable: 'Default',
102105
variables: [],
103106
openVariables: false,
@@ -152,6 +155,7 @@ export const useGlobalStore = create<StoreState>((set, get) => ({
152155
setPlotDim: (plotDim) => set({ plotDim }),
153156
setFlipY: (flipY) => set({ flipY }),
154157
setInitStore: (initStore) => set({ initStore }),
158+
setStoreFromURL: (storeFromURL) => set({ storeFromURL }),
155159
setVariable: (variable) => set({ variable }),
156160
setVariables: (variables) => set({ variables }),
157161
setOpenVariables: (openVariables) => set({ openVariables }),

src/components/LandingHome.tsx

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ async function sendPing() {
2525

2626
export function LandingHome() {
2727
const {
28-
initStore, timeSeries, variable, plotOn,
29-
setZMeta, setVariables, setTitleDescription,
28+
initStore, timeSeries, variable, storeFromURL,
29+
setZMeta, setVariables, setTitleDescription, setOpenVariables, setStoreFromURL,
3030
} = useGlobalStore(useShallow(state => ({
3131
initStore: state.initStore,
3232
timeSeries: state.timeSeries,
3333
variable: state.variable,
34-
plotOn: state.plotOn,
34+
storeFromURL: state.storeFromURL,
3535
setZMeta: state.setZMeta,
3636
setVariables: state.setVariables,
3737
setTitleDescription: state.setTitleDescription,
38+
setOpenVariables: state.setOpenVariables,
39+
setStoreFromURL: state.setStoreFromURL,
3840
})))
3941

4042
const { currentStore, fetchKey,
@@ -49,27 +51,27 @@ export function LandingHome() {
4951
setUseNC: state.setUseNC
5052
})))
5153

52-
function resetSlices() {
53-
setZSlice([0, null])
54-
setYSlice([0, null])
55-
setXSlice([0, null])
56-
}
57-
5854
useEffect(() => {
59-
resetSlices();
55+
void fetchKey;
56+
setZSlice([0, null]);
57+
setYSlice([0, null]);
58+
setXSlice([0, null]);
6059
if (initStore.startsWith('local:')) {
6160
const path = initStore.replace('local:', '');
62-
if (!NETCDF_EXT_REGEX.test(path)) return; // TODO: handled zarr
61+
if (!NETCDF_EXT_REGEX.test(path)) return;
6362
const filename = path.split('/').pop() ?? 'file.nc';
6463
fetch(`/file?path=${encodeURIComponent(path)}`)
6564
.then(res => {
6665
if (!res.ok) throw new Error(`HTTP ${res.status}`);
6766
return res.blob();
6867
})
69-
.then(blob => {
70-
loadNetCDF(blob, filename);
71-
useGlobalStore.setState({openVariables: true})
72-
return
68+
.then(async blob => {
69+
await loadNetCDF(blob, filename);
70+
if (storeFromURL) {
71+
setOpenVariables(true);
72+
setStoreFromURL(false);
73+
}
74+
return;
7375
})
7476
.catch(e => useGlobalStore.getState().setStatus(`Failed to load: ${e instanceof Error ? e.message : String(e)}`));
7577
return;
@@ -86,23 +88,40 @@ export function LandingHome() {
8688
// Clear after use
8789
useZarrStore.getState().setIcechunkOptions(null);
8890
useZarrStore.getState().setFetchOptions(null);
89-
}, [initStore, fetchKey, setCurrentStore])
91+
}, [initStore, fetchKey, setCurrentStore, setUseNC, setZSlice, setYSlice, setXSlice, storeFromURL, setOpenVariables, setStoreFromURL]);
9092

9193
useEffect(() => {
94+
const { initStore } = useGlobalStore.getState();
95+
if (initStore.startsWith('local')) return;
96+
9297
let isMounted = true;
98+
const activeStore = currentStore;
9399

94-
GetTitleDescription(currentStore).then((result) => {
95-
if (isMounted) setTitleDescription(result);
100+
GetTitleDescription(activeStore).then((result) => {
101+
if (isMounted && currentStore === activeStore) setTitleDescription(result);
96102
});
97103

98-
const fullmetadata = GetZarrMetadata(currentStore);
104+
const fullmetadata = GetZarrMetadata(activeStore);
99105
const variables = GetVariableNames(fullmetadata);
100106

101-
fullmetadata.then(e => setZMeta(e))
102-
variables.then(e => setVariables(e))
107+
fullmetadata.then((e) => {
108+
if (isMounted && currentStore === activeStore) setZMeta(e);
109+
});
110+
variables.then((e) => {
111+
if (isMounted && currentStore === activeStore) {
112+
setVariables(e);
113+
const { storeFromURL } = useGlobalStore.getState();
114+
if (storeFromURL) {
115+
setOpenVariables(true);
116+
setStoreFromURL(false);
117+
}
118+
}
119+
});
103120

104-
return () => { isMounted = false; };
105-
}, [currentStore, setZMeta, setVariables, setTitleDescription])
121+
return () => {
122+
isMounted = false;
123+
};
124+
}, [currentStore, setZMeta, setVariables, setTitleDescription, setOpenVariables, setStoreFromURL]);
106125

107126
useEffect(()=>{
108127
if (process.env.NODE_ENV !== "development") {

src/components/StoreInitializer.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,28 @@ import { isRemoteStore } from '@/utils/isRemoteStore';
99
function StoreInitializerInner() {
1010
const searchParams = useSearchParams();
1111
const setInitStore = useGlobalStore(s => s.setInitStore);
12+
const setStoreFromURL = useGlobalStore(s => s.setStoreFromURL);
1213
const { setUseNC, setFetchNC } = useZarrStore(useShallow(s => ({
1314
setUseNC: s.setUseNC,
1415
setFetchNC: s.setFetchNC,
1516
})));
1617

1718
useEffect(() => {
1819
const store = searchParams.get("store");
19-
if (!store) return;
20+
if (!store) {
21+
setStoreFromURL(false);
22+
return;
23+
}
2024

2125
const isNC = searchParams.get("format") === "nc";
2226
setUseNC(isNC);
2327
setFetchNC(isNC);
24-
setInitStore(isRemoteStore(store) ? store : `local:${store}`);
25-
}, []);
28+
const initValue = isRemoteStore(store) ? store : `local:${store}`;
29+
setInitStore(initValue);
30+
// mark that a store was provided in the URL; LandingHome will open variables once
31+
// the custom store is actually loaded.
32+
setStoreFromURL(true);
33+
}, [searchParams, setUseNC, setFetchNC, setInitStore, setStoreFromURL]);
2634

2735
return null;
2836
}

0 commit comments

Comments
 (0)