"use client"; import { useState } from "react"; import { v4 as uuidv4 } from "uuid"; import { useLiveQuery } from "dexie-react-hooks"; import { db } from "@/lib/db"; import { analyzeJournalWithLocalAI } from "@/lib/ai"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; const moods = [ { id: "happy", label: "Mutlu", emoji: "😊" }, { id: "neutral", label: "Nötr", emoji: "😐" }, { id: "sad", label: "Üzgün", emoji: "😔" }, { id: "angry", label: "Sinirli", emoji: "😠" }, ]; export default function JournalPage() { const [content, setContent] = useState(""); const [mood, setMood] = useState("happy"); const [energy, setEnergy] = useState("3"); const [isSubmitting, setIsSubmitting] = useState(false); // Dexie.js üzerinden günlükleri tarih sırasına göre çekiyoruz (en yeni en üstte) const journals = useLiveQuery( () => db.journals.orderBy("date").reverse().toArray() ); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!content.trim()) return; setIsSubmitting(true); const now = new Date().toISOString(); const entryId = uuidv4(); const currentContent = content; // Analiz için metni kopyala try { // 1. Veriyi veritabanına hemen ekle (Kullanıcı beklemesin) await db.journals.add({ id: entryId, date: now.split("T")[0], mood, energy: parseInt(energy, 10), content: currentContent, created_at: now, updated_at: now, }); // UI'ı Sıfırla setContent(""); setEnergy("3"); setMood("happy"); // 2. Arka planda AI Analizi başlat try { const aiResult = await analyzeJournalWithLocalAI(currentContent); if (aiResult) { await db.journals.update(entryId, { ai_tags: aiResult.ai_tags, ai_sentiment_score: aiResult.ai_sentiment_score, ai_summary: aiResult.ai_summary, }); // Eğer AI görev önerdiyse Görevler tablosuna at (pending / onay bekliyor yapısı eklenebilir ama direkt atalım) if (aiResult.suggested_tasks && aiResult.suggested_tasks.length > 0) { for (const taskTitle of aiResult.suggested_tasks) { await db.tasks.add({ id: uuidv4(), journal_id: entryId, title: `AI Önerisi: ${taskTitle}`, status: "todo", ai_generated: true, date: now.split("T")[0], created_at: now, }); } } } } catch (aiErr) { console.error("Arka plan AI analizi hatası:", aiErr); } } catch (error) { console.error("Günlük kaydedilemedi:", error); } finally { setIsSubmitting(false); } }; const handleDelete = async (id: string) => { if(confirm("Bu günlüğü silmek istediğinden emin misin?")) { await db.journals.delete(id); } }; return (
Zihnini boşalt, hislerini kaydet. Verilerin sadece cihazında kalır.
Yükleniyor...
) : journals.length === 0 ? (Henüz kayıt bulunmuyor.
) : ({journal.content}
{/* AI Sonuçlarını Göster */} {journal.ai_tags && (