forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-upload.tsx
More file actions
149 lines (136 loc) · 3.86 KB
/
Copy pathimage-upload.tsx
File metadata and controls
149 lines (136 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use client';
import React, { useRef, useState, useEffect } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Upload, X, Loader2 } from 'lucide-react';
import { isValidImage, isValidFileSize } from '@/lib/utils/upload';
import { toast } from 'sonner';
interface ImageUploadProps {
value: string;
onChange: (url: string) => void;
onFileSelect?: (file: File | null) => void;
pendingFile?: File | null;
label?: string;
placeholder?: string;
disabled?: boolean;
}
export function ImageUpload({
value,
onChange,
onFileSelect,
pendingFile,
label = 'Image',
placeholder = 'https://... or upload',
disabled = false,
}: ImageUploadProps) {
const fileInputRef = useRef<HTMLInputElement>(null);
const [localPreview, setLocalPreview] = useState<string | null>(null);
// Create local preview URL for pending file
useEffect(() => {
if (pendingFile) {
const url = URL.createObjectURL(pendingFile);
setLocalPreview(url);
return () => URL.revokeObjectURL(url);
} else {
setLocalPreview(null);
}
}, [pendingFile]);
const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
if (!isValidImage(file)) {
toast.error('Please select a valid image (JPEG, PNG, GIF, or WebP)');
return;
}
if (!isValidFileSize(file, 5)) {
toast.error('Image must be less than 5MB');
return;
}
// Store file for later upload, clear any existing URL
if (onFileSelect) {
onFileSelect(file);
onChange(''); // Clear URL since we have a pending file
}
// Reset file input
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
};
const handleClear = () => {
onChange('');
if (onFileSelect) {
onFileSelect(null);
}
setLocalPreview(null);
};
const handleUrlChange = (url: string) => {
onChange(url);
// Clear pending file if user types a URL
if (url && onFileSelect) {
onFileSelect(null);
}
};
const previewUrl = localPreview || value;
const hasContent = previewUrl || pendingFile;
return (
<div className="space-y-2">
{label && <Label>{label}</Label>}
<div className="flex gap-2">
<Input
placeholder={placeholder}
value={value}
onChange={(e) => handleUrlChange(e.target.value)}
className="flex-1"
disabled={disabled || !!pendingFile}
/>
<input
type="file"
ref={fileInputRef}
onChange={handleFileSelect}
accept="image/jpeg,image/png,image/gif,image/webp"
className="hidden"
/>
<Button
type="button"
variant="outline"
size="icon"
onClick={() => fileInputRef.current?.click()}
disabled={disabled}
title="Upload image"
>
<Upload className="h-4 w-4" />
</Button>
{hasContent && (
<Button
type="button"
variant="outline"
size="icon"
onClick={handleClear}
disabled={disabled}
title="Clear"
>
<X className="h-4 w-4" />
</Button>
)}
</div>
{pendingFile && (
<p className="text-xs text-muted-foreground">
📎 {pendingFile.name} (will upload on save)
</p>
)}
{previewUrl && (
<div className="mt-2 relative rounded-md border overflow-hidden bg-muted/50 w-fit">
<img
src={previewUrl}
alt="Preview"
className="max-h-32 max-w-full object-contain"
onError={(e) => {
(e.target as HTMLImageElement).style.display = 'none';
}}
/>
</div>
)}
</div>
);
}