File size: 13,787 Bytes
921d377 | 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | /**
* CatalogPanel β CRUD for ix_action_catalog.
*
* Table view of actions (label, intent, level gate, scheme, XP,
* cooldown) plus a "+ New action" modal. Delete is gated by a
* confirm modal so one misclick doesn't nuke a gating action.
*
* Mutations are optimistic: create injects a tombstone row into
* the cached data with a local id, rolls back on failure; delete
* removes the row immediately and re-inserts it if the server
* rejects. Both paths surface a toast.
*/
import React, { useCallback, useMemo, useState } from "react";
import { ListChecks, Plus, Trash2 } from "lucide-react";
import type { InteractiveApi } from "./api";
import { InteractiveApiError } from "./types";
import type { ActionItem, ProgressionScheme } from "./types";
import {
DangerButton,
EmptyState,
ErrorBanner,
Modal,
Panel,
PrimaryButton,
SecondaryButton,
SkeletonRow,
useAsyncResource,
useToast,
} from "./ui";
export interface CatalogPanelProps {
api: InteractiveApi;
projectId: string;
}
const SCHEMES: ProgressionScheme[] = [
"xp_level", "mastery", "cefr", "affinity_tier", "certification",
];
export function CatalogPanel({ api, projectId }: CatalogPanelProps) {
const toast = useToast();
const resource = useAsyncResource<ActionItem[]>(
(signal) => api.listActions(projectId, signal),
[api, projectId],
);
const [createOpen, setCreateOpen] = useState(false);
const [confirmDelete, setConfirmDelete] = useState<ActionItem | null>(null);
const items = useMemo(() => resource.data || [], [resource.data]);
const onCreated = useCallback((created: ActionItem) => {
resource.setData((prev) => [...(prev || []), created]);
}, [resource]);
const onDelete = useCallback(async () => {
const target = confirmDelete;
if (!target) return;
const snapshot = items;
resource.setData((prev) => (prev || []).filter((a) => a.id !== target.id));
setConfirmDelete(null);
try {
await api.deleteAction(target.id);
toast.toast({ variant: "success", title: "Action deleted" });
} catch (err) {
const e = err as InteractiveApiError;
resource.setData(snapshot);
toast.toast({
variant: "error",
title: "Couldn't delete",
message: e.message || "The action has been restored.",
});
}
}, [api, confirmDelete, items, resource, toast]);
return (
<Panel
title="Action catalog"
subtitle="Actions viewers can take during playback. Level gates, cooldowns, and XP rewards apply per-turn."
actions={
<PrimaryButton
onClick={() => setCreateOpen(true)}
size="sm"
icon={<Plus className="w-4 h-4" aria-hidden />}
>
New action
</PrimaryButton>
}
>
{resource.error ? (
<ErrorBanner
title="Couldn't load the catalog"
message={resource.error}
onRetry={resource.reload}
/>
) : resource.loading && !resource.data ? (
<div className="flex flex-col gap-2" aria-busy="true">
{Array.from({ length: 4 }).map((_, i) => <SkeletonRow key={i} />)}
</div>
) : items.length === 0 ? (
<EmptyState
icon={<ListChecks className="w-12 h-12" aria-hidden />}
title="No actions yet"
description="Add at least one action to give viewers something to do during playback."
action={
<PrimaryButton
onClick={() => setCreateOpen(true)}
icon={<Plus className="w-4 h-4" aria-hidden />}
>
Add your first action
</PrimaryButton>
}
/>
) : (
<ActionTable items={items} onDelete={setConfirmDelete} />
)}
<NewActionModal
open={createOpen}
onClose={() => setCreateOpen(false)}
api={api}
projectId={projectId}
onCreated={(a) => {
onCreated(a);
setCreateOpen(false);
toast.toast({ variant: "success", title: "Action created", message: a.label });
}}
/>
<Modal
open={!!confirmDelete}
onClose={() => setConfirmDelete(null)}
title="Delete action?"
footer={
<>
<SecondaryButton onClick={() => setConfirmDelete(null)}>Cancel</SecondaryButton>
<DangerButton onClick={onDelete} icon={<Trash2 className="w-4 h-4" aria-hidden />}>
Delete
</DangerButton>
</>
}
>
<p className="text-sm text-[#cfd8dc]">
This will remove <span className="font-medium">{confirmDelete?.label}</span> from the
catalog. Existing sessions keep running; new sessions won't see it.
</p>
</Modal>
</Panel>
);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Table
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function ActionTable({
items, onDelete,
}: {
items: ActionItem[];
onDelete: (a: ActionItem) => void;
}) {
return (
<div className="overflow-x-auto -mx-5 px-5">
<table className="w-full min-w-[720px] text-sm">
<thead>
<tr className="text-left text-[11px] uppercase tracking-wide text-[#777] border-b border-[#3f3f3f]">
<th className="py-2 pr-3 font-medium">Label</th>
<th className="py-2 pr-3 font-medium">Intent</th>
<th className="py-2 pr-3 font-medium">Level gate</th>
<th className="py-2 pr-3 font-medium">Scheme</th>
<th className="py-2 pr-3 font-medium text-right">XP</th>
<th className="py-2 pr-3 font-medium text-right">Cooldown</th>
<th className="py-2 pl-3 font-medium text-right"> </th>
</tr>
</thead>
<tbody className="divide-y divide-[#2a2a2a]">
{items.map((a) => (
<tr key={a.id} className="hover:bg-[#121212]">
<td className="py-2.5 pr-3 text-[#f1f1f1] font-medium">{a.label}</td>
<td className="py-2.5 pr-3 text-[#cfd8dc]">
{a.intent_code
? <code className="text-xs bg-[#121212] border border-[#3f3f3f] rounded px-1.5 py-0.5">{a.intent_code}</code>
: <span className="text-[#777]">β</span>}
</td>
<td className="py-2.5 pr-3 text-[#cfd8dc]">
{a.required_level ?? 1}
<span className="text-[#777] text-xs"> / {a.required_metric_key || "level"}</span>
</td>
<td className="py-2.5 pr-3 text-[#cfd8dc]">{a.required_scheme || "xp_level"}</td>
<td className="py-2.5 pr-3 text-[#cfd8dc] text-right">
{a.xp_award ? <span>+{a.xp_award}</span> : <span className="text-[#777]">0</span>}
</td>
<td className="py-2.5 pr-3 text-[#cfd8dc] text-right">
{a.cooldown_sec ? `${a.cooldown_sec}s` : <span className="text-[#777]">β</span>}
</td>
<td className="py-2.5 pl-3 text-right">
<button
type="button"
onClick={() => onDelete(a)}
aria-label={`Delete ${a.label}`}
className="text-[#aaa] hover:text-red-400 p-1 rounded focus:outline-none focus-visible:ring-2 focus-visible:ring-red-500"
>
<Trash2 className="w-4 h-4" aria-hidden />
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// New-action modal
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function NewActionModal({
open, onClose, api, projectId, onCreated,
}: {
open: boolean;
onClose: () => void;
api: InteractiveApi;
projectId: string;
onCreated: (a: ActionItem) => void;
}) {
const toast = useToast();
const [label, setLabel] = useState("");
const [intentCode, setIntentCode] = useState("");
const [requiredLevel, setRequiredLevel] = useState(1);
const [scheme, setScheme] = useState<ProgressionScheme>("xp_level");
const [xpAward, setXpAward] = useState(0);
const [cooldown, setCooldown] = useState(0);
const [submitting, setSubmitting] = useState(false);
const reset = useCallback(() => {
setLabel(""); setIntentCode(""); setRequiredLevel(1);
setScheme("xp_level"); setXpAward(0); setCooldown(0);
}, []);
const canSubmit = label.trim().length > 0 && !submitting;
const submit = useCallback(async () => {
if (!canSubmit) return;
setSubmitting(true);
try {
const created = await api.createAction(projectId, {
label: label.trim(),
intent_code: intentCode.trim(),
required_level: requiredLevel,
required_scheme: scheme,
required_metric_key: scheme === "xp_level" ? "level" : "",
xp_award: xpAward,
cooldown_sec: cooldown,
});
reset();
onCreated(created);
} catch (err) {
const e = err as InteractiveApiError;
toast.toast({
variant: "error",
title: "Couldn't create action",
message: e.message || "Check the inputs and try again.",
});
setSubmitting(false);
}
}, [api, canSubmit, cooldown, intentCode, label, onCreated, projectId, requiredLevel, reset, scheme, toast, xpAward]);
return (
<Modal
open={open}
onClose={() => { if (!submitting) { reset(); onClose(); } }}
title="New action"
widthClass="max-w-xl"
footer={
<>
<SecondaryButton onClick={() => { reset(); onClose(); }} disabled={submitting}>
Cancel
</SecondaryButton>
<PrimaryButton onClick={submit} disabled={!canSubmit} loading={submitting}>
Create
</PrimaryButton>
</>
}
>
<div className="flex flex-col gap-4">
<FormField label="Label" required>
<input
type="text"
value={label}
onChange={(e) => setLabel(e.target.value)}
placeholder="e.g. Greet the host"
maxLength={80}
className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
/>
</FormField>
<FormField label="Intent code" hint="Free-form string used to route to edges; e.g. 'greeting', 'flirt'.">
<input
type="text"
value={intentCode}
onChange={(e) => setIntentCode(e.target.value)}
placeholder="(optional)"
maxLength={40}
className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
/>
</FormField>
<div className="grid grid-cols-2 gap-3">
<FormField label="Required level">
<input
type="number"
min={1} max={50}
value={requiredLevel}
onChange={(e) => setRequiredLevel(Math.max(1, parseInt(e.target.value, 10) || 1))}
className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
/>
</FormField>
<FormField label="Progression scheme">
<select
value={scheme}
onChange={(e) => setScheme(e.target.value as ProgressionScheme)}
className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
>
{SCHEMES.map((s) => <option key={s} value={s}>{s}</option>)}
</select>
</FormField>
</div>
<div className="grid grid-cols-2 gap-3">
<FormField label="XP award" hint="XP granted when this action is taken (xp_level scheme).">
<input
type="number"
min={0} max={500}
value={xpAward}
onChange={(e) => setXpAward(Math.max(0, parseInt(e.target.value, 10) || 0))}
className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
/>
</FormField>
<FormField label="Cooldown (seconds)" hint="Per-session cooldown between repeat uses.">
<input
type="number"
min={0} max={3600}
value={cooldown}
onChange={(e) => setCooldown(Math.max(0, parseInt(e.target.value, 10) || 0))}
className="w-full bg-[#121212] border border-[#3f3f3f] rounded-md px-3 py-2 text-sm outline-none focus:border-[#3ea6ff]"
/>
</FormField>
</div>
</div>
</Modal>
);
}
function FormField({
label, hint, required, children,
}: {
label: string;
hint?: string;
required?: boolean;
children: React.ReactNode;
}) {
return (
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-[#cfd8dc]">
{label}
{required && <span className="text-[#3ea6ff] ml-0.5" aria-label="required">*</span>}
</label>
{hint && <p className="text-[11px] text-[#777] -mt-0.5">{hint}</p>}
{children}
</div>
);
}
|