// src/components/ImageUploadButton.tsx // Upload a photo of a hand-drawn genogram/ecomap and OCR it via Gemini Vision import { useState, useRef, useCallback } from 'react' import { trpc } from '@/lib/trpc' interface ImageUploadButtonProps { type: 'genogram' | 'ecomap' onResult: (data: unknown) => void onError?: (error: string) => void color?: string } const COLOR_MAP: Record = { indigo: { bg: 'bg-indigo-600', hover: 'hover:bg-indigo-700', disabled: 'disabled:bg-indigo-300' }, blue: { bg: 'bg-blue-600', hover: 'hover:bg-blue-700', disabled: 'disabled:bg-blue-300' }, amber: { bg: 'bg-amber-600', hover: 'hover:bg-amber-700', disabled: 'disabled:bg-amber-300' }, emerald: { bg: 'bg-emerald-600', hover: 'hover:bg-emerald-700', disabled: 'disabled:bg-emerald-300' }, purple: { bg: 'bg-purple-600', hover: 'hover:bg-purple-700', disabled: 'disabled:bg-purple-300' }, } const DEFAULT_COLOR = COLOR_MAP.indigo const ACCEPT = 'image/png,image/jpeg,image/webp' function fileToBase64(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader() reader.onloadend = () => { const result = reader.result as string // Strip data URI prefix: "data:image/jpeg;base64,..." const base64 = result.split(',')[1] || result resolve(base64) } reader.onerror = reject reader.readAsDataURL(file) }) } export default function ImageUploadButton({ type, onResult, onError, color = 'indigo', }: ImageUploadButtonProps) { const [errorMsg, setErrorMsg] = useState(null) const [preview, setPreview] = useState(null) const inputRef = useRef(null) const genogramMutation = trpc.ai.ocrGenogram.useMutation() const ecomapMutation = trpc.ai.ocrEcomap.useMutation() const isLoading = genogramMutation.isPending || ecomapMutation.isPending const colors = COLOR_MAP[color] ?? DEFAULT_COLOR const handleFile = useCallback(async (file: File) => { // Validate file type if (!file.type.match(/^image\/(png|jpeg|webp)$/)) { const msg = '僅支援 PNG、JPEG、WebP 格式' setErrorMsg(msg) onError?.(msg) return } // Validate file size (max 10MB) if (file.size > 10 * 1024 * 1024) { const msg = '圖片檔案過大(上限 10MB)' setErrorMsg(msg) onError?.(msg) return } // Show preview const previewUrl = URL.createObjectURL(file) setPreview(previewUrl) setErrorMsg(null) try { const imageBase64 = await fileToBase64(file) const mimeType = (file.type || 'image/jpeg') as 'image/jpeg' const mutation = type === 'genogram' ? genogramMutation : ecomapMutation const result = await mutation.mutateAsync({ imageBase64, mimeType }) if (!result.ok) { setErrorMsg(result.error ?? '圖片辨識失敗') onError?.(result.error ?? '圖片辨識失敗') return } onResult(result.data) } catch (err) { const msg = err instanceof Error ? err.message : '圖片辨識失敗' setErrorMsg(msg) onError?.(msg) } }, [type, genogramMutation, ecomapMutation, onResult, onError]) const handleChange = useCallback((e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) handleFile(file) // Reset input so same file can be re-selected if (inputRef.current) inputRef.current.value = '' }, [handleFile]) const handleClick = useCallback(() => { inputRef.current?.click() }, []) const label = type === 'genogram' ? '家系圖' : '生態圖' return (
{/* Hidden file input */} {/* Upload button */} {/* Image preview */} {preview && (
{`${label}圖片預覽`} {!isLoading && ( )}
)} {/* Error message */} {errorMsg && (
{errorMsg}
)}
) }