import React, { useEffect, useId, useRef, useState } from 'react'; import { Check, Globe, ImagePlus, Loader2, Pipette, Type, Upload, X } from 'lucide-react'; import { HostBrowserIconError } from '../../domain/hostBrowser'; import { useI18n } from '../../application/i18n/I18nProvider'; import { cn } from '../../lib/utils'; import { Button } from '../ui/button'; import { Input } from '../ui/input'; import { Label } from '../ui/label'; export interface BrowserIconSelection { mode: 'website' | 'text' | 'image'; glyph: string; color: string; image: string | null; } const COLORS = ['#247969', '#2563eb', '#7c3aed', '#c026d3', '#e11d48', '#ea580c', '#ca8a04', '#64748b']; const MODES = [{ value: 'website', Icon: Globe }, { value: 'text', Icon: Type }, { value: 'image', Icon: ImagePlus }] as const; export function BrowserIconEditor({ value, onChange, readIcon, onBusyChange, allowWebsite = true }: { value: BrowserIconSelection; onChange: (changes: Partial) => void; readIcon: (file: File) => Promise; onBusyChange: (busy: boolean) => void; allowWebsite?: boolean; }) { const { t } = useI18n(); const inputId = useId(); const fileInput = useRef(null); const readRevision = useRef(0); const [busy, setBusy] = useState(false); const [dragging, setDragging] = useState(false); const [filename, setFilename] = useState(''); const [error, setError] = useState(null); useEffect(() => () => { readRevision.current++; }, []); const selectFile = async (file?: File) => { if (!file) return; const revision = ++readRevision.current; setError(null); setBusy(true); onBusyChange(true); try { const image = await readIcon(file); if (revision !== readRevision.current) return; if (!image) throw new HostBrowserIconError('decode'); onChange({ image }); setFilename(file.name); } catch (reason) { if (revision === readRevision.current) { const key = reason instanceof HostBrowserIconError ? reason.reason : 'decode'; setError(t(`terminal.browser.iconError.${key}`)); } } finally { if (revision === readRevision.current) { setBusy(false); onBusyChange(false); } } }; return
{MODES.filter((mode) => allowWebsite || mode.value !== 'website').map(({ value: mode, Icon }) => )}
{value.mode === 'website' &&

{t('terminal.browser.websiteIconHint')}

} {value.mode === 'text' &&
onChange({ glyph: event.target.value })} placeholder="A" />
{COLORS.map((color) => )}
} {value.mode === 'image' &&
{ event.preventDefault(); setDragging(true); }} onDragLeave={(event) => { if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setDragging(false); }} onDrop={(event) => { event.preventDefault(); setDragging(false); if (!busy) void selectFile(event.dataTransfer.files[0]); }}> { void selectFile(event.target.files?.[0]); event.target.value = ''; }} /> {value.image ?
{t('terminal.browser.iconPreview')}

{filename || t('terminal.browser.savedImage')}

: }

{t('terminal.browser.imageHint')}

} {error &&

{error}

}
; }