| 1 | 'use client' |
| 2 | |
| 3 | import { Eraser, FolderOpen, Pen } from 'lucide-react' |
| 4 | import Image from 'next/image |
| 5 | import { useEffect, useRef |
| 6 | import type ReactSignatureCanvas from |
| 7 | |
| 8 | import { Button } |
| 9 | import { |
| 10 | Dialog, |
| 11 | DialogContent, |
| 12 | DialogDescription, |
| 13 | DialogFooter, |
| 14 | DialogHeader, |
| 15 | DialogTitle, |
| 16 | } from '@/components/ui/dialog' |
| 17 | |
| 18 | import { type MediaItem, |
| 19 | |
| 20 | const PAD_HEIGHT = 200 |
| 21 | |
| 22 | type SignatureSource |
| 23 | |
| 24 | export interface SignatureResult |
| 25 | dataUrl: string |
| 26 | source: SignatureSource |
| 27 | } |
| 28 | |
| 29 | export interface SignatureDialogProps |
| 30 | /** Extra content rendered below the pad / library picker */ |
| 31 | children?: React |
| 32 | description?: string |
| 33 | disabled?: boolean |
| 34 | /** Previously uploaded signature images for reuse */ |
| 35 | mediaItems?: MediaItem |
| 36 | onConfirm: (result |
| 37 | onOpenChange: (open |
| 38 | open: boolean |
| 39 | /** Signature pad pen colour */ |
| 40 | penColor?: string |
| 41 | title?: string |
| 42 | } |
| 43 | |
| 44 | export function SignatureDialog({ |
| 45 | open, |
| 46 | onOpenChange, |
| 47 | onConfirm, |
| 48 | mediaItems = [], |
| 49 | title = 'Add Signature' |
| 50 | description = 'Draw your signature below or pick one from the library.' |
| 51 | penColor = 'black' |
| 52 | disabled = false, |
| 53 | children, |
| 54 | }: SignatureDialogProps) |
| 55 | const padRef = |
| 56 | |
| 57 | const [source, |
| 58 | const [isEmpty, |
| 59 | const [item, |
| 60 | const [libraryOpen, |
| 61 | const [SignatureCanvasComp, |
| 62 | typeof ReactSignatureCanvas | null |
| 63 | >(null) |
| 64 | |
| 65 | useEffect(() => |
| 66 | if (!open || |
| 67 | return |
| 68 | } |
| 69 | let cancelled = |
| 70 | import('react-signature-canvas').then |
| 71 | if (!cancelled) |
| 72 | setSignatureCanvasComp(() => |
| 73 | } |
| 74 | }) |
| 75 | return () => |
| 76 | cancelled = true |
| 77 | } |
| 78 | }, [open, |
| 79 | |
| 80 | const handleOpenChange = |
| 81 | if (!next) |
| 82 | resetState() |
| 83 | } |
| 84 | onOpenChange(next) |
| 85 | } |
| 86 | |
| 87 | const resetState = |
| 88 | padRef.current?.clear |
| 89 | setIsEmpty(true) |
| 90 | |
| 91 | setSource('pad') |
| 92 | } |
| 93 | |
| 94 | const handleClear = |
| 95 | padRef.current?.clear |
| 96 | setIsEmpty(true) |
| 97 | } |
| 98 | |
| 99 | const handleEnd = |
| 100 | setIsEmpty(padRef.current |
| 101 | } |
| 102 | |
| 103 | const handleConfirm = |
| 104 | if (source === |
| 105 | onConfirm({ dataUrl: |
| 106 | handleOpenChange(false) |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | if (source === |
| 111 | const dataUrl = |
| 112 | onConfirm({ dataUrl, |
| 113 | handleOpenChange(false) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | const handleLibrarySelect = |
| 118 | const mediaItem = |
| 119 | if (mediaItem) { |
| 120 | setSource('library') |
| 121 | setItem(mediaItem.id |
| 122 | } |
| 123 | setLibraryOpen(false) |
| 124 | } |
| 125 | |
| 126 | const switchToPad = |
| 127 | setSource('pad') |
| 128 | } |
| 129 | |
| 130 | const hasMedia = |
| 131 | const canConfirm = |
| 132 | |
| 133 | return ( |
| 134 | <> |
| 135 | <Dialog onOpenChange={ |
| 136 | <DialogContent className=' |
| 137 | <DialogHeader> |
| 138 | <DialogTitle>{title} |
| 139 | <DialogDescription> |
| 140 | {hasMedia ? description |
| 141 | </DialogDescription> |
| 142 | </DialogHeader> |
| 143 | |
| 144 | {source === 'pad |
| 145 | <div className='space-y-3 |
| 146 | <div className='relative overflow-hidden rounded-md border bg-white |
| 147 | {SignatureCanvasComp ? ( |
| 148 | <SignatureCanvasComp |
| 149 | canvasProps={{ |
| 150 | className: 'w-full cursor-crosshair', |
| 151 | height: PAD_HEIGHT, |
| 152 | style: { width: ' |
| 153 | }} |
| 154 | onEnd={handleEnd} |
| 155 | penColor={penColor} |
| 156 | ref={padRef} |
| 157 | /> |
| 158 | ) : ( |
| 159 | <div |
| 160 | className='flex items-center justify-center text-muted-foreground text-sm' |
| 161 | style={{ height: |
| 162 | > |
| 163 | Loading… |
| 164 | </div> |
| 165 | )} |
| 166 | |
| 167 | {!isEmpty && ( |
| 168 | <Button |
| 169 | className='absolute top-2 right-2' |
| 170 | onClick={handleClear} |
| 171 | size='icon-xs' |
| 172 | type='button' |
| 173 | variant='outline' |
| 174 | > |
| 175 | <Eraser className=' |
| 176 | </Button> |
| 177 | )} |
| 178 | </div> |
| 179 | <div> |
| 180 | {hasMedia && ( |
| 181 | <Button |
| 182 | onClick={() => |
| 183 | type='button' |
| 184 | variant='outline' |
| 185 | > |
| 186 | <FolderOpen className=' |
| 187 | Pick from library |
| 188 | </Button> |
| 189 | )} |
| 190 | </div> |
| 191 | </div> |
| 192 | ) : ( |
| 193 | <div className='space-y-3 |
| 194 | <div |
| 195 | className='flex items-center justify-center rounded-md border bg-white p-4' |
| 196 | style={{ minHeight: |
| 197 | > |
| 198 | {item && ( |
| 199 | <Image |
| 200 | alt='Selected signature' |
| 201 | className='max-h-[160px] object-contain' |
| 202 | height={160} |
| 203 | src={`/api/files/${item |
| 204 | unoptimized |
| 205 | width={300} |
| 206 | /> |
| 207 | )} |
| 208 | </div> |
| 209 | |
| 210 | <div className='flex justify-end |
| 211 | <Button onClick={ |
| 212 | <Pen className=' |
| 213 | Draw instead |
| 214 | </Button> |
| 215 | </div> |
| 216 | </div> |
| 217 | )} |
| 218 | |
| 219 | {children} |
| 220 | |
| 221 | <DialogFooter> |
| 222 | <Button |
| 223 | onClick={() => |
| 224 | type='button' |
| 225 | variant='outline' |
| 226 | > |
| 227 | Cancel |
| 228 | </Button> |
| 229 | <Button |
| 230 | disabled={!canConfirm |
| 231 | onClick={handleConfirm} |
| 232 | type='button' |
| 233 | > |
| 234 | Confirm |
| 235 | </Button> |
| 236 | </DialogFooter> |
| 237 | </DialogContent> |
| 238 | </Dialog> |
| 239 | |
| 240 | <MediaLibraryDialog |
| 241 | mediaItems={mediaItems} |
| 242 | onOpenChange={setLibraryOpen} |
| 243 | onSelect={handleLibrarySelect} |
| 244 | open={libraryOpen} |
| 245 | /> |
| 246 | </> |
| 247 | ) |
| 248 | } |