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