diff --git a/src/app/actions.ts b/src/app/actions.ts index c229080..04b176a 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -4,7 +4,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 { auth } from "@clerk/nextjs/server"; +import { auth, clerkMiddleware } from "@clerk/nextjs/server"; /* Clerk helpers @@ -38,6 +38,7 @@ export async function setUserProperty( } export async function getUserData() { + clerkMiddleware(); const { userId } = auth(); if (!userId) return null; @@ -78,3 +79,18 @@ export async function getPaperlessDocuments(query: string) { return data; } + +export async function getRecording(name: string): Promise { + const userData: UsersTableType | undefined | null = await getUserData(); + if (!userData) { + throw new Error("User data is undefined"); + } + + const url = `${userData.whishperURL}/api/documents/${name}/download/`; + const response = await fetch(url); + if (!response.ok) { + throw new Error("Failed to fetch recording"); + } + const blob = await response.blob(); + return URL.createObjectURL(blob); +} diff --git a/src/app/page.tsx b/src/app/page.tsx index a39c3c9..e764f90 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,5 +1,4 @@ import OpenExternalLink from "@/components/external-link"; -import { getUserData } from "./actions"; export default async function HomePage() { diff --git a/src/app/whishper/page.tsx b/src/app/whishper/page.tsx index 0bf6164..d94fe44 100644 --- a/src/app/whishper/page.tsx +++ b/src/app/whishper/page.tsx @@ -46,7 +46,9 @@ import { BadgeCheck, Badge, BadgeAlert } from "lucide-react"; const queryClient = new QueryClient(); -async function getWhishperRecordings(query: string) { +async function getWhishperRecordings( + query: string, +): Promise { const userData = await getUserData(); if (!query || query == "null" || query.length < 3 || !userData) return null; diff --git a/src/components/audio-preview.tsx b/src/components/audio-preview.tsx index 097be07..eb9f897 100644 --- a/src/components/audio-preview.tsx +++ b/src/components/audio-preview.tsx @@ -7,62 +7,43 @@ import { QueryClient, } from "@tanstack/react-query"; import LoadingSpinner from "./loading-spinner"; -import { UsersTableType } from "@/server/db/schema"; +import type { UsersTableType } from "@/server/db/schema"; +import { getRecording } from "@/app/actions"; const queryClient = new QueryClient(); -async function getRecording( - name: string, - userData: UsersTableType, -): Promise { - if (!userData) { - console.error("Error getting user data"); - return null; - } - - try { - const url = `${userData.whishperURL}/api/documents/${name}/download/`; - const response = await fetch(url); - console.log(response); - if (response.ok) { - const blob = await response.blob(); - return URL.createObjectURL(blob); - } else { - console.error("Failed to fetch recording"); - return null; - } - } catch (error) { - console.error("An error occurred:", error); - return null; - } -} - function Player(props: { name: string }) { - const user = useQuery({ - queryKey: ["audioURL", props.name], + const { + data: userData, + isLoading, + error, + } = useQuery({ + queryKey: ["audioURL"], queryFn: async () => { - return getUserData; + return getUserData(); }, }); - const url = useQuery({ - queryKey: ["audioURL", props.name], - queryFn: async () => { - return getRecording(props.name, user.data); - }, + const { data: url } = useQuery({ + queryKey: ["url", props.name], + queryFn: getRecording, }); - if (user.isLoading ?? url.isLoading) { - return Loading ...; + if (isLoading ?? !userData) { + return Loading...; + } + + if (error instanceof Error) { + return

Error: {error.message}

; } - return

{url.data}

; + return

{url}

; } -export default function AudioPreview(props: { name: string }) { +export default function AudioPreview({ name }: { name: string }) { return ( - + ); } diff --git a/src/middleware.ts b/src/middleware.ts index 816601a..6c8b4b5 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,7 +1,7 @@ import { clerkMiddleware } from "@clerk/nextjs/server"; -export default clerkMiddleware(); +export default clerkMiddleware({ debug: true }); export const config = { - matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'], -}; \ No newline at end of file + matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"], +};