From 65ac4852dfaca4b1a3e40e7cc7e5635c8b017e93 Mon Sep 17 00:00:00 2001 From: Drew Haven Date: Wed, 28 May 2025 15:42:49 -0700 Subject: [PATCH] Adds relationships and loads secrets into the session details --- .../1748471315_created_relationships.js | 100 ++++++++++++++++++ .../1748471393_updated_relationships.js | 42 ++++++++ .../1748471513_updated_relationships.js | 27 +++++ pb_migrations/1748471982_updated_documents.js | 34 ++++++ .../1748472065_updated_relationships.js | 28 +++++ src/lib/types.ts | 14 ++- .../_authenticated/document.$documentId.tsx | 30 +++++- 7 files changed, 269 insertions(+), 6 deletions(-) create mode 100644 pb_migrations/1748471315_created_relationships.js create mode 100644 pb_migrations/1748471393_updated_relationships.js create mode 100644 pb_migrations/1748471513_updated_relationships.js create mode 100644 pb_migrations/1748471982_updated_documents.js create mode 100644 pb_migrations/1748472065_updated_relationships.js diff --git a/pb_migrations/1748471315_created_relationships.js b/pb_migrations/1748471315_created_relationships.js new file mode 100644 index 0000000..c3cd46c --- /dev/null +++ b/pb_migrations/1748471315_created_relationships.js @@ -0,0 +1,100 @@ +/// +migrate((app) => { + const collection = new Collection({ + "createRule": null, + "deleteRule": null, + "fields": [ + { + "autogeneratePattern": "[a-z0-9]{15}", + "hidden": false, + "id": "text3208210256", + "max": 15, + "min": 15, + "name": "id", + "pattern": "^[a-z0-9]+$", + "presentable": false, + "primaryKey": true, + "required": true, + "system": true, + "type": "text" + }, + { + "cascadeDelete": false, + "collectionId": "pbc_3332084752", + "hidden": false, + "id": "relation390457990", + "maxSelect": 1, + "minSelect": 0, + "name": "primary", + "presentable": false, + "required": false, + "system": false, + "type": "relation" + }, + { + "cascadeDelete": false, + "collectionId": "pbc_3332084752", + "hidden": false, + "id": "relation458454037", + "maxSelect": 999, + "minSelect": 0, + "name": "secondary", + "presentable": false, + "required": false, + "system": false, + "type": "relation" + }, + { + "hidden": false, + "id": "select2363381545", + "maxSelect": 1, + "name": "type", + "presentable": false, + "required": false, + "system": false, + "type": "select", + "values": [ + "plannedFor", + "discoveredIn" + ] + }, + { + "hidden": false, + "id": "autodate2990389176", + "name": "created", + "onCreate": true, + "onUpdate": false, + "presentable": false, + "system": false, + "type": "autodate" + }, + { + "hidden": false, + "id": "autodate3332085495", + "name": "updated", + "onCreate": true, + "onUpdate": true, + "presentable": false, + "system": false, + "type": "autodate" + } + ], + "id": "pbc_617371094", + "indexes": [ + "CREATE INDEX `idx_relationships_documents` ON `relationships` (\n `primary`,\n `secondary`\n)", + "CREATE INDEX `idx_relationships_types` ON `relationships` (`type`)" + ], + "listRule": null, + "name": "relationships", + "system": false, + "type": "base", + "updateRule": null, + "viewRule": null + }); + + return app.save(collection); +}, (app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094"); + + return app.delete(collection); +}) diff --git a/pb_migrations/1748471393_updated_relationships.js b/pb_migrations/1748471393_updated_relationships.js new file mode 100644 index 0000000..54391dc --- /dev/null +++ b/pb_migrations/1748471393_updated_relationships.js @@ -0,0 +1,42 @@ +/// +migrate((app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094") + + // update field + collection.fields.addAt(3, new Field({ + "hidden": false, + "id": "select2363381545", + "maxSelect": 1, + "name": "type", + "presentable": false, + "required": false, + "system": false, + "type": "select", + "values": [ + "discoveredIn", + "plannedSecrets" + ] + })) + + return app.save(collection) +}, (app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094") + + // update field + collection.fields.addAt(3, new Field({ + "hidden": false, + "id": "select2363381545", + "maxSelect": 1, + "name": "type", + "presentable": false, + "required": false, + "system": false, + "type": "select", + "values": [ + "plannedFor", + "discoveredIn" + ] + })) + + return app.save(collection) +}) diff --git a/pb_migrations/1748471513_updated_relationships.js b/pb_migrations/1748471513_updated_relationships.js new file mode 100644 index 0000000..1f7951e --- /dev/null +++ b/pb_migrations/1748471513_updated_relationships.js @@ -0,0 +1,27 @@ +/// +migrate((app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094") + + // update collection data + unmarshal({ + "indexes": [ + "CREATE INDEX `idx_relationships_documents` ON `relationships` (\n `primary`,\n `secondary`\n)", + "CREATE INDEX `idx_relationships_types` ON `relationships` (`type`)", + "CREATE UNIQUE INDEX `idx_relationships_primary_type` ON `relationships` (\n `primary`,\n `type`\n)" + ] + }, collection) + + return app.save(collection) +}, (app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094") + + // update collection data + unmarshal({ + "indexes": [ + "CREATE INDEX `idx_relationships_documents` ON `relationships` (\n `primary`,\n `secondary`\n)", + "CREATE INDEX `idx_relationships_types` ON `relationships` (`type`)" + ] + }, collection) + + return app.save(collection) +}) diff --git a/pb_migrations/1748471982_updated_documents.js b/pb_migrations/1748471982_updated_documents.js new file mode 100644 index 0000000..928528f --- /dev/null +++ b/pb_migrations/1748471982_updated_documents.js @@ -0,0 +1,34 @@ +/// +migrate((app) => { + const collection = app.findCollectionByNameOrId("pbc_3332084752") + + // update field + collection.fields.addAt(2, new Field({ + "hidden": false, + "id": "json2918445923", + "maxSize": 0, + "name": "data", + "presentable": true, + "required": false, + "system": false, + "type": "json" + })) + + return app.save(collection) +}, (app) => { + const collection = app.findCollectionByNameOrId("pbc_3332084752") + + // update field + collection.fields.addAt(2, new Field({ + "hidden": false, + "id": "json2918445923", + "maxSize": 0, + "name": "data", + "presentable": false, + "required": false, + "system": false, + "type": "json" + })) + + return app.save(collection) +}) diff --git a/pb_migrations/1748472065_updated_relationships.js b/pb_migrations/1748472065_updated_relationships.js new file mode 100644 index 0000000..32a6cd8 --- /dev/null +++ b/pb_migrations/1748472065_updated_relationships.js @@ -0,0 +1,28 @@ +/// +migrate((app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094") + + // update collection data + unmarshal({ + "createRule": "", + "deleteRule": "@request.auth.id = primary.campaign.owner.id", + "listRule": "@request.auth.id = primary.campaign.owner.id", + "updateRule": "@request.auth.id = primary.campaign.owner.id", + "viewRule": "@request.auth.id = primary.campaign.owner.id" + }, collection) + + return app.save(collection) +}, (app) => { + const collection = app.findCollectionByNameOrId("pbc_617371094") + + // update collection data + unmarshal({ + "createRule": null, + "deleteRule": null, + "listRule": null, + "updateRule": null, + "viewRule": null + }, collection) + + return app.save(collection) +}) diff --git a/src/lib/types.ts b/src/lib/types.ts index 7f3a836..8ccfe5c 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,16 +1,18 @@ +import type { RecordModel } from "pocketbase"; + export type Id = string & { __type: T }; export type UserId = Id<"User">; export type CampaignId = Id<"Campaign">; export type DocumentId = Id<"Document">; -export type Campaign = { +export type Campaign = RecordModel & { id: CampaignId; name: string; owner: UserId; }; -export type Document = { +export type Document = RecordModel & { id: DocumentId; campaign: Campaign; data: {}; @@ -35,6 +37,12 @@ export type Secret = Document & "secret", { text: string; - discoveredOn: ISO8601Date; + discoveredOn: ISO8601Date | null; } >; + +export type Relationship = RecordModel & { + primary: DocumentId; + secondary: DocumentId[]; + type: "plannedSecrets" | "discoveredIn"; +}; diff --git a/src/routes/_authenticated/document.$documentId.tsx b/src/routes/_authenticated/document.$documentId.tsx index 572fbb8..3f47269 100644 --- a/src/routes/_authenticated/document.$documentId.tsx +++ b/src/routes/_authenticated/document.$documentId.tsx @@ -7,17 +7,29 @@ export const Route = createFileRoute( )({ loader: async ({ params }) => { const doc = await pb.collection("documents").getOne(params.documentId); - return { document: doc }; + // Fetch relationships where this document is the primary and type is "plannedSecrets" + const relationships = await pb.collection("relationships").getFullList({ + filter: `primary = "${params.documentId}" && type = "plannedSecrets"`, + }); + // Get all related secret document IDs from the secondary field + const secretIds = relationships.map((rel: any) => rel.secondary); + // Fetch all related secret documents + let secrets: any[] = []; + if (secretIds.length > 0) { + secrets = await pb.collection("documents").getFullList({ + filter: secretIds.map(id => `id = "${id}"`).join(" || "), + }); + } + return { document: doc, secrets }; }, component: RouteComponent, }); function RouteComponent() { - const { document } = Route.useLoaderData(); + const { document, secrets } = Route.useLoaderData(); const strongStart = document?.data?.session?.strongStart || ""; async function handleSaveStrongStart(newValue: string) { - // Update the document in Pocketbase await pb.collection("documents").update(document.id, { data: { ...document.data, @@ -38,6 +50,18 @@ function RouteComponent() { placeholder="Enter a strong start for this session..." aria-label="Strong Start" /> +

Planned Secrets

+ {secrets && secrets.length > 0 ? ( +
    + {secrets.map((secret: any) => ( +
  • + {secret.data?.secret?.text || (No secret text)} +
  • + ))} +
+ ) : ( +
No planned secrets for this session.
+ )} ); }