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