Moves editing into forms. Every doc has a page now. BUG: state not refreshed after mutation

This commit is contained in:
2025-06-13 16:58:26 -07:00
parent 293e1f9f62
commit ad8fb07c69
14 changed files with 313 additions and 59 deletions

View File

@@ -0,0 +1,56 @@
import {
isLocation,
isMonster,
isNpc,
isScene,
isSecret,
isSession,
isTreasure,
type AnyDocument,
} from "@/lib/types";
import { LocationEditForm } from "./location/LocationEditForm";
import { MonsterEditForm } from "./monsters/MonsterEditForm";
import { NpcEditForm } from "./npc/NpcEditForm";
import { SceneEditForm } from "./scene/SceneEditForm";
import { SecretEditForm } from "./secret/SecretEditForm";
import { SessionEditForm } from "./session/SessionEditForm";
import { TreasureEditForm } from "./treasure/TreasureEditForm";
function assertUnreachable(_x: never): never {
throw new Error("DocumentForm switch is not exhaustive");
}
/**
* Renders a form for any document type depending on the relationship.
*/
export const DocumentEditForm = ({ document }: { document: AnyDocument }) => {
if (isLocation(document)) {
return <LocationEditForm location={document} />;
}
if (isMonster(document)) {
return <MonsterEditForm monster={document} />;
}
if (isNpc(document)) {
return <NpcEditForm npc={document} />;
}
if (isScene(document)) {
return <SceneEditForm scene={document} />;
}
if (isSecret(document)) {
return <SecretEditForm secret={document} />;
}
if (isSession(document)) {
return <SessionEditForm session={document} />;
}
if (isTreasure(document)) {
return <TreasureEditForm treasure={document} />;
}
return assertUnreachable(document);
};

View File

@@ -1,7 +1,6 @@
// DocumentRow.tsx
// Generic row component for displaying any document type.
import { SecretRow } from "@/components/documents/secret/SecretRow";
import { SessionRow } from "@/components/documents/session/SessionRow";
import { SecretToggleRow } from "@/components/documents/secret/SecretToggleRow";
import {
isLocation,
isMonster,
@@ -13,11 +12,12 @@ import {
type Document,
type Session,
} from "@/lib/types";
import { LocationRow } from "./location/LocationRow";
import { MonsterRow } from "./monsters/MonsterRow";
import { NpcRow } from "./npc/NpcRow";
import { SceneRow } from "./scene/SceneRow";
import { TreasureRow } from "./treasure/TreasureRow";
import { LocationPrintRow } from "./location/LocationPrintRow";
import { MonsterPrintRow } from "./monsters/MonsterPrintRow";
import { NpcPrintRow } from "./npc/NpcPrintRow";
import { ScenePrintRow } from "./scene/ScenePrintRow";
import { SessionPrintRow } from "./session/SessionPrintRow";
import { TreasureToggleRow } from "./treasure/TreasureToggleRow";
/**
* Renders a row for any document type. Prioritizes Session, then Secret, then falls back to ID and creation time.
@@ -31,31 +31,31 @@ export const DocumentRow = ({
session?: Session;
}) => {
if (isLocation(document)) {
return <LocationRow location={document} />;
return <LocationPrintRow location={document} />;
}
if (isMonster(document)) {
return <MonsterRow monster={document} />;
return <MonsterPrintRow monster={document} />;
}
if (isNpc(document)) {
return <NpcRow npc={document} />;
return <NpcPrintRow npc={document} />;
}
if (isSession(document)) {
return <SessionRow session={document} />;
return <SessionPrintRow session={document} />;
}
if (isSecret(document)) {
return <SecretRow secret={document} session={session} />;
return <SecretToggleRow secret={document} session={session} />;
}
if (isScene(document)) {
return <SceneRow scene={document} />;
return <ScenePrintRow scene={document} />;
}
if (isTreasure(document)) {
return <TreasureRow treasure={document} session={session} />;
return <TreasureToggleRow treasure={document} session={session} />;
}
// Fallback: show ID and creation time

View File

@@ -3,9 +3,9 @@ import { pb } from "@/lib/pocketbase";
import type { Location } from "@/lib/types";
/**
* Renders an editable location row
* Renders an editable location form
*/
export const LocationRow = ({ location }: { location: Location }) => {
export const LocationEditForm = ({ location }: { location: Location }) => {
async function saveLocationName(name: string) {
await pb.collection("documents").update(location.id, {
data: {

View File

@@ -5,7 +5,7 @@ import type { Monster } from "@/lib/types";
/**
* Renders an editable monster row
*/
export const MonsterRow = ({ monster }: { monster: Monster }) => {
export const MonsterEditForm = ({ monster }: { monster: Monster }) => {
async function saveMonsterName(name: string) {
await pb.collection("documents").update(monster.id, {
data: {

View File

@@ -3,9 +3,9 @@ import { pb } from "@/lib/pocketbase";
import type { Npc } from "@/lib/types";
/**
* Renders an editable npc row
* Renders an editable npc form
*/
export const NpcRow = ({ npc }: { npc: Npc }) => {
export const NpcEditForm = ({ npc }: { npc: Npc }) => {
async function saveNpcName(name: string) {
await pb.collection("documents").update(npc.id, {
data: {

View File

@@ -3,9 +3,9 @@ import { pb } from "@/lib/pocketbase";
import type { Scene } from "@/lib/types";
/**
* Renders an editable scene row
* Renders an editable scene form
*/
export const SceneRow = ({ scene }: { scene: Scene }) => {
export const SceneEditForm = ({ scene }: { scene: Scene }) => {
async function saveScene(text: string) {
await pb.collection("documents").update(scene.id, {
data: {

View File

@@ -0,0 +1,90 @@
// Displays a single secret with discovered checkbox and text.
import type { Secret, Session } from "@/lib/types";
import { pb } from "@/lib/pocketbase";
import { useState } from "react";
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
/**
* Renders an editable secret form.
* Handles updating the discovered state and discoveredIn relationship.
*/
export const SecretEditForm = ({
secret,
session,
}: {
secret: Secret;
session?: Session;
}) => {
const [checked, setChecked] = useState(
!!(secret.data as any)?.secret?.discovered,
);
const [loading, setLoading] = useState(false);
async function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
const newChecked = e.target.checked;
setLoading(true);
setChecked(newChecked);
try {
await pb.collection("documents").update(secret.id, {
data: {
...secret.data,
secret: {
...(secret.data as any).secret,
discovered: newChecked,
},
},
});
if (session || !newChecked) {
// If the session exists or the element is being unchecked, remove any
// existing discoveredIn relationship
const rels = await pb.collection("relationships").getList(1, 1, {
filter: `primary = "${secret.id}" && type = "discoveredIn"`,
});
if (rels.items.length > 0) {
await pb.collection("relationships").delete(rels.items[0].id);
}
}
if (session) {
if (newChecked) {
await pb.collection("relationships").create({
primary: secret.id,
secondary: [session.id],
type: "discoveredIn",
});
}
}
} finally {
setLoading(false);
}
}
async function saveText(text: string) {
await pb.collection("documents").update(secret.id, {
data: {
...secret.data,
secret: {
...secret.data.secret,
text,
},
},
});
}
return (
<div className="flex items-center gap-3">
<input
type="checkbox"
checked={checked}
onChange={handleChange}
className="accent-emerald-500 w-5 h-5"
aria-label="Discovered"
disabled={loading}
/>
<AutoSaveTextarea
multiline={false}
value={secret.data.secret.text}
onSave={saveText}
/>
</div>
);
};

View File

@@ -8,7 +8,7 @@ import { useState } from "react";
* Renders a secret row with a discovered checkbox and secret text.
* Handles updating the discovered state and discoveredIn relationship.
*/
export const SecretRow = ({
export const SecretToggleRow = ({
secret,
session,
}: {

View File

@@ -1,19 +1,26 @@
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
import { pb } from "@/lib/pocketbase";
import type { Session } from "@/lib/types";
export const EditSessionForm = ({
session,
onSubmit,
}: {
session: Session;
onSubmit: (data: Session["data"]) => Promise<void>;
}) => {
export const SessionEditForm = ({ session }: { session: Session }) => {
async function saveStrongStart(strongStart: string) {
await pb.collection("documents").update(session.id, {
data: {
...session.data,
session: {
...session.data.session,
strongStart,
},
},
});
}
return (
<form>
<h3 className="text-lg font-bold mb-4 text-slate-100">Strong Start</h3>
<AutoSaveTextarea
value={session.data.session.strongStart}
onSave={(value) => onSubmit({ session: { strongStart: value } })}
onSave={saveStrongStart}
placeholder="Enter a strong start for this session..."
aria-label="Strong Start"
/>

View File

@@ -0,0 +1,90 @@
// Displays a single treasure with discovered checkbox and text.
import type { Treasure, Session } from "@/lib/types";
import { pb } from "@/lib/pocketbase";
import { useState } from "react";
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
/**
* Renders an editable treasure form.
* Handles updating the discovered state and discoveredIn relationship.
*/
export const TreasureEditForm = ({
treasure,
session,
}: {
treasure: Treasure;
session?: Session;
}) => {
const [checked, setChecked] = useState(
!!(treasure.data as any)?.treasure?.discovered,
);
const [loading, setLoading] = useState(false);
async function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
const newChecked = e.target.checked;
setLoading(true);
setChecked(newChecked);
try {
await pb.collection("documents").update(treasure.id, {
data: {
...treasure.data,
treasure: {
...(treasure.data as any).treasure,
discovered: newChecked,
},
},
});
if (session || !newChecked) {
// If the session exists or the element is being unchecked, remove any
// existing discoveredIn relationship
const rels = await pb.collection("relationships").getList(1, 1, {
filter: `primary = "${treasure.id}" && type = "discoveredIn"`,
});
if (rels.items.length > 0) {
await pb.collection("relationships").delete(rels.items[0].id);
}
}
if (session) {
if (newChecked) {
await pb.collection("relationships").create({
primary: treasure.id,
secondary: [session.id],
type: "discoveredIn",
});
}
}
} finally {
setLoading(false);
}
}
async function saveText(text: string) {
await pb.collection("documents").update(treasure.id, {
data: {
...treasure.data,
treasure: {
...treasure.data.treasure,
text,
},
},
});
}
return (
<div className="flex items-center gap-3">
<input
type="checkbox"
checked={checked}
onChange={handleChange}
className="accent-emerald-500 w-5 h-5"
aria-label="Discovered"
disabled={loading}
/>
<AutoSaveTextarea
multiline={false}
value={treasure.data.treasure.text}
onSave={saveText}
/>
</div>
);
};

View File

@@ -8,7 +8,7 @@ import { useState } from "react";
* Renders a treasure row with a discovered checkbox and treasure text.
* Handles updating the discovered state and discoveredIn relationship.
*/
export const TreasureRow = ({
export const TreasureToggleRow = ({
treasure,
session,
}: {