Add JSON import/export for full encounter state

Export and import encounter, undo/redo history, and player characters
as a downloadable .json file. Export/import actions are in the action
bar overflow menu. Import validates using existing rehydration functions
and shows a confirmation dialog when replacing a non-empty encounter.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-03-27 14:28:39 +01:00
parent f6766b729d
commit fba83bebd6
19 changed files with 1339 additions and 2 deletions

View File

@@ -0,0 +1,62 @@
import { useEffect, useRef } from "react";
import { Button } from "./ui/button.js";
interface ImportConfirmDialogProps {
open: boolean;
onConfirm: () => void;
onCancel: () => void;
}
export function ImportConfirmDialog({
open,
onConfirm,
onCancel,
}: Readonly<ImportConfirmDialogProps>) {
const dialogRef = useRef<HTMLDialogElement>(null);
useEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
if (open && !dialog.open) dialog.showModal();
else if (!open && dialog.open) dialog.close();
}, [open]);
useEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
function handleCancel(e: Event) {
e.preventDefault();
onCancel();
}
function handleBackdropClick(e: MouseEvent) {
if (e.target === dialog) onCancel();
}
dialog.addEventListener("cancel", handleCancel);
dialog.addEventListener("mousedown", handleBackdropClick);
return () => {
dialog.removeEventListener("cancel", handleCancel);
dialog.removeEventListener("mousedown", handleBackdropClick);
};
}, [onCancel]);
return (
<dialog
ref={dialogRef}
className="rounded-lg border border-border bg-card p-6 text-foreground shadow-xl backdrop:bg-black/50"
>
<h2 className="mb-2 font-semibold text-lg">Replace current encounter?</h2>
<p className="mb-4 text-muted-foreground text-sm">
Importing will replace your current encounter, undo/redo history, and
player characters. This cannot be undone.
</p>
<div className="flex justify-end gap-2">
<Button type="button" variant="ghost" onClick={onCancel}>
Cancel
</Button>
<Button type="button" onClick={onConfirm}>
Import
</Button>
</div>
</dialog>
);
}