import React, { useId, useState } from 'react'; import { Check, Globe, Loader2, Monitor } from 'lucide-react'; import type { BrowserBookmark } from '../../domain/browserBookmark'; import type { HostBrowserBookmark } 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'; import { AsidePanelContent, AsidePanelFooter } from '../ui/aside-panel'; import { BrowserIcon } from './BrowserIcon'; import { BrowserIconEditor, type BrowserIconSelection } from './BrowserIconEditor'; export function BrowserBookmarkEditor({ initial, onSave, onCancel, readIcon }: { initial: Partial; onSave: (bookmark: Partial) => Promise; onCancel: () => void; readIcon: (file: File) => Promise; }) { const { t } = useI18n(); const id = useId(); const [draft, setDraft] = useState(initial); const [icon, setIcon] = useState({ mode: initial.iconDataUrl ? 'image' : initial.iconMode === 'custom' ? 'text' : 'website', glyph: initial.icon || '', color: initial.color || '#247969', image: initial.iconDataUrl || null, }); const [reading, setReading] = useState(false); const [saving, setSaving] = useState(false); const invalidIcon = reading || (icon.mode === 'image' && !icon.image); const previewImage = icon.mode === 'image' ? icon.image : icon.mode === 'website' ? initial.siteIconDataUrl : null; const previewGlyph = icon.mode === 'text' ? icon.glyph || draft.title?.trim().slice(0, 1) : icon.mode === 'website' ? initial.siteIconGlyph : null; let addressHint = t('terminal.browser.bookmarkPreviewHint'); try { if (draft.url) addressHint = new URL(draft.url).host; } catch { /* Keep the hint while an address is being typed. */ } return
{ event.preventDefault(); if (invalidIcon || saving) return; setSaving(true); try { const saved = await onSave({ ...draft, title: draft.title?.trim(), url: draft.url?.trim(), color: icon.color, iconMode: icon.mode === 'website' ? 'auto' : 'custom', iconDataUrl: icon.mode === 'image' ? icon.image : null, icon: icon.mode === 'text' ? icon.glyph : '' }); if (saved) onCancel(); } finally { setSaving(false); } }}>

{draft.title?.trim() || t('terminal.browser.untitledBookmark')}

{addressHint}

setDraft((current) => ({ ...current, title: event.target.value }))} />
setDraft((current) => ({ ...current, url: event.target.value }))} />

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

setIcon((current) => ({ ...current, ...changes }))} readIcon={readIcon} onBusyChange={setReading} />
{t('terminal.browser.scope')} {(['host', 'all-hosts'] as const).map((scope) => { const selected = (draft.scope || 'host') === scope; const Icon = scope === 'host' ? Monitor : Globe; return ; })}
; }