Skip to content

Commit 8f4a4dc

Browse files
committed
chore: update example project to make sure it runs with the latest package changes
1 parent a5fbd41 commit 8f4a4dc

3 files changed

Lines changed: 124 additions & 8 deletions

File tree

examples/react-example/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@chakra-ui/icons": "1.1.1",
7+
"@chakra-ui/react": "~1.8.9",
8+
"@emotion/react": "^11.14.0",
9+
"@emotion/styled": "^11.14.1",
610
"@imagekit/editor": "workspace:*",
711
"@types/node": "^20.11.24",
812
"@types/react": "^17.0.2",
913
"@types/react-dom": "^17.0.2",
14+
"framer-motion": "6.5.1",
1015
"react": "^17.0.2",
11-
"react-dom": "^17.0.2"
16+
"react-dom": "^17.0.2",
17+
"react-select": "^5.2.1"
1218
},
1319
"devDependencies": {
1420
"@vitejs/plugin-react": "^4.5.2",

examples/react-example/src/index.tsx

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,117 @@
1-
import { Icon } from "@chakra-ui/react"
21
import {
3-
createLocalStorageProvider,
42
ImageKitEditor,
53
type ImageKitEditorProps,
64
type ImageKitEditorRef,
5+
type TemplateStorageProvider,
76
TRANSFORMATION_STATE_VERSION,
7+
type Transformation,
88
} from "@imagekit/editor"
9-
import { PiDownload } from "@react-icons/all-files/pi/PiDownload"
109
import React, { useCallback, useEffect } from "react"
1110
import ReactDOM from "react-dom"
1211

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+
13115
function App() {
14116
const [open, setOpen] = React.useState(true)
15117
const [editorProps, setEditorProps] =
@@ -115,12 +217,14 @@ function App() {
115217
// })),
116218
],
117219
onAddImage: handleAddImage,
118-
onClose: () => setOpen(false),
220+
onClose: ({ destroy }) => {
221+
destroy()
222+
setOpen(false)
223+
},
119224
exportOptions: [
120225
{
121226
type: "button",
122227
label: "Export",
123-
icon: <Icon boxSize={"5"} as={PiDownload} />,
124228
isVisible: true,
125229
onClick: (images, currentImage) => {
126230
console.log("Export images:", images, currentImage)
@@ -149,7 +253,7 @@ function App() {
149253
console.log("Signed URL", request.url)
150254
return Promise.resolve(request.url)
151255
},
152-
templateStorage: createLocalStorageProvider(),
256+
templateStorage: createLocalTemplateStorage(),
153257
})
154258
}, [handleAddImage])
155259

yarn.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ __metadata:
986986
languageName: node
987987
linkType: hard
988988

989-
"@chakra-ui/react@npm:1.8.9":
989+
"@chakra-ui/react@npm:1.8.9, @chakra-ui/react@npm:~1.8.9":
990990
version: 1.8.9
991991
resolution: "@chakra-ui/react@npm:1.8.9"
992992
dependencies:
@@ -6266,13 +6266,19 @@ __metadata:
62666266
version: 0.0.0-use.local
62676267
resolution: "react-example@workspace:examples/react-example"
62686268
dependencies:
6269+
"@chakra-ui/icons": "npm:1.1.1"
6270+
"@chakra-ui/react": "npm:~1.8.9"
6271+
"@emotion/react": "npm:^11.14.0"
6272+
"@emotion/styled": "npm:^11.14.1"
62696273
"@imagekit/editor": "workspace:*"
62706274
"@types/node": "npm:^20.11.24"
62716275
"@types/react": "npm:^17.0.2"
62726276
"@types/react-dom": "npm:^17.0.2"
62736277
"@vitejs/plugin-react": "npm:^4.5.2"
6278+
framer-motion: "npm:6.5.1"
62746279
react: "npm:^17.0.2"
62756280
react-dom: "npm:^17.0.2"
6281+
react-select: "npm:^5.2.1"
62766282
typescript: "npm:4.9.3"
62776283
vite: "npm:^6.3.5"
62786284
languageName: unknown

0 commit comments

Comments
 (0)