ui/ disable buttons if health checks not working

This commit is contained in:
Leon van Zyl
2025-08-13 12:37:48 +02:00
parent 9dcb5aa9e3
commit 5741fcc486
7 changed files with 174 additions and 53 deletions

View File

@@ -3,10 +3,13 @@
import { useSession } from "@/lib/auth-client"; import { useSession } from "@/lib/auth-client";
import { UserProfile } from "@/components/auth/user-profile"; import { UserProfile } from "@/components/auth/user-profile";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Lock } from "lucide-react";
import { useDiagnostics } from "@/hooks/use-diagnostics";
import Link from "next/link"; import Link from "next/link";
export default function DashboardPage() { export default function DashboardPage() {
const { data: session, isPending } = useSession(); const { data: session, isPending } = useSession();
const { isAiReady, loading: diagnosticsLoading } = useDiagnostics();
if (isPending) { if (isPending) {
return ( return (
@@ -19,7 +22,14 @@ export default function DashboardPage() {
if (!session) { if (!session) {
return ( return (
<div className="container mx-auto px-4 py-12"> <div className="container mx-auto px-4 py-12">
<div className="max-w-3xl mx-auto"> <div className="max-w-3xl mx-auto text-center">
<div className="mb-8">
<Lock className="w-16 h-16 mx-auto mb-4 text-muted-foreground" />
<h1 className="text-2xl font-bold mb-2">Protected Page</h1>
<p className="text-muted-foreground mb-6">
You need to sign in to access the dashboard
</p>
</div>
<UserProfile /> <UserProfile />
</div> </div>
</div> </div>
@@ -38,9 +48,15 @@ export default function DashboardPage() {
<p className="text-muted-foreground mb-4"> <p className="text-muted-foreground mb-4">
Start a conversation with AI using the Vercel AI SDK Start a conversation with AI using the Vercel AI SDK
</p> </p>
{(diagnosticsLoading || !isAiReady) ? (
<Button disabled={true}>
Go to Chat
</Button>
) : (
<Button asChild> <Button asChild>
<Link href="/chat">Go to Chat</Link> <Link href="/chat">Go to Chat</Link>
</Button> </Button>
)}
</div> </div>
<div className="p-6 border border-border rounded-lg"> <div className="p-6 border border-border rounded-lg">

View File

@@ -14,8 +14,8 @@ const geistMono = Geist_Mono({
}); });
export const metadata: Metadata = { export const metadata: Metadata = {
title: "Create Next App", title: "Next.js Full-Stack Boilerplate",
description: "Generated by create next app", description: "Complete Next.js starter template with authentication, database, AI integration, and modern tooling by Leon van Zyl",
}; };
export default function RootLayout({ export default function RootLayout({

View File

@@ -1,8 +1,12 @@
"use client";
import Link from "next/link"; import Link from "next/link";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { SetupChecklist } from "@/components/setup-checklist"; import { SetupChecklist } from "@/components/setup-checklist";
import { useDiagnostics } from "@/hooks/use-diagnostics";
export default function Home() { export default function Home() {
const { isAuthReady, isAiReady, loading } = useDiagnostics();
return ( return (
<div className="min-h-screen flex flex-col grain"> <div className="min-h-screen flex flex-col grain">
<main className="flex-1 container mx-auto px-4 py-12"> <main className="flex-1 container mx-auto px-4 py-12">
@@ -76,17 +80,42 @@ export default function Home() {
<div className="p-4 border rounded-lg"> <div className="p-4 border rounded-lg">
<h4 className="font-medium mb-2">3. Try the features</h4> <h4 className="font-medium mb-2">3. Try the features</h4>
<div className="space-y-2"> <div className="space-y-2">
<Button asChild size="sm" className="w-full glow"> {(loading || !isAuthReady) ? (
<Link href="/dashboard">View Dashboard</Link> <Button
size="sm"
className="w-full glow"
disabled={true}
>
View Dashboard
</Button> </Button>
) : (
<Button asChild size="sm" className="w-full glow">
<Link href="/dashboard">
View Dashboard
</Link>
</Button>
)}
{(loading || !isAiReady) ? (
<Button
variant="outline"
size="sm"
className="w-full"
disabled={true}
>
Try AI Chat
</Button>
) : (
<Button <Button
asChild asChild
variant="outline" variant="outline"
size="sm" size="sm"
className="w-full" className="w-full"
> >
<Link href="/chat">Try AI Chat</Link> <Link href="/chat">
Try AI Chat
</Link>
</Button> </Button>
)}
</div> </div>
</div> </div>
<div className="p-4 border rounded-lg"> <div className="p-4 border rounded-lg">
@@ -103,7 +132,21 @@ export default function Home() {
<footer className="border-t py-8 text-center text-sm text-muted-foreground"> <footer className="border-t py-8 text-center text-sm text-muted-foreground">
<div className="container mx-auto px-4"> <div className="container mx-auto px-4">
Built with Next.js, Better Auth, Drizzle ORM, and Vercel AI SDK <p className="mb-2">
Boilerplate template by Leon van Zyl
</p>
<p>
Visit{" "}
<a
href="https://youtube.com/@leonvanzyl"
target="_blank"
rel="noopener noreferrer"
className="text-primary hover:underline"
>
@leonvanzyl on YouTube
</a>
{" "}for tutorials on using this template
</p>
</div> </div>
</footer> </footer>
</div> </div>

View File

@@ -1,33 +1,31 @@
"use client" "use client";
import { signOut, useSession } from "@/lib/auth-client" import { signOut, useSession } from "@/lib/auth-client";
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button";
import { useRouter } from "next/navigation";
export function SignOutButton() { export function SignOutButton() {
const { data: session, isPending } = useSession() const { data: session, isPending } = useSession();
const router = useRouter();
if (isPending) { if (isPending) {
return <Button disabled>Loading...</Button> return <Button disabled>Loading...</Button>;
} }
if (!session) { if (!session) {
return null return null;
} }
return ( return (
<Button <Button
variant="outline" variant="outline"
onClick={async () => { onClick={async () => {
await signOut({ await signOut();
fetchOptions: { router.replace("/");
onSuccess: () => { router.refresh();
window.location.href = "/"
},
},
})
}} }}
> >
Sign out Sign out
</Button> </Button>
) );
} }

View File

@@ -21,9 +21,11 @@ export function UserProfile() {
} }
return ( return (
<div className="flex flex-col items-center gap-4 p-6"> <div className="flex items-center gap-3">
<div className="text-center"> <span className="text-sm text-muted-foreground">
<Avatar className="size-16 mx-auto mb-4"> Welcome {session.user?.name}
</span>
<Avatar className="size-8">
<AvatarImage <AvatarImage
src={session.user?.image || ""} src={session.user?.image || ""}
alt={session.user?.name || "User"} alt={session.user?.name || "User"}
@@ -37,9 +39,6 @@ export function UserProfile() {
).toUpperCase()} ).toUpperCase()}
</AvatarFallback> </AvatarFallback>
</Avatar> </Avatar>
<h2 className="text-xl font-semibold">{session.user?.name}</h2>
<p className="text-muted-foreground">{session.user?.email}</p>
</div>
<SignOutButton /> <SignOutButton />
</div> </div>
); );

View File

@@ -10,7 +10,7 @@ export function SiteHeader() {
href="/" href="/"
className="bg-clip-text text-transparent [background-image:linear-gradient(90deg,color-mix(in_oklab,var(--primary)_85%,white_0%),color-mix(in_oklab,var(--primary)_50%,white_0%))] hover:opacity-90" className="bg-clip-text text-transparent [background-image:linear-gradient(90deg,color-mix(in_oklab,var(--primary)_85%,white_0%),color-mix(in_oklab,var(--primary)_50%,white_0%))] hover:opacity-90"
> >
Next.js Starter Kit Next.js Boilerplate
</Link> </Link>
</h1> </h1>
<UserProfile /> <UserProfile />

View File

@@ -0,0 +1,65 @@
"use client";
import { useEffect, useState } from "react";
type DiagnosticsResponse = {
timestamp: string;
env: {
DATABASE_URL: boolean;
BETTER_AUTH_SECRET: boolean;
GOOGLE_CLIENT_ID: boolean;
GOOGLE_CLIENT_SECRET: boolean;
OPENAI_API_KEY: boolean;
NEXT_PUBLIC_APP_URL: boolean;
};
database: {
connected: boolean;
schemaApplied: boolean;
error?: string;
};
auth: {
configured: boolean;
routeResponding: boolean | null;
};
ai: {
configured: boolean;
};
overallStatus: "ok" | "warn" | "error";
};
export function useDiagnostics() {
const [data, setData] = useState<DiagnosticsResponse | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
async function fetchDiagnostics() {
setLoading(true);
setError(null);
try {
const res = await fetch("/api/diagnostics", { cache: "no-store" });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = (await res.json()) as DiagnosticsResponse;
setData(json);
} catch (e) {
setError(e instanceof Error ? e.message : "Failed to load diagnostics");
} finally {
setLoading(false);
}
}
useEffect(() => {
fetchDiagnostics();
}, []);
const isAuthReady = data?.auth.configured && data?.database.connected && data?.database.schemaApplied;
const isAiReady = data?.ai.configured;
return {
data,
loading,
error,
refetch: fetchDiagnostics,
isAuthReady: Boolean(isAuthReady),
isAiReady: Boolean(isAiReady),
};
}