import { useState, useCallback, useEffect } from 'react'
import { ThreadProvider } from '@standardagents/react'
import { Sidebar } from './components/Sidebar'
import { Chat } from './components/Chat'
import { EmptyState } from './components/EmptyState'
import { Login } from './components/Login'
import { useThreads, type Thread } from './hooks/useThreads'
import { useAuth } from './hooks/useAuth'
export default function App() {
const { isAuthenticated, isLoading: authLoading } = useAuth()
// Show loading state while checking auth
if (authLoading) {
return (
)
}
// Show login if not authenticated
if (!isAuthenticated) {
return
}
return
}
function AuthenticatedApp() {
const { threads, loading, refreshThreads, deleteThread, updateThreadTitle } = useThreads()
const [currentThreadId, setCurrentThreadId] = useState(() => {
// Check URL for thread ID on initial load
const params = new URLSearchParams(window.location.search)
return params.get('thread')
})
const [selectedAgentId, setSelectedAgentId] = useState('')
const [sidebarOpen, setSidebarOpen] = useState(true)
// Update URL when thread changes
useEffect(() => {
const url = new URL(window.location.href)
if (currentThreadId) {
url.searchParams.set('thread', currentThreadId)
} else {
url.searchParams.delete('thread')
}
window.history.replaceState({}, '', url.toString())
}, [currentThreadId])
const handleNewThread = useCallback(() => {
setCurrentThreadId(null)
}, [])
const handleSelectThread = useCallback((threadId: string) => {
setCurrentThreadId(threadId)
}, [])
const handleThreadCreated = useCallback((thread: Thread) => {
refreshThreads()
setCurrentThreadId(thread.id)
}, [refreshThreads])
const handleAgentChange = useCallback((agentId: string) => {
setSelectedAgentId(agentId)
}, [])
const handleDeleteThread = useCallback(async (threadId: string) => {
await deleteThread(threadId)
if (currentThreadId === threadId) {
setCurrentThreadId(null)
}
}, [deleteThread, currentThreadId])
return (
setSidebarOpen(!sidebarOpen)}
loading={loading}
/>
{/* Main content with animated margin */}
{/* Expand sidebar button - shown when collapsed */}
{currentThreadId ? (
t.id === currentThreadId)}
onUpdateTitle={(title) => updateThreadTitle(currentThreadId, title)}
onRunStateChange={refreshThreads}
sidebarOpen={sidebarOpen}
/>
) : (
)}
)
}