void
+ setSearchTerms: (value: string) => void
+ syncFarm: (farmId: string) => void
}
export const useFieldFilterStore = create()(
persist(
- (set) => ({
+ (set, get) => ({
+ farmId: null,
showProductiveOnly: false, // Default to showing all fields
+ searchTerms: "",
toggleShowProductiveOnly: () =>
set((state) => ({
showProductiveOnly: !state.showProductiveOnly,
})),
+ setSearchTerms: (value) => {
+ set({
+ searchTerms: value,
+ })
+ },
+ syncFarm(farmId: string) {
+ if (get().farmId !== farmId) {
+ set({ farmId, searchTerms: "" })
+ }
+ },
}),
{
name: "field-filter-storage", // unique name
- storage: createJSONStorage(() => ssrSafeJSONStorage), // Use SSR-safe storage
+ storage: createJSONStorage(() => ssrSafeSessionJSONStorage), // Use SSR-safe storage
},
),
)
diff --git a/fdm-app/app/store/field-selection.ts b/fdm-app/app/store/field-selection.ts
new file mode 100644
index 000000000..654b7d7f6
--- /dev/null
+++ b/fdm-app/app/store/field-selection.ts
@@ -0,0 +1,31 @@
+import { create } from "zustand"
+import { createJSONStorage, persist } from "zustand/middleware"
+import { ssrSafeSessionJSONStorage } from "./storage"
+
+interface FieldSelectionState {
+ farmId: string | null
+ fieldIds: string[]
+ setFieldIds: (fieldIds: string[]) => void
+ syncFarm: (farmId: string) => void
+}
+
+export const useFieldSelectionStore = create()(
+ persist(
+ (set, get) => ({
+ farmId: null,
+ fieldIds: [],
+ setFieldIds(fieldIds: string[]) {
+ set({ fieldIds })
+ },
+ syncFarm(farmId: string) {
+ if (get().farmId !== farmId) {
+ set({ farmId, fieldIds: [] })
+ }
+ },
+ }),
+ {
+ name: "field-selection-storage", // unique name
+ storage: createJSONStorage(() => ssrSafeSessionJSONStorage), // Use SSR-safe storage
+ },
+ ),
+)
diff --git a/fdm-app/app/store/storage.ts b/fdm-app/app/store/storage.ts
index 4c8339626..b07b2a948 100644
--- a/fdm-app/app/store/storage.ts
+++ b/fdm-app/app/store/storage.ts
@@ -1,8 +1,8 @@
import type { StateStorage } from "zustand/middleware"
-const createSSRStorage = (): StateStorage => {
+const createSSRStorage = (name: keyof Window): StateStorage => {
if (typeof window !== "undefined") {
- return localStorage
+ return window[name]
}
// Return a no-op storage for SSR
@@ -13,4 +13,5 @@ const createSSRStorage = (): StateStorage => {
}
}
-export const ssrSafeJSONStorage = createSSRStorage()
+export const ssrSafeJSONStorage = createSSRStorage("localStorage")
+export const ssrSafeSessionJSONStorage = createSSRStorage("sessionStorage")