Adds scenes
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { DocumentList } from "@/components/DocumentList";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import type { Document, RelationshipType } from "@/lib/types";
|
||||
import { DocumentList } from "@/components/DocumentList";
|
||||
import { useState } from "react";
|
||||
import { Loader } from "./Loader";
|
||||
import { DocumentRow } from "./documents/DocumentRow";
|
||||
import { DocumentForm } from "./documents/DocumentForm";
|
||||
import { DocumentRow } from "./documents/DocumentRow";
|
||||
|
||||
interface RelationshipListProps {
|
||||
root: Document;
|
||||
items: Document[];
|
||||
relationshipType: RelationshipType;
|
||||
}
|
||||
|
||||
@@ -17,50 +18,13 @@ interface RelationshipListProps {
|
||||
*/
|
||||
export function RelationshipList({
|
||||
root,
|
||||
items: initialItems,
|
||||
relationshipType,
|
||||
}: RelationshipListProps) {
|
||||
const [items, setItems] = useState<Document[]>([]);
|
||||
const [items, setItems] = useState<Document[]>(initialItems);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// Fetch related documents on mount or when root/relationshipType changes
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
async function fetchRelated() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const relationships = await pb
|
||||
.collection("relationships")
|
||||
.getList(1, 1, {
|
||||
filter: `primary = "${root.id}" && type = "${relationshipType}"`,
|
||||
});
|
||||
const secondaryIds =
|
||||
relationships.items.length > 0
|
||||
? relationships.items[0].secondary
|
||||
: [];
|
||||
let docs: Document[] = [];
|
||||
if (Array.isArray(secondaryIds) && secondaryIds.length > 0) {
|
||||
docs = (await pb.collection("documents").getFullList({
|
||||
filter: secondaryIds
|
||||
.map((id: string) => `id = "${id}"`)
|
||||
.join(" || "),
|
||||
})) as Document[];
|
||||
}
|
||||
if (!cancelled) setItems(docs);
|
||||
} catch (e: any) {
|
||||
if (!cancelled)
|
||||
setError(e?.message || "Failed to load related documents.");
|
||||
} finally {
|
||||
if (!cancelled) setLoading(false);
|
||||
}
|
||||
}
|
||||
fetchRelated();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [root.id, relationshipType]);
|
||||
|
||||
// Handles creation of a new document and adds it to the relationship
|
||||
const handleCreate = async (doc: Document) => {
|
||||
setLoading(true);
|
||||
|
||||
Reference in New Issue
Block a user