feat: cache ai results on server + show success banner

This commit is contained in:
vas3k
2025-05-20 22:32:38 +02:00
parent c352f5eadd
commit f5c5bf75f6
11 changed files with 142 additions and 84 deletions

View File

@@ -1,10 +1,14 @@
"use client"
import { Check, X, Trash2 } from "lucide-react"
import { createContext, ReactNode, useContext, useState } from "react"
type BannerType = "success" | "deleted" | "failed" | "default"
type Notification = {
code: string
message: string
type?: BannerType
}
type NotificationContextType = {
@@ -22,10 +26,51 @@ export function NotificationProvider({ children }: { children: ReactNode }) {
const showNotification = (notification: Notification) => {
setNotification(notification)
if (notification.code === "global.banner") {
setTimeout(() => setNotification(null), 2000)
}
}
const getBannerStyles = (type: BannerType = "default") => {
switch (type) {
case "success":
return "bg-green-500 text-teal-50"
case "deleted":
return "bg-black text-white"
case "failed":
return "bg-red-500 text-white"
case "default":
return "bg-white text-black"
}
}
const getBannerIcon = (type: BannerType = "default") => {
switch (type) {
case "success":
return <Check className="h-10 w-10 animate-bounce" />
case "deleted":
return <Trash2 className="h-10 w-10 animate-bounce" />
case "failed":
return <X className="h-10 w-10 animate-bounce" />
case "default":
return null
}
}
return (
<NotificationContext.Provider value={{ notification, showNotification }}>{children}</NotificationContext.Provider>
<NotificationContext.Provider value={{ notification, showNotification }}>
{children}
{notification?.code === "global.banner" && (
<div className="fixed inset-0 flex items-center justify-center z-50">
<div
className={`border rounded-lg p-8 flex flex-col items-center justify-center gap-4 shadow-lg h-[160px] w-[160px] ${getBannerStyles(notification.type)}`}
>
{getBannerIcon(notification.type)}
<p className="text-xl font-medium">{notification.message}</p>
</div>
</div>
)}
</NotificationContext.Provider>
)
}

View File

@@ -67,7 +67,7 @@ export async function analyzeFileAction(
const schema = fieldsToJsonSchema(fields)
const results = await analyzeTransaction(prompt, schema, attachments, apiKey)
const results = await analyzeTransaction(prompt, schema, attachments, apiKey, file.id, user.id)
console.log("Analysis results:", results)

View File

@@ -3,7 +3,7 @@ import { Loader2 } from "lucide-react"
export default function Loading() {
return (
<div className="flex flex-col gap-4 p-4 w-full max-w-6xl">
<div className="flex flex-col gap-6 p-4 w-full max-w-6xl">
<header className="flex items-center justify-between">
<h2 className="text-3xl font-bold tracking-tight flex flex-row gap-2">
<span>Loading unsorted files...</span>

View File

@@ -31,7 +31,7 @@ export default async function UnsortedPage() {
const settings = await getSettings(user.id)
return (
<div className="flex flex-col gap-4 p-4 w-full max-w-6xl">
<div className="flex flex-col gap-6 p-4 w-full max-w-6xl">
<header className="flex items-center justify-between">
<h2 className="text-3xl font-bold tracking-tight">You have {files.length} unsorted files</h2>
</header>

View File

@@ -42,7 +42,7 @@ export const viewport: Viewport = {
userScalable: false,
}
export default async function RootLayout({ children }: { children: React.ReactNode }) {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body className="min-h-screen bg-white antialiased">{children}</body>