Skip to content

Commit

Permalink
It doesn't work, but its a base to go on
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Aug 18, 2024
1 parent c83e6a9 commit 470c2f5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@t3-oss/env-nextjs": "^0.11.0",
"@tanstack/eslint-plugin-query": "^5.43.1",
"@tanstack/react-query": "^5.45.0",
"@tanstack/react-query-devtools": "^5.50.1",
"@tanstack/react-query-devtools": "^5.51.23",
"@tanstack/react-table": "^8.19.2",
"@vercel/postgres": "^0.9.0",
"class-variance-authority": "^0.7.0",
Expand Down
46 changes: 36 additions & 10 deletions src/app/paperless/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { getUserData } from "@/app/actions";
import Link from "next/link";
import OpenInternalLink from "@/components/internal-link";
import type { PaperlessDocumentsType } from "@/types";
import { UsersTableType } from "@/server/db/schema";
import type { UsersTableType } from "@/server/db/schema";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";

const queryClient = new QueryClient();

Expand Down Expand Up @@ -134,7 +135,11 @@ function DocumentsPage() {
const searchParams = useSearchParams();
const query = searchParams.get("query");

const PaperlessDocuments = useQuery({
const {
data: PaperlessDocuments,
isLoading: isLoadingDocuments,
error: documentsError,
} = useQuery({
queryKey: ["key", query],
queryFn: async () => {
if (!query) {
Expand All @@ -143,13 +148,16 @@ function DocumentsPage() {
const response = await getPaperlessDocuments(query);
return response;
},
// 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({
const {
data: userData,
isLoading: isLoadingUserData,
error: userDataError,
} = useQuery({
queryKey: ["userData"],
queryFn: async () => {
const data = await getUserData();
Expand All @@ -159,20 +167,37 @@ function DocumentsPage() {
refetchOnWindowFocus: false,
});

const documentIds = PaperlessDocuments.results.map((document) => document.id);

const { data: imageUrls, isLoading: isLoadingImages } = useQuery({
queryKey: ["imageUrls", documentIds, userData],
queryFn: async () => {
const imageUrls = new Map<number, string | null>();
if (documentIds && userData) {
for (const id of documentIds) {
const url = await getPaperlessThumbnail(id, userData);
imageUrls.set(id, url);
}
}
return imageUrls;
},
enabled: !!PaperlessDocuments?.results,
});

if (!query) {
return <h1 className="text-2xl font-bold">Start Searching!</h1>;
}

if (PaperlessDocuments.isLoading || userData.isLoading) {
if (isLoadingDocuments || isLoadingUserData || isLoadingImages) {
return <LoadingSpinner>Loading...</LoadingSpinner>;
} else if (!userData.data?.paperlessURL) {
} else if (userDataError || !userData?.paperlessURL) {
return (
<h1 className="text-2xl font-bold">
You need to set your paperless url in
<OpenInternalLink href="/settings">settings</OpenInternalLink>
</h1>
);
} else if (!PaperlessDocuments.data || PaperlessDocuments.error) {
} else if (documentsError || !PaperlessDocuments) {
return (
<h1 className="text-2xl font-bold">
Connection failed! Check that the paperless url/token is set correctly
Expand All @@ -181,9 +206,9 @@ function DocumentsPage() {
);
}

const paperlessDocumentMap = PaperlessDocuments.data.results;
const paperlessDocumentMap = PaperlessDocuments.results;

if (!paperlessDocumentMap ?? paperlessDocumentMap.length === 0) {
if (paperlessDocumentMap.length === 0) {
return <h1 className="text-2xl font-bold">No results!</h1>;
}

Expand All @@ -195,7 +220,7 @@ function DocumentsPage() {
className="rounded-lg border p-4 shadow transition-shadow duration-300 hover:shadow-lg"
>
<img
src={`${userData.data?.paperlessURL}/api/documents/${document.id}/thumb/`}
src={imageUrls?.get(document.id) || ""}
alt={document.title}
className="h-32 w-full rounded object-cover"
/>
Expand Down Expand Up @@ -234,6 +259,7 @@ export default function PaperlessPage() {
Search Results
</h1>
<DocumentsPage />
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</div>
</div>
Expand Down

0 comments on commit 470c2f5

Please sign in to comment.