Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

10.4 KB
Raw Blame
import { useCallback, useState } from 'react'
import { Link, useNavigate } from '@tanstack/react-router'
import {
  LoaderCircleIcon,
  MessageSquareIcon,
  PlusIcon,
  Trash2Icon,
} from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '#/components/ui/alert-dialog'
import { Button } from '#/components/ui/button'
import { useStudioService } from '#/hooks/use-studio-service'
import { useAppConnection } from '#/hooks/use-app-connection'
import {
  useCreateSession,
  useDeleteSession,
  useSessionListByRecency,
} from '#/lib/sessions/use-sessions'
import { useSessionTitles } from '#/lib/sessions/use-session-titles'
import { normalizeOvClientError } from '#/lib/ov-client'
import { cn } from '#/lib/utils'

interface ThreadListProps {
  activeSessionId?: string
  className?: string
}

export function ThreadList({ activeSessionId, className }: ThreadListProps) {
  const { i18n, t } = useTranslation('sessions')
  const { provider } = useStudioService()
  const { identityScopeKey } = useAppConnection()
  const navigate = useNavigate()
  const {
    data: sessions,
    error: listError,
    isError,
    isFetching,
    isPending,
    refetch,
  } = useSessionListByRecency()
  const { getTitle, removeTitle, setTitle } = useSessionTitles(identityScopeKey)
  const createSession = useCreateSession()
  const deleteSession = useDeleteSession()
  const [sessionToDelete, setSessionToDelete] = useState<{
    id: string
    title: string
  } | null>(null)

  const handleNewSession = useCallback(async () => {
    const result = await createSession.mutateAsync(undefined)
    setTitle(result.session_id, t('threadList.newSession'))
    void navigate({ to: '/sessions', search: { s: result.session_id } })
  }, [createSession, navigate, setTitle, t])

  const handleDeleteSession = useCallback(async () => {
    if (!sessionToDelete) return

    try {
      await deleteSession.mutateAsync(sessionToDelete.id)
      removeTitle(sessionToDelete.id)

      if (activeSessionId === sessionToDelete.id) {
        const nextSession = sessions.find(
          (session) => session.session_id !== sessionToDelete.id,
        )
        void navigate({
          to: '/sessions',
          search: { s: nextSession?.session_id },
        })
      }
      toast.success(t('threadList.deleteSuccess'))
      setSessionToDelete(null)
    } catch (error) {
      toast.error(
        t('threadList.deleteFailed', {
          error: error instanceof Error ? error.message : String(error),
        }),
      )
    }
  }, [
    activeSessionId,
    deleteSession,
    navigate,
    removeTitle,
    sessionToDelete,
    sessions,
    t,
  ])

  return (
    <aside
      className={cn(
        'flex h-full w-72 shrink-0 flex-col border-r border-border/70 bg-muted/20',
        className,
      )}
    >
      <div className="flex h-16 shrink-0 items-center justify-between border-b border-border/70 px-4">
        <div className="min-w-0">
          <h1 className="text-sm font-semibold text-foreground">
            {provider === 'volcengine'
              ? t('studio:sessionRecords')
              : t('threadList.title')}
          </h1>
          {!isPending && (!isError || sessions.length > 0) ? (
            <p className="mt-0.5 text-xs text-muted-foreground">
              {t('threadList.count', { count: sessions.length })}
            </p>
          ) : null}
        </div>
        {provider === 'opensource' ? (
          <Button
            type="button"
            size="icon-sm"
            variant="outline"
            disabled={createSession.isPending}
            onClick={handleNewSession}
            aria-label={t('threadList.newSession')}
            title={t('threadList.newSession')}
          >
            {createSession.isPending ? (
              <LoaderCircleIcon className="animate-spin" />
            ) : (
              <PlusIcon />
            )}
          </Button>
        ) : null}
      </div>

      <div className="min-h-0 flex-1 overflow-y-auto p-2">
        {isError ? (
          <div
            role="alert"
            className="mb-2 grid gap-2 rounded-lg border border-destructive/30 p-3 text-sm"
          >
            <p className="font-medium text-destructive">
              {t('threadList.loadFailed')}
            </p>
            <p className="wrap-anywhere text-xs text-muted-foreground">
              {normalizeOvClientError(listError).message}
            </p>
            <Button
              type="button"
              variant="outline"
              size="sm"
              className="justify-self-start"
              disabled={isFetching}
              onClick={() => void refetch()}
            >
              {t('threadList.retry')}
            </Button>
          </div>
        ) : null}
        {isPending ? (
          <div className="flex h-28 items-center justify-center gap-2 text-sm text-muted-foreground">
            <LoaderCircleIcon className="size-4 animate-spin" />
            <span>{t('threadList.loading')}</span>
          </div>
        ) : sessions.length === 0 ? (
          isError ? null : (
            <div className="flex h-40 flex-col items-center justify-center px-6 text-center">
              <div className="flex size-9 items-center justify-center rounded-xl bg-muted">
                <MessageSquareIcon className="size-4 text-muted-foreground" />
              </div>
              <p className="mt-3 text-sm font-medium text-foreground">
                {t('threadList.emptyTitle')}
              </p>
              <p className="mt-1 text-xs leading-5 text-muted-foreground">
                {provider === 'opensource'
                  ? t('threadList.emptyDescription')
                  : t('studio:noSessionRecords')}
              </p>
            </div>
          )
        ) : (
          <div className="space-y-1">
            {sessions.map((session) => {
              const isActive = activeSessionId === session.session_id
              const title = getTitle(session.session_id)

              return (
                <div
                  key={session.session_id}
                  className={cn(
                    'group/session relative rounded-xl transition-colors',
                    isActive
                      ? 'bg-accent text-accent-foreground shadow-sm ring-1 ring-border/60'
                      : 'hover:bg-muted/80',
                  )}
                >
                  <Link
                    to="/sessions"
                    search={{ s: session.session_id }}
                    className="flex min-w-0 items-start gap-2.5 px-3 py-2.5 pr-9 outline-none focus-visible:ring-2 focus-visible:ring-ring"
                  >
                    <MessageSquareIcon
                      className={cn(
                        'mt-0.5 size-4 shrink-0',
                        isActive ? 'text-primary' : 'text-muted-foreground',
                      )}
                    />
                    <span className="min-w-0 flex-1">
                      <span className="block truncate text-sm font-medium">
                        {title}
                      </span>
                      <span className="mt-1 block truncate font-mono text-[11px] text-muted-foreground">
                        {formatSessionTime(
                          session.mod_time,
                          i18n.resolvedLanguage,
                        )}
                      </span>
                    </span>
                  </Link>
                  <button
                    type="button"
                    onClick={(event) => {
                      event.preventDefault()
                      event.stopPropagation()
                      setSessionToDelete({
                        id: session.session_id,
                        title,
                      })
                    }}
                    disabled={deleteSession.isPending}
                    className="absolute right-2 top-2.5 flex size-6 items-center justify-center rounded-md text-muted-foreground opacity-0 transition-[opacity,color,background-color] hover:bg-destructive/10 hover:text-destructive focus-visible:opacity-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring group-hover/session:opacity-100"
                    aria-label={t('threadList.deleteSession', { title })}
                    title={t('threadList.deleteSession', { title })}
                  >
                    <Trash2Icon className="size-3.5" />
                  </button>
                </div>
              )
            })}
          </div>
        )}
      </div>

      <AlertDialog
        open={Boolean(sessionToDelete)}
        onOpenChange={(open) => {
          if (!open && !deleteSession.isPending) setSessionToDelete(null)
        }}
      >
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>
              {t('threadList.deleteConfirmTitle')}
            </AlertDialogTitle>
            <AlertDialogDescription>
              {t('threadList.deleteConfirmDescription', {
                title: sessionToDelete?.title,
              })}
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel disabled={deleteSession.isPending}>
              {t('threadList.cancel')}
            </AlertDialogCancel>
            <AlertDialogAction
              className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
              disabled={deleteSession.isPending}
              onClick={(event) => {
                event.preventDefault()
                void handleDeleteSession()
              }}
            >
              {deleteSession.isPending
                ? t('threadList.deleting')
                : t('threadList.confirmDelete')}
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>

      {provider === 'opensource' ? (
        <div className="shrink-0 border-t border-border/70 px-4 py-3 text-[11px] text-muted-foreground">
          {t('threadList.shortcut')}
        </div>
      ) : null}
    </aside>
  )
}

function formatSessionTime(value: string, locale?: string): string {
  if (!value) return ''

  const date = new Date(value)
  if (Number.isNaN(date.getTime())) return ''

  return new Intl.DateTimeFormat(locale, {
    dateStyle: 'short',
    timeStyle: 'short',
  }).format(date)
}