diff --git a/src/app/actions.ts b/src/app/actions.ts index 90b49c5..28ef27d 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -4,7 +4,9 @@ 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, clerkMiddleware } from "@clerk/nextjs/server"; +import { auth } from "@clerk/nextjs/server"; +import type { WhishperRecordingType } from "@/types"; + /* Clerk helpers @@ -79,7 +81,36 @@ export async function getPaperlessDocuments(query: string) { return data; } -export async function formatWhishperName(name: string) { - const frontPart = name.split("_WHSHPR_")[1] ?? name; - return frontPart.replace(".m4a", "") ?? name; +export async function getWhishperRecordings( + query: string, +): Promise { + 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 WhishperRecordingType[]; + const lowerCaseQuery = query.toLowerCase(); + const filteredAndScored = data + .filter( + (item) => + item.fileName.toLowerCase().includes(lowerCaseQuery) || + item.result.text.toLowerCase().includes(lowerCaseQuery), + ) + .map((item) => { + const fileNameOccurrences = ( + item.fileName.toLowerCase().match(new RegExp(lowerCaseQuery, "g")) ?? [] + ).length; + const textOccurrences = ( + item.result.text.toLowerCase().match(new RegExp(lowerCaseQuery, "g")) ?? + [] + ).length; + const score = fileNameOccurrences + textOccurrences; + return { ...item, score }; + }); + const sortedByScore = filteredAndScored.sort((a, b) => b.score - a.score); + + // Step 4: Return the sorted array without the score + return sortedByScore.map(({ ...item }) => item); } diff --git a/src/app/whishper/page.tsx b/src/app/whishper/page.tsx index 13baf2c..91d2f6a 100644 --- a/src/app/whishper/page.tsx +++ b/src/app/whishper/page.tsx @@ -22,9 +22,8 @@ import { QueryClientProvider, QueryClient, } from "@tanstack/react-query"; -import { formatWhishperName, getUserData } from "../actions"; +import { getUserData, getWhishperRecordings } from "../actions"; import LoadingSpinner from "@/components/loading-spinner"; -import type { WhishperRecordingType } from "@/types"; import OpenInternalLink from "@/components/internal-link"; import { type ColumnDef, @@ -46,39 +45,7 @@ import { BadgeCheck, Badge, BadgeAlert } from "lucide-react"; const queryClient = new QueryClient(); -async function getWhishperRecordings( - query: string, -): Promise { - 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 WhishperRecordingType[]; - const lowerCaseQuery = query.toLowerCase(); - const filteredAndScored = data - .filter( - (item) => - item.fileName.toLowerCase().includes(lowerCaseQuery) || - item.result.text.toLowerCase().includes(lowerCaseQuery), - ) - .map((item) => { - const fileNameOccurrences = ( - item.fileName.toLowerCase().match(new RegExp(lowerCaseQuery, "g")) ?? [] - ).length; - const textOccurrences = ( - item.result.text.toLowerCase().match(new RegExp(lowerCaseQuery, "g")) ?? - [] - ).length; - const score = fileNameOccurrences + textOccurrences; - return { ...item, score }; - }); - const sortedByScore = filteredAndScored.sort((a, b) => b.score - a.score); - - // Step 4: Return the sorted array without the score - return sortedByScore.map(({ ...item }) => item); -} function SearchForm() { const formSchema = z.object({ @@ -193,7 +160,7 @@ function RecordingsList() { return ( <> -

Search Results

+

Search Results

=> { function SkeletonLoader() { return ( -
+
-
+
{/* Title Skeleton */} -
+
{/* Audio Skeleton */}
{/* Button Skeletons */} -
-
-
-
+
+ {Array.from({ length: 5 }, (_, index) => ( +
@@ -46,7 +48,7 @@ function SkeletonLoader() { } function AudioInfo(props: { name: string }) { - // Fetch user data using useQuery hook + const router = useRouter(); const { data: userData, @@ -72,10 +74,18 @@ function AudioInfo(props: { name: string }) {
-
-

+
+
{formattedName} -

+ +
{/* Audio */}
-
- {Array.from({ length: 5 }, (_, index) => ( - + +
{ + "use server"; + await getWhishperRecordings(props.name); + }} + > + +
diff --git a/src/server/querys.ts b/src/server/querys.ts new file mode 100644 index 0000000..b9b14cf --- /dev/null +++ b/src/server/querys.ts @@ -0,0 +1,17 @@ +import "server-only"; + +import { getUserData } from "@/app/actions"; + +export async function deleteRecording(id: string) { + const userData = await getUserData(); + if (!userData?.whishperURL) { + throw new Error("Can't do that"); + } + const response = await fetch(`${userData.whishperURL}/api/transcriptions/${id}`, { + method: "DELETE", + }); + if (!response.ok) { + throw new Error("Network error"); + } + return response.json(); +} \ No newline at end of file