diff --git a/src/routes/_authenticated/document.$documentId.tsx b/src/routes/_authenticated/document.$documentId.tsx index 3f47269..0c6e7c5 100644 --- a/src/routes/_authenticated/document.$documentId.tsx +++ b/src/routes/_authenticated/document.$documentId.tsx @@ -1,21 +1,22 @@ import { createFileRoute } from '@tanstack/react-router' import { pb } from "@/lib/pocketbase"; import { AutoSaveTextarea } from "@/components/AutoSaveTextarea"; +import { useState } from "react"; export const Route = createFileRoute( '/_authenticated/document/$documentId', )({ loader: async ({ params }) => { const doc = await pb.collection("documents").getOne(params.documentId); - // Fetch relationships where this document is the primary and type is "plannedSecrets" - const relationships = await pb.collection("relationships").getFullList({ + // Fetch the unique relationship where this document is the primary and type is "plannedSecrets" + const relationships = await pb.collection("relationships").getList(1, 1, { filter: `primary = "${params.documentId}" && type = "plannedSecrets"`, }); // Get all related secret document IDs from the secondary field - const secretIds = relationships.map((rel: any) => rel.secondary); + const secretIds = relationships.items.length > 0 ? relationships.items[0].secondary : []; // Fetch all related secret documents let secrets: any[] = []; - if (secretIds.length > 0) { + if (Array.isArray(secretIds) && secretIds.length > 0) { secrets = await pb.collection("documents").getFullList({ filter: secretIds.map(id => `id = "${id}"`).join(" || "), }); @@ -28,6 +29,10 @@ export const Route = createFileRoute( function RouteComponent() { const { document, secrets } = Route.useLoaderData(); const strongStart = document?.data?.session?.strongStart || ""; + const [newSecret, setNewSecret] = useState(""); + const [adding, setAdding] = useState(false); + const [secretList, setSecretList] = useState(secrets); + const [error, setError] = useState(null); async function handleSaveStrongStart(newValue: string) { await pb.collection("documents").update(document.id, { @@ -41,6 +46,47 @@ function RouteComponent() { }); } + async function handleAddSecret() { + if (!newSecret.trim()) return; + setAdding(true); + setError(null); + try { + // 1. Create the secret document + const secretDoc = await pb.collection("documents").create({ + campaign: document.campaign, // assuming campaign is an id or object + data: { + secret: { + text: newSecret, + discoveredOn: null, + }, + }, + }); + // 2. Check for existing relationship + const existing = await pb.collection("relationships").getOne({ + filter: `primary = "${document.id}" && type = "plannedSecrets"`, + }); + if (existing.length > 0) { + // Update existing relationship to add new secret to secondary array + await pb.collection("relationships").update(existing[0].id, { + "+secondary": secretDoc.id, + }); + } else { + // Create new relationship + await pb.collection("relationships").create({ + primary: document.id, + secondary: [secretDoc.id], + type: "plannedSecrets", + }); + } + setSecretList([...secretList, secretDoc]); + setNewSecret(""); + } catch (e: any) { + setError(e?.message || "Failed to add secret."); + } finally { + setAdding(false); + } + } + return (

Session Strong Start

@@ -51,9 +97,9 @@ function RouteComponent() { aria-label="Strong Start" />

Planned Secrets

- {secrets && secrets.length > 0 ? ( + {secretList && secretList.length > 0 ? (
); }