Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.5 KB
Raw Blame
import { agentDisplayName } from '@lobechat/types';
import { Breadcrumb as BaseBreadcrumb, Icon, Text } from '@lobehub/ui';
import { ChevronRight } from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router';
import { useShallow } from 'zustand/react/shallow';

import WorkspaceLink from '@/features/Workspace/WorkspaceLink';
import { useTaskStore } from '@/store/task';
import { isMobileSurface } from '@/utils/mobileSurface';

import { styles } from './style';
import { taskDetailPath } from './taskDetailPath';
import { useTaskListCrumbTarget, useTaskListOriginState } from './taskListOrigin';
import { useAgentDisplayMeta } from './useAgentDisplayMeta';

interface BreadcrumbProps {
  taskId?: string;
}

const Breadcrumb = memo<BreadcrumbProps>(({ taskId }) => {
  const { t } = useTranslation('chat');
  const { aid } = useParams<{ aid?: string }>();
  const agentMeta = useAgentDisplayMeta(aid);
  // Crumbs lead back to the list the detail was opened from, filters included,
  // when that list is the crumb's own list; otherwise to the bare list.
  const allTasksTarget = useTaskListCrumbTarget('/tasks');
  const agentTasksTarget = useTaskListCrumbTarget(aid ? `/agent/${aid}/tasks` : '/tasks');
  const taskListOriginState = useTaskListOriginState();
  const taskTitle = useTaskStore((s) =>
    taskId ? s.taskDetailMap[s.taskRefIndex?.[taskId] ?? taskId]?.name : undefined,
  );
  const taskIdentifier = useTaskStore((s) =>
    taskId ? s.taskDetailMap[s.taskRefIndex?.[taskId] ?? taskId]?.identifier : undefined,
  );
  const ancestors = useTaskStore(
    useShallow((s) => {
      if (!taskId) return [];
      const chain: Array<{
        id: string;
        agentId?: string | null;
        identifier: string;
        name?: string | null;
      }> = [];
      const visited = new Set<string>([taskId]);
      let cursor = s.taskDetailMap[s.taskRefIndex?.[taskId] ?? taskId]?.parent;
      while (cursor?.id && !visited.has(cursor.id)) {
        const detail = s.taskDetailMap[cursor.id];
        visited.add(cursor.id);
        chain.push({
          id: cursor.id,
          agentId: cursor.agentId === undefined ? detail?.agentId : cursor.agentId,
          identifier: cursor.identifier,
          name: cursor.name ?? detail?.name,
        });
        cursor = detail?.parent;
      }
      return chain.reverse();
    }),
  );

  const allTasksLabel = (
    <Text color={'inherit'} style={{ whiteSpace: 'nowrap' }} weight={500}>
      {t('taskList.all')}
    </Text>
  );

  const agentCrumb =
    aid && agentMeta
      ? {
          key: `agent-${aid}`,
          title: (
            <Text
              ellipsis
              color={'inherit'}
              style={{ maxWidth: 160 }}
              type={taskId ? undefined : 'secondary'}
              weight={500}
            >
              {agentDisplayName(agentMeta)}
            </Text>
          ),
        }
      : undefined;

  // The agent crumb links to its task list only when it is not the current page
  // (i.e. when a deeper task crumb follows it).
  const agentCrumbNode =
    agentCrumb && taskId
      ? {
          ...agentCrumb,
          title: (
            <WorkspaceLink escape={agentTasksTarget.escape} to={agentTasksTarget.to}>
              {agentCrumb.title}
            </WorkspaceLink>
          ),
        }
      : agentCrumb;

  const ancestorCrumbs = ancestors.map(({ id, identifier, agentId, name }) => ({
    key: id,
    title: (
      <WorkspaceLink
        state={taskListOriginState}
        to={taskDetailPath(identifier, agentId ?? undefined, name)}
      >
        <Text color={'inherit'} weight={500}>
          {identifier}
        </Text>
      </WorkspaceLink>
    ),
  }));

  const currentTaskCrumb = taskId
    ? {
        title: (
          <span
            style={{
              alignItems: 'center',
              display: 'inline-flex',
              gap: 6,
              maxWidth: '100%',
              minWidth: 0,
            }}
          >
            {taskIdentifier && (
              <Text
                as={'span'}
                color={'inherit'}
                style={{ flexShrink: 0 }}
                type={'secondary'}
                weight={500}
              >
                {taskIdentifier}
              </Text>
            )}
            <Text
              ellipsis
              as={'span'}
              color={'inherit'}
              style={{ flex: '1 1 auto', maxWidth: 240, minWidth: 0 }}
              weight={500}
            >
              {taskTitle || taskId}
            </Text>
          </span>
        ),
      }
    : undefined;

  return (
    <BaseBreadcrumb
      className={styles.breadcrumb}
      separator={<Icon icon={ChevronRight} />}
      items={[
        // On a phone the header's back button already leads up, and there is
        // no room for the root crumb beside the task title.
        ...(isMobileSurface() && (taskId || agentCrumbNode)
          ? []
          : [
              {
                title:
                  taskId || agentCrumbNode ? (
                    <WorkspaceLink escape={allTasksTarget.escape} to={allTasksTarget.to}>
                      {allTasksLabel}
                    </WorkspaceLink>
                  ) : (
                    allTasksLabel
                  ),
              },
            ]),
        ...(agentCrumbNode ? [agentCrumbNode] : []),
        ...ancestorCrumbs,
        ...(currentTaskCrumb ? [currentTaskCrumb] : []),
      ]}
    />
  );
});

export default Breadcrumb;