Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x | import {
ConfirmDialog,
DetailPage,
MetaGrid,
MetaItem,
SectionCard,
StatusBadge,
StatusScale,
TagList,
encodeResourceId,
useResourceId,
} from "@/components/detail";
import { SimulationScenario, SimulationStatus } from "@/objects/Simulation";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Archive, Calendar, Dices, Globe, Loader2, Play, Trash2 } from "lucide-react";
import React, { useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { parseServerDate } from "@/util/time";
/** How a scenario's agents relate to one another. Its own vocabulary, so its
* own scale — the tones come from the shared table either way. */
const STRATEGY_SCALE: StatusScale = {
cooperative: "success",
competitive: "danger",
mixed: "done",
adversarial: "warning",
custom: "neutral",
};
const STATUS_CYCLE: SimulationStatus[] = ["draft", "active", "archived"];
const ScenarioViewer: React.FC = () => {
const scenarioId = useResourceId("scenarioId");
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const navigate = useNavigate();
const [confirmDelete, setConfirmDelete] = useState(false);
// We don't have a dedicated getScenario-by-id without world context,
// so fetch all worlds then search across their scenarios.
const { data: worlds = [] } = useQuery({
queryKey: [QUERY_KEYS.GET.SIMULATION_WORLDS],
queryFn: () => getApiHandler().simulation_router.listWorlds(),
});
const {
data: scenario,
isLoading,
error,
} = useQuery<SimulationScenario | null>({
queryKey: [QUERY_KEYS.GET.SIMULATION_SCENARIO_BY_ID, scenarioId],
queryFn: async () => {
const api = getApiHandler();
for (const world of worlds) {
const s = await api.simulation_router.getScenario(world.id, scenarioId!);
if (s) return s;
}
return null;
},
enabled: worlds.length > 0 && !!scenarioId,
});
const world = scenario
? worlds.find((w) => w.id === scenario.world_id)
: undefined;
const updateMutation = useMutation({
mutationFn: (status: SimulationStatus) =>
getApiHandler().simulation_router.updateScenario(
scenario!.world_id,
scenarioId!,
{ status },
),
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.SIMULATION_SCENARIO_BY_ID, scenarioId],
}),
});
const deleteMutation = useMutation({
mutationFn: () =>
getApiHandler().simulation_router.deleteScenario(
scenario!.world_id,
scenarioId!,
),
onSuccess: () => {
void queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.SIMULATION_SCENARIOS],
});
void navigate(APP_ROUTES.USER.INFERENCE.agents);
},
});
const nextStatus = scenario
? STATUS_CYCLE[
(STATUS_CYCLE.indexOf(scenario.status) + 1) % STATUS_CYCLE.length
]
: null;
const actions = [
nextStatus && (
<button
key="status"
onClick={() => updateMutation.mutate(nextStatus)}
disabled={updateMutation.isPending}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors disabled:opacity-50"
>
{updateMutation.isPending ? (
<Loader2 className="size-3.5 animate-spin" />
) : nextStatus === "active" ? (
<Play className="size-3.5" />
) : (
<Archive className="size-3.5" />
)}
Mark {nextStatus}
</button>
),
<button
key="delete"
onClick={() => setConfirmDelete(true)}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-red-200 dark:border-red-800 text-red-500 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/30 transition-colors"
>
<Trash2 className="size-3.5" />
Delete
</button>,
].filter(Boolean) as React.ReactNode[];
return (
<DetailPage<SimulationScenario>
resource={scenario}
loading={isLoading}
error={error}
label="scenario"
title={scenario?.name}
icon={<Dices className="size-10 text-violet-400 dark:text-violet-500" />}
back={APP_ROUTES.USER.INFERENCE.agents}
actions={actions}
// Scenarios are versioned through their world, not in their own right —
// simulation_router's history endpoints hang off `worlds`.
history={false}
overlay={
confirmDelete &&
scenario && (
<ConfirmDialog
title="Delete Scenario"
icon={Trash2}
body={`Delete "${scenario.name}"? This cannot be undone.`}
loading={deleteMutation.isPending}
onConfirm={() => deleteMutation.mutate()}
onCancel={() => setConfirmDelete(false)}
/>
)
}
>
{(s) => (
<>
<SectionCard title="Details" icon={Dices} className="mb-4">
<MetaGrid>
<MetaItem
label="Status"
size="sm"
value={<StatusBadge status={s.status} />}
/>
<MetaItem
label="Strategy"
size="sm"
value={
<StatusBadge status={s.strategy} scale={STRATEGY_SCALE} />
}
/>
<MetaItem
icon={Calendar}
label="Created"
size="sm"
value={parseServerDate(s.created_at).toLocaleDateString(undefined, {
year: "numeric",
month: "short",
day: "numeric",
})}
/>
{world && (
<MetaItem
icon={Globe}
label="World"
size="sm"
value={
<Link
to={APP_ROUTES.USER.INFERENCE.SIMULATIONS.WORLDS.id.replace(
ROUTE_PARAMS.WORLD.id,
encodeResourceId(s.world_id),
)}
className="hover:underline"
>
{world.name}
</Link>
}
/>
)}
{s.description && (
<MetaItem
label="Description"
size="sm"
value={s.description}
className="col-span-2 sm:col-span-3"
/>
)}
{s.tags && s.tags.length > 0 && (
<MetaItem
label="Tags"
size="sm"
value={<TagList tags={s.tags} />}
className="col-span-2 sm:col-span-3"
/>
)}
</MetaGrid>
</SectionCard>
{s.config && Object.keys(s.config).length > 0 && (
<SectionCard title="Configuration" icon={Dices}>
<pre className="text-xs text-gray-600 dark:text-gray-300 bg-gray-50 dark:bg-gray-700/40 rounded-lg p-3 overflow-auto max-h-60 font-mono leading-relaxed">
{JSON.stringify(s.config, null, 2)}
</pre>
</SectionCard>
)}
</>
)}
</DetailPage>
);
};
export default ScenarioViewer;
|