Appearance, VM busy state, the in-app update, file drops and the theme event stream are now hooks in lib/. VMAction moved to types.ts, so the VM page and its parts no longer import each other, and error toasts have one builder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
327 lines
16 KiB
TypeScript
327 lines
16 KiB
TypeScript
import * as Ctx from '@radix-ui/react-context-menu'
|
|
import { AppWindow, Camera, CopyPlus, Cpu, Download, FolderOpen, Info, Maximize2, Pause, Play, Plus, Power, Puzzle, RotateCw, Settings, Square, Trash } from 'lucide-react'
|
|
import React, { useRef } from 'react'
|
|
import { I18nKey, localeDir, t } from '../i18n'
|
|
import { guestFamilyOf, vmFeatures } from '../lib/endpoints'
|
|
import { Locale, VMAction, VMBusyAction, VMListItem } from '../types'
|
|
import { hasVisibleDialog } from '../lib/dialogState'
|
|
import { vmRefKey } from '../lib/vmRef'
|
|
import { IconButton, StatusShape, Tip, statusKind } from '../ui/controls'
|
|
import { VersionArt } from '../macos/VersionArt'
|
|
import { BrandMark, HostIcon } from '../ui/themeVisuals'
|
|
import { GuestArt, hasGuestArt } from './GuestIcon'
|
|
|
|
export const HOST_ID = '__host__'
|
|
export const INSTALLERS_ID = '__installers__'
|
|
export const PLUGINS_ID = '__plugins__'
|
|
export const SETTINGS_ID = '__settings__'
|
|
export const ABOUT_ID = '__about__'
|
|
|
|
/** Pages that are not VMs; everything else selectable in the sidebar is a VM name. */
|
|
export const PAGE_IDS: readonly string[] = [HOST_ID, INSTALLERS_ID, PLUGINS_ID, SETTINGS_ID, ABOUT_ID]
|
|
|
|
interface Props {
|
|
locale: Locale
|
|
vms: VMListItem[]
|
|
transitions: Record<string, string>
|
|
/** VM reference key (`vmRefKey`) → currently running action (disables conflicting context menu actions). */
|
|
busyActions?: Record<string, VMBusyAction>
|
|
selected: string
|
|
hostReady: boolean
|
|
/** Whether the active runtime supports VM suspend/resume (macOS VMs; other VMs use their `suspend` capability). */
|
|
canSuspend: boolean
|
|
onSelect: (id: string) => void
|
|
/** VM reference key → display name (its note); empty or missing shows the name. */
|
|
displayNames?: Record<string, string>
|
|
/** VM reference key → macOS major version for its icon; null shows the name's initial. */
|
|
macosMajors?: Record<string, number | null>
|
|
/** Double-click on a VM: its main action (start, or show its window). */
|
|
onActivate?: (name: string) => void
|
|
onNew: () => void
|
|
onVMContextAction?: (name: string, action: VMAction | 'settings') => void
|
|
/** An installer download is running (busy dot on the Installers entry). */
|
|
downloading?: boolean
|
|
/** Safe mode is active (third-party plugins skipped). */
|
|
safeMode?: boolean
|
|
/** Update capsule card shown under the brand (null when nothing to show). */
|
|
updateCard?: React.ReactNode
|
|
/** A newer release exists: dot on the About entry. */
|
|
updateDot?: boolean
|
|
/** Declared plugin pages shown after the built-in Plugins entry. */
|
|
pluginViews?: { id: string; title: string; badge?: string; icon?: string }[]
|
|
}
|
|
|
|
export const Sidebar: React.FC<Props> = ({
|
|
locale, vms, transitions, busyActions = {}, selected, hostReady, canSuspend, onSelect, displayNames = {}, macosMajors = {}, onActivate, onNew, onVMContextAction, downloading, safeMode, updateCard, updateDot, pluginViews = [],
|
|
}) => {
|
|
const tr = (k: I18nKey) => t(locale, k)
|
|
const listRef = useRef<HTMLDivElement>(null)
|
|
const ctxOpenedByPointerRef = useRef(false)
|
|
const ids = [...vms.map((v) => vmRefKey(v)), INSTALLERS_ID, PLUGINS_ID, ...pluginViews.map((view) => view.id), HOST_ID, SETTINGS_ID, ABOUT_ID]
|
|
|
|
// Listbox keyboard model: ↑/↓/Home/End move selection and focus together.
|
|
const onKeyDown = (e: React.KeyboardEvent) => {
|
|
const idx = ids.indexOf(selected)
|
|
let next = -1
|
|
if (e.key === 'ArrowDown') next = Math.min(ids.length - 1, idx + 1)
|
|
else if (e.key === 'ArrowUp') next = Math.max(0, idx - 1)
|
|
else if (e.key === 'Home') next = 0
|
|
else if (e.key === 'End') next = ids.length - 1
|
|
if (next < 0) return
|
|
e.preventDefault()
|
|
onSelect(ids[next])
|
|
listRef.current?.querySelector<HTMLElement>(`[data-id="${CSS.escape(ids[next])}"]`)?.focus()
|
|
}
|
|
|
|
const hasSelectedInList = ids.includes(selected)
|
|
const navBtn = (
|
|
id: string,
|
|
label: string,
|
|
icon: React.ReactNode,
|
|
badgeClass: string,
|
|
stateLabel: string,
|
|
kind: ReturnType<typeof statusKind> | null,
|
|
onDoubleClick?: () => void,
|
|
dot?: boolean,
|
|
vm?: { state: string; driver: string; busy: boolean; family: string },
|
|
badgeText?: string,
|
|
) => (
|
|
<button
|
|
key={id}
|
|
type="button"
|
|
role="option"
|
|
data-id={id}
|
|
className="nav-item rg-p-nav-item"
|
|
data-selected={selected === id ? 'true' : 'false'}
|
|
data-vm-state={vm?.state}
|
|
data-driver={vm?.driver}
|
|
data-guest-family={vm?.family}
|
|
data-busy={vm ? (vm.busy ? 'true' : 'false') : undefined}
|
|
aria-selected={selected === id}
|
|
aria-label={stateLabel ? t(locale, 'sidebarItemAria', { label, state: stateLabel }) : label}
|
|
tabIndex={selected === id || (!hasSelectedInList && id === ids[0]) ? 0 : -1}
|
|
onClick={() => onSelect(id)}
|
|
onDoubleClick={onDoubleClick}
|
|
>
|
|
<span className={`nav-icon-badge ${badgeClass}`} aria-hidden="true">{icon}</span>
|
|
<span className="nav-label">{label}</span>
|
|
{dot && <span className="nav-dot" aria-hidden="true" />}
|
|
{badgeText && <span className="badge rg-p-badge badge-idle">{badgeText}</span>}
|
|
{kind && (
|
|
<Tip label={stateLabel} side="right">
|
|
<span className="hstack"><StatusShape kind={kind} /></span>
|
|
</Tip>
|
|
)}
|
|
{vm && <span className="rg-slot-anchor nav-item-trailing" data-rg-slot="nav.item.trailing" data-rg-key={id} aria-hidden="true" />}
|
|
</button>
|
|
)
|
|
|
|
return (
|
|
<div className="sidebar-wrap">
|
|
<aside className="sidebar rg-p-sidebar">
|
|
<div className="sidebar-brand">
|
|
<BrandMark className="sidebar-brand-mark" />
|
|
<span className="sidebar-brand-name">Reims-Go</span>
|
|
</div>
|
|
<div className="rg-slot-anchor" data-rg-slot="sidebar.header" />
|
|
{updateCard}
|
|
<div className="sidebar-head rg-p-sidebar-head">
|
|
<span>{tr('sidebarVMs')}</span>
|
|
<IconButton small label={tr('newVM')} shortcut="Ctrl+N" icon={<HostIcon name="new-vm"><Plus size={15} /></HostIcon>} onClick={onNew} tipSide="right" />
|
|
</div>
|
|
<div className="nav rg-p-nav" role="listbox" aria-label={tr('sidebarVMs')} ref={listRef} onKeyDown={onKeyDown}>
|
|
<div className="nav-group rg-p-nav-group nav-vms" role="group" aria-label={tr('sidebarVMs')}>
|
|
{vms.length === 0 && <div className="nav-empty rg-p-empty" data-rg-slot="empty.state">{tr('emptyVMs')}</div>}
|
|
{vms.map((vm) => {
|
|
const key = vmRefKey(vm)
|
|
const st = transitions[key] ?? vm.state
|
|
const running = st === 'running'
|
|
const stopped = st === 'stopped'
|
|
const suspended = st === 'suspended'
|
|
const transitioning = st === 'starting' || st === 'stopping' || st === 'suspending' || st === 'resuming'
|
|
const busyAction = Object.prototype.hasOwnProperty.call(busyActions, key) ? busyActions[key] : undefined
|
|
const vmBusy = !!busyAction
|
|
const label = displayNames[key] || vm.name
|
|
const features = vmFeatures(vm)
|
|
const family = guestFamilyOf(vm)
|
|
const vmCanSuspend = features.macos ? canSuspend : features.suspend
|
|
const guestArt = hasGuestArt(family)
|
|
const trigger = navBtn(
|
|
key,
|
|
label,
|
|
guestArt ? <GuestArt family={family} shape="tile" /> : <VersionArt shape="tile" major={macosMajors[key] ?? null} name={label} />,
|
|
guestArt ? 'nav-icon-guest' : 'nav-icon-art',
|
|
t(locale, `state_${st}` as I18nKey),
|
|
statusKind(st),
|
|
onActivate ? () => onActivate(key) : undefined,
|
|
undefined,
|
|
{ state: st, driver: vm.driver_id ?? '', busy: vmBusy || transitioning, family },
|
|
)
|
|
if (!onVMContextAction) return trigger
|
|
return (
|
|
<Ctx.Root key={key} dir={localeDir(locale)}>
|
|
<Ctx.Trigger
|
|
asChild
|
|
onContextMenu={(e) => {
|
|
ctxOpenedByPointerRef.current = e.button === 2 && (e.clientX !== 0 || e.clientY !== 0)
|
|
}}
|
|
>
|
|
{trigger}
|
|
</Ctx.Trigger>
|
|
<Ctx.Portal>
|
|
<Ctx.Content
|
|
className="menu rg-p-menu"
|
|
onCloseAutoFocus={(e) => {
|
|
if (
|
|
ctxOpenedByPointerRef.current
|
|
|| hasVisibleDialog()
|
|
) {
|
|
e.preventDefault()
|
|
}
|
|
}}
|
|
>
|
|
{stopped && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'start')}>
|
|
<HostIcon name="start"><Play size={14} /></HostIcon> {tr('start')}
|
|
</Ctx.Item>
|
|
)}
|
|
{suspended && (
|
|
<>
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy || !vmCanSuspend} onSelect={() => onVMContextAction(key, 'resume')}>
|
|
<HostIcon name="resume"><Play size={14} /></HostIcon> {tr('resume')}
|
|
</Ctx.Item>
|
|
{!vmCanSuspend && <Ctx.Label className="menu-group-label">{tr('resumeUnavailable')}</Ctx.Label>}
|
|
<Ctx.Item className="menu-item rg-p-menu-item danger" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'discardSuspend')}>
|
|
<Power size={14} /> {tr('discardSuspend')}
|
|
</Ctx.Item>
|
|
</>
|
|
)}
|
|
{running && (
|
|
<>
|
|
{features.window && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'focus')}>
|
|
<HostIcon name="window"><AppWindow size={14} /></HostIcon> {tr('showWindow')}
|
|
</Ctx.Item>
|
|
)}
|
|
{features.fullscreen && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'fullscreen')}>
|
|
<HostIcon name="fullscreen"><Maximize2 size={14} /></HostIcon> {tr('toggleFullscreen')}
|
|
</Ctx.Item>
|
|
)}
|
|
{features.screenshot && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'screenshot')}>
|
|
<HostIcon name="screenshot"><Camera size={14} /></HostIcon> {tr('copyScreenshot')}
|
|
</Ctx.Item>
|
|
)}
|
|
{vmCanSuspend && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'suspend')}>
|
|
<HostIcon name="suspend"><Pause size={14} /></HostIcon> {tr('suspend')}
|
|
</Ctx.Item>
|
|
)}
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'stop')}>
|
|
<HostIcon name="stop"><Square size={14} /></HostIcon> {tr('stop')}
|
|
</Ctx.Item>
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'restart')}>
|
|
<HostIcon name="restart"><RotateCw size={14} /></HostIcon> {tr('restart')}
|
|
</Ctx.Item>
|
|
</>
|
|
)}
|
|
{(running || transitioning) && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item danger" disabled={busyAction === 'forceStop'} onSelect={() => onVMContextAction(key, 'forceStop')}>
|
|
<HostIcon name="force-stop"><Power size={14} /></HostIcon> {tr('forceStop')}
|
|
</Ctx.Item>
|
|
)}
|
|
<Ctx.Separator className="menu-sep" />
|
|
{features.macos && (
|
|
<>
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={suspended || transitioning} onSelect={() => onVMContextAction(key, 'settings')}>
|
|
<HostIcon name="settings"><Settings size={14} /></HostIcon> {tr('settingsEllipsis')}
|
|
</Ctx.Item>
|
|
<Ctx.Item className="menu-item rg-p-menu-item" onSelect={() => onVMContextAction(key, 'openSharedFolder')}>
|
|
<HostIcon name="folder"><FolderOpen size={14} /></HostIcon> {tr('openSharedFolder')}
|
|
</Ctx.Item>
|
|
</>
|
|
)}
|
|
{stopped && features.clone && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" disabled={vmBusy} onSelect={() => onVMContextAction(key, 'cloneVM')}>
|
|
<HostIcon name="clone"><CopyPlus size={14} /></HostIcon> {tr('cloneVM')}
|
|
</Ctx.Item>
|
|
)}
|
|
{features.macos && (
|
|
<Ctx.Item className="menu-item rg-p-menu-item" onSelect={() => onVMContextAction(key, 'openFolder')}>
|
|
<HostIcon name="folder"><FolderOpen size={14} /></HostIcon> {tr('openFolder')}
|
|
</Ctx.Item>
|
|
)}
|
|
<Ctx.Separator className="menu-sep" />
|
|
<Ctx.Item className="menu-item rg-p-menu-item danger" disabled={vmBusy || running || suspended || transitioning} onSelect={() => onVMContextAction(key, 'delete')}>
|
|
<HostIcon name="delete"><Trash size={14} /></HostIcon> {tr('delete')}
|
|
</Ctx.Item>
|
|
</Ctx.Content>
|
|
</Ctx.Portal>
|
|
</Ctx.Root>
|
|
)
|
|
})}
|
|
</div>
|
|
<div className="nav-group rg-p-nav-group nav-system" role="group" aria-label={tr('sidebarSystem')}>
|
|
{navBtn(
|
|
INSTALLERS_ID,
|
|
tr('sidebarInstallers'),
|
|
<Download size={13} />,
|
|
'nav-icon-installers',
|
|
downloading ? tr('installersDownloading') : '',
|
|
downloading ? 'busy' : null,
|
|
)}
|
|
{navBtn(
|
|
PLUGINS_ID,
|
|
tr('sidebarPlugins'),
|
|
<HostIcon name="plugin"><Puzzle size={13} /></HostIcon>,
|
|
'nav-icon-plugins',
|
|
safeMode ? tr('pluginSafeModeShort') : '',
|
|
safeMode ? 'warn' : null,
|
|
)}
|
|
{pluginViews.map((view) => navBtn(
|
|
view.id,
|
|
view.title,
|
|
view.icon ? <span className="plugin-view-icon" aria-hidden="true" style={{ WebkitMaskImage: `url("${view.icon}")`, maskImage: `url("${view.icon}")` }} /> : <Puzzle size={13} />,
|
|
'nav-icon-plugins',
|
|
'',
|
|
null,
|
|
undefined,
|
|
false,
|
|
undefined,
|
|
view.badge,
|
|
))}
|
|
{navBtn(
|
|
HOST_ID,
|
|
tr('sidebarHost'),
|
|
<Cpu size={13} />,
|
|
'nav-icon-host',
|
|
tr(hostReady ? 'hostReady' : 'hostNeedsAction'),
|
|
hostReady ? 'running' : 'warn',
|
|
)}
|
|
{navBtn(
|
|
SETTINGS_ID,
|
|
tr('sidebarSettings'),
|
|
<HostIcon name="settings"><Settings size={13} /></HostIcon>,
|
|
'nav-icon-settings',
|
|
'',
|
|
null,
|
|
)}
|
|
{navBtn(
|
|
ABOUT_ID,
|
|
tr('settingsAbout'),
|
|
<HostIcon name="info"><Info size={13} /></HostIcon>,
|
|
'nav-icon-about',
|
|
'',
|
|
null,
|
|
undefined,
|
|
updateDot,
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="rg-slot-anchor" data-rg-slot="sidebar.footer" />
|
|
</aside>
|
|
</div>
|
|
)
|
|
}
|