import { useState, useRef, useEffect } from 'react'; import { api } from '../lib/api'; import { getCurrentLocation } from '../lib/geolocation'; interface Props { isOpen: boolean; onClose: () => void; } export default function QuickAddWidget({ isOpen, onClose }: Props) { const [type, setType] = useState('event'); const [content, setContent] = useState(''); const [loading, setLoading] = useState(false); const [locked, setLocked] = useState(false); const inputRef = useRef(null); const today = new Date().toISOString().split('T')[0]; useEffect(() => { if (isOpen) { checkDiaryStatus(); setTimeout(() => inputRef.current?.focus(), 50); } }, [isOpen]); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); if (content.trim() && !locked && !loading) { const form = inputRef.current?.form; if (form) { const submitEvent = new Event('submit', { bubbles: true, cancelable: true }); form.dispatchEvent(submitEvent); } } } }; if (isOpen) { window.addEventListener('keydown', handleKeyDown); } return () => window.removeEventListener('keydown', handleKeyDown); }, [isOpen, content, locked, loading]); const checkDiaryStatus = async () => { const res = await api.getDay(today); if (res.data) { setLocked(!!res.data.journal); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!content.trim() || locked) return; setLoading(true); const location = await getCurrentLocation(); await api.createEvent(today, type, content, undefined, location ?? undefined); setContent(''); setLoading(false); onClose(); }; if (!isOpen) return null; if (locked) { return (

Today's diary is locked

Delete the diary to add more events.

View Diary
); } return (
setContent(e.target.value)} placeholder={type === 'health' ? 'How are you feeling?' : 'Log an event...'} className="flex-1 px-4 py-3 bg-slate-900 rounded-lg border border-slate-700 focus:border-purple-500 focus:outline-none" />
); }