feat(web): /machine dev-box page with status, terminal, and home file browser
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
'use client';
|
||||
|
||||
import { Loader2, Play, RotateCw, Server, Square } from 'lucide-react';
|
||||
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
Card,
|
||||
CardContent,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@spoon/ui';
|
||||
|
||||
export type BoxStatus = {
|
||||
running: boolean;
|
||||
image: string | null;
|
||||
startedAt: string | null;
|
||||
memoryLimitBytes: number | null;
|
||||
containerName: string;
|
||||
};
|
||||
|
||||
export type LifecycleAction = 'start' | 'stop' | 'restart';
|
||||
|
||||
const formatUptime = (startedAt: string | null): string => {
|
||||
if (!startedAt) return '—';
|
||||
const started = new Date(startedAt).getTime();
|
||||
if (Number.isNaN(started)) return '—';
|
||||
const seconds = Math.max(0, Math.floor((Date.now() - started) / 1000));
|
||||
const days = Math.floor(seconds / 86_400);
|
||||
const hours = Math.floor((seconds % 86_400) / 3_600);
|
||||
const minutes = Math.floor((seconds % 3_600) / 60);
|
||||
if (days > 0) return `${days}d ${hours}h`;
|
||||
if (hours > 0) return `${hours}h ${minutes}m`;
|
||||
if (minutes > 0) return `${minutes}m`;
|
||||
return `${seconds}s`;
|
||||
};
|
||||
|
||||
const formatMemory = (bytes: number | null): string => {
|
||||
if (bytes === null) return 'Unlimited';
|
||||
const gib = bytes / 1024 ** 3;
|
||||
return `${gib.toFixed(gib < 10 ? 1 : 0)} GiB`;
|
||||
};
|
||||
|
||||
const Detail = ({ label, value }: { label: string; value: string }) => (
|
||||
<div className='min-w-0'>
|
||||
<p className='text-muted-foreground text-[11px] font-medium tracking-wide uppercase'>
|
||||
{label}
|
||||
</p>
|
||||
<p className='truncate font-mono text-sm'>{value}</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
export const BoxStatusCard = ({
|
||||
status,
|
||||
pendingAction,
|
||||
onAction,
|
||||
}: {
|
||||
status: BoxStatus | null | undefined;
|
||||
pendingAction: LifecycleAction | null;
|
||||
onAction: (action: LifecycleAction) => void;
|
||||
}) => {
|
||||
const loading = status === undefined;
|
||||
const running = Boolean(status?.running);
|
||||
const busy = pendingAction !== null;
|
||||
|
||||
return (
|
||||
<Card className='shadow-none'>
|
||||
<CardHeader className='flex flex-row items-center justify-between gap-4 space-y-0'>
|
||||
<CardTitle className='flex items-center gap-2 text-base'>
|
||||
<Server className='size-4' />
|
||||
My Machine
|
||||
</CardTitle>
|
||||
{loading ? (
|
||||
<Badge variant='outline' className='gap-1'>
|
||||
<Loader2 className='size-3 animate-spin' />
|
||||
Checking…
|
||||
</Badge>
|
||||
) : running ? (
|
||||
<Badge className='gap-1'>Running</Badge>
|
||||
) : (
|
||||
<Badge variant='secondary'>Stopped</Badge>
|
||||
)}
|
||||
</CardHeader>
|
||||
<CardContent className='space-y-4'>
|
||||
<div className='grid grid-cols-2 gap-4 sm:grid-cols-3'>
|
||||
<Detail label='Image' value={status?.image ?? '—'} />
|
||||
<Detail
|
||||
label='Uptime'
|
||||
value={running ? formatUptime(status?.startedAt ?? null) : '—'}
|
||||
/>
|
||||
<Detail
|
||||
label='Memory'
|
||||
value={formatMemory(status?.memoryLimitBytes ?? null)}
|
||||
/>
|
||||
</div>
|
||||
<div className='flex flex-wrap gap-2'>
|
||||
{running ? (
|
||||
<>
|
||||
<Button
|
||||
type='button'
|
||||
variant='outline'
|
||||
size='sm'
|
||||
disabled={busy}
|
||||
onClick={() => onAction('stop')}
|
||||
>
|
||||
{pendingAction === 'stop' ? (
|
||||
<Loader2 className='size-4 animate-spin' />
|
||||
) : (
|
||||
<Square className='size-4' />
|
||||
)}
|
||||
Stop
|
||||
</Button>
|
||||
<Button
|
||||
type='button'
|
||||
variant='outline'
|
||||
size='sm'
|
||||
disabled={busy}
|
||||
onClick={() => onAction('restart')}
|
||||
>
|
||||
{pendingAction === 'restart' ? (
|
||||
<Loader2 className='size-4 animate-spin' />
|
||||
) : (
|
||||
<RotateCw className='size-4' />
|
||||
)}
|
||||
Restart
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button
|
||||
type='button'
|
||||
size='sm'
|
||||
disabled={busy || loading}
|
||||
onClick={() => onAction('start')}
|
||||
>
|
||||
{pendingAction === 'start' ? (
|
||||
<Loader2 className='size-4 animate-spin' />
|
||||
) : (
|
||||
<Play className='size-4' />
|
||||
)}
|
||||
Start
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user