File size: 7,197 Bytes
4a70373 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | "use client";
import { useState, useEffect, useCallback, useTransition } from "react";
import NoteCard from "./NoteCard";
import NoteForm from "./NoteForm";
import QueryBench from "./QueryBench";
import DbInfo from "./DbInfo";
import {
fetchNotes,
fetchStats,
createNote,
updateNote,
deleteNote,
type Note,
} from "@/lib/actions";
type Stats = {
total: number;
pinned: number;
latest: Date | null;
colors: { color: string; count: number }[];
};
type Tab = "notes" | "bench" | "db";
export default function NotesClient({
initialNotes,
initialQueryMs,
initialStats,
initialStatsMs,
}: {
initialNotes: Note[];
initialQueryMs: number;
initialStats: Stats;
initialStatsMs: number;
}) {
const [notes, setNotes] = useState(initialNotes);
const [stats, setStats] = useState(initialStats);
const [queryMs, setQueryMs] = useState(initialQueryMs);
const [statsMs, setStatsMs] = useState(initialStatsMs);
const [search, setSearch] = useState("");
const [editing, setEditing] = useState<Note | null>(null);
const [showForm, setShowForm] = useState(false);
const [tab, setTab] = useState<Tab>("notes");
const [isPending, startTransition] = useTransition();
const [lastAction, setLastAction] = useState<{ name: string; ms: number } | null>(null);
const reload = useCallback(() => {
startTransition(async () => {
const [nr, sr] = await Promise.all([
fetchNotes(search || undefined),
fetchStats(),
]);
setNotes(nr.data);
setQueryMs(nr.ms);
setStats(sr.data);
setStatsMs(sr.ms);
});
}, [search]);
useEffect(() => { reload(); }, [reload]);
const onSave = async (data: { title: string; content: string; color: string; pinned: boolean }) => {
const r = editing
? await updateNote(editing.id, data)
: await createNote(data);
setLastAction({ name: editing ? "UPDATE" : "CREATE", ms: r.ms });
setEditing(null);
setShowForm(false);
reload();
};
const onDel = async (id: string) => {
const r = await deleteNote(id);
setLastAction({ name: "DELETE", ms: r.ms });
reload();
};
const onPin = async (note: Note) => {
const r = await updateNote(note.id, { pinned: !note.pinned });
setLastAction({ name: "PIN", ms: r.ms });
reload();
};
const tabs: { key: Tab; label: string; icon: string }[] = [
{ key: "notes", label: "Notes", icon: "π" },
{ key: "bench", label: "Benchmark", icon: "β‘" },
{ key: "db", label: "Database", icon: "πΎ" },
];
return (
<main className="max-w-5xl mx-auto px-3 sm:px-4 py-6 sm:py-8">
<header className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-6">
<h1 className="text-2xl sm:text-3xl font-bold tracking-tight">
<span className="text-emerald-400">β</span> Nexova
<span className="text-[10px] text-zinc-600 font-normal ml-2 align-middle">Server Actions</span>
</h1>
<div className="flex gap-1.5 sm:gap-2">
{tabs.map((t) => (
<button
key={t.key}
onClick={() => setTab(t.key)}
className={`px-3 py-1.5 rounded-lg text-xs sm:text-sm font-medium transition whitespace-nowrap ${
tab === t.key ? "bg-emerald-600 text-white" : "bg-zinc-800 text-zinc-400 hover:text-white"
}`}
>
{t.icon} {t.label}
</button>
))}
</div>
</header>
{tab === "notes" && (
<>
<div className="flex flex-wrap gap-x-4 gap-y-1 mb-3 text-xs text-zinc-500">
<span>π {stats.total} notes</span>
<span>π {stats.pinned} pinned</span>
{stats.latest && (
<span className="hidden sm:inline">π {new Date(stats.latest).toLocaleString()}</span>
)}
<div className="flex gap-1 ml-auto">
{stats.colors.map((c) => (
<span
key={c.color}
className="w-3 h-3 rounded-full inline-block"
style={{ backgroundColor: c.color }}
title={`${c.count}`}
/>
))}
</div>
</div>
<div className="flex flex-wrap items-center gap-2 mb-4 text-xs text-zinc-500">
<span className="inline-flex items-center gap-1 bg-zinc-900 border border-zinc-800 rounded-md px-2 py-1">
π Query: <span className={`font-mono font-bold ${msColor(queryMs)}`}>{queryMs}ms</span>
</span>
<span className="inline-flex items-center gap-1 bg-zinc-900 border border-zinc-800 rounded-md px-2 py-1">
π Stats: <span className={`font-mono font-bold ${msColor(statsMs)}`}>{statsMs}ms</span>
</span>
{lastAction && (
<span className="inline-flex items-center gap-1 bg-zinc-900 border border-emerald-900/50 rounded-md px-2 py-1">
β
{lastAction.name}: <span className={`font-mono font-bold ${msColor(lastAction.ms)}`}>{lastAction.ms}ms</span>
</span>
)}
{isPending && <span className="text-yellow-500 animate-pulse">β³ loading...</span>}
<span className="text-zinc-600 ml-auto hidden sm:inline">server-side via Prisma</span>
</div>
<div className="flex gap-2 sm:gap-3 mb-6">
<input
type="text"
placeholder="Search notes..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="flex-1 min-w-0 bg-zinc-900 border border-zinc-800 rounded-lg px-3 sm:px-4 py-2.5 text-sm focus:outline-none focus:border-emerald-500 transition"
/>
<button
onClick={() => { setEditing(null); setShowForm(true); }}
className="bg-emerald-600 hover:bg-emerald-500 px-3 sm:px-4 py-2.5 rounded-lg text-sm font-medium transition shrink-0"
>
+ New
</button>
</div>
{showForm && (
<NoteForm
initial={editing}
onSave={onSave}
onCancel={() => { setShowForm(false); setEditing(null); }}
/>
)}
{notes.length === 0 ? (
<div className="text-center py-16 sm:py-20 text-zinc-500">
<p className="text-4xl mb-3">π</p>
<p>No notes yet. Create one!</p>
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-4">
{notes.map((n) => (
<NoteCard
key={n.id}
note={n}
onEdit={() => { setEditing(n); setShowForm(true); }}
onDelete={() => onDel(n.id)}
onPin={() => onPin(n)}
/>
))}
</div>
)}
</>
)}
{tab === "bench" && <QueryBench />}
{tab === "db" && <DbInfo />}
</main>
);
}
const msColor = (ms: number) =>
ms < 1 ? "text-emerald-400" : ms < 5 ? "text-yellow-400" : ms < 20 ? "text-orange-400" : "text-red-400";
|