refactor(sessions): simplify session creation by removing modal and using direct button

The SessionModal component was removed and replaced with a direct button click handler that creates a session using the agent data. Also added error handling to display an alert when session fetching fails.
This commit is contained in:
icarus 2025-09-22 17:14:58 +08:00
parent 2385fba695
commit 9425437480

View File

@ -1,9 +1,10 @@
import { Button, Spinner } from '@heroui/react' import { Alert, Button, Spinner } from '@heroui/react'
import { SessionModal } from '@renderer/components/Popups/agent/SessionModal' import { useAgent } from '@renderer/hooks/agents/useAgent'
import { useSessions } from '@renderer/hooks/agents/useSessions' import { useSessions } from '@renderer/hooks/agents/useSessions'
import { useRuntime } from '@renderer/hooks/useRuntime' import { useRuntime } from '@renderer/hooks/useRuntime'
import { useAppDispatch } from '@renderer/store' import { useAppDispatch } from '@renderer/store'
import { setActiveSessionIdAction, setActiveTopicOrSessionAction } from '@renderer/store/runtime' import { setActiveSessionIdAction, setActiveTopicOrSessionAction } from '@renderer/store/runtime'
import { CreateSessionForm } from '@renderer/types'
import { AnimatePresence, motion } from 'framer-motion' import { AnimatePresence, motion } from 'framer-motion'
import { Plus } from 'lucide-react' import { Plus } from 'lucide-react'
import { memo, useCallback, useEffect } from 'react' import { memo, useCallback, useEffect } from 'react'
@ -19,7 +20,8 @@ interface SessionsProps {
const Sessions: React.FC<SessionsProps> = ({ agentId }) => { const Sessions: React.FC<SessionsProps> = ({ agentId }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { sessions, isLoading, deleteSession } = useSessions(agentId) const { agent } = useAgent(agentId)
const { sessions, isLoading, error, deleteSession, createSession } = useSessions(agentId)
const { chat } = useRuntime() const { chat } = useRuntime()
const { activeSessionId } = chat const { activeSessionId } = chat
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
@ -32,6 +34,15 @@ const Sessions: React.FC<SessionsProps> = ({ agentId }) => {
[dispatch] [dispatch]
) )
const handleCreateSession = useCallback(() => {
if (!agent) return
const session = {
...agent,
id: undefined
} satisfies CreateSessionForm
createSession(session)
}, [agent, createSession])
const currentActiveSessionId = activeSessionId[agentId] const currentActiveSessionId = activeSessionId[agentId]
useEffect(() => { useEffect(() => {
@ -52,7 +63,7 @@ const Sessions: React.FC<SessionsProps> = ({ agentId }) => {
) )
} }
// if (error) return if (error) return <Alert color="danger" content={t('agent.session.get.error.failed')} />
return ( return (
<motion.div <motion.div
@ -64,20 +75,12 @@ const Sessions: React.FC<SessionsProps> = ({ agentId }) => {
initial={{ opacity: 0, y: -10 }} initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }} animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.2, delay: 0.1 }}> transition={{ duration: 0.2, delay: 0.1 }}>
<SessionModal <Button
agentId={agentId} onPress={handleCreateSession}
onSessionCreated={(created) => setActiveSessionId(agentId, created.id)} className="mb-2 w-full justify-start bg-transparent text-foreground-500 hover:bg-accent">
trigger={{ <Plus size={16} className="mr-1 shrink-0" />
content: ( {t('agent.session.add.title')}
<Button </Button>
onPress={(e) => e.continuePropagation()}
className="mb-2 w-full justify-start bg-transparent text-foreground-500 hover:bg-accent">
<Plus size={16} className="mr-1 shrink-0" />
{t('agent.session.add.title')}
</Button>
)
}}
/>
</motion.div> </motion.div>
<AnimatePresence> <AnimatePresence>
{sessions.map((session, index) => ( {sessions.map((session, index) => (