|
| 1 | +import { create } from 'zustand' |
| 2 | +import { type StateStorage, createJSONStorage, persist } from 'zustand/middleware' |
| 3 | + |
| 4 | +import { defaultContent } from '@/components/editor/markdown/default-content' |
| 5 | + |
| 6 | +const MARKDOWN_STORAGE_KEY = 'render-md:editor-content' |
| 7 | +const MARKDOWN_STORAGE_TTL_MS = 30 * 24 * 60 * 60 * 1000 |
| 8 | + |
| 9 | +type PersistedEditorContent = { |
| 10 | + markdown: string |
| 11 | + expiresAt: number | null |
| 12 | +} |
| 13 | + |
| 14 | +interface EditorContentStore extends PersistedEditorContent { |
| 15 | + setMarkdown: (markdown: string) => void |
| 16 | + clearMarkdown: () => void |
| 17 | +} |
| 18 | + |
| 19 | +function getLocalStorage() { |
| 20 | + if (typeof window === 'undefined') return null |
| 21 | + |
| 22 | + try { |
| 23 | + return window.localStorage |
| 24 | + } catch { |
| 25 | + return null |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 30 | + return typeof value === 'object' && value !== null |
| 31 | +} |
| 32 | + |
| 33 | +function getStoredMarkdownExpiresAt(storedValue: string) { |
| 34 | + try { |
| 35 | + const parsed: unknown = JSON.parse(storedValue) |
| 36 | + |
| 37 | + if (!isRecord(parsed) || !isRecord(parsed.state)) return null |
| 38 | + if (typeof parsed.state.markdown !== 'string') return null |
| 39 | + if (typeof parsed.state.expiresAt !== 'number') return null |
| 40 | + |
| 41 | + return parsed.state.expiresAt |
| 42 | + } catch { |
| 43 | + return null |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function shouldDiscardStoredMarkdown(storedValue: string) { |
| 48 | + const expiresAt = getStoredMarkdownExpiresAt(storedValue) |
| 49 | + |
| 50 | + return expiresAt === null || expiresAt <= Date.now() |
| 51 | +} |
| 52 | + |
| 53 | +const expiringLocalStorage: StateStorage = { |
| 54 | + getItem: (name) => { |
| 55 | + const storage = getLocalStorage() |
| 56 | + if (!storage) return null |
| 57 | + |
| 58 | + const storedValue = storage.getItem(name) |
| 59 | + if (!storedValue) return null |
| 60 | + |
| 61 | + if (shouldDiscardStoredMarkdown(storedValue)) { |
| 62 | + storage.removeItem(name) |
| 63 | + return null |
| 64 | + } |
| 65 | + |
| 66 | + return storedValue |
| 67 | + }, |
| 68 | + setItem: (name, value) => { |
| 69 | + const storage = getLocalStorage() |
| 70 | + if (!storage) return |
| 71 | + |
| 72 | + storage.setItem(name, value) |
| 73 | + }, |
| 74 | + removeItem: (name) => { |
| 75 | + const storage = getLocalStorage() |
| 76 | + if (!storage) return |
| 77 | + |
| 78 | + storage.removeItem(name) |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +export const useEditorContentStore = create<EditorContentStore>()( |
| 83 | + persist( |
| 84 | + (set) => ({ |
| 85 | + markdown: defaultContent, |
| 86 | + expiresAt: null, |
| 87 | + setMarkdown: (markdown) => |
| 88 | + set({ |
| 89 | + markdown, |
| 90 | + expiresAt: Date.now() + MARKDOWN_STORAGE_TTL_MS, |
| 91 | + }), |
| 92 | + clearMarkdown: () => { |
| 93 | + set({ |
| 94 | + markdown: defaultContent, |
| 95 | + expiresAt: null, |
| 96 | + }) |
| 97 | + useEditorContentStore.persist.clearStorage() |
| 98 | + }, |
| 99 | + }), |
| 100 | + { |
| 101 | + name: MARKDOWN_STORAGE_KEY, |
| 102 | + storage: createJSONStorage(() => expiringLocalStorage), |
| 103 | + partialize: (state): PersistedEditorContent => ({ |
| 104 | + markdown: state.markdown, |
| 105 | + expiresAt: state.expiresAt, |
| 106 | + }), |
| 107 | + }, |
| 108 | + ), |
| 109 | +) |
0 commit comments