mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-13 12:17:34 +00:00
7637091e84
* feat(ui): add glassmorphism design tokens and utility classes Introduce glass design system foundation: translucent oklch color variables for both light and dark themes, glass/glass-border/glass-highlight tokens, semantic status colors (success/warning/info), .glass and .glass-strong utility classes with backdrop-filter, reduced shadow values, and standardized spring animation presets in lib/motion.ts. * feat(ui): apply glass treatment to core components Update card, dialog, input, button, popover, sheet, tooltip, dropdown-menu, context-menu, select, alert-dialog, and tabs components with glassmorphism styling: translucent backgrounds via new CSS variables, backdrop-blur layers, glass-border luminous edges, and glass-highlight hover states. * refactor(settings): decompose Settings Modal into section components Extract 10 inline sections from the 1,987-line SettingsModal into dedicated files under components/settings/. Introduce section registry pattern replacing 14 conditional blocks. Add shared types, sidebar navigation grouping with separators, glass treatment on sidebar and nav buttons, and responsive modal height. SettingsModal shell shrinks to ~380 lines. * refactor(ui): unify all tabs to animate-ui TabsHighlight with glass styling Migrate 4 tab instances (EditorLayout, FleetView, ResourcesView, NotificationsSection) from inconsistent patterns (manual layoutId, underline border-b-2, default fade) to the shared TabsHighlight primitive with glass-highlight indicator and springs.snappy transition. Standardize EditorLayout nav highlight spring config, apply glass-highlight to sidebar stack list hover/active states, and update mobile nav styling. * refactor(ui): migrate hardcoded colors to semantic CSS variables Replace hardcoded Tailwind color classes across ~19 component files with semantic CSS variable classes: emerald/green to success, orange/amber to warning, blue to info. Preserves brand/decorative colors (Crown amber, Admiral blue). Enables consistent theming of status indicators across the entire application. * refactor(ui): Linear dark precision aesthetic — solid surfaces, depth cues, text hierarchy Replace glassmorphism with Linear.app-inspired design: solid surface tokens (card #111111, sidebar #0d0d0d, root #0a0a0a), backdrop-blur restricted to floating overlays only (blur(10px) saturate(1.15)), desaturated teal accent, font-weight 500 everywhere, monochrome chart palette, and three depth cues: root ambient glow, luminous card top-edge, steep text brightness ramp. * refactor(ui): precision polish — fix muddy dark, snowblind light, add design anchors - Replace 34 hardcoded rgba values with theme-aware stat-* CSS tokens - Fix light theme: solid white cards, off-white background, readable text - Add card-border tokens with sharper directional lighting (top edge 2x) - Add chart-grid/chart-tick tokens for theme-aware axis rendering - Upgrade body glow: teal-tinted (dark), warm amber (light) - Terminal-inspired sidebar: monospaced UP/DN status codes, Geist Mono - Add tabular-nums to stat values to prevent layout jitter - Light mode cards get shadow-sm for depth against off-white background * refactor(ui): Linear materiality pass — ghosted nav, translucent sidebar, font unity - De-escalate Delete button from solid destructive to ghost with hover fill - Make sidebar translucent (80% opacity + backdrop-blur) so body glow bleeds through - Bump dark nav accent to 0.07 for ghosted backlit selection - Unify all terminal/editor fonts to Geist Mono (was JetBrains/Consolas mix) - Add Monaco editor fontFamily for YAML/env editing consistency - Add threshold-based color to Host RAM and Host Disk stat values (warn/crit) * refactor(ui): material simulation — inherent depth, layer separation, recessed terminal - Bump dark background 0.065→0.08, card surfaces 0.10→0.12 for 4% layer separation - Add card-bevel token (inset top shimmer) for permanent structural depth - Add button-inner-glow token for physical key feel on outline buttons - Recess terminal with inset shadow and dimmed label - Reduce action icon strokeWidth to 1.5 for refined industrial feel - Add teal LED backlight bar on active nav item via blur pseudo-element * fix(ui): parse usagePercent string to number for getValueColor usagePercent is typed as string in SystemStats but getValueColor expects number, causing TS2345 in CI builds.
273 lines
14 KiB
TypeScript
273 lines
14 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { Skeleton } from '@/components/ui/skeleton';
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog';
|
|
import { toast } from 'sonner';
|
|
import { apiFetch } from '@/lib/api';
|
|
import { AdmiralGate } from './AdmiralGate';
|
|
import { TierBadge } from './TierBadge';
|
|
import { Zap, Plus, Copy, Trash2, CheckCircle, RefreshCw, Clock } from 'lucide-react';
|
|
|
|
interface ApiTokenListItem {
|
|
id: number;
|
|
name: string;
|
|
scope: string;
|
|
created_at: number;
|
|
last_used_at: number | null;
|
|
expires_at: number | null;
|
|
revoked_at: number | null;
|
|
}
|
|
|
|
const SCOPE_LABELS: Record<string, string> = {
|
|
'read-only': 'Read Only',
|
|
'deploy-only': 'Deploy Only',
|
|
'full-admin': 'Full Admin',
|
|
};
|
|
|
|
const SCOPE_BADGE_VARIANT: Record<string, 'default' | 'secondary' | 'destructive'> = {
|
|
'read-only': 'default',
|
|
'deploy-only': 'secondary',
|
|
'full-admin': 'destructive',
|
|
};
|
|
|
|
function formatDate(ts: number): string {
|
|
return new Date(ts).toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
|
|
}
|
|
|
|
function formatRelative(ts: number): string {
|
|
const diff = Date.now() - ts;
|
|
const mins = Math.floor(diff / 60000);
|
|
if (mins < 1) return 'Just now';
|
|
if (mins < 60) return `${mins}m ago`;
|
|
const hours = Math.floor(mins / 60);
|
|
if (hours < 24) return `${hours}h ago`;
|
|
const days = Math.floor(hours / 24);
|
|
if (days < 30) return `${days}d ago`;
|
|
return formatDate(ts);
|
|
}
|
|
|
|
export function ApiTokensSection() {
|
|
const [tokens, setTokens] = useState<ApiTokenListItem[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [creating, setCreating] = useState(false);
|
|
const [showForm, setShowForm] = useState(false);
|
|
const [newToken, setNewToken] = useState<{ id: number; token: string } | null>(null);
|
|
|
|
const [formName, setFormName] = useState('');
|
|
const [formScope, setFormScope] = useState('read-only');
|
|
const [formExpiry, setFormExpiry] = useState<number | null>(null);
|
|
|
|
const fetchTokens = async () => {
|
|
try {
|
|
const res = await apiFetch('/api-tokens', { localOnly: true });
|
|
if (res.ok) {
|
|
const data: ApiTokenListItem[] = await res.json();
|
|
setTokens(data.filter(t => !t.revoked_at));
|
|
}
|
|
} catch { toast.error('Failed to load API tokens.'); } finally { setLoading(false); }
|
|
};
|
|
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
useEffect(() => { fetchTokens(); }, []);
|
|
|
|
const handleCreate = async () => {
|
|
if (!formName.trim()) {
|
|
toast.error('Token name is required.');
|
|
return;
|
|
}
|
|
setCreating(true);
|
|
try {
|
|
const res = await apiFetch('/api-tokens', {
|
|
method: 'POST',
|
|
localOnly: true,
|
|
body: JSON.stringify({ name: formName.trim(), scope: formScope, expires_in: formExpiry }),
|
|
});
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
setNewToken({ id: data.id, token: data.token });
|
|
setShowForm(false);
|
|
setFormName('');
|
|
setFormScope('read-only');
|
|
setFormExpiry(null);
|
|
fetchTokens();
|
|
toast.success('API token created.');
|
|
} else {
|
|
const err = await res.json().catch(() => ({}));
|
|
toast.error(err?.error || err?.message || 'Failed to create token.');
|
|
}
|
|
} catch (e: unknown) {
|
|
toast.error((e as Error)?.message || 'Network error.');
|
|
} finally { setCreating(false); }
|
|
};
|
|
|
|
const handleRevoke = async (id: number) => {
|
|
try {
|
|
const res = await apiFetch(`/api-tokens/${id}`, { method: 'DELETE', localOnly: true });
|
|
if (res.ok) {
|
|
toast.success('API token revoked.');
|
|
fetchTokens();
|
|
} else {
|
|
const err = await res.json().catch(() => ({}));
|
|
toast.error(err?.error || err?.message || 'Failed to revoke token.');
|
|
}
|
|
} catch { toast.error('Network error.'); }
|
|
};
|
|
|
|
const copyToClipboard = (text: string, label: string) => {
|
|
navigator.clipboard.writeText(text);
|
|
toast.success(`${label} copied to clipboard.`);
|
|
};
|
|
|
|
return (
|
|
<AdmiralGate featureName="API Tokens">
|
|
<div className="space-y-6">
|
|
<div className="flex items-start justify-between pr-8">
|
|
<div>
|
|
<h3 className="text-lg font-medium tracking-tight flex items-center gap-2">
|
|
API Tokens <TierBadge />
|
|
</h3>
|
|
<p className="text-sm text-muted-foreground">
|
|
Generate scoped tokens for CI/CD pipelines, scripts, and automation.
|
|
</p>
|
|
</div>
|
|
<Button size="sm" onClick={() => setShowForm(!showForm)}>
|
|
<Plus className="w-4 h-4 mr-1.5" /> Create Token
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Create form */}
|
|
{showForm && (
|
|
<div className="space-y-4 bg-muted/10 p-4 border border-border rounded-xl">
|
|
<div className="space-y-2">
|
|
<Label>Name</Label>
|
|
<Input
|
|
placeholder="CI deploy pipeline"
|
|
value={formName}
|
|
onChange={e => setFormName(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>Permission Scope</Label>
|
|
<Select value={formScope} onValueChange={setFormScope}>
|
|
<SelectTrigger><SelectValue /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="read-only">Read Only - GET requests only</SelectItem>
|
|
<SelectItem value="deploy-only">Deploy Only - read + deploy actions</SelectItem>
|
|
<SelectItem value="full-admin">Full Admin - unrestricted access</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label>Expiration</Label>
|
|
<Select value={formExpiry === null ? 'never' : String(formExpiry)} onValueChange={v => setFormExpiry(v === 'never' ? null : Number(v))}>
|
|
<SelectTrigger><SelectValue /></SelectTrigger>
|
|
<SelectContent>
|
|
<SelectItem value="30">30 days</SelectItem>
|
|
<SelectItem value="60">60 days</SelectItem>
|
|
<SelectItem value="90">90 days</SelectItem>
|
|
<SelectItem value="365">1 year</SelectItem>
|
|
<SelectItem value="never">No expiration</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
<div className="flex justify-end gap-2 pt-2">
|
|
<Button variant="outline" size="sm" onClick={() => setShowForm(false)}>Cancel</Button>
|
|
<Button size="sm" onClick={handleCreate} disabled={creating}>
|
|
{creating ? <><RefreshCw className="w-4 h-4 mr-1.5 animate-spin" />Creating...</> : 'Create'}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Token reveal (shown once after creation) */}
|
|
{newToken && (
|
|
<div className="bg-success-muted border border-success/30 rounded-xl p-4 space-y-3">
|
|
<div className="flex items-center gap-2 text-sm font-medium text-success">
|
|
<CheckCircle className="w-4 h-4" /> Token created - copy it now
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">This token will not be shown again. Store it securely.</p>
|
|
<div className="flex items-center gap-2">
|
|
<code className="flex-1 text-xs font-mono bg-muted px-3 py-2 rounded-lg break-all select-all">{newToken.token}</code>
|
|
<Button variant="outline" size="sm" onClick={() => copyToClipboard(newToken.token, 'Token')}>
|
|
<Copy className="w-4 h-4" />
|
|
</Button>
|
|
</div>
|
|
<Button variant="outline" size="sm" onClick={() => setNewToken(null)}>Dismiss</Button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Loading state */}
|
|
{loading && (
|
|
<div className="space-y-3">
|
|
<Skeleton className="h-20 w-full rounded-xl" />
|
|
<Skeleton className="h-20 w-full rounded-xl" />
|
|
</div>
|
|
)}
|
|
|
|
{/* Empty state */}
|
|
{!loading && tokens.length === 0 && !showForm && (
|
|
<div className="flex flex-col items-center justify-center py-12 text-center">
|
|
<Zap className="w-10 h-10 text-muted-foreground/50 mb-3" />
|
|
<p className="text-sm text-muted-foreground">No API tokens yet.</p>
|
|
<p className="text-xs text-muted-foreground mt-1">Create one to authenticate CI/CD pipelines and scripts.</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* Token list */}
|
|
{!loading && tokens.map(token => (
|
|
<div key={token.id} className="border border-border rounded-xl p-4 space-y-3">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2 min-w-0">
|
|
<Zap className="w-4 h-4 text-muted-foreground shrink-0" />
|
|
<span className="font-medium text-sm truncate">{token.name}</span>
|
|
<Badge variant={SCOPE_BADGE_VARIANT[token.scope] || 'default'} className="text-[10px] shrink-0">
|
|
{SCOPE_LABELS[token.scope] || token.scope}
|
|
</Badge>
|
|
</div>
|
|
<AlertDialog>
|
|
<AlertDialogTrigger asChild>
|
|
<Button variant="ghost" size="sm" className="text-destructive hover:text-destructive shrink-0">
|
|
<Trash2 className="w-4 h-4" />
|
|
</Button>
|
|
</AlertDialogTrigger>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Revoke API token?</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
Revoking <strong>{token.name}</strong> will immediately invalidate it. Any pipelines or scripts using this token will stop working.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction onClick={() => handleRevoke(token.id)} className="bg-destructive text-destructive-foreground hover:bg-destructive/90">
|
|
Revoke
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</div>
|
|
<div className="flex items-center gap-4 text-xs text-muted-foreground">
|
|
<span className="flex items-center gap-1">
|
|
<Clock className="w-3 h-3" />
|
|
Created {formatDate(token.created_at)}
|
|
</span>
|
|
<span>
|
|
Last used: {token.last_used_at ? formatRelative(token.last_used_at) : 'Never'}
|
|
</span>
|
|
{token.expires_at && (
|
|
<span className={token.expires_at < Date.now() ? 'text-destructive' : ''}>
|
|
{token.expires_at < Date.now() ? 'Expired' : `Expires ${formatDate(token.expires_at)}`}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</AdmiralGate>
|
|
);
|
|
}
|