import type { PlayerState, RouteRequest } from "@deadhead/schema"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { useApi } from "../api/context"; import { serverNow, syncClock } from "../clock/clock"; import { locationName } from "../format"; import { errorMessage, useLoadedPlayer } from "../state/actions"; import { PLAYER_STATE_KEY } from "../state/player"; import { Button } from "../ui"; const MINUTE = 60_000; const selectClass = "min-w-0 flex-1 rounded-lg border border-glass-line bg-space-900 px-2 py-1.5 text-sm text-ink-100"; /** * Settings → Developer (non-production builds only, CR-011): fast-forward the * server clock, add credits, land every flight now, and adjust pilots, ships * and discoveries for testing. */ export function DevTools() { const api = useApi(); const queryClient = useQueryClient(); const state = useLoadedPlayer(); const refresh = async () => { await syncClock(api); await queryClient.invalidateQueries({ queryKey: PLAYER_STATE_KEY }); await queryClient.invalidateQueries({ queryKey: ["board"] }); }; /** The pilot, ship and discovery tools answer with the new state. */ const applyState = async (next: PlayerState) => { queryClient.setQueryData(PLAYER_STATE_KEY, next); await queryClient.invalidateQueries({ queryKey: ["board"] }); }; const advance = useMutation({ mutationFn: (advanceMs: number) => api.call("devAdvanceClock", { advanceMs }), onSuccess: refresh, }); const credits = useMutation({ mutationFn: () => api.call("devAddCredits", { amount: 10_000 }), onSuccess: refresh, }); const pilotTool = useMutation({ mutationFn: (input: RouteRequest<"devPilot">) => api.call("devPilot", input), onSuccess: applyState, }); const shipTool = useMutation({ mutationFn: (input: RouteRequest<"devShip">) => api.call("devShip", input), onSuccess: applyState, }); const discoverAll = useMutation({ mutationFn: () => api.call("devDiscoverAll", {}), onSuccess: applyState, }); const [pilotId, setPilotId] = useState(state.pilots[0]?.id ?? ""); const [shipId, setShipId] = useState(state.ships[0]?.id ?? ""); const discovered = state.locations.filter((l) => l.discovered).map((l) => l.id); const [destination, setDestination] = useState(discovered[0] ?? "earth"); const pilot = state.pilots.find((p) => p.id === pilotId); const ship = state.ships.find((s) => s.id === shipId); const lastArrival = Math.max(0, ...state.activeDispatches.map((d) => d.arrivalAt)); const busy = advance.isPending || credits.isPending || pilotTool.isPending || shipTool.isPending || discoverAll.isPending; const error = advance.error ?? credits.error ?? pilotTool.error ?? shipTool.error ?? discoverAll.error; return (
{state.pilots.length > 0 && (
Pilot
)} {state.ships.length > 0 && (
Ship
)} {error && (

{errorMessage(error)}

)}
); }