Skip to content

Commit

Permalink
Stop refetching and seperate components
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jul 25, 2024
1 parent f250b32 commit 73b4cd8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/app/paperless/document/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { useRouter } from "next/navigation";
import BodyMessage from "@/components/body-message";

export default function FullPageDocumentPage({
params,
Expand All @@ -11,7 +12,7 @@ export default function FullPageDocumentPage({
router.replace(`/paperless/details/${params.id}`);
return (
<main className="h-full w-full">
<h1>Redirecting ...</h1>
<BodyMessage>Redirecting ...</BodyMessage>
</main>
);
}
4 changes: 4 additions & 0 deletions src/app/paperless/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ function DocumentsPage() {
},
// This ensures the query does not run if there's no query string
enabled: !!query,
staleTime: 60 * 1000, // 1 minute in milliseconds
refetchOnWindowFocus: false,
});

const userData = useQuery({
Expand All @@ -105,6 +107,8 @@ function DocumentsPage() {
const data = await getUserData();
return data;
},
staleTime: 24 * 60 * 60 * 1000, // 1 day in milliseconds
refetchOnWindowFocus: false,
});

if (!query) {
Expand Down
21 changes: 3 additions & 18 deletions src/components/audio-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import { toast } from "sonner";
import BodyMessage from "./body-message";

const queryClient = new QueryClient();

Expand Down Expand Up @@ -123,31 +124,15 @@ function AudioInfo({ id }: AudioInfoProps) {
}

if (userDataError ?? !userData) {
return (
<div className="flex justify-center">
<div className="mx-auto max-w-sm rounded-lg bg-slate-700 p-4 shadow-md">
<h1 className="w-full text-center text-2xl font-bold">
Error loading user data
</h1>
</div>
</div>
);
return <BodyMessage>Error loading user data</BodyMessage>;
}

if (isRecordingDataLoading) {
return <SkeletonLoader />;
}

if (recordingDataError ?? !recordingData) {
return (
<div className="flex justify-center">
<div className="mx-auto max-w-sm rounded-lg bg-slate-700 p-4 shadow-md">
<h1 className="w-full text-center text-2xl font-bold">
Error loading recording data
</h1>
</div>
</div>
);
return <BodyMessage>Error loading recording data</BodyMessage>;
}

const decodedName = decodeURIComponent(recordingData.fileName);
Expand Down
13 changes: 13 additions & 0 deletions src/components/body-message.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function BodyMessage({
children,
}: {
children: React.ReactNode;
}) {
return (
<div className="flex justify-center">
<div className="mx-auto max-w-sm rounded-lg bg-slate-700 p-4 shadow-md">
<h1 className="w-full text-center text-2xl font-bold">{children}</h1>
</div>
</div>
);
}
5 changes: 4 additions & 1 deletion src/components/document-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ function DocumentDetailsInner(props: { id: number }) {
const { data: userData, isLoading: isUserDataLoading } = useQuery({
queryKey: ["userData"],
queryFn: fetchUserData,
staleTime: 24 * 60 * 60 * 1000, // 1 day in milliseconds
refetchOnWindowFocus: false,
});

const { data: pdfUrl, isLoading: isPdfUrlLoading } = useQuery({
Expand All @@ -123,6 +125,7 @@ function DocumentDetailsInner(props: { id: number }) {
return await getPaperlessDocument(props.id, userData!);
},
enabled: !!userData,
refetchOnWindowFocus: false,
});

const { data: documentData, isLoading: isdocumentDataLoading } = useQuery({
Expand All @@ -131,6 +134,7 @@ function DocumentDetailsInner(props: { id: number }) {
return await getPaperlessDocumentData(props.id, userData!);
},
enabled: !!userData,
refetchOnWindowFocus: false,
});

if (isUserDataLoading || isdocumentDataLoading || isPdfUrlLoading) {
Expand All @@ -146,7 +150,6 @@ function DocumentDetailsInner(props: { id: number }) {
<div className="flex h-4/5 flex-col rounded-xl bg-slate-600/50 md:w-1/2">
<div className="m-4 flex flex-grow flex-col justify-center gap-8 md:m-8 md:flex-row md:gap-16">
<div>{documentData?.title}</div>
<DocumentPreview id={props.id} />
<div className="flex flex-col gap-8">
<Button
onClick={(e) => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/document-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function Preview(props: { id: number }) {
return await getPaperlessThumbnail(props.id, userData!);
},
enabled: !!userData,
refetchOnWindowFocus: false,
});

if (isPdfUrlLoading ?? isUserDataLoading) {
Expand All @@ -100,7 +101,7 @@ function Preview(props: { id: number }) {
if (!pdfUrl || !userData) {
return <BodyMessage>Failed to get document</BodyMessage>;
}
return <img src={pdfUrl} alt="Document Preview" />;
return <img src={pdfUrl} alt="Document Preview" className="size-fit" />;
}

export default function DocumentPreview(props: { id: number }) {
Expand Down

0 comments on commit 73b4cd8

Please sign in to comment.