|
| 1 | +--- |
| 2 | +title: Persistence |
| 3 | +description: Save and restore filter rules |
| 4 | +--- |
| 5 | + |
| 6 | +Filter rules can be saved to `localStorage` (or any storage) and restored later, so users don't lose their filters on page reload. |
| 7 | + |
| 8 | +## Serialization |
| 9 | + |
| 10 | +A `FilterGroup` is a plain object, so `JSON.stringify` works for most cases. However, if your schema includes `Date` fields, you need custom handling since `JSON.stringify` converts dates to strings. |
| 11 | + |
| 12 | +```ts |
| 13 | +import type { FilterGroup } from "@fn-sphere/filter"; |
| 14 | + |
| 15 | +export function serializeFilterGroup(filterGroup: FilterGroup): string { |
| 16 | + const replacer = function (this: any, key: string) { |
| 17 | + return this[key] instanceof Date |
| 18 | + ? { __type: "Date", value: this[key].toISOString() } |
| 19 | + : this[key]; |
| 20 | + }; |
| 21 | + return JSON.stringify(filterGroup, replacer); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +## Deserialization |
| 26 | + |
| 27 | +When reading back, revive `Date` objects and validate the structure before using it. |
| 28 | + |
| 29 | +```ts |
| 30 | +import type { FilterGroup } from "@fn-sphere/filter"; |
| 31 | + |
| 32 | +export function deserializeFilterGroup(serialized: string): FilterGroup { |
| 33 | + const deserialized = JSON.parse(serialized, (_, value) => { |
| 34 | + if (value && typeof value === "object" && value.__type === "Date") { |
| 35 | + return new Date(value.value); |
| 36 | + } |
| 37 | + return value; |
| 38 | + }); |
| 39 | + return deserialized as FilterGroup; |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Save to localStorage |
| 44 | + |
| 45 | +Use the `onRuleChange` callback to save the filter rule whenever it changes. |
| 46 | + |
| 47 | +```tsx |
| 48 | +import { |
| 49 | + FilterBuilder, |
| 50 | + FilterSphereProvider, |
| 51 | + useFilterSphere, |
| 52 | +} from "@fn-sphere/filter"; |
| 53 | + |
| 54 | +const STORAGE_KEY = "my-filter-rule"; |
| 55 | + |
| 56 | +function MyFilter({ schema }) { |
| 57 | + const { context } = useFilterSphere({ |
| 58 | + schema, |
| 59 | + defaultRule: loadFilterRule(), |
| 60 | + onRuleChange: ({ filterRule }) => { |
| 61 | + localStorage.setItem(STORAGE_KEY, serializeFilterGroup(filterRule)); |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + return ( |
| 66 | + <FilterSphereProvider context={context}> |
| 67 | + <FilterBuilder /> |
| 68 | + </FilterSphereProvider> |
| 69 | + ); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Restore from localStorage |
| 74 | + |
| 75 | +Read from storage on mount and pass it as `defaultRule`. Wrap it in a try-catch so corrupted data doesn't break the UI. |
| 76 | + |
| 77 | +```ts |
| 78 | +import { createFilterGroup, createSingleFilter } from "@fn-sphere/filter"; |
| 79 | + |
| 80 | +const fallbackRule = createFilterGroup({ |
| 81 | + op: "and", |
| 82 | + conditions: [createSingleFilter()], |
| 83 | +}); |
| 84 | + |
| 85 | +function loadFilterRule() { |
| 86 | + try { |
| 87 | + const saved = localStorage.getItem(STORAGE_KEY); |
| 88 | + if (!saved) return fallbackRule; |
| 89 | + return deserializeFilterGroup(saved); |
| 90 | + } catch { |
| 91 | + return fallbackRule; |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Full Example |
| 97 | + |
| 98 | +Putting it all together: |
| 99 | + |
| 100 | +```tsx |
| 101 | +import { |
| 102 | + FilterBuilder, |
| 103 | + FilterSphereProvider, |
| 104 | + useFilterSphere, |
| 105 | + createFilterGroup, |
| 106 | + createSingleFilter, |
| 107 | + type FilterGroup, |
| 108 | +} from "@fn-sphere/filter"; |
| 109 | +import { z } from "zod"; |
| 110 | + |
| 111 | +const STORAGE_KEY = "my-filter-rule"; |
| 112 | + |
| 113 | +const schema = z.object({ |
| 114 | + name: z.string().describe("Name"), |
| 115 | + createdAt: z.date().describe("Created At"), |
| 116 | +}); |
| 117 | + |
| 118 | +const fallbackRule = createFilterGroup({ |
| 119 | + op: "and", |
| 120 | + conditions: [createSingleFilter()], |
| 121 | +}); |
| 122 | + |
| 123 | +// --- Serialization --- |
| 124 | + |
| 125 | +function serializeFilterGroup(filterGroup: FilterGroup): string { |
| 126 | + const replacer = function (this: any, key: string) { |
| 127 | + return this[key] instanceof Date |
| 128 | + ? { __type: "Date", value: this[key].toISOString() } |
| 129 | + : this[key]; |
| 130 | + }; |
| 131 | + return JSON.stringify(filterGroup, replacer); |
| 132 | +} |
| 133 | + |
| 134 | +function deserializeFilterGroup(serialized: string): FilterGroup { |
| 135 | + const deserialized = JSON.parse(serialized, (_, value) => { |
| 136 | + if (value && typeof value === "object" && value.__type === "Date") { |
| 137 | + return new Date(value.value); |
| 138 | + } |
| 139 | + return value; |
| 140 | + }); |
| 141 | + if ( |
| 142 | + !deserialized || |
| 143 | + deserialized.type !== "FilterGroup" || |
| 144 | + !Array.isArray(deserialized.conditions) |
| 145 | + ) { |
| 146 | + throw new Error("Invalid FilterGroup structure"); |
| 147 | + } |
| 148 | + return deserialized; |
| 149 | +} |
| 150 | + |
| 151 | +// --- Storage --- |
| 152 | + |
| 153 | +function loadFilterRule(): FilterGroup { |
| 154 | + try { |
| 155 | + const saved = localStorage.getItem(STORAGE_KEY); |
| 156 | + if (!saved) return fallbackRule; |
| 157 | + return deserializeFilterGroup(saved); |
| 158 | + } catch { |
| 159 | + return fallbackRule; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// --- Component --- |
| 164 | + |
| 165 | +export default function PersistentFilter() { |
| 166 | + const { context } = useFilterSphere({ |
| 167 | + schema, |
| 168 | + defaultRule: loadFilterRule(), |
| 169 | + onRuleChange: ({ filterRule }) => { |
| 170 | + localStorage.setItem(STORAGE_KEY, serializeFilterGroup(filterRule)); |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + return ( |
| 175 | + <FilterSphereProvider context={context}> |
| 176 | + <FilterBuilder /> |
| 177 | + </FilterSphereProvider> |
| 178 | + ); |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | +## Using Other Serialization Libraries |
| 183 | + |
| 184 | +If you prefer not to write custom serialization, you can use <a href="https://github.com/flightcontrolhq/superjson" target="_blank">superjson</a> which handles `Date`, `Map`, `Set`, `RegExp`, and other types automatically. |
| 185 | + |
| 186 | +import { Aside } from "@astrojs/starlight/components"; |
| 187 | + |
| 188 | +<Aside title="Tip"> |
| 189 | + If your schema has no `Date` fields, you can skip the custom replacer/reviver |
| 190 | + and use `JSON.stringify` / `JSON.parse` directly. |
| 191 | +</Aside> |
0 commit comments