From fc7c0e7287bf3d80de553afb6bc5cfcd8234c3f9 Mon Sep 17 00:00:00 2001 From: Aamir Azad Date: Fri, 5 Jul 2024 22:08:36 -0400 Subject: [PATCH] Whishper works, that was easy --- src/app/actions.ts | 17 +++------- src/app/paperless/page.tsx | 6 ++-- src/app/settings/page.tsx | 69 ++++++++++++++++++++++++++++++++++++++ src/app/whishper/page.tsx | 68 +++++++++++++++++++++++++++---------- src/types/index.ts | 30 +++++++++++++++++ 5 files changed, 159 insertions(+), 31 deletions(-) diff --git a/src/app/actions.ts b/src/app/actions.ts index c0dc92a..8076bf3 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -3,7 +3,7 @@ import { db } from "@/server/db"; import type { UsersTableType } from "@/server/db/schema"; import { users } from "@/server/db/schema"; -import type { PaperlessDocumentsType } from "@/types"; +import type { PaperlessDocumentsType, WhishperRecordingsType } from "@/types"; import { auth } from "@clerk/nextjs/server"; /* @@ -95,18 +95,11 @@ export async function getWhishperRecordings(query: string) { if (!query || query == "null" || query.length < 3 || !userData) return null; - const response = await fetch( - `${userData.whishperURL}/api/documents/?query=${query}&page=1&page_size=10&truncate_content=true`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Token ${userData.paperlessToken}`, - }, - }, - ); + const response = await fetch(`${userData.whishperURL}/api/transcriptions`); - const data = (await response.json()) as PaperlessDocumentsType; + const data = (await response.json()) as WhishperRecordingsType; + + console.log(data) return data; } diff --git a/src/app/paperless/page.tsx b/src/app/paperless/page.tsx index d81d96e..afeabe4 100644 --- a/src/app/paperless/page.tsx +++ b/src/app/paperless/page.tsx @@ -130,7 +130,7 @@ function DocumentsPage() { const paperlessDocumentMap = PaperlessDocuments.data.results; - if (paperlessDocumentMap.length === 0) { + if (!paperlessDocumentMap ?? paperlessDocumentMap.length === 0) { return

No results!

; } @@ -164,7 +164,9 @@ export default function PaperlessPage() {
- +
+ +
diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx index c31b715..756bf29 100644 --- a/src/app/settings/page.tsx +++ b/src/app/settings/page.tsx @@ -171,6 +171,70 @@ function PaperlessToken({ setActiveTab, userData }: FormProps) { ); } +function WhishperURL({ setActiveTab, userData }: FormProps) { + const [isAutofilled, setIsAutofilled] = useState(false); + const formSchema = z.object({ + URL: z.string(), + }); + + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: { + URL: "", + }, + }); + + if (userData.whishperURL && !isAutofilled) { + form.setValue("URL", userData.whishperURL); + setIsAutofilled(true); + } + + async function onSubmit(values: z.infer) { + if (values.URL == "") { + setActiveTab((prevTab) => prevTab + 2); // Skip api key form + } else { + setActiveTab((prevTab) => prevTab + 1); // Increment activeTab + } + try { + await setUserProperty("whishperURL", values.URL); + // Operation succeeded, show success toast + toast("Your whishper URL preferences was saved"); + // Optionally, move to a new tab or take another action to indicate success + } catch { + // Operation failed, show error toast + toast("Uh oh! Something went wrong.", { + description: "Your whishper URL preferences were not saved.", + action: { + label: "Go back", + onClick: () => setActiveTab((prevTab) => prevTab - 1), // Go back to try again + }, + }); + } + } + + return ( +
+ + ( + + Whishper URL + + + + Leave empty to disable + + + )} + /> + + + + ); +} + interface ProgressIndicatorProps { activeTab: number; totalTabs: number; @@ -227,6 +291,11 @@ export function Forms() { setActiveTab={setActiveTab} userData={userData} />, + , ]; return ( <> diff --git a/src/app/whishper/page.tsx b/src/app/whishper/page.tsx index a669718..401095a 100644 --- a/src/app/whishper/page.tsx +++ b/src/app/whishper/page.tsx @@ -17,9 +17,30 @@ import { Input } from "@/components/ui/input"; import Link from "next/link"; import { zodResolver } from "@hookform/resolvers/zod"; import { useCallback } from "react"; -import { useQuery } from "@tanstack/react-query"; -import { getUserData, getWhishperRecordings } from "../actions"; +import { + useQuery, + QueryClientProvider, + QueryClient, +} from "@tanstack/react-query"; +import { getUserData } from "../actions"; import LoadingSpinner from "@/components/loading-spinner"; +import { WhishperRecordingsType } from "@/types"; + +async function getWhishperRecordings(query: string) { + const userData = await getUserData(); + + if (!query || query == "null" || query.length < 3 || !userData) return null; + + const response = await fetch(`${userData.whishperURL}/api/transcriptions`); + + const data = (await response.json()) as WhishperRecordingsType; + + console.log(data); + + return data; +} + +const queryClient = new QueryClient(); function SearchForm() { const formSchema = z.object({ @@ -100,20 +121,24 @@ function RecordingsList() { }, }); + if (!userData.data?.whishperURL) { + return ( +

+ You need to set your whishper url in{" "} + settings +

+ ); + } + if (!query) { return

Start Searching!

; } if (WhishperRecordings.isLoading || userData.isLoading) { return Loading...; - } else if (!userData.data?.whishperURL) { - return ( -

- You need to set your paperless url in{" "} - settings -

- ); - } else if (!WhishperRecordings.data || WhishperRecordings.error) { + } + + if (!WhishperRecordings.data || WhishperRecordings.error) { return (

Connection failed! Check that the whishper url is set correctly in{" "} @@ -122,9 +147,9 @@ function RecordingsList() { ); } - const WhishperRecordingsMap = WhishperRecordings.data.results; + const WhishperRecordingsMap = WhishperRecordings.data; - if (WhishperRecordingsMap.length === 0) { + if (!WhishperRecordingsMap ?? WhishperRecordingsMap.length === 0) { return

No results!

; } @@ -134,12 +159,12 @@ function RecordingsList() {
    {WhishperRecordingsMap.map((recording, index) => (
  • - - {recording.title} - + {recording.fileName.split("_WHSHPR_")[1]} +
  • ))}
@@ -157,7 +182,16 @@ export default function WhishperPage() {
- +
+
+ +
+
+ + + +
+
diff --git a/src/types/index.ts b/src/types/index.ts index 6966b0f..9f7f984 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -67,3 +67,33 @@ export type PaperlessDocumentsType = { }; }[]; }; + +export type WhishperRecordingsType = { + id: string; + status: number; + language: string; + modelSize: string; + task: string; + device: string; + fileName: string; + sourceUrl: string; + result: { + language: string; + duration: number; + segments: { + end: number; + id: string; + start: number; + score: number; + text: string; + words: { + end: number; + start: number; + word: string; + score: number; + }[]; + }[]; + text: string; + }; + translations: []; +}[];