|
1 | | -import { Icon } from "@chakra-ui/react" |
2 | 1 | import { |
3 | | - createLocalStorageProvider, |
4 | 2 | ImageKitEditor, |
5 | 3 | type ImageKitEditorProps, |
6 | 4 | type ImageKitEditorRef, |
| 5 | + type TemplateStorageProvider, |
7 | 6 | TRANSFORMATION_STATE_VERSION, |
| 7 | + type Transformation, |
8 | 8 | } from "@imagekit/editor" |
9 | | -import { PiDownload } from "@react-icons/all-files/pi/PiDownload" |
10 | 9 | import React, { useCallback, useEffect } from "react" |
11 | 10 | import ReactDOM from "react-dom" |
12 | 11 |
|
| 12 | +const TEMPLATE_STORAGE_KEY = "ik-editor:templates:v1" |
| 13 | + |
| 14 | +type StoredTemplateRecord = { |
| 15 | + id: string |
| 16 | + clientNumber: string |
| 17 | + isPrivate: boolean |
| 18 | + isPinned: boolean |
| 19 | + name: string |
| 20 | + transformations: Omit<Transformation, "id">[] |
| 21 | + createdBy: { userId: string; name: string; email: string } |
| 22 | + updatedBy: { userId: string; name: string; email: string } |
| 23 | + createdAt: number |
| 24 | + updatedAt: number |
| 25 | + lastUsedAt?: number |
| 26 | +} |
| 27 | + |
| 28 | +function readAllTemplates(): StoredTemplateRecord[] { |
| 29 | + const raw = localStorage.getItem(TEMPLATE_STORAGE_KEY) |
| 30 | + if (!raw) return [] |
| 31 | + try { |
| 32 | + const parsed = JSON.parse(raw) |
| 33 | + return Array.isArray(parsed) ? (parsed as StoredTemplateRecord[]) : [] |
| 34 | + } catch { |
| 35 | + return [] |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function writeAllTemplates(records: StoredTemplateRecord[]) { |
| 40 | + localStorage.setItem(TEMPLATE_STORAGE_KEY, JSON.stringify(records)) |
| 41 | +} |
| 42 | + |
| 43 | +function createLocalTemplateStorage(): TemplateStorageProvider { |
| 44 | + const session = { |
| 45 | + userId: "demo-user", |
| 46 | + name: "Demo User", |
| 47 | + email: "demo@example.com", |
| 48 | + clientNumber: "demo-client", |
| 49 | + } |
| 50 | + |
| 51 | + return { |
| 52 | + async listTemplates() { |
| 53 | + return readAllTemplates().sort((a, b) => b.updatedAt - a.updatedAt) |
| 54 | + }, |
| 55 | + async getTemplate(id: string) { |
| 56 | + return readAllTemplates().find((t) => t.id === id) ?? null |
| 57 | + }, |
| 58 | + async saveTemplate(input) { |
| 59 | + const now = Date.now() |
| 60 | + const all = readAllTemplates() |
| 61 | + const existing = input.id |
| 62 | + ? (all.find((t) => t.id === input.id) ?? null) |
| 63 | + : null |
| 64 | + |
| 65 | + const id = existing?.id ?? crypto.randomUUID?.() ?? String(now) |
| 66 | + const record: StoredTemplateRecord = { |
| 67 | + id, |
| 68 | + clientNumber: input.clientNumber ?? existing?.clientNumber ?? "demo", |
| 69 | + isPrivate: input.isPrivate ?? existing?.isPrivate ?? false, |
| 70 | + isPinned: input.isPinned ?? existing?.isPinned ?? false, |
| 71 | + name: input.name, |
| 72 | + transformations: input.transformations, |
| 73 | + createdBy: input.createdBy ?? |
| 74 | + existing?.createdBy ?? { |
| 75 | + userId: session.userId, |
| 76 | + name: session.name, |
| 77 | + email: session.email, |
| 78 | + }, |
| 79 | + updatedBy: input.updatedBy ?? { |
| 80 | + userId: session.userId, |
| 81 | + name: session.name, |
| 82 | + email: session.email, |
| 83 | + }, |
| 84 | + createdAt: input.createdAt ?? existing?.createdAt ?? now, |
| 85 | + updatedAt: input.updatedAt ?? now, |
| 86 | + lastUsedAt: existing?.lastUsedAt, |
| 87 | + } |
| 88 | + |
| 89 | + const next = [record, ...all.filter((t) => t.id !== id)] |
| 90 | + writeAllTemplates(next) |
| 91 | + return record |
| 92 | + }, |
| 93 | + async deleteTemplate(id: string) { |
| 94 | + writeAllTemplates(readAllTemplates().filter((t) => t.id !== id)) |
| 95 | + }, |
| 96 | + async setTemplatePinned(id: string, isPinned: boolean) { |
| 97 | + const all = readAllTemplates() |
| 98 | + const existing = all.find((t) => t.id === id) |
| 99 | + if (!existing) { |
| 100 | + throw new Error("Template not found") |
| 101 | + } |
| 102 | + const updated = { ...existing, isPinned, updatedAt: Date.now() } |
| 103 | + writeAllTemplates([updated, ...all.filter((t) => t.id !== id)]) |
| 104 | + return updated |
| 105 | + }, |
| 106 | + getProviderName() { |
| 107 | + return "localStorage" |
| 108 | + }, |
| 109 | + getCurrentUserSession() { |
| 110 | + return session |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
13 | 115 | function App() { |
14 | 116 | const [open, setOpen] = React.useState(true) |
15 | 117 | const [editorProps, setEditorProps] = |
@@ -115,12 +217,14 @@ function App() { |
115 | 217 | // })), |
116 | 218 | ], |
117 | 219 | onAddImage: handleAddImage, |
118 | | - onClose: () => setOpen(false), |
| 220 | + onClose: ({ destroy }) => { |
| 221 | + destroy() |
| 222 | + setOpen(false) |
| 223 | + }, |
119 | 224 | exportOptions: [ |
120 | 225 | { |
121 | 226 | type: "button", |
122 | 227 | label: "Export", |
123 | | - icon: <Icon boxSize={"5"} as={PiDownload} />, |
124 | 228 | isVisible: true, |
125 | 229 | onClick: (images, currentImage) => { |
126 | 230 | console.log("Export images:", images, currentImage) |
@@ -149,7 +253,7 @@ function App() { |
149 | 253 | console.log("Signed URL", request.url) |
150 | 254 | return Promise.resolve(request.url) |
151 | 255 | }, |
152 | | - templateStorage: createLocalStorageProvider(), |
| 256 | + templateStorage: createLocalTemplateStorage(), |
153 | 257 | }) |
154 | 258 | }, [handleAddImage]) |
155 | 259 |
|
|
0 commit comments