repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
certificates
stars
latest
clone command
git clone gitlawb://did:key:z6Mkqhmm...XL9c/certificatesgit clone gitlawb://did:key:z6Mkqhmm.../certificates019974a8sync from playground15h ago| #1 | import { useState, useEffect, useCallback } from "react"; |
| #2 | |
| #3 | export interface CustomTemplate { |
| #4 | id: string; |
| #5 | name: string; |
| #6 | code: string; |
| #7 | createdAt: string; |
| #8 | } |
| #9 | |
| #10 | const STORAGE_KEY = "cert-custom-templates"; |
| #11 | |
| #12 | function loadTemplates(): CustomTemplate[] { |
| #13 | try { |
| #14 | const raw = localStorage.getItem(STORAGE_KEY); |
| #15 | return raw ? JSON.parse(raw) : []; |
| #16 | } catch { |
| #17 | return []; |
| #18 | } |
| #19 | } |
| #20 | |
| #21 | function saveTemplates(templates: CustomTemplate[]) { |
| #22 | localStorage.setItem(STORAGE_KEY, JSON.stringify(templates)); |
| #23 | } |
| #24 | |
| #25 | export function useCustomTemplates() { |
| #26 | const [templates, setTemplates] = useState<CustomTemplate[]>(loadTemplates); |
| #27 | |
| #28 | useEffect(() => { |
| #29 | const handler = () => setTemplates(loadTemplates()); |
| #30 | window.addEventListener("custom-templates-changed", handler); |
| #31 | return () => window.removeEventListener("custom-templates-changed", handler); |
| #32 | }, []); |
| #33 | |
| #34 | const notify = () => window.dispatchEvent(new Event("custom-templates-changed")); |
| #35 | |
| #36 | const addTemplate = useCallback((name: string, code: string) => { |
| #37 | const id = `c${Date.now().toString(36)}`; |
| #38 | const existing = loadTemplates(); |
| #39 | const updated = [...existing, { id, name, code, createdAt: new Date().toISOString() }]; |
| #40 | saveTemplates(updated); |
| #41 | notify(); |
| #42 | return id; |
| #43 | }, []); |
| #44 | |
| #45 | const updateTemplate = useCallback((id: string, name: string, code: string) => { |
| #46 | const existing = loadTemplates(); |
| #47 | const updated = existing.map((t) => (t.id === id ? { ...t, name, code } : t)); |
| #48 | saveTemplates(updated); |
| #49 | notify(); |
| #50 | }, []); |
| #51 | |
| #52 | const deleteTemplate = useCallback((id: string) => { |
| #53 | const existing = loadTemplates(); |
| #54 | saveTemplates(existing.filter((t) => t.id !== id)); |
| #55 | notify(); |
| #56 | }, []); |
| #57 | |
| #58 | const getTemplate = useCallback((id: string) => { |
| #59 | return loadTemplates().find((t) => t.id === id); |
| #60 | }, []); |
| #61 | |
| #62 | return { templates, addTemplate, updateTemplate, deleteTemplate, getTemplate }; |
| #63 | } |
| #64 |