Fixes tab loading
This commit is contained in:
@@ -1,15 +1,20 @@
|
|||||||
import { DocumentList } from "@/components/DocumentList";
|
import { DocumentList } from "@/components/DocumentList";
|
||||||
import { pb } from "@/lib/pocketbase";
|
import { pb } from "@/lib/pocketbase";
|
||||||
import { displayName } from "@/lib/relationships";
|
import { displayName } from "@/lib/relationships";
|
||||||
import type { AnyDocument, Document, RelationshipType } from "@/lib/types";
|
import type {
|
||||||
import { useState } from "react";
|
AnyDocument,
|
||||||
|
Document,
|
||||||
|
Relationship,
|
||||||
|
RelationshipType,
|
||||||
|
} from "@/lib/types";
|
||||||
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import { Loader } from "./Loader";
|
import { Loader } from "./Loader";
|
||||||
import { DocumentRow } from "./documents/DocumentRow";
|
import { DocumentRow } from "./documents/DocumentRow";
|
||||||
import { NewRelatedDocumentForm } from "./documents/NewRelatedDocumentForm";
|
import { NewRelatedDocumentForm } from "./documents/NewRelatedDocumentForm";
|
||||||
|
|
||||||
interface RelationshipListProps {
|
interface RelationshipListProps {
|
||||||
root: AnyDocument;
|
root: AnyDocument;
|
||||||
items: AnyDocument[];
|
|
||||||
relationshipType: RelationshipType;
|
relationshipType: RelationshipType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,12 +24,39 @@ interface RelationshipListProps {
|
|||||||
*/
|
*/
|
||||||
export function RelationshipList({
|
export function RelationshipList({
|
||||||
root,
|
root,
|
||||||
items: initialItems,
|
|
||||||
relationshipType,
|
relationshipType,
|
||||||
}: RelationshipListProps) {
|
}: RelationshipListProps) {
|
||||||
const [items, setItems] = useState<Document[]>(initialItems);
|
const [items, setItems] = useState<Document[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function fetchItems() {
|
||||||
|
const { items } = await queryClient.fetchQuery({
|
||||||
|
queryKey: [root.id, "relationship", relationshipType],
|
||||||
|
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 { items: relationship.expand?.secondary ?? [] };
|
||||||
|
},
|
||||||
|
});
|
||||||
|
setItems(items);
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchItems();
|
||||||
|
});
|
||||||
|
|
||||||
// Handles creation of a new document and adds it to the relationship
|
// Handles creation of a new document and adds it to the relationship
|
||||||
const handleCreate = async (doc: Document) => {
|
const handleCreate = async (doc: Document) => {
|
||||||
@@ -48,6 +80,9 @@ export function RelationshipList({
|
|||||||
type: relationshipType,
|
type: relationshipType,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: [root.id, "relationship", relationshipType],
|
||||||
|
});
|
||||||
setItems((prev) => [...prev, doc]);
|
setItems((prev) => [...prev, doc]);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
setError(e?.message || "Failed to add document to relationship.");
|
setError(e?.message || "Failed to add document to relationship.");
|
||||||
|
|||||||
@@ -2,46 +2,27 @@ import { RelationshipList } from "@/components/RelationshipList";
|
|||||||
import { DocumentEditForm } from "@/components/documents/DocumentEditForm";
|
import { DocumentEditForm } from "@/components/documents/DocumentEditForm";
|
||||||
import { pb } from "@/lib/pocketbase";
|
import { pb } from "@/lib/pocketbase";
|
||||||
import { displayName } from "@/lib/relationships";
|
import { displayName } from "@/lib/relationships";
|
||||||
import {
|
import { RelationshipType, type AnyDocument } from "@/lib/types";
|
||||||
RelationshipType,
|
|
||||||
type AnyDocument,
|
|
||||||
type Relationship,
|
|
||||||
} from "@/lib/types";
|
|
||||||
import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@headlessui/react";
|
import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@headlessui/react";
|
||||||
import { createFileRoute, Link } from "@tanstack/react-router";
|
import { createFileRoute, Link } from "@tanstack/react-router";
|
||||||
import _ from "lodash";
|
|
||||||
|
|
||||||
export const Route = createFileRoute(
|
export const Route = createFileRoute(
|
||||||
"/_app/_authenticated/document/$documentId",
|
"/_app/_authenticated/document/$documentId",
|
||||||
)({
|
)({
|
||||||
loader: async ({ params }) => {
|
loader: async ({ params }) => {
|
||||||
const doc = await pb.collection("documents").getOne(params.documentId);
|
const doc = await pb.collection("documents").getOne(params.documentId);
|
||||||
const relationships: Relationship[] = await pb
|
|
||||||
.collection("relationships")
|
|
||||||
.getFullList({
|
|
||||||
filter: `primary = "${params.documentId}"`,
|
|
||||||
expand: "secondary",
|
|
||||||
});
|
|
||||||
console.log("Fetched data: ", relationships);
|
|
||||||
return {
|
return {
|
||||||
document: doc,
|
document: doc,
|
||||||
relationships: _.mapValues(
|
|
||||||
_.groupBy(relationships, (r) => r.type),
|
|
||||||
(rs: Relationship[]) => rs.flatMap((r) => r.expand?.secondary),
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
component: RouteComponent,
|
component: RouteComponent,
|
||||||
});
|
});
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
const { document, relationships } = Route.useLoaderData() as {
|
const { document } = Route.useLoaderData() as {
|
||||||
document: AnyDocument;
|
document: AnyDocument;
|
||||||
relationships: Record<RelationshipType, AnyDocument[]>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log("Parsed data: ", relationships);
|
|
||||||
|
|
||||||
const relationshipList = [
|
const relationshipList = [
|
||||||
RelationshipType.Scenes,
|
RelationshipType.Scenes,
|
||||||
RelationshipType.Secrets,
|
RelationshipType.Secrets,
|
||||||
@@ -76,7 +57,6 @@ function RouteComponent() {
|
|||||||
key={relationshipType}
|
key={relationshipType}
|
||||||
root={document}
|
root={document}
|
||||||
relationshipType={relationshipType}
|
relationshipType={relationshipType}
|
||||||
items={relationships[relationshipType] ?? []}
|
|
||||||
/>
|
/>
|
||||||
</TabPanel>
|
</TabPanel>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ function RouteComponent() {
|
|||||||
return (
|
return (
|
||||||
<div className="fill-w py-8 columns-2 gap-8 text-sm">
|
<div className="fill-w py-8 columns-2 gap-8 text-sm">
|
||||||
<SessionPrintRow session={session}></SessionPrintRow>
|
<SessionPrintRow session={session}></SessionPrintRow>
|
||||||
|
|
||||||
{[
|
{[
|
||||||
RelationshipType.Scenes,
|
RelationshipType.Scenes,
|
||||||
RelationshipType.Secrets,
|
RelationshipType.Secrets,
|
||||||
|
|||||||
Reference in New Issue
Block a user