feat: add form components and UI elements for enhanced user interaction

- Introduced Form, FormField, FormItem, FormLabel, FormControl, FormDescription, FormMessage components for structured form handling.
- Added Input, Textarea, and Select components for user input.
- Implemented Label and Separator components for better UI organization.
- Created Skeleton component for loading states.
- Developed Toast and Toaster components for user notifications.
- Integrated useToast hook for managing toast notifications.
- Established AI analysis functionality with analyzeJournalWithLocalAI.
- Set up Dexie for local database management with Journal and Task models.
- Configured Supabase client for server-side and browser-side interactions.
- Defined database schema for journals, tasks, chat sessions, and messages with Row Level Security policies.

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Poyraz Avsever
2026-05-06 16:20:14 +03:00
co-authored by Copilot
parent 3432bbab33
commit c73bf0e499
40 changed files with 9104 additions and 569 deletions
+176 -410
View File
@@ -1,442 +1,208 @@
"use client";
import { motion } from "framer-motion";
import Image from "next/image";
import logo from "../assets/logo.png";
import { useMemo } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Activity, Brain, PenTool, CheckCircle } from "lucide-react";
import { useLiveQuery } from "dexie-react-hooks";
import { db } from "@/lib/db";
import {
Cell,
Legend,
Line,
LineChart,
Pie,
PieChart,
ResponsiveContainer,
Tooltip,
Line,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
BarChart,
Bar,
Legend
} from "recharts";
import { FormEvent, useEffect, useMemo, useState } from "react";
type Mood = "happy" | "neutral" | "sad" | "angry";
type MoodScore = 4 | 3 | 2 | 1;
type Energy = 1 | 2 | 3 | 4 | 5;
const moodScores: Record<string, number> = { happy: 4, neutral: 3, sad: 2, angry: 1 };
type MoodEntry = {
id: string;
date: string;
mood: Mood;
mood_score: MoodScore;
energy: Energy;
};
export default function DashboardPage() {
const journals = useLiveQuery(() => db.journals.orderBy("date").toArray()) || [];
const tasks = useLiveQuery(() => db.tasks.toArray()) || [];
const pendingTasksCount = tasks.filter(t => t.status === "todo").length;
// Bugüne ait enerji seviyesi
const today = new Date().toISOString().split("T")[0];
const todaysJournal = journals.find(j => j.date === today);
const todaysEnergy = todaysJournal ? todaysJournal.energy : "--";
const STORAGE_KEY = "mood-tracker-entries";
const moodConfig: Record<
Mood,
{ label: string; icon: string; score: MoodScore; color: string }
> = {
happy: { label: "Mutlu", icon: "😊", score: 4, color: "#2f7d63" },
neutral: { label: "Nötr", icon: "😐", score: 3, color: "#4f7fbf" },
sad: { label: "Üzgün", icon: "😔", score: 2, color: "#e3aa38" },
angry: { label: "Sinirli", icon: "😠", score: 1, color: "#d8644a" },
};
const moodOptions = Object.entries(moodConfig) as Array<
[Mood, (typeof moodConfig)[Mood]]
>;
const today = () => new Date().toISOString().slice(0, 10);
const createId = () =>
typeof crypto !== "undefined" && "randomUUID" in crypto
? crypto.randomUUID()
: `${Date.now()}-${Math.random().toString(16).slice(2)}`;
export default function Home() {
const [entries, setEntries] = useState<MoodEntry[]>([]);
const [date, setDate] = useState(today);
const [mood, setMood] = useState<Mood>("happy");
const [energy, setEnergy] = useState<Energy>(3);
const [message, setMessage] = useState("");
const [isLoaded, setIsLoaded] = useState(false);
useEffect(() => {
const rawEntries = window.localStorage.getItem(STORAGE_KEY);
if (!rawEntries) {
setIsLoaded(true);
return;
}
try {
const parsedEntries = JSON.parse(rawEntries) as MoodEntry[];
if (Array.isArray(parsedEntries)) {
setEntries(sortEntries(parsedEntries));
// 1. Grafik: Son 7 günün Ruh Hali ve Enerji trendi
const trendData = useMemo(() => {
const dataMap: Record<string, { date: string, moodSum: number, energySum: number, count: number }> = {};
journals.forEach(j => {
if (!dataMap[j.date]) {
dataMap[j.date] = { date: j.date, moodSum: 0, energySum: 0, count: 0 };
}
} catch {
window.localStorage.removeItem(STORAGE_KEY);
} finally {
setIsLoaded(true);
}
}, []);
useEffect(() => {
if (!isLoaded) {
return;
}
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(entries));
}, [entries, isLoaded]);
const lineData = useMemo(
() =>
entries.map((entry) => ({
date: formatShortDate(entry.date),
mood: entry.mood_score,
energy: entry.energy,
})),
[entries],
);
const pieData = useMemo(
() =>
moodOptions
.map(([key, config]) => ({
name: config.label,
value: entries.filter((entry) => entry.mood === key).length,
color: config.color,
}))
.filter((item) => item.value > 0),
[entries],
);
const insights = useMemo(() => buildInsights(entries), [entries]);
function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!date) {
setMessage("Lütfen tarih seç.");
return;
}
if (!mood) {
setMessage("Lütfen ruh halini seç.");
return;
}
if (energy < 1 || energy > 5) {
setMessage("Enerji seviyesi 1 ile 5 arasında olmalı.");
return;
}
const moodScore = moodConfig[mood].score;
setEntries((currentEntries) => {
const existingEntry = currentEntries.find((entry) => entry.date === date);
const nextEntry: MoodEntry = {
id: existingEntry?.id ?? createId(),
date,
mood,
mood_score: moodScore,
energy,
};
const nextEntries = existingEntry
? currentEntries.map((entry) =>
entry.date === date ? nextEntry : entry,
)
: [...currentEntries, nextEntry];
return sortEntries(nextEntries);
dataMap[j.date].moodSum += moodScores[j.mood] || 3;
dataMap[j.date].energySum += j.energy;
dataMap[j.date].count += 1;
});
setMessage("Kayıt kaydedildi. Dashboard güncellendi.");
}
return Object.values(dataMap)
.map(d => ({
date: d.date.slice(5), // Sadece MM-DD formatı alalım
Mood: Number((d.moodSum / d.count).toFixed(1)),
Enerji: Number((d.energySum / d.count).toFixed(1)),
}))
.slice(-7); // Sadece son 7 günü göster
}, [journals]);
// 2. Grafik: En çok kullanılan AI etiketleri
const tagData = useMemo(() => {
const counts: Record<string, number> = {};
journals.forEach(j => {
if (j.ai_tags) {
j.ai_tags.forEach(tag => {
counts[tag] = (counts[tag] || 0) + 1;
});
}
});
return Object.entries(counts)
.map(([name, value]) => ({ name, Değer: value }))
.sort((a, b) => b.Değer - a.Değer)
.slice(0, 5); // En çok geçen 5 etiket
}, [journals]);
// Son günlüğe ait AI Summary
const lastInsight = [...journals].reverse().find(j => j.ai_summary)?.ai_summary;
return (
<main className="mx-auto flex min-h-screen w-full max-w-7xl flex-col gap-6 px-4 py-6 sm:px-6 lg:px-8">
<header className="flex flex-col gap-2 border-b border-ink/10 pb-5 sm:flex-row sm:items-end sm:justify-between">
<div className="flex items-center gap-4">
<Image
src={logo}
alt="Mood Tracker logo"
className="h-16 w-16 rounded-md object-contain sm:h-20 sm:w-20"
priority
/>
<h1 className="text-3xl font-bold text-ink sm:text-4xl">
Günlük ruh hali dashboard'u
</h1>
</div>
<div className="rounded-md border border-ink/10 bg-white px-4 py-3 text-sm text-ink/70 shadow-soft">
{entries.length} kayıt
</div>
</header>
<div className="space-y-6 max-w-6xl mx-auto animate-in fade-in slide-in-from-bottom-4 duration-500">
<div className="flex flex-col gap-2">
<h1 className="text-3xl font-bold tracking-tight">Bugün Nasılsın?</h1>
<p className="text-muted-foreground">Kişisel özetin ve yapay zeka analizlerin burada görünecek.</p>
</div>
<section className="grid gap-5 lg:grid-cols-[360px_1fr]">
<motion.form
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35 }}
onSubmit={handleSubmit}
className="rounded-lg border border-ink/10 bg-white p-5 shadow-soft"
>
<div className="mb-5">
<h2 className="text-xl font-semibold text-ink">Bugünün kaydı</h2>
<p className="mt-1 text-sm text-ink/60">
Aynı tarih tekrar kaydedilirse eski kayıt güncellenir.
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">Günlük Kayıtları</CardTitle>
<PenTool className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{journals.length}</div>
<p className="text-xs text-muted-foreground mt-1">
{journals.length === 0 ? "Henüz kayıt girilmedi" : "Toplam kayıt eklendi"}
</p>
</div>
<label className="block text-sm font-medium text-ink" htmlFor="date">
Tarih
</label>
<input
id="date"
type="date"
value={date}
onChange={(event) => setDate(event.target.value)}
className="mt-2 w-full rounded-md border border-ink/15 bg-mist px-3 py-2 outline-none transition focus:border-leaf focus:ring-2 focus:ring-leaf/20"
/>
<fieldset className="mt-5">
<legend className="text-sm font-medium text-ink">Ruh hali</legend>
<div className="mt-2 grid grid-cols-2 gap-2">
{moodOptions.map(([key, config]) => (
<button
key={key}
type="button"
onClick={() => setMood(key)}
className={`flex min-h-20 flex-col items-center justify-center rounded-md border px-3 py-3 text-center transition ${
mood === key
? "border-leaf bg-leaf text-white"
: "border-ink/10 bg-mist text-ink hover:border-leaf/60"
}`}
>
<span className="text-2xl" aria-hidden="true">
{config.icon}
</span>
<span className="mt-1 text-sm font-semibold">
{config.label}
</span>
</button>
))}
</div>
</fieldset>
<label
className="mt-5 block text-sm font-medium text-ink"
htmlFor="energy"
>
Enerji seviyesi: {energy}
</label>
<input
id="energy"
type="range"
min="1"
max="5"
step="1"
value={energy}
onChange={(event) => setEnergy(Number(event.target.value) as Energy)}
className="mt-3 w-full accent-leaf"
/>
<div className="mt-1 flex justify-between text-xs text-ink/50">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
<button
type="submit"
className="mt-6 w-full rounded-md bg-ink px-4 py-3 text-sm font-semibold text-white transition hover:bg-leaf"
>
Kaydet
</button>
{message ? (
<p className="mt-3 rounded-md bg-mist px-3 py-2 text-sm text-ink/70">
{message}
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">Aktif Görevler</CardTitle>
<CheckCircle className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{pendingTasksCount}</div>
<p className="text-xs text-muted-foreground mt-1">
Bekleyen görev
</p>
) : null}
</motion.form>
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">Bugünkü Enerji</CardTitle>
<Activity className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold">{todaysEnergy}</div>
<p className="text-xs text-muted-foreground mt-1">
{todaysJournal ? "/ 5 Seviyesinde" : "Kayıt bekleniyor"}
</p>
</CardContent>
</Card>
<div className="grid gap-5">
<ChartCard title="Mood ve enerji trendi">
{entries.length > 0 ? (
<ResponsiveContainer width="100%" height={300}>
<LineChart data={lineData} margin={{ left: 0, right: 16 }}>
<XAxis dataKey="date" tickLine={false} axisLine={false} />
<YAxis
domain={[1, 5]}
tickCount={5}
tickLine={false}
axisLine={false}
/>
<Tooltip />
<Legend />
<Line
type="monotone"
dataKey="mood"
name="Mood score"
stroke="#2f7d63"
strokeWidth={3}
dot={{ r: 4 }}
/>
<Line
type="monotone"
dataKey="energy"
name="Enerji"
stroke="#d8644a"
strokeWidth={3}
dot={{ r: 4 }}
<Card>
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-medium">Local AI Durumu</CardTitle>
<Brain className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold text-green-500">Hazır</div>
<p className="text-xs text-muted-foreground mt-1">
Ollama / Veri bekliyor
</p>
</CardContent>
</Card>
</div>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-7 ">
{/* Line Chart */}
<Card className="lg:col-span-4 p-6 flex flex-col justify-between">
<div className="mb-4">
<h3 className="text-lg font-medium mb-1">Ruh Hali & Enerji Trendi</h3>
<p className="text-sm text-muted-foreground">Son 7 günlük ortalamalar (1-5 Arası Puanlama)</p>
</div>
<div className="h-[250px] w-full">
{trendData.length > 0 ? (
<ResponsiveContainer width="100%" height="100%">
<LineChart data={trendData}>
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#88888833" />
<XAxis dataKey="date" axisLine={false} tickLine={false} tick={{ fontSize: 12 }} />
<YAxis domain={[1, 5]} axisLine={false} tickLine={false} tick={{ fontSize: 12 }} width={30} />
<Tooltip
contentStyle={{ borderRadius: "8px", backgroundColor: "#fff", color: "#000", border: "none" }}
itemStyle={{ fontWeight: "500" }}
/>
<Legend wrapperStyle={{ paddingTop: "10px", fontSize: "14px" }} />
<Line type="monotone" dataKey="Mood" stroke="#3b82f6" strokeWidth={3} dot={{ r: 4 }} activeDot={{ r: 6 }} />
<Line type="monotone" dataKey="Enerji" stroke="#10b981" strokeWidth={3} dot={{ r: 4 }} activeDot={{ r: 6 }} />
</LineChart>
</ResponsiveContainer>
) : (
<EmptyState text="Trend grafiği için ilk mood kaydını ekle." />
<div className="h-full flex items-center justify-center border-2 border-dashed rounded-md bg-muted/20">
<p className="text-sm text-muted-foreground">Veri bekleniyor...</p>
</div>
)}
</ChartCard>
<div className="grid gap-5 xl:grid-cols-[1fr_1fr]">
<ChartCard title="Mood dağılımı">
{pieData.length > 0 ? (
<ResponsiveContainer width="100%" height={260}>
<PieChart>
<Pie
data={pieData}
dataKey="value"
nameKey="name"
innerRadius={56}
outerRadius={92}
paddingAngle={3}
>
{pieData.map((item) => (
<Cell key={item.name} fill={item.color} />
))}
</Pie>
<Tooltip />
<Legend />
</PieChart>
</div>
</Card>
{/* Bar Chart & Insight */}
<div className="lg:col-span-3 space-y-4 flex flex-col">
<Card className="p-6 flex-1 flex flex-col">
<div className="mb-4">
<h3 className="text-lg font-medium mb-1">AI Konu Dağılımı</h3>
<p className="text-sm text-muted-foreground">Günlüklerinden çıkarılan en sık 5 etiket</p>
</div>
<div className="h-[150px] w-full mt-auto">
{tagData.length > 0 ? (
<ResponsiveContainer width="100%" height="100%">
<BarChart data={tagData} layout="vertical" margin={{ left: -20 }}>
<CartesianGrid strokeDasharray="3 3" horizontal={false} stroke="#88888833" />
<XAxis type="number" hide />
<YAxis dataKey="name" type="category" axisLine={false} tickLine={false} tick={{ fontSize: 13 }} width={90} />
<Tooltip
cursor={{fill: 'transparent'}}
contentStyle={{ borderRadius: "8px", backgroundColor: "#fff", color: "#000", border: "none" }}
/>
<Bar dataKey="Değer" fill="#8b5cf6" radius={[0, 4, 4, 0]} barSize={20} />
</BarChart>
</ResponsiveContainer>
) : (
<EmptyState text="Dağılım grafiği kayıt eklendikten sonra görünür." />
<div className="h-full flex items-center justify-center border-2 border-dashed rounded-md bg-muted/20">
<p className="text-sm text-muted-foreground">Yeterli etiket yok.</p>
</div>
)}
</ChartCard>
</div>
</Card>
<ChartCard title="Insight">
{insights.length > 0 ? (
<ul className="space-y-3">
{insights.map((insight) => (
<li
key={insight}
className="rounded-md border border-ink/10 bg-mist px-3 py-3 text-sm leading-6 text-ink/75"
>
{insight}
</li>
))}
</ul>
) : (
<EmptyState text="Insight üretmek için en az bir kayıt ekle." />
)}
</ChartCard>
</div>
<Card className="p-6 bg-primary/5 border-primary/20 flex-1">
<h3 className="text-lg font-medium mb-3 flex items-center gap-2">
<Brain className="w-5 h-5 text-primary" /> Son AI İçgörüsü
</h3>
<p className="text-sm text-foreground/80 leading-relaxed italic">
{lastInsight ? `"${lastInsight}"` : "Henüz bir içgörü oluşmadı. Biraz günlük yaz, AI analiz yapsın."}
</p>
</Card>
</div>
</section>
</main>
);
}
function ChartCard({
title,
children,
}: {
title: string;
children: React.ReactNode;
}) {
return (
<motion.section
initial={{ opacity: 0, y: 12 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35 }}
className="rounded-lg border border-ink/10 bg-white p-5 shadow-soft"
>
<h2 className="mb-4 text-lg font-semibold text-ink">{title}</h2>
{children}
</motion.section>
);
}
function EmptyState({ text }: { text: string }) {
return (
<div className="flex min-h-56 items-center justify-center rounded-md border border-dashed border-ink/15 bg-mist px-4 text-center text-sm text-ink/55">
{text}
</div>
</div>
);
}
function sortEntries(entries: MoodEntry[]) {
return [...entries].sort((a, b) => a.date.localeCompare(b.date));
}
function formatShortDate(date: string) {
return new Intl.DateTimeFormat("tr-TR", {
day: "2-digit",
month: "short",
}).format(new Date(`${date}T00:00:00`));
}
function buildInsights(entries: MoodEntry[]) {
if (entries.length === 0) {
return [];
}
const latestSeven = [...entries]
.sort((a, b) => b.date.localeCompare(a.date))
.slice(0, 7);
const moodAverage = average(latestSeven.map((entry) => entry.mood_score));
const energyAverage = average(latestSeven.map((entry) => entry.energy));
const mostFrequentMood = getMostFrequentMood(entries);
const insights = [
moodAverage >= 3
? "Son 7 kayıtta genel ruh halin pozitif görünüyor."
: "Son 7 kayıtta ruh hali ortalaman düşük görünüyor.",
];
if (energyAverage < 3) {
insights.push("Enerji seviyen son kayıtlarda düşük seyrediyor.");
} else {
insights.push("Enerji seviyen son kayıtlarda dengeli görünüyor.");
}
if (mostFrequentMood) {
insights.push(
`En sık görülen ruh halin: ${moodConfig[mostFrequentMood].label}.`,
);
}
return insights;
}
function average(values: number[]) {
return values.reduce((total, value) => total + value, 0) / values.length;
}
function getMostFrequentMood(entries: MoodEntry[]) {
const counts = entries.reduce<Record<Mood, number>>(
(currentCounts, entry) => {
currentCounts[entry.mood] += 1;
return currentCounts;
},
{ happy: 0, neutral: 0, sad: 0, angry: 0 },
);
return moodOptions.reduce<Mood | null>((winner, [moodKey]) => {
if (!winner || counts[moodKey] > counts[winner]) {
return moodKey;
}
return winner;
}, null);
}