Compare commits
1 Commits
db4ce36c27
...
eeb2ef04fd
| Author | SHA1 | Date | |
|---|---|---|---|
| eeb2ef04fd |
@@ -1,5 +1,4 @@
|
||||
import { DocumentList } from "@/components/DocumentList";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { displayName } from "@/lib/relationships";
|
||||
import type {
|
||||
@@ -8,10 +7,12 @@ import type {
|
||||
Relationship,
|
||||
RelationshipType,
|
||||
} from "@/lib/types";
|
||||
import { useState } from "react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Loader } from "./Loader";
|
||||
import { DocumentRow } from "./documents/DocumentRow";
|
||||
import { NewRelatedDocumentForm } from "./documents/NewRelatedDocumentForm";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
interface RelationshipListProps {
|
||||
root: AnyDocument;
|
||||
@@ -26,16 +27,105 @@ export function RelationshipList({
|
||||
root,
|
||||
relationshipType,
|
||||
}: RelationshipListProps) {
|
||||
const [_loading, setLoading] = useState(true);
|
||||
// const [items, setItems] = useState<AnyDocument[]>([]);
|
||||
// const [relationshipId, setRelationshipId] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// const queryClient = useQueryClient();
|
||||
|
||||
// useEffect(() => {
|
||||
// async function fetchItems() {
|
||||
// const { relationship } = await queryClient.fetchQuery({
|
||||
// queryKey: ["relationship", relationshipType, root.id],
|
||||
// staleTime: 5 * 60 * 1000, // 5 mintues
|
||||
// queryFn: async () => {
|
||||
// setLoading(true);
|
||||
// const relationship: Relationship = await pb
|
||||
// .collection("relationships")
|
||||
// .getFirstListItem(
|
||||
// `primary = "${root.id}" && type = "${relationshipType}"`,
|
||||
// {
|
||||
// expand: "secondary",
|
||||
// },
|
||||
// );
|
||||
//
|
||||
// setLoading(false);
|
||||
//
|
||||
// return { relationship };
|
||||
// },
|
||||
// });
|
||||
// setRelationshipId(relationship.id);
|
||||
// setItems(relationship.expand?.secondary ?? []);
|
||||
// }
|
||||
//
|
||||
// fetchItems();
|
||||
// }, [root, relationshipType]);
|
||||
|
||||
// Handles creation of a new document and adds it to the relationship
|
||||
// const handleCreate = async (doc: AnyDocument) => {
|
||||
// setLoading(true);
|
||||
// setError(null);
|
||||
// try {
|
||||
// // Check for existing relationship
|
||||
// if (relationshipId) {
|
||||
// console.debug("Adding to existing relationship", relationshipId);
|
||||
// await pb.collection("relationships").update(relationshipId, {
|
||||
// "+secondary": doc.id,
|
||||
// });
|
||||
// } else {
|
||||
// console.debug("Creating new relationship");
|
||||
// const relationship = await pb.collection("relationships").create({
|
||||
// primary: root.id,
|
||||
// secondary: [doc.id],
|
||||
// type: relationshipType,
|
||||
// });
|
||||
// setRelationshipId(relationship.id);
|
||||
// }
|
||||
// queryClient.invalidateQueries({
|
||||
// queryKey: ["relationship", relationshipType, root.id],
|
||||
// });
|
||||
// setItems((prev) => [doc, ...prev]);
|
||||
// } catch (e: any) {
|
||||
// setError(e?.message || "Failed to add document to relationship.");
|
||||
// } finally {
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// const handleRemove = async (documentId: DocumentId) => {
|
||||
// setLoading(true);
|
||||
// setError(null);
|
||||
//
|
||||
// try {
|
||||
// if (relationshipId) {
|
||||
// console.debug("Removing from existing relationship", relationshipId);
|
||||
// await pb.collection("relationships").update(relationshipId, {
|
||||
// "secondary-": documentId,
|
||||
// });
|
||||
// }
|
||||
// queryClient.invalidateQueries({
|
||||
// queryKey: ["relationship", relationshipType, root.id],
|
||||
// });
|
||||
// setItems((prev) => prev.filter((item) => item.id != documentId));
|
||||
// } catch (e: any) {
|
||||
// setError(
|
||||
// e?.message || `Failed to remove document from ${relationshipType}.`,
|
||||
// );
|
||||
// } finally {
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// if (loading) {
|
||||
// <Loader />;
|
||||
// }
|
||||
|
||||
const { state, dispatch } = useDocument();
|
||||
|
||||
if (state.status === "loading") {
|
||||
return <Loader />;
|
||||
}
|
||||
|
||||
console.info("Rendering relationship list: ", relationshipType);
|
||||
|
||||
const relationship = state.relationships[relationshipType];
|
||||
const itemIds = relationship?.secondary ?? [];
|
||||
const items = itemIds.map((id) => state.relatedDocs[id]).filter((d) => !!d);
|
||||
@@ -69,6 +159,10 @@ export function RelationshipList({
|
||||
type: "setRelationship",
|
||||
relationship: updatedRelationship,
|
||||
});
|
||||
dispatch({
|
||||
type: "setRelatedDocument",
|
||||
doc,
|
||||
});
|
||||
}
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add document to relationship.");
|
||||
|
||||
@@ -1,31 +1,27 @@
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Location } from "@/lib/types";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders an editable location form
|
||||
*/
|
||||
export const LocationEditForm = ({ location }: { location: Location }) => {
|
||||
const { dispatch } = useDocument();
|
||||
async function saveLocationName(name: string) {
|
||||
const updated: Location = await pb.collection("documents").update(location.id, {
|
||||
await pb.collection("documents").update(location.id, {
|
||||
data: {
|
||||
...location.data,
|
||||
name,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
async function saveLocationDescription(description: string) {
|
||||
const updated: Location = await pb.collection("documents").update(location.id, {
|
||||
await pb.collection("documents").update(location.id, {
|
||||
data: {
|
||||
...location.data,
|
||||
description,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,7 +4,6 @@ import { pb } from "@/lib/pocketbase";
|
||||
import { BaseForm } from "@/components/form/BaseForm";
|
||||
import { MultiLineInput } from "@/components/form/MultiLineInput";
|
||||
import { SingleLineInput } from "@/components/form/SingleLineInput";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new location. Calls onCreate with the new location document.
|
||||
@@ -16,7 +15,6 @@ export const NewLocationForm = ({
|
||||
campaign: CampaignId;
|
||||
onCreate: (location: Location) => Promise<void>;
|
||||
}) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [name, setName] = useState("");
|
||||
const [description, setDescription] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
@@ -38,7 +36,6 @@ export const NewLocationForm = ({
|
||||
});
|
||||
setName("");
|
||||
setDescription("");
|
||||
dispatch({ type: "setDocument", doc: locationDoc});
|
||||
await onCreate(locationDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add location.");
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Monster } from "@/lib/types";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders an editable monster row
|
||||
*/
|
||||
export const MonsterEditForm = ({ monster }: { monster: Monster }) => {
|
||||
const { dispatch } = useDocument();
|
||||
async function saveMonsterName(name: string) {
|
||||
const updated = await pb.collection("documents").update(monster.id, {
|
||||
await pb.collection("documents").update(monster.id, {
|
||||
data: {
|
||||
...monster.data,
|
||||
name,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -3,7 +3,6 @@ import type { CampaignId, Monster } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { BaseForm } from "@/components/form/BaseForm";
|
||||
import { SingleLineInput } from "@/components/form/SingleLineInput";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new monster. Calls onCreate with the new monster document.
|
||||
@@ -15,7 +14,6 @@ export const NewMonsterForm = ({
|
||||
campaign: CampaignId;
|
||||
onCreate: (monster: Monster) => Promise<void>;
|
||||
}) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [name, setName] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -34,7 +32,6 @@ export const NewMonsterForm = ({
|
||||
},
|
||||
});
|
||||
setName("");
|
||||
dispatch({ type: "setDocument", doc: monsterDoc });
|
||||
await onCreate(monsterDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add monster.");
|
||||
|
||||
@@ -4,7 +4,6 @@ import { pb } from "@/lib/pocketbase";
|
||||
import { BaseForm } from "@/components/form/BaseForm";
|
||||
import { SingleLineInput } from "@/components/form/SingleLineInput";
|
||||
import { MultiLineInput } from "@/components/form/MultiLineInput";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new npc. Calls onCreate with the new npc document.
|
||||
@@ -16,7 +15,6 @@ export const NewNpcForm = ({
|
||||
campaign: CampaignId;
|
||||
onCreate: (npc: Npc) => Promise<void>;
|
||||
}) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [name, setName] = useState("");
|
||||
const [description, setDescription] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
@@ -38,7 +36,6 @@ export const NewNpcForm = ({
|
||||
});
|
||||
setName("");
|
||||
setDescription("");
|
||||
dispatch({ type: "setDocument", doc: npcDoc });
|
||||
await onCreate(npcDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add npc.");
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Npc } from "@/lib/types";
|
||||
|
||||
@@ -7,25 +6,22 @@ import type { Npc } from "@/lib/types";
|
||||
* Renders an editable npc form
|
||||
*/
|
||||
export const NpcEditForm = ({ npc }: { npc: Npc }) => {
|
||||
const { dispatch } = useDocument();
|
||||
async function saveNpcName(name: string) {
|
||||
const updated: Npc = await pb.collection("documents").update(npc.id, {
|
||||
await pb.collection("documents").update(npc.id, {
|
||||
data: {
|
||||
...npc.data,
|
||||
name,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
async function saveNpcDescription(description: string) {
|
||||
const updated: Npc = await pb.collection("documents").update(npc.id, {
|
||||
await pb.collection("documents").update(npc.id, {
|
||||
data: {
|
||||
...npc.data,
|
||||
description,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { CampaignId, Scene } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { BaseForm } from "@/components/form/BaseForm";
|
||||
import { MultiLineInput } from "@/components/form/MultiLineInput";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new scene. Calls onCreate with the new scene document.
|
||||
@@ -17,7 +16,6 @@ export const NewSceneForm = ({
|
||||
campaign: CampaignId;
|
||||
onCreate: (scene: Scene) => Promise<void>;
|
||||
}) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [text, setText] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -36,7 +34,6 @@ export const NewSceneForm = ({
|
||||
},
|
||||
});
|
||||
setText("");
|
||||
dispatch({ type: "setDocument", doc: sceneDoc });
|
||||
await onCreate(sceneDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add scene.");
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Scene } from "@/lib/types";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
/**
|
||||
* Renders an editable scene form
|
||||
*/
|
||||
export const SceneEditForm = ({ scene }: { scene: Scene }) => {
|
||||
const { dispatch } = useDocument();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
async function saveScene(text: string) {
|
||||
const updated: Scene = await pb.collection("documents").update(scene.id, {
|
||||
await pb.collection("documents").update(scene.id, {
|
||||
data: {
|
||||
...scene.data,
|
||||
text,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["relationship"],
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { CampaignId, Secret } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { BaseForm } from "@/components/form/BaseForm";
|
||||
import { SingleLineInput } from "@/components/form/SingleLineInput";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new secret. Calls onCreate with the new secret document.
|
||||
@@ -17,7 +16,6 @@ export const NewSecretForm = ({
|
||||
campaign: CampaignId;
|
||||
onCreate: (secret: Secret) => Promise<void>;
|
||||
}) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [newSecret, setNewSecret] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -37,7 +35,6 @@ export const NewSecretForm = ({
|
||||
},
|
||||
});
|
||||
setNewSecret("");
|
||||
dispatch({ type: "setDocument", doc: secretDoc as Secret});
|
||||
await onCreate(secretDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add secret.");
|
||||
@@ -48,7 +45,7 @@ export const NewSecretForm = ({
|
||||
|
||||
return (
|
||||
<BaseForm
|
||||
title="Create new secret"
|
||||
title="Create new treasure"
|
||||
isLoading={adding || !newSecret.trim()}
|
||||
onSubmit={handleSubmit}
|
||||
error={error}
|
||||
@@ -56,7 +53,7 @@ export const NewSecretForm = ({
|
||||
<SingleLineInput
|
||||
value={newSecret}
|
||||
onChange={setNewSecret}
|
||||
placeholder="Secret description"
|
||||
placeholder="Treasure description"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
// Displays a single secret with discovered checkbox and text.
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import type { Secret, Session } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Secret } from "@/lib/types";
|
||||
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 }: { secret: Secret }) => {
|
||||
const { dispatch } = useDocument();
|
||||
export const SecretEditForm = ({
|
||||
secret,
|
||||
session,
|
||||
}: {
|
||||
secret: Secret;
|
||||
session?: Session;
|
||||
}) => {
|
||||
const [checked, setChecked] = useState(
|
||||
!!(secret.data as any)?.secret?.discovered,
|
||||
);
|
||||
@@ -21,28 +25,43 @@ export const SecretEditForm = ({ secret }: { secret: Secret }) => {
|
||||
setLoading(true);
|
||||
setChecked(newChecked);
|
||||
try {
|
||||
const updated: Secret = await pb
|
||||
.collection("documents")
|
||||
.update(secret.id, {
|
||||
await pb.collection("documents").update(secret.id, {
|
||||
data: {
|
||||
...secret.data,
|
||||
discovered: newChecked,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
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) {
|
||||
const updated: Secret = await pb.collection("documents").update(secret.id, {
|
||||
await pb.collection("documents").update(secret.id, {
|
||||
data: {
|
||||
...secret.data,
|
||||
text,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Session } from "@/lib/types";
|
||||
|
||||
export const SessionEditForm = ({ session }: { session: Session }) => {
|
||||
const { dispatch } = useDocument();
|
||||
|
||||
async function saveStrongStart(strongStart: string) {
|
||||
const doc: Session = await pb.collection("documents").update(session.id, {
|
||||
const freshRecord: Session = await pb
|
||||
.collection("documents")
|
||||
.update(session.id, {
|
||||
data: {
|
||||
...session.data,
|
||||
strongStart,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: "setDocument",
|
||||
doc,
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { CampaignId, Treasure } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { BaseForm } from "@/components/form/BaseForm";
|
||||
import { SingleLineInput } from "@/components/form/SingleLineInput";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
|
||||
/**
|
||||
* Renders a form to add a new treasure. Calls onCreate with the new treasure document.
|
||||
@@ -17,7 +16,6 @@ export const NewTreasureForm = ({
|
||||
campaign: CampaignId;
|
||||
onCreate: (treasure: Treasure) => Promise<void>;
|
||||
}) => {
|
||||
const { dispatch } = useDocument();
|
||||
const [newTreasure, setNewTreasure] = useState("");
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -37,10 +35,6 @@ export const NewTreasureForm = ({
|
||||
},
|
||||
});
|
||||
setNewTreasure("");
|
||||
dispatch({
|
||||
type: "setDocument",
|
||||
doc: treasureDoc,
|
||||
});
|
||||
await onCreate(treasureDoc);
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Failed to add treasure.");
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
// Displays a single treasure with discovered checkbox and text.
|
||||
import { AutoSaveTextarea } from "@/components/AutoSaveTextarea";
|
||||
import { useDocument } from "@/context/document/DocumentContext";
|
||||
import type { Treasure, Session } from "@/lib/types";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Treasure } from "@/lib/types";
|
||||
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 }: { treasure: Treasure }) => {
|
||||
const { dispatch } = useDocument();
|
||||
export const TreasureEditForm = ({
|
||||
treasure,
|
||||
session,
|
||||
}: {
|
||||
treasure: Treasure;
|
||||
session?: Session;
|
||||
}) => {
|
||||
const [checked, setChecked] = useState(
|
||||
!!(treasure.data as any)?.treasure?.discovered,
|
||||
);
|
||||
@@ -21,9 +25,7 @@ export const TreasureEditForm = ({ treasure }: { treasure: Treasure }) => {
|
||||
setLoading(true);
|
||||
setChecked(newChecked);
|
||||
try {
|
||||
const updated: Treasure = await pb
|
||||
.collection("documents")
|
||||
.update(treasure.id, {
|
||||
await pb.collection("documents").update(treasure.id, {
|
||||
data: {
|
||||
...treasure.data,
|
||||
treasure: {
|
||||
@@ -32,22 +34,37 @@ export const TreasureEditForm = ({ treasure }: { treasure: Treasure }) => {
|
||||
},
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
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) {
|
||||
const updated: Treasure = await pb
|
||||
.collection("documents")
|
||||
.update(treasure.id, {
|
||||
await pb.collection("documents").update(treasure.id, {
|
||||
data: {
|
||||
...treasure.data,
|
||||
text,
|
||||
},
|
||||
});
|
||||
dispatch({ type: "setDocument", doc: updated });
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { type AnyDocument, type DocumentId } from "@/lib/types";
|
||||
import type { RecordModel } from "pocketbase";
|
||||
import type { ReactNode } from "react";
|
||||
import { createContext, useContext, useEffect, useReducer } from "react";
|
||||
import type { DocumentAction } from "./actions";
|
||||
import { reducer } from "./reducer";
|
||||
import { loading, type DocumentState } from "./state";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import type { RecordModel } from "pocketbase";
|
||||
|
||||
type DocumentContextValue = {
|
||||
state: DocumentState<AnyDocument>;
|
||||
@@ -26,15 +27,19 @@ export function DocumentProvider({
|
||||
documentId: DocumentId;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const queryClient = useQueryClient();
|
||||
const [state, dispatch] = useReducer(reducer, loading());
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchDocumentAndRelations() {
|
||||
const doc: AnyDocument = await pb
|
||||
.collection("documents")
|
||||
.getOne(documentId, {
|
||||
const doc: AnyDocument = await queryClient.fetchQuery({
|
||||
queryKey: ["document", documentId],
|
||||
staleTime: 5 * 60 * 1000, // 5 mintues
|
||||
queryFn: () =>
|
||||
pb.collection("documents").getOne(documentId, {
|
||||
expand:
|
||||
"relationships_via_primary,relationships_via_primary.secondary",
|
||||
}),
|
||||
});
|
||||
|
||||
dispatch({
|
||||
@@ -42,7 +47,7 @@ export function DocumentProvider({
|
||||
doc,
|
||||
relationships: doc.expand?.relationships_via_primary || [],
|
||||
relatedDocuments:
|
||||
doc.expand?.relationships_via_primary?.flatMap(
|
||||
doc.expand?.relationships_via_primary.flatMap(
|
||||
(r: RecordModel) => r.expand?.secondary,
|
||||
) || [],
|
||||
});
|
||||
|
||||
@@ -11,10 +11,14 @@ export type DocumentAction<D extends AnyDocument> =
|
||||
relatedDocuments: AnyDocument[];
|
||||
}
|
||||
| {
|
||||
type: "setDocument";
|
||||
doc: AnyDocument;
|
||||
type: "update";
|
||||
data: D["data"];
|
||||
}
|
||||
| {
|
||||
type: "setRelationship";
|
||||
relationship: Relationship;
|
||||
}
|
||||
| {
|
||||
type: "setRelatedDocument";
|
||||
doc: AnyDocument;
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@ import type {
|
||||
AnyDocument,
|
||||
DocumentId,
|
||||
Relationship,
|
||||
RelationshipId,
|
||||
RelationshipType,
|
||||
} from "@/lib/types";
|
||||
import type { DocumentAction } from "./actions";
|
||||
@@ -42,23 +43,18 @@ export function reducer<D extends AnyDocument>(
|
||||
>,
|
||||
};
|
||||
|
||||
case "setDocument":
|
||||
return ifStatus("ready", state, (state) => {
|
||||
if (state.doc.id === action.doc.id) {
|
||||
case "update":
|
||||
if (state.status === "ready") {
|
||||
return {
|
||||
...state,
|
||||
doc: action.doc as D,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
relatedDocs: {
|
||||
...state.relatedDocs,
|
||||
[action.doc.id]: action.doc,
|
||||
doc: {
|
||||
...state.doc,
|
||||
data: action.data,
|
||||
},
|
||||
};
|
||||
});
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
case "setRelationship":
|
||||
return ifStatus("ready", state, (state) => ({
|
||||
...state,
|
||||
@@ -67,5 +63,13 @@ export function reducer<D extends AnyDocument>(
|
||||
[action.relationship.type]: action.relationship,
|
||||
},
|
||||
}));
|
||||
case "setRelatedDocument":
|
||||
return ifStatus("ready", state, (state) => ({
|
||||
...state,
|
||||
relatedDocs: {
|
||||
...state.relatedDocs,
|
||||
[action.doc.id]: action.doc,
|
||||
},
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user