From 5096a7e513ebddeb03456613324868b35f6635a1 Mon Sep 17 00:00:00 2001 From: Aamir Azad <82281117+aamirazad@users.noreply.github.com> Date: Mon, 24 Jun 2024 16:24:13 +0530 Subject: [PATCH] feat: use paperless documents api instead of search to get more accurate results (#24) --- src/app/actions.ts | 2 +- src/app/paperless/page.tsx | 49 +++++++++++++++++------------------ src/components/ui/popover.tsx | 31 ++++++++++++++++++++++ src/types/index.ts | 36 ++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 27 deletions(-) create mode 100644 src/components/ui/popover.tsx diff --git a/src/app/actions.ts b/src/app/actions.ts index 6559d12..0de7e09 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -59,7 +59,7 @@ export async function getPaperlessDocuments(query: string) { if (!query || query == "null" || query.length < 3 || !userData) return null; const response = await fetch( - `${userData.paperlessURL}/api/search/?query=${query}`, + `${userData.paperlessURL}/api/documents/?query=${query}`, { method: "GET", headers: { diff --git a/src/app/paperless/page.tsx b/src/app/paperless/page.tsx index 2466303..9ee33d1 100644 --- a/src/app/paperless/page.tsx +++ b/src/app/paperless/page.tsx @@ -55,9 +55,7 @@ function DocumentsSearch() { function onSubmit(values: z.infer) { if (values.query) - router.replace( - pathname + "?" + createQueryString("query", values.query), - ); + router.replace(pathname + "?" + createQueryString("query", values.query)); } return ( @@ -123,7 +121,6 @@ function DocumentsPage() { ); } else if (!PaperlessDocuments.data || PaperlessDocuments.error) { - console.log(PaperlessDocuments.data); return (

Connection failed! Check that the paperless url/token is set correctly @@ -134,29 +131,31 @@ function DocumentsPage() { const paperlessURL = userData.data?.paperlessURL; - const paperlessDocumentMap = PaperlessDocuments.data.documents; + const paperlessDocumentMap = PaperlessDocuments.data.results; + + if (paperlessDocumentMap.length === 0) { + return

No results!

; + } return ( -
-
-

Search Results

- -
-
+ <> +

Search Results

+ + ); } diff --git a/src/components/ui/popover.tsx b/src/components/ui/popover.tsx new file mode 100644 index 0000000..eb3e6a9 --- /dev/null +++ b/src/components/ui/popover.tsx @@ -0,0 +1,31 @@ +"use client" + +import * as React from "react" +import * as PopoverPrimitive from "@radix-ui/react-popover" + +import { cn } from "@/lib/utils" + +const Popover = PopoverPrimitive.Root + +const PopoverTrigger = PopoverPrimitive.Trigger + +const PopoverContent = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, align = "center", sideOffset = 4, ...props }, ref) => ( + + + +)) +PopoverContent.displayName = PopoverPrimitive.Content.displayName + +export { Popover, PopoverTrigger, PopoverContent } diff --git a/src/types/index.ts b/src/types/index.ts index 6ea11af..63c4d7c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -export type PaperlessDocumentsType = { +export type PaperlessSearchType = { total: number; documents: { added: string; @@ -33,3 +33,37 @@ export type PaperlessDocumentsType = { custom_fields: []; workflows: []; }; + +export type PaperlessDocumentsType = { + count: number; + next: null | string; + previous: null | string; + all: number[]; + results: { + id: number; + correspondent: number; // Change type as per actual data or use a specific type/interface + document_type: number; + storage_path: number; + title: string; + content: string; + tags: number[]; + created: string; // ISO 8601 date string + created_date: string; // Date string + modified: string; // ISO 8601 date string + added: string; // ISO 8601 date string + archive_serial_number: number; // Change type as per actual data or use a specific type/interface + original_file_name: string; + archived_file_name: string; + owner: number; + user_can_change: boolean; + is_shared_by_requester: boolean; + notes: string[]; // Change type as per actual data or use a specific type/interface + custom_fields: string[]; // Change type as per actual data or use a specific type/interface + __search_hit__: { + score: number; + highlights: string; + note_highlights: string; + rank: number; + }; + }[]; +};