import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { PageMeta } from '@/components/common/PageMeta' import { useAuthStore } from '@/store/authStore' import { l1Api } from '@/api/l1' import { toast } from '@/lib/toast' import { EmptyStateCard } from '@/components/l1/EmptyStateCard' import { ResumeInProgress } from '@/components/l1/ResumeInProgress' import type { QueueRow } from '@/types/l1' export default function L1Dashboard() { const user = useAuthStore((s) => s.user) const navigate = useNavigate() const [problem, setProblem] = useState('') const [customerName, setCustomerName] = useState('') const [customerContact, setCustomerContact] = useState('') const [submitting, setSubmitting] = useState(false) const [queue, setQueue] = useState([]) const [isEmpty, setIsEmpty] = useState(false) useEffect(() => { l1Api.queue('open').then(setQueue).catch(() => setQueue([])) // Phase 1: emptiness detection is just "is the queue empty AND no resumable sessions" — // we conservatively show the empty-state card on accounts with literally no L1 activity yet. // (A stricter KB-empty detection arrives in Phase 2 when the kb_documents table exists.) }, []) useEffect(() => { // Show empty-state ONLY for first-run state — no queue items and no active sessions if (queue.length === 0) { l1Api .listActiveSessions() .then((active) => setIsEmpty(active.length === 0)) .catch(() => setIsEmpty(false)) } else { setIsEmpty(false) } }, [queue]) const handleStart = async () => { if (!problem.trim()) return setSubmitting(true) try { const response = await l1Api.intake({ problem_statement: problem.trim(), customer_name: customerName.trim() || undefined, customer_contact: customerContact.trim() || undefined, }) navigate(`/l1/walk/${response.session_id}`) } catch (err) { const detail = (err as { response?: { data?: { detail?: string } } }).response?.data?.detail const msg = typeof detail === 'string' ? detail : 'Failed to start walk. Try again.' toast.error(msg) } finally { setSubmitting(false) } } const now = new Date() const greeting = now.getHours() < 12 ? 'morning' : now.getHours() < 18 ? 'afternoon' : 'evening' const firstName = user?.name?.split(' ')[0] || 'there' return (
{/* Greeting */}

{now.toLocaleDateString('en-US', { weekday: 'long', month: 'long', day: 'numeric', })}

Good {greeting}, {firstName}.

{/* Empty state (first-run) */} {isEmpty && } {/* Describe the problem */}
Describe the problem