import React, { useEffect, useRef, useState } from 'react'; import { ArrowLeft, ArrowRight, Bookmark, Download, Edit2, Loader2, Plus, RotateCw, Square, Trash2, X } from 'lucide-react'; import { useHostBrowserPanel } from '../application/state/useHostBrowserPanel'; import { useHostBrowserControls } from '../application/state/useHostBrowserControls'; import { useI18n } from '../application/i18n/I18nProvider'; import type { BrowserBookmark } from '../domain/browserBookmark'; import type { HostBrowserTab } from '../domain/hostBrowser'; import { cn } from '../lib/utils'; import { Button } from './ui/button'; import { Input } from './ui/input'; import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip'; import { Popover, PopoverContent, PopoverTrigger } from './ui/popover'; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from './ui/context-menu'; import { Dialog, DialogContent, DialogTitle } from './ui/dialog'; import { BrowserIcon } from './host-browser/BrowserIcon'; import { BrowserBookmarkEditor } from './host-browser/BrowserBookmarkEditor'; import { BrowserDownloads } from './host-browser/BrowserDownloads'; import { BrowserIconEditor, type BrowserIconSelection } from './host-browser/BrowserIconEditor'; function ToolbarButton({ label, children, ...props }: React.ButtonHTMLAttributes & { label: string }) { return {label}; } export function HostBrowserSidePanel(props: Parameters[0]) { const { elementRef, error: mountError, snapshot, retry, ready } = useHostBrowserPanel(props); const browser = useHostBrowserControls(props.browserId, ready && props.isVisible); const { t } = useI18n(); const active = browser.state?.tabs.find((tab) => tab.id === browser.state?.activeTabId); const connected = browser.state?.route === 'connected'; const [address, setAddress] = useState(''); const addressRef = useRef(null); const lastActive = useRef(undefined); const [bookmarksOpen, setBookmarksOpen] = useState(false); const [downloadsOpen, setDownloadsOpen] = useState(false); const [editingBookmark, setEditingBookmark] = useState | null>(null); const [editingIcon, setEditingIcon] = useState(null); useEffect(() => { if (lastActive.current !== active?.id || document.activeElement !== addressRef.current) setAddress(active?.url || ''); lastActive.current = active?.id; }, [active?.id, active?.url]); useEffect(() => { if (!browser.focusAddress) return; addressRef.current?.focus(); addressRef.current?.select(); }, [browser.focusAddress]); useEffect(() => { setEditingBookmark(null); setEditingIcon(null); setBookmarksOpen(false); setDownloadsOpen(false); }, [props.browserId]); useEffect(() => { if (browser.downloadStarted) { setDownloadsOpen(true); setBookmarksOpen(false); } }, [browser.downloadStarted]); const newBookmark = () => { setBookmarksOpen(true); setEditingBookmark({ title: active?.url ? active.title || '' : '', url: active?.url || '', scope: 'host', color: '#247969', order: browser.bookmarks.length }); }; const navigate = (url: string) => { void browser.command({ type: 'navigate', url }); }; const error = mountError || browser.error; return
{ if (!(event.metaKey || event.ctrlKey) || event.altKey) return; const key = event.key.toLowerCase(); if (!['l', 't', 'w', 'r'].includes(key)) return; event.preventDefault(); event.stopPropagation(); if (key === 'l') browser.requestAddressFocus(); if (key === 't') void browser.command({ type: 'new-tab' }); if (key === 'w') void browser.command({ type: 'close-tab' }); if (key === 'r') void browser.command({ type: 'reload' }); }}>
{browser.state?.tabs.map((tab) =>
void browser.command({ type: 'activate', tabId: tab.id })} onKeyDown={(event) => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); void browser.command({ type: 'activate', tabId: tab.id }); } }} onDragStart={(event) => { event.dataTransfer.setData('text/plain', tab.id); event.dataTransfer.effectAllowed = 'move'; }} onDragOver={(event) => { event.preventDefault(); event.dataTransfer.dropEffect = 'move'; }} onDrop={(event) => { event.preventDefault(); const id = event.dataTransfer.getData('text/plain'); if (browser.state?.tabs.some((item) => item.id === id)) void browser.command({ type: 'reorder', tabId: id, beforeTabId: tab.id }); }}> {tab.loading ? : } {tab.url ? tab.title || tab.url : t('terminal.browser.newTab')}
setEditingIcon(tab)}>{t('terminal.browser.changeIcon')} void browser.command({ type: 'reset-icon', tabId: tab.id })}>{t('terminal.browser.resetIcon')}
)}
void browser.command({ type: 'new-tab' })}>
void browser.command({ type: 'back' })}> void browser.command({ type: 'forward' })}> void browser.command({ type: active?.loading ? 'stop' : 'reload' })}>{active?.loading ? : }
{ event.preventDefault(); navigate(address); addressRef.current?.blur(); }}> setAddress(event.target.value)} autoComplete="off" spellCheck={false} className="h-7 min-w-0 rounded-md border-border/50 bg-secondary/35 px-2 text-xs" />
{ setBookmarksOpen((value) => !value); setDownloadsOpen(false); }}> {t('terminal.browser.downloads')}
{error &&
{error}{mountError ? : }
}
{snapshot.map((layer, index) => )} {!connected ?

{t(browser.state?.route === 'disconnected' ? 'terminal.browser.disconnected' : 'terminal.browser.connecting')}

{browser.state?.error &&

{browser.state.error}

}{browser.state?.route === 'disconnected' && }
: !active?.url &&
{t('terminal.browser.bookmarks')}
{!browser.bookmarks.length ?

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

:
{browser.bookmarks.map((item) => )}
}
}
{bookmarksOpen && }
{editingIcon && setEditingIcon(null)} onSave={async (values) => { const saved = await browser.command({ type: 'set-icon', tabId: editingIcon.id, ...values }); if (saved) setEditingIcon(null); }} />}
; } function BrowserTabIconEditor({ tab, readIcon, onClose, onSave }: { tab: HostBrowserTab; readIcon: (file: File) => Promise; onClose: () => void; onSave: (values: { glyph: string; color: string; dataUrl?: string | null }) => Promise }) { const { t } = useI18n(); const [icon, setIcon] = useState({ mode: tab.iconDataUrl ? 'image' : 'text', glyph: tab.iconGlyph || '', color: tab.iconColor || '#247969', image: tab.iconDataUrl }); const [saving, setSaving] = useState(false); const [reading, setReading] = useState(false); return { if (!open) onClose(); }}> {t('terminal.browser.changeIcon')}
{ event.preventDefault(); setSaving(true); try { await onSave({ glyph: icon.mode === 'text' ? icon.glyph : '', color: icon.color, dataUrl: icon.mode === 'image' ? icon.image : null }); } finally { setSaving(false); } }}>

{tab.title || tab.url}

setIcon((current) => ({ ...current, ...changes }))} readIcon={readIcon} onBusyChange={setReading} allowWebsite={false} />
; }