diff --git a/frontend/src/pages/Dashboard.tsx b/frontend/src/pages/Dashboard.tsx index a66c192..4d35932 100644 --- a/frontend/src/pages/Dashboard.tsx +++ b/frontend/src/pages/Dashboard.tsx @@ -1,11 +1,21 @@ import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { getSession, getCurrentUser, getProfile, signOut } from '../lib/supabase'; -import { roomsApi, resourcesApi } from '../lib/api'; +import { roomsApi, resourcesApi, journalApi } from '../lib/api'; import ChatRoom from '../components/ChatRoom'; type Tab = 'rooms' | 'journal' | 'habits' | 'resources'; +interface JournalEntry { + id: string; + title: string; + content: string; + created_at: string; + mood?: number; + tags?: string[]; + updated_at?: string; +} + export default function Dashboard() { const [loading, setLoading] = useState(true); const [activeTab, setActiveTab] = useState('rooms'); @@ -207,6 +217,87 @@ function RoomsTab() { } function JournalTab() { + const [entries, setEntries] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const [searchQuery, setSearchQuery] = useState(''); + + useEffect(() => { + let isMounted = true; + + async function loadEntries() { + try { + const data = await journalApi.getAll(); + if (!isMounted) return; + setEntries(Array.isArray(data) ? data : []); + } catch (err) { + console.error('Error loading journal entries:', err); + if (!isMounted) return; + setError('Failed to load journal entries.'); + } finally { + if (!isMounted) return; + setLoading(false); + } + } + + loadEntries(); + + return () => { + isMounted = false; + }; + }, []); + + const normalizedQuery = searchQuery.trim().toLowerCase(); + const filteredEntries = normalizedQuery + ? entries.filter((entry) => { + const title = (entry.title || '').toLowerCase(); + const content = (entry.content || '').toLowerCase(); + return title.includes(normalizedQuery) || content.includes(normalizedQuery); + }) + : entries; + + let content; + + if (loading) { + content = ( +
+ Loading your journal entries... +
+ ); + } else if (entries.length === 0) { + content = ( +
+ No journal entries yet. +
+ ); + } else if (normalizedQuery && filteredEntries.length === 0) { + content = ( +
+ No entries match your search. +
+ ); + } else { + content = ( +
+ {filteredEntries.map((entry) => ( +
+
+

+ {entry.title || 'Untitled entry'} +

+ + {entry.created_at ? new Date(entry.created_at).toLocaleDateString() : ''} + +
+

+ {entry.content} +

+
+ ))} +
+ ); + } + return (
@@ -214,14 +305,23 @@ function JournalTab() {
-
-

- 📝 Journal feature coming soon! Track your thoughts, moods, and reflections. -

-

- All journal entries are completely private and only visible to you. -

+
+ setSearchQuery(e.target.value)} + placeholder="Search by title or content..." + className="input-field w-full" + />
+ + {error && !loading && ( +
+ {error} +
+ )} + + {content}
); }