import { useAuth } from '../contexts/AuthContext' import { Link } from 'react-router-dom' import { useState, useRef } from 'react' import { createEntry } from '../lib/api' import { encryptEntry } from '../lib/crypto' import BottomNav from '../components/BottomNav' export default function HomePage() { const { user, userId, secretKey, loading } = useAuth() const [entry, setEntry] = useState('') const [title, setTitle] = useState('') const [saving, setSaving] = useState(false) const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null) const titleInputRef = useRef(null) const contentTextareaRef = useRef(null) if (loading) { return (

Loading…

) } if (!user) { return (

Grateful Journal

Sign in to start your journal.

Go to login
) } // Format date: "THURSDAY, OCT 24" const today = new Date() const dateString = today .toLocaleDateString('en-US', { weekday: 'long', month: 'short', day: 'numeric' }) .toUpperCase() const handleTitleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && title.trim()) { e.preventDefault() contentTextareaRef.current?.focus() } } const handleWrite = async () => { if (!userId || !title.trim() || !entry.trim()) { setMessage({ type: 'error', text: 'Please add a title and entry content' }) return } if (!secretKey) { setMessage({ type: 'error', text: 'Encryption key not available. Please log in again.' }) return } setSaving(true) setMessage(null) try { const token = await user.getIdToken() // Combine title and content for encryption const contentToEncrypt = `${title.trim()}\n\n${entry.trim()}` // Encrypt the entry with master key const { ciphertext, nonce } = await encryptEntry( contentToEncrypt, secretKey ) // Send encrypted data to backend // Note: title and content are null for encrypted entries await createEntry( userId, { title: undefined, content: undefined, isPublic: false, encryption: { encrypted: true, ciphertext, nonce, algorithm: 'XSalsa20-Poly1305', }, }, token ) setMessage({ type: 'success', text: 'Entry saved securely!' }) setTitle('') setEntry('') // Clear success message after 3 seconds setTimeout(() => setMessage(null), 3000) } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to save entry' setMessage({ type: 'error', text: errorMessage }) } finally { setSaving(false) } } return (
{dateString}

What are you grateful for today?

setTitle(e.target.value)} onKeyDown={handleTitleKeyDown} enterKeyHint="next" ref={titleInputRef} disabled={saving} />