Every VM action had two bindings: one by name and one by driver and name. The window used only the second, so the first is gone and the WithDriver suffix with it. Unused bindings and frontend exports are removed too. Tests now reject bindings the window never calls, stale generated bindings and unused frontend exports. TypeScript rejects unused locals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.1 KiB
TypeScript
121 lines
4.1 KiB
TypeScript
import * as RTooltip from '@radix-ui/react-tooltip'
|
|
import { LoaderCircle } from 'lucide-react'
|
|
import React from 'react'
|
|
import type { UIContractIcon } from '../styles/ui-contract'
|
|
import { HostIcon } from './themeVisuals'
|
|
|
|
export const TooltipProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
<RTooltip.Provider delayDuration={400} skipDelayDuration={200}>{children}</RTooltip.Provider>
|
|
)
|
|
|
|
interface TipProps {
|
|
label: string
|
|
shortcut?: string
|
|
side?: 'top' | 'bottom' | 'left' | 'right'
|
|
disabled?: boolean
|
|
children: React.ReactElement
|
|
}
|
|
|
|
/** Tooltip around a single focusable child. */
|
|
export const Tip: React.FC<TipProps> = ({ label, shortcut, side = 'bottom', disabled, children }) => {
|
|
const [open, setOpen] = React.useState(false)
|
|
React.useEffect(() => {
|
|
if (disabled) setOpen(false)
|
|
}, [disabled])
|
|
return (
|
|
<RTooltip.Root open={disabled ? false : open} onOpenChange={(next) => setOpen(disabled ? false : next)}>
|
|
<RTooltip.Trigger asChild>{children}</RTooltip.Trigger>
|
|
<RTooltip.Portal>
|
|
<RTooltip.Content className="tooltip rg-p-tooltip" side={side} sideOffset={6} collisionPadding={8}>
|
|
{label}
|
|
{shortcut && <kbd>{shortcut}</kbd>}
|
|
</RTooltip.Content>
|
|
</RTooltip.Portal>
|
|
</RTooltip.Root>
|
|
)
|
|
}
|
|
|
|
type ButtonVariant = 'secondary' | 'primary' | 'danger' | 'ghost'
|
|
|
|
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
variant?: ButtonVariant
|
|
busy?: boolean
|
|
icon?: React.ReactNode
|
|
}
|
|
|
|
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
|
({ variant = 'secondary', busy, icon, children, disabled, className, ...rest }, ref) => (
|
|
<button
|
|
ref={ref}
|
|
type="button"
|
|
className={`btn rg-p-button${variant === 'secondary' ? '' : ` btn-${variant}`}${className ? ` ${className}` : ''}`}
|
|
data-variant={variant}
|
|
data-busy={busy ? 'true' : undefined}
|
|
disabled={disabled || busy}
|
|
aria-busy={busy || undefined}
|
|
{...rest}
|
|
>
|
|
{busy ? <LoaderCircle size={14} className="spin" aria-hidden="true" /> : icon}
|
|
{children}
|
|
</button>
|
|
),
|
|
)
|
|
Button.displayName = 'Button'
|
|
|
|
interface IconButtonProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'children'> {
|
|
label: string
|
|
shortcut?: string
|
|
icon: React.ReactNode
|
|
busy?: boolean
|
|
small?: boolean
|
|
tipSide?: TipProps['side']
|
|
}
|
|
|
|
/** Icon-only button: always carries a tooltip and an accessible name. */
|
|
export const IconButton = React.forwardRef<HTMLButtonElement, IconButtonProps>(
|
|
({ label, shortcut, icon, busy, small, disabled, className, tipSide, ...rest }, ref) => (
|
|
<Tip label={label} shortcut={shortcut} side={tipSide}>
|
|
<button
|
|
ref={ref}
|
|
type="button"
|
|
aria-label={label}
|
|
className={`icon-btn rg-p-icon-button${small ? ' sm' : ''}${className ? ` ${className}` : ''}`}
|
|
data-busy={busy ? 'true' : undefined}
|
|
disabled={disabled || busy}
|
|
aria-busy={busy || undefined}
|
|
{...rest}
|
|
>
|
|
{busy ? <LoaderCircle size={16} className="spin" aria-hidden="true" /> : icon}
|
|
</button>
|
|
</Tip>
|
|
),
|
|
)
|
|
IconButton.displayName = 'IconButton'
|
|
|
|
type StatusKind = 'running' | 'stopped' | 'suspended' | 'busy' | 'broken' | 'warn'
|
|
|
|
export function statusKind(state: string): StatusKind {
|
|
if (state === 'running') return 'running'
|
|
if (state === 'suspended') return 'suspended'
|
|
if (state === 'starting' || state === 'stopping' || state === 'suspending' || state === 'resuming' || state === 'creating') return 'busy'
|
|
if (state === 'driver_unavailable') return 'warn'
|
|
if (state === 'broken') return 'broken'
|
|
return 'stopped'
|
|
}
|
|
|
|
const STATUS_ICON: Record<StatusKind, UIContractIcon> = {
|
|
running: 'vm.state.running',
|
|
stopped: 'vm.state.stopped',
|
|
suspended: 'vm.state.suspended',
|
|
busy: 'vm.state.busy',
|
|
broken: 'vm.state.error',
|
|
warn: 'vm.state.error',
|
|
}
|
|
|
|
/** Status shape: differs by shape as well as colour; themes may replace it by icon name. */
|
|
export const StatusShape: React.FC<{ kind: StatusKind }> = ({ kind }) => (
|
|
<HostIcon name={STATUS_ICON[kind]} size={10}>
|
|
<span className={`status status-${kind}`} aria-hidden="true" />
|
|
</HostIcon>
|
|
)
|