'use client' import { useCallback, useState } from 'react' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import type { ExploreResult, SystemState } from '@/lib/game/save' import type { FoundItem } from '@/lib/game/world' // LOCAL SYSTEM tab — the bodies of the current system (planets + staggered moons) with an // EXPLORE button per unexplored row. The parent (MissionControl) owns the /api/system state so // the right column's CARGO HOLD mirrors scans immediately; this panel reports each scan's // response up via onChanged and keeps only its own UI bits locally (no polling). Turn-based: // one scan costs a turn, each body once. interface LocalSystemPanelProps { system: SystemState | null onChanged: (next: SystemState) => void } export function LocalSystemPanel({ system, onChanged }: LocalSystemPanelProps) { const [lastOutcome, setLastOutcome] = useState(null) const [error, setError] = useState(null) const [exploringId, setExploringId] = useState(null) const explore = useCallback( async (planetId: number) => { setExploringId(planetId) try { const res = await fetch('/api/explore', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ planetId }), }) const body = (await res.json()) as ExploreResult & { error?: string } if (!res.ok) throw new Error(body.error ?? `HTTP ${res.status}`) onChanged(body) // fresh state straight from the response — parent mirrors it to CARGO HOLD setLastOutcome(body.outcome) } catch (err: unknown) { setError(err instanceof Error ? err.message : String(err)) } finally { setExploringId(null) } }, [onChanged] ) if (!system) { return (
SCANNING SYSTEM…
) } const badgeFor = (explored: boolean, hazard: boolean) => explored ? { label: 'SCANNED', variant: 'ACTIVE' as const } : hazard ? { label: 'HAZARD', variant: 'WARNING' as const } : { label: 'UNEXPLORED', variant: 'SCANNING' as const } return (
EXPLORABLE WORLDS // {system.systemName.toUpperCase()} — TURN {system.turn}
{system.planets.map((row, i) => { const depth = row.parent !== null ? 1 : 0 const badge = badgeFor(row.explored, row.hazard) return (
{row.name}
{depth ? `MOON OF ${system.planets.find((p) => p.id === row.parent)?.name ?? '?'} // ${row.type}` : row.type}
{badge.label} {!row.explored && ( )}
) })}
{lastOutcome !== null && (
SCAN RESULT //{' '} {lastOutcome.length > 0 ? lastOutcome.map((i) => `${i.qty}× ${i.name.toUpperCase()}`).join(', ') : 'NOTHING FOUND'}
)} {error && (
SCAN FAILED // {error.toUpperCase()}
)}
) }