From 5457f84c9529a0551e977e389751e3110ef22a36 Mon Sep 17 00:00:00 2001 From: Aamir Azad Date: Sun, 23 Jun 2024 17:17:02 +0530 Subject: [PATCH] feat: use paperless documents api instead of search to get more accurate results --- src/app/actions.ts | 2 +- src/app/paperless/page.tsx | 49 +++++++++++++++++++------------------- src/types/index.ts | 36 +++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 27 deletions(-) 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 4223dbe..5bae879 100644 --- a/src/app/paperless/page.tsx +++ b/src/app/paperless/page.tsx @@ -54,9 +54,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 ( @@ -122,7 +120,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 @@ -133,29 +130,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/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; + }; + }[]; +};