image-input.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import React, { useState, useRef, useEffect } from 'react';
  2. import { Tabs, TabsContent, TabsList, TabsTrigger } from './tabs';
  3. import { Button } from './button';
  4. import { Input } from './input';
  5. import { Label } from './label';
  6. import { Alert, AlertDescription } from './alert';
  7. import {
  8. Upload,
  9. Link as LinkIcon,
  10. X,
  11. Loader2,
  12. CheckCircle,
  13. AlertCircle,
  14. Image as ImageIcon,
  15. Info
  16. } from 'lucide-react';
  17. import {
  18. validateImageInput,
  19. validateImageUrlWithBase64,
  20. getImageDisplayName,
  21. fileToDataURL,
  22. type ImageValidationResult
  23. } from '../../lib/image-validation';
  24. export interface ImageInputProps {
  25. value?: File | string | null;
  26. onChange: (file: File | string | null) => void;
  27. onValidation?: (result: ImageValidationResult) => void;
  28. disabled?: boolean;
  29. className?: string;
  30. maxSize?: number; // in bytes, default 10MB
  31. accept?: string; // file accept attribute
  32. placeholder?: string;
  33. showPreview?: boolean;
  34. previewClassName?: string;
  35. }
  36. export interface ImageInputState {
  37. mode: 'file' | 'url';
  38. validation: ImageValidationResult | null;
  39. isValidating: boolean;
  40. error: string | null;
  41. previewUrl: string | null;
  42. }
  43. export function ImageInput({
  44. value,
  45. onChange,
  46. onValidation,
  47. disabled = false,
  48. className = '',
  49. maxSize = 10 * 1024 * 1024, // 10MB
  50. accept = 'image/*',
  51. placeholder = 'Enter image URL or select a file',
  52. showPreview = true,
  53. previewClassName = ''
  54. }: ImageInputProps) {
  55. const [state, setState] = useState<ImageInputState>({
  56. mode: 'file',
  57. validation: null,
  58. isValidating: false,
  59. error: null,
  60. previewUrl: null
  61. });
  62. const [urlInput, setUrlInput] = useState('');
  63. const fileInputRef = useRef<HTMLInputElement>(null);
  64. const validationTimeoutRef = useRef<NodeJS.Timeout | null>(null);
  65. // Handle external value changes
  66. useEffect(() => {
  67. if (value === null) {
  68. setState(prev => ({
  69. ...prev,
  70. validation: null,
  71. error: null,
  72. previewUrl: null
  73. }));
  74. setUrlInput('');
  75. return;
  76. }
  77. if (value instanceof File) {
  78. // File mode
  79. setState(prev => ({
  80. ...prev,
  81. mode: 'file',
  82. validation: null,
  83. error: null,
  84. previewUrl: null
  85. }));
  86. setUrlInput('');
  87. // Validate file immediately
  88. handleFileValidation(value);
  89. } else {
  90. // URL mode - value is a string (not File, not null)
  91. // Keep the existing preview URL while validation is in progress
  92. setState(prev => ({
  93. ...prev,
  94. mode: 'url',
  95. validation: null,
  96. error: null,
  97. previewUrl: typeof value === 'string' ? value : null
  98. }));
  99. // value should be a string here, but cast it to be safe
  100. setUrlInput(value || '');
  101. // Validate URL (with debounce)
  102. if (validationTimeoutRef.current) {
  103. clearTimeout(validationTimeoutRef.current);
  104. }
  105. validationTimeoutRef.current = setTimeout(() => {
  106. // Only validate if we have a non-empty string
  107. const urlValue = typeof value === 'string' ? value : null;
  108. if (urlValue && urlValue.trim()) {
  109. handleUrlValidation(urlValue);
  110. } else {
  111. handleUrlValidation(null);
  112. }
  113. }, 500);
  114. }
  115. }, [value, maxSize]);
  116. // Cleanup timeout on unmount
  117. useEffect(() => {
  118. return () => {
  119. if (validationTimeoutRef.current) {
  120. clearTimeout(validationTimeoutRef.current);
  121. }
  122. };
  123. }, []);
  124. const handleFileValidation = async (file: File) => {
  125. setState(prev => ({ ...prev, isValidating: true, error: null }));
  126. const result = await validateImageInput(file);
  127. let previewUrl: string | null = null;
  128. if (result.isValid) {
  129. try {
  130. previewUrl = await fileToDataURL(file);
  131. } catch (error) {
  132. console.error('Failed to create preview URL:', error);
  133. }
  134. }
  135. setState(prev => ({
  136. ...prev,
  137. isValidating: false,
  138. validation: result,
  139. error: result.isValid ? null : (result.error || null),
  140. previewUrl
  141. }));
  142. onValidation?.(result);
  143. };
  144. const handleUrlValidation = async (url: string | null) => {
  145. if (!url || !url.trim()) {
  146. setState(prev => ({
  147. ...prev,
  148. validation: null,
  149. error: null,
  150. previewUrl: null
  151. }));
  152. onValidation?.({ isValid: false, error: 'Please enter a URL' });
  153. return;
  154. }
  155. setState(prev => ({ ...prev, isValidating: true, error: null }));
  156. const result = await validateImageUrlWithBase64(url);
  157. // Use base64 data for preview if available, otherwise use original URL
  158. const previewUrl = result.isValid ? (result.base64Data || url) : null;
  159. setState(prev => ({
  160. ...prev,
  161. isValidating: false,
  162. validation: result,
  163. error: result.isValid ? null : (result.error || null),
  164. previewUrl
  165. }));
  166. onValidation?.(result);
  167. };
  168. const handleFileSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
  169. const file = event.target.files?.[0];
  170. if (file) {
  171. onChange(file);
  172. }
  173. };
  174. const handleUrlInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  175. const url = event.target.value;
  176. setUrlInput(url);
  177. onChange(url || null);
  178. };
  179. const handleModeChange = (mode: 'file' | 'url') => {
  180. setState(prev => ({
  181. ...prev,
  182. mode,
  183. validation: null,
  184. error: null,
  185. previewUrl: null
  186. }));
  187. onChange(null);
  188. setUrlInput('');
  189. };
  190. const handleClear = () => {
  191. onChange(null);
  192. setUrlInput('');
  193. setState(prev => ({
  194. ...prev,
  195. validation: null,
  196. error: null,
  197. previewUrl: null
  198. }));
  199. };
  200. const isValid = state.validation?.isValid;
  201. const hasError = state.error && !isValid;
  202. const canPreview = isValid && state.previewUrl;
  203. const isCorsBlocked = state.validation?.isCorsBlocked;
  204. const hasBase64Data = !!state.validation?.base64Data;
  205. return (
  206. <div className={`image-input ${className}`}>
  207. <Tabs value={state.mode} onValueChange={(value) => handleModeChange(value as 'file' | 'url')}>
  208. <TabsList className="grid w-full grid-cols-2">
  209. <TabsTrigger value="file" disabled={disabled}>
  210. <Upload className="w-4 h-4 mr-2" />
  211. Upload File
  212. </TabsTrigger>
  213. <TabsTrigger value="url" disabled={disabled}>
  214. <LinkIcon className="w-4 h-4 mr-2" />
  215. From URL
  216. </TabsTrigger>
  217. </TabsList>
  218. <TabsContent value="file" className="space-y-4">
  219. <div className="space-y-2">
  220. <Label htmlFor="file-upload">Choose Image File</Label>
  221. <div className="flex gap-2">
  222. <Input
  223. ref={fileInputRef}
  224. id="file-upload"
  225. type="file"
  226. accept={accept}
  227. onChange={handleFileSelect}
  228. disabled={disabled}
  229. className="flex-1"
  230. />
  231. {value instanceof File && (
  232. <Button
  233. type="button"
  234. variant="outline"
  235. onClick={() => fileInputRef.current?.click()}
  236. disabled={disabled}
  237. >
  238. Browse
  239. </Button>
  240. )}
  241. </div>
  242. {typeof value === 'string' && value && (
  243. <p className="text-sm text-gray-500">
  244. Current: {getImageDisplayName(value)}
  245. </p>
  246. )}
  247. </div>
  248. </TabsContent>
  249. <TabsContent value="url" className="space-y-4">
  250. <div className="space-y-2">
  251. <Label htmlFor="url-input">Image URL</Label>
  252. <Input
  253. id="url-input"
  254. type="url"
  255. value={urlInput}
  256. onChange={handleUrlInputChange}
  257. placeholder={placeholder}
  258. disabled={disabled}
  259. className="flex-1"
  260. />
  261. <p className="text-xs text-gray-500">
  262. Enter a URL that ends with an image extension (.jpg, .png, .gif, etc.)
  263. </p>
  264. </div>
  265. </TabsContent>
  266. </Tabs>
  267. {/* Validation Status */}
  268. {state.isValidating && (
  269. <div className="flex items-center gap-2 text-sm text-gray-500">
  270. <Loader2 className="w-4 h-4 animate-spin" />
  271. Validating and downloading image...
  272. </div>
  273. )}
  274. {isValid && (
  275. <Alert className="mt-4">
  276. <CheckCircle className="h-4 w-4" />
  277. <AlertDescription>
  278. Image is valid and ready to use
  279. {state.validation?.filename && ` (${state.validation.filename})`}
  280. {hasBase64Data && (
  281. <span className="block mt-1 text-xs text-green-600">
  282. ✓ Image downloaded and cached for preview
  283. </span>
  284. )}
  285. {isCorsBlocked && (
  286. <span className="block mt-1 text-xs text-yellow-600">
  287. Note: Downloaded using fallback method due to CORS restrictions
  288. </span>
  289. )}
  290. </AlertDescription>
  291. </Alert>
  292. )}
  293. {hasError && (
  294. <Alert variant="destructive" className="mt-4">
  295. <AlertCircle className="h-4 w-4" />
  296. <AlertDescription>
  297. {state.error}
  298. {state.error?.includes('CORS') && (
  299. <span className="block mt-1 text-xs">
  300. Try using a different image URL or upload the file directly
  301. </span>
  302. )}
  303. </AlertDescription>
  304. </Alert>
  305. )}
  306. {/* Image Preview */}
  307. {showPreview && canPreview && (
  308. <div className={`mt-4 ${previewClassName}`}>
  309. <Label>Preview</Label>
  310. <div className="mt-2 border rounded-lg p-4 bg-gray-50">
  311. <div className="flex items-center justify-center h-48 bg-white rounded border">
  312. <img
  313. src={state.previewUrl || ''}
  314. alt="Image preview"
  315. className="max-w-full max-h-full object-contain"
  316. />
  317. </div>
  318. </div>
  319. </div>
  320. )}
  321. {/* Clear Button */}
  322. {value && (
  323. <div className="mt-4 flex justify-end">
  324. <Button
  325. type="button"
  326. variant="outline"
  327. onClick={handleClear}
  328. disabled={disabled}
  329. className="text-red-600 hover:text-red-700"
  330. >
  331. <X className="w-4 h-4 mr-2" />
  332. Clear Selection
  333. </Button>
  334. </div>
  335. )}
  336. </div>
  337. );
  338. }
  339. export default ImageInput;