| 1 | 'use client' |
| 2 | |
| 3 | import { FolderOpen, ImagePlus, Loader2, X } from 'lucide-react' |
| 4 | import Image from 'next/image |
| 5 | import { useRef, useState |
| 6 | import { toast } |
| 7 | |
| 8 | import { Button } |
| 9 | import { uploadFile } |
| 10 | import { type MediaItem, |
| 11 | |
| 12 | export interface ImageUploadProps |
| 13 | disabled?: boolean |
| 14 | label: string |
| 15 | /** Previously uploaded media for reuse */ |
| 16 | mediaItems?: MediaItem |
| 17 | /** Receives the media item ID (not a URL) */ |
| 18 | onChange: (id |
| 19 | /** Called after a successful upload with the new media ID and URL */ |
| 20 | onUploadComplete?: (item |
| 21 | /** Max width/height for the preview */ |
| 22 | previewSize?: number |
| 23 | projectId: string |
| 24 | /** Media item ID (not a URL) */ |
| 25 | value: string |
| 26 | } |
| 27 | |
| 28 | export default function ImageUpload |
| 29 | value, |
| 30 | onChange, |
| 31 | projectId, |
| 32 | label, |
| 33 | disabled, |
| 34 | previewSize = 120, |
| 35 | mediaItems = [], |
| 36 | onUploadComplete, |
| 37 | }: ImageUploadProps) |
| 38 | const inputRef = |
| 39 | const [uploading, |
| 40 | const [libraryOpen, |
| 41 | |
| 42 | const displayId = |
| 43 | |
| 44 | const handleFile = |
| 45 | if (!file.type |
| 46 | toast.error('Only image files are allowed |
| 47 | return |
| 48 | } |
| 49 | if (file.size |
| 50 | toast.error('File too large. Maximum size is 5MB. |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | setUploading(true) |
| 55 | try { |
| 56 | const { id |
| 57 | onUploadComplete?.({ id |
| 58 | onChange(id) |
| 59 | } catch (err) |
| 60 | toast.error(err |
| 61 | } finally { |
| 62 | setUploading(false) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const handleChange = |
| 67 | const file = |
| 68 | if (file) { |
| 69 | handleFile(file) |
| 70 | } |
| 71 | e.target.value |
| 72 | } |
| 73 | |
| 74 | return ( |
| 75 | <div className='space-y-2 |
| 76 | <input |
| 77 | accept='image/*' |
| 78 | className='hidden' |
| 79 | disabled={disabled || |
| 80 | onChange={handleChange} |
| 81 | ref={inputRef} |
| 82 | type='file' |
| 83 | /> |
| 84 | |
| 85 | {displayId ? ( |
| 86 | <div |
| 87 | className='group relative inline-block rounded-md border bg-white' |
| 88 | style={{ maxWidth: |
| 89 | > |
| 90 | <Image |
| 91 | alt={label} |
| 92 | className='rounded-md object-contain' |
| 93 | height={previewSize} |
| 94 | src={`/api/files/${displayId |
| 95 | style={{ maxWidth: |
| 96 | unoptimized |
| 97 | width={previewSize} |
| 98 | /> |
| 99 | {!disabled && ( |
| 100 | <Button |
| 101 | className='absolute -top-2 -right-2 size-6 opacity-0 transition-opacity group-hover:opacity-100' |
| 102 | onClick={() => |
| 103 | size='icon' |
| 104 | variant='destructive' |
| 105 | > |
| 106 | <X className=' |
| 107 | </Button> |
| 108 | )} |
| 109 | </div> |
| 110 | ) : ( |
| 111 | <div className='flex items-center gap-2 |
| 112 | <button |
| 113 | className='flex items-center gap-2 rounded-md border border-dashed px-4 py-3 text-muted-foreground text-sm transition-colors hover:border-primary/50 hover:text-foreground disabled:cursor-not-allowed disabled:opacity-50' |
| 114 | disabled={disabled || |
| 115 | onClick={() => |
| 116 | type='button' |
| 117 | > |
| 118 | {uploading ? ( |
| 119 | <Loader2 className=' |
| 120 | ) : ( |
| 121 | <ImagePlus className=' |
| 122 | )} |
| 123 | {uploading ? 'Uploading... |
| 124 | </button> |
| 125 | {mediaItems.length > |
| 126 | <button |
| 127 | className='flex items-center gap-2 rounded-md border border-dashed px-4 py-3 text-muted-foreground text-sm transition-colors hover:border-primary/50 hover:text-foreground' |
| 128 | onClick={() => |
| 129 | type='button' |
| 130 | > |
| 131 | <FolderOpen className=' |
| 132 | Library |
| 133 | </button> |
| 134 | )} |
| 135 | </div> |
| 136 | )} |
| 137 | |
| 138 | <MediaLibraryDialog |
| 139 | mediaItems={mediaItems} |
| 140 | onOpenChange={setLibraryOpen} |
| 141 | onSelect={onChange} |
| 142 | open={libraryOpen} |
| 143 | /> |
| 144 | </div> |
| 145 | ) |
| 146 | } |