Files
page-agent/packages/extension/src/entrypoints/sidepanel/App.tsx
2026-01-26 19:33:57 +08:00

156 lines
4.3 KiB
TypeScript

import { Send, Settings, Square } from 'lucide-react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { Button } from '@/components/ui/button'
import {
InputGroup,
InputGroupAddon,
InputGroupButton,
InputGroupTextarea,
} from '@/components/ui/input-group'
import { useAgent } from '../../agent/useAgent'
import { ConfigPanel } from './components/ConfigPanel'
import { ActivityCard, EventCard } from './components/cards'
import { EmptyState, Logo, StatusDot } from './components/misc'
export default function App() {
const [showConfig, setShowConfig] = useState(false)
const [task, setTask] = useState('')
const historyRef = useRef<HTMLDivElement>(null)
const textareaRef = useRef<HTMLTextAreaElement>(null)
const { status, history, activity, currentTask, config, execute, stop, configure } = useAgent()
// Auto-scroll to bottom on new events
useEffect(() => {
if (historyRef.current) {
historyRef.current.scrollTop = historyRef.current.scrollHeight
}
}, [history, activity])
const handleSubmit = useCallback(
(e?: React.FormEvent) => {
e?.preventDefault()
if (!task.trim() || status === 'running') return
const taskToExecute = task.trim()
setTask('')
console.log('[SidePanel] Executing task:', taskToExecute)
execute(taskToExecute).catch((error) => {
console.error('[SidePanel] Failed to execute task:', error)
})
},
[task, status, execute]
)
const handleStop = useCallback(() => {
console.log('[SidePanel] Stopping task...')
stop()
}, [stop])
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
handleSubmit()
}
}
if (showConfig) {
return (
<ConfigPanel
config={config}
onSave={async (newConfig) => {
await configure(newConfig)
setShowConfig(false)
}}
onClose={() => setShowConfig(false)}
/>
)
}
const isRunning = status === 'running'
const showEmptyState = !currentTask && history.length === 0 && !isRunning
return (
<div className="flex flex-col h-screen bg-background">
{/* Header */}
<header className="flex items-center justify-between border-b px-3 py-2">
<div className="flex items-center gap-2">
<Logo className="size-5" />
<span className="text-sm font-medium">Page Agent Ext</span>
</div>
<div className="flex items-center gap-3">
<StatusDot status={status} />
<Button variant="ghost" size="icon-sm" onClick={() => setShowConfig(true)}>
<Settings className="size-3.5" />
</Button>
</div>
</header>
{/* Content */}
<main className="flex-1 overflow-hidden flex flex-col">
{/* Current task */}
{currentTask && (
<div className="border-b px-3 py-2 bg-muted/30">
<div className="text-[10px] text-muted-foreground uppercase tracking-wide">Task</div>
<div className="text-xs font-medium truncate" title={currentTask}>
{currentTask}
</div>
</div>
)}
{/* History */}
<div ref={historyRef} className="flex-1 overflow-y-auto p-3 space-y-2">
{showEmptyState && <EmptyState />}
{history.map((event, index) => (
<EventCard key={index} event={event} />
))}
{/* Activity indicator at bottom */}
{activity && <ActivityCard activity={activity} />}
</div>
</main>
{/* Input */}
<footer className="border-t p-3">
<InputGroup className="relative rounded-lg">
<InputGroupTextarea
ref={textareaRef}
placeholder="Describe your task... (Enter to send)"
value={task}
onChange={(e) => setTask(e.target.value)}
onKeyDown={handleKeyDown}
disabled={isRunning}
className="text-xs pr-12 min-h-10"
/>
<InputGroupAddon align="inline-end" className="absolute bottom-0 right-0">
{isRunning ? (
<InputGroupButton
size="icon-sm"
variant="destructive"
onClick={handleStop}
className="size-7"
>
<Square className="size-3" />
</InputGroupButton>
) : (
<InputGroupButton
size="icon-sm"
variant="default"
onClick={() => handleSubmit()}
disabled={!task.trim()}
className="size-7"
>
<Send className="size-3" />
</InputGroupButton>
)}
</InputGroupAddon>
</InputGroup>
</footer>
</div>
)
}