Scrapboard.org client
1/* eslint-disable @typescript-eslint/no-explicit-any */
2import type { PersistStorage, StorageValue } from "zustand/middleware";
3
4export function createMapStorage<T>(
5 key: string
6): PersistStorage<any> | undefined {
7 return {
8 getItem: (name) => {
9 const str = localStorage.getItem(name);
10 if (!str) return null;
11 const existingValue = JSON.parse(str);
12 return {
13 ...existingValue,
14 state: {
15 ...existingValue.state,
16 [key]: new Map(existingValue.state[key]),
17 },
18 };
19 },
20 setItem: (name, newValue: StorageValue<any>) => {
21 const mapValue = newValue.state?.[key];
22 const serializedMap =
23 mapValue instanceof Map ? Array.from(mapValue.entries()) : [];
24 const str = JSON.stringify({
25 ...newValue,
26 state: {
27 ...newValue.state,
28 [key]: serializedMap,
29 },
30 });
31 localStorage.setItem(name, str);
32 },
33 removeItem: (name) => localStorage.removeItem(name),
34 };
35}