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.

21.2 KB
Raw Blame
'use client';

import {
  ActionIcon,
  Block,
  Button,
  createStaticStyles,
  cssVar,
  Empty,
  Flexbox,
  ScrollShadow,
  SearchBar,
  Segmented,
  Text,
} from '@lobehub/ui';
import { LayoutGridIcon, ListIcon, PlusIcon, RefreshCwIcon, SearchXIcon } from 'lucide-react';
import { memo, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import GoalSkeleton, { GoalListRowsSkeleton } from '@/components/Skeleton/Goal';
import { AgentGoalsAndTasksTabs } from '@/features/AgentTasks/AgentGoalsAndTasksTabs';
import NavHeader from '@/features/NavHeader';
import WideScreenContainer from '@/features/WideScreenContainer';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useCacheScope } from '@/libs/swr/useCacheScope';
import type { GoalListFilter } from '@/store/goal';
import { goalSelectors, useGoalStore } from '@/store/goal';
import { globalGoalListScopeId, goalListKey, goalListScopeId } from '@/store/goal/projection';
import { useDensityTokens } from '@/styles/density';
import { densityVars } from '@/styles/densityVars';

import { createGoalModal } from './CreateGoalModal';
import GoalAgentFilter from './GoalAgentFilter';
import { goalAgentFilterOptions } from './goalAgentFilterOptions';
import { GoalCardItem } from './GoalCardItem';
import GoalEmptyState from './GoalEmptyState';
import type { GoalExampleSeed } from './goalExamples';
import { filterGoalsByKeyword, normalizeGoalKeyword } from './goalKeywordFilter';
import { GoalListItem } from './GoalListItem';
import { useGoalListAutoLoad } from './useGoalListAutoLoad';

const styles = createStaticStyles(({ css }) => ({
  countBadge: css`
    padding-block: 1px;
    padding-inline: 7px;
    border-radius: 99px;

    font-size: 12px;
    font-variant-numeric: tabular-nums;
    line-height: 18px;
    color: ${cssVar.colorTextSecondary};

    background: ${cssVar.colorFillTertiary};
  `,
  overview: css`
    padding-block: 6px 18px;
  `,
  list: css`
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: ${densityVars.contentGap};

    @media (width <= 900px) {
      grid-template-columns: minmax(0, 1fr);
    }
  `,
  listRows: css`
    display: flex;
    flex-direction: column;
    border-block: 1px solid ${cssVar.colorBorderSecondary};
  `,
  metric: css`
    min-width: 88px;
    padding-inline-start: ${densityVars.cardPadding};
    border-inline-start: 1px solid ${cssVar.colorBorderSecondary};

    &:first-child {
      padding-inline-start: 0;
      border-inline-start: 0;
    }
  `,
  search: css`
    flex: none;
    width: 200px;
  `,
  sentinel: css`
    height: 1px;
  `,
}));

/**
 * The list failed to load — the page's own read, or the selected tab's. Both
 * carry the same retry, which revalidates every tab's entry at once.
 */
const GoalLoadError = ({ onRetry }: { onRetry: () => void }) => {
  const { t } = useTranslation('chat');

  return (
    <Block padding={32} variant={'outlined'}>
      <Flexbox align={'center'} gap={12}>
        <Text weight={600}>{t('goalList.loadError')}</Text>
        <Text fontSize={13} type={'secondary'}>
          {t('goalList.loadErrorDescription')}
        </Text>
        <Button icon={RefreshCwIcon} onClick={onRetry}>
          {t('goalList.retry')}
        </Button>
      </Flexbox>
    </Block>
  );
};

interface AgentGoalsPageProps {
  /** Neither id: the cross-agent page, listing every agent's goals. */
  agentId?: string;
  projectId?: string;
}

const AgentGoalsPage = memo<AgentGoalsPageProps>(({ agentId, projectId }) => {
  const { t } = useTranslation('chat');
  const { pageTitleFontSize } = useDensityTokens();
  const navigate = useWorkspaceAwareNavigate();
  const cacheScope = useCacheScope();
  const activeWorkspaceId = useActiveWorkspaceId();
  const isGlobal = !agentId && !projectId;
  const agentFilter = useGoalStore((s) => s.goalListAgentFilter);
  const setAgentFilter = useGoalStore((s) => s.setGoalListAgentFilter);
  // The cross-agent page narrowed to one agent reads that agent's own list —
  // the same server query and cache entry as the agent's Goals page.
  const queryAgentId = isGlobal ? agentFilter : agentId;
  const globalScope = isGlobal && !queryAgentId ? cacheScope : undefined;
  const scopeId = goalListScopeId({ agentId: queryAgentId, globalScope, projectId });
  const allGoalsScopeId = isGlobal ? globalGoalListScopeId(cacheScope) : scopeId;
  const useFetchGoals = useGoalStore((s) => s.useFetchGoals);
  const refreshGoals = useGoalStore((s) => s.refreshGoals);
  const filter = useGoalStore((s) => s.goalListFilter);
  const viewMode = useGoalStore((s) => s.goalViewMode);
  const visibleLimit = useGoalStore((s) => s.goalListVisibleLimit);
  const setFilter = useGoalStore((s) => s.setGoalListFilter);
  const keyword = useGoalStore((s) => s.goalListKeyword);
  const setKeyword = useGoalStore((s) => s.setGoalListKeyword);
  const setViewMode = useGoalStore((s) => s.setGoalViewMode);
  const loadMoreGoals = useGoalStore((s) => s.loadMoreGoals);
  // The rows live in the replica view; the hook only orchestrates the fetch, so
  // a revisit paints the persisted projection on the first frame instead of an
  // empty-state frame while the sync lands.
  const { error, isLoading } = useFetchGoals(queryAgentId, projectId, 'all', globalScope);
  // The active tab reads its own statuses from the server. `all` shares the
  // window read's entry, so the page only pays for an extra request once the
  // user opens a narrow tab.
  const tabSync = useFetchGoals(queryAgentId, projectId, filter, globalScope);
  // An agent's or project's page: `delivered` is a whole-set count, not a count
  // of the page the list happens to hold — read the review set's own total so
  // the overview can never disagree with what the Needs-review tab shows once a
  // goal sits past the list's page. The read has to run whichever tab is
  // active, since the overview shows it regardless.
  // The cross-agent page has no overview; it reads every agent's goals instead,
  // which the agent filter lists its options from even while narrowed.
  useFetchGoals(
    isGlobal ? undefined : agentId,
    projectId,
    isGlobal ? 'all' : 'review',
    isGlobal ? cacheScope : undefined,
  );
  // Archived goals are left out of `all`, so a scope whose every goal was put
  // away reads as empty. Ask the Archived tab of the same scope the empty check
  // reads, but only then: the onboarding state would otherwise hide the tab the
  // goals went to.
  const archivedProbe = {
    agentId: isGlobal ? undefined : agentId,
    filter: 'archived' as const,
    globalScope: isGlobal ? cacheScope : undefined,
    projectId,
  };
  const listView = useGoalStore(goalSelectors.goalListView(scopeId));
  const allGoalsView = useGoalStore(goalSelectors.goalListView(allGoalsScopeId));
  const tabView = useGoalStore(
    goalSelectors.goalListView(
      filter === 'all'
        ? scopeId
        : goalListKey({ agentId: queryAgentId, filter, globalScope, projectId }),
    ),
  );
  const reviewView = useGoalStore(
    goalSelectors.goalListView(goalListKey({ agentId, filter: 'review', projectId })),
  );
  const goals = useMemo(() => listView?.goals ?? [], [listView]);
  // Whether the scope has any goal at all — on the cross-agent page that is
  // every agent's list, so narrowing to one agent shows the filtered-empty
  // state rather than the first-goal onboarding.
  const isScopeEmpty =
    (isGlobal ? (allGoalsView?.goals.length ?? 0) : goals.length) === 0 && !isLoading;
  // The cross-agent Archived tab also lists its agent options from the scope's
  // archived goals: `all` leaves them out, so an agent whose every goal was
  // archived would otherwise be missing from the picker of the tab holding them.
  const readsArchivedScope = isScopeEmpty || (isGlobal && filter === 'archived');
  const archivedSync = useFetchGoals(
    readsArchivedScope ? archivedProbe.agentId : undefined,
    readsArchivedScope ? archivedProbe.projectId : undefined,
    'archived',
    readsArchivedScope ? archivedProbe.globalScope : undefined,
  );
  const archivedView = useGoalStore(goalSelectors.goalListView(goalListKey(archivedProbe)));
  const agentFilterOptions = useMemo(
    () =>
      isGlobal
        ? goalAgentFilterOptions((filter === 'archived' ? archivedView : allGoalsView)?.goals ?? [])
        : [],
    [allGoalsView, archivedView, filter, isGlobal],
  );
  // Onboarding only once the probe has answered "nothing archived either":
  // while it is in flight the page holds its skeleton, and a failed probe keeps
  // the tabs (the Archived tab then reads and reports its own failure) rather
  // than a first-goal screen that would hide them.
  // A persisted empty projection is not an answer either: the last live goal
  // may have been archived elsewhere since, so an empty hydrated row waits for
  // the revalidation in flight.
  const isArchivedProbePending =
    isScopeEmpty &&
    !archivedSync.error &&
    (archivedView === undefined || (archivedView.goals.length === 0 && archivedSync.isValidating));
  const hasNoGoals =
    isScopeEmpty &&
    !isArchivedProbePending &&
    archivedView !== undefined &&
    archivedView.goals.length === 0;
  const isListPending = isLoading || isArchivedProbePending;
  const summary = useMemo(() => {
    const total = listView?.total ?? goals.length;
    // An aggregate that has not answered is unknown, not zero. Falling back to
    // the review goals visible in the list's page would understate it for
    // exactly the accounts this page reads past one page, and present that
    // partial count as the delivered total.
    const delivered = reviewView?.total;

    return {
      delivered,
      pursuing: delivered === undefined ? undefined : total - delivered,
      total,
    };
  }, [goals.length, listView?.total, reviewView?.total]);
  // A page of the newest goals cannot prove a tab empty, and neither can a read
  // that failed: hold the list's shape while the tab's own read is in flight,
  // and surface its failure rather than spinning on it.
  const isTabUnresolved = filter !== 'all' && tabView === undefined;
  const isTabPending = isTabUnresolved && !tabSync.error;
  const isTabError = isTabUnresolved && Boolean(tabSync.error);
  // The rows are the tab's own read, with no client-side fallback: a page of the
  // newest goals cannot stand in for the tab's answer, and presenting it as if
  // it could is what let a tab claim an outcome it had no way to know.
  const filteredGoals = useMemo(
    () => (filter === 'all' ? goals : (tabView?.goals ?? [])),
    [filter, goals, tabView],
  );
  // The search box narrows the tab's rows on the client. It can only see the
  // page the tab loaded, so a tab holding more goals than that says so below
  // the matches instead of presenting them as the whole answer.
  const isSearching = normalizeGoalKeyword(keyword) !== '';
  const searchedGoals = useMemo(
    () => filterGoalsByKeyword(filteredGoals, keyword),
    [filteredGoals, keyword],
  );
  const tabTotal = filter === 'all' ? listView?.total : tabView?.total;
  const isSearchPartial =
    isSearching && !isTabUnresolved && tabTotal !== undefined && tabTotal > filteredGoals.length;
  const isFilterEmpty = !isTabUnresolved && filteredGoals.length === 0;
  const isSearchEmpty = isSearching && !isFilterEmpty && searchedGoals.length === 0;
  // The tab's count is the server's answer for that tab. Until it arrives — or
  // when it failed — the list's page cannot stand in for it either.
  const visibleGoalCount = isTabUnresolved ? '—' : searchedGoals.length;
  const GoalItem = viewMode === 'list' ? GoalListItem : GoalCardItem;
  const hasMoreGoals = !isTabUnresolved && visibleLimit < searchedGoals.length;
  const sentinelRef = useGoalListAutoLoad(hasMoreGoals, loadMoreGoals, visibleLimit);
  const openCreateGoal = (seed?: GoalExampleSeed) => {
    createGoalModal({
      agentId,
      initialRequirement: seed?.requirement,
      initialRoundBudget: seed?.roundBudget,
      initialTitle: seed?.title,
      projectId,
      // Land the user inside the goal right away: the detail page polls while
      // the goal is still planning, so the exploration graph grows in place
      // instead of the modal blocking on it.
      onCreated: (goal) => {
        void refreshGoals(scopeId);
        if (allGoalsScopeId !== scopeId) void refreshGoals(allGoalsScopeId);
        // The cross-agent page keeps the user in the workspace, like a task
        // opened from the global task list.
        const ownerId = isGlobal ? undefined : (goal.agentId ?? agentId);
        navigate(ownerId ? `/agent/${ownerId}/goal/${goal.goalId}` : `/goal/${goal.goalId}`);
      },
    });
  };

  return (
    <Flexbox flex={1} height={'100%'}>
      <NavHeader
        left={
          agentId || isGlobal ? (
            <AgentGoalsAndTasksTabs
              agentId={agentId}
              showMine={isGlobal && !!activeWorkspaceId}
              view={'goals'}
            />
          ) : (
            <Text weight={600}>{t('goalList.title')}</Text>
          )
        }
        right={
          !isListPending &&
          !hasNoGoals && (
            <Button icon={PlusIcon} size={'small'} type={'fill'} onClick={() => openCreateGoal()}>
              {t('goalPage.create')}
            </Button>
          )
        }
      />
      <WideScreenContainer
        fullWidth
        flex={1}
        gap={16}
        paddingBlock={16}
        paddingInline={16}
        wrapperStyle={{ flex: 1, overflowY: 'auto' }}
      >
        {isListPending ? (
          <GoalSkeleton chrome={'body'} />
        ) : error && listView === undefined ? (
          // Only a read that left nothing to show becomes the page: a failed
          // revalidation keeps the hydrated rows on screen (the same guard the
          // home rail, the goal portal and the detail page use), so a network
          // blip no longer hides goals the user already has locally.
          <GoalLoadError onRetry={() => void refreshGoals(scopeId)} />
        ) : hasNoGoals ? (
          <GoalEmptyState onCreate={openCreateGoal} />
        ) : (
          <>
            {/* The overview sizes up one agent's or project's goals; across
              every agent the list and its tabs already carry that. */}
            {!isGlobal && (
              <Flexbox className={styles.overview}>
                <Flexbox
                  horizontal
                  align={'center'}
                  gap={20}
                  justify={'space-between'}
                  wrap={'wrap'}
                >
                  <Flexbox gap={3}>
                    <Text fontSize={20} weight={600}>
                      {t('goalPage.title')}
                    </Text>
                    <Text type={'secondary'}>{t('goalPage.description')}</Text>
                  </Flexbox>
                  <Flexbox horizontal gap={20}>
                    <Flexbox className={styles.metric} gap={2}>
                      <Text fontSize={20} weight={600}>
                        {summary.total}
                      </Text>
                      <Text fontSize={12} type={'secondary'}>
                        {t('goalPage.metrics.total')}
                      </Text>
                    </Flexbox>
                    <Flexbox className={styles.metric} gap={2}>
                      <Text fontSize={20} weight={600}>
                        {summary.pursuing ?? '—'}
                      </Text>
                      <Text fontSize={12} type={'secondary'}>
                        {t('goalPage.metrics.pursuing')}
                      </Text>
                    </Flexbox>
                    <Flexbox className={styles.metric} gap={2}>
                      <Text fontSize={20} weight={600}>
                        {summary.delivered ?? '—'}
                      </Text>
                      <Text fontSize={12} type={'secondary'}>
                        {t('goalPage.metrics.delivered')}
                      </Text>
                    </Flexbox>
                  </Flexbox>
                </Flexbox>
              </Flexbox>
            )}
            <Flexbox gap={10}>
              {/* Wraps so the filters drop below the title on a phone instead of crushing it. */}
              <Flexbox horizontal align={'center'} gap={8} justify={'space-between'} wrap={'wrap'}>
                <Flexbox horizontal align={'center'} flex={'none'} gap={8}>
                  <Text fontSize={pageTitleFontSize} weight={600}>
                    {t('goalPage.listTitle')}
                  </Text>
                  <span className={styles.countBadge}>{visibleGoalCount}</span>
                </Flexbox>
                {/* On a phone the filters can outgrow the row; the edge fade shows it scrolls. */}
                <ScrollShadow
                  hideScrollBar
                  horizontal
                  align={'center'}
                  gap={8}
                  orientation={'horizontal'}
                  size={24}
                  style={{ maxWidth: '100%' }}
                >
                  <SearchBar
                    allowClear
                    aria-label={t('goalPage.searchPlaceholder')}
                    className={styles.search}
                    placeholder={t('goalPage.searchPlaceholder')}
                    size={'small'}
                    value={keyword}
                    variant={'filled'}
                    onInputChange={setKeyword}
                  />
                  {isGlobal && (
                    <GoalAgentFilter
                      options={agentFilterOptions}
                      value={agentFilter}
                      onChange={setAgentFilter}
                    />
                  )}
                  <Segmented
                    size={'small'}
                    value={filter}
                    options={[
                      {
                        label: t('goalPage.filter.all'),
                        value: 'all',
                      },
                      {
                        label: t('goalPage.filter.review'),
                        value: 'review',
                      },
                      {
                        label: t('goalPage.filter.running'),
                        value: 'running',
                      },
                      {
                        label: t('goalPage.filter.achieved'),
                        value: 'achieved',
                      },
                      {
                        label: t('goalPage.filter.archived'),
                        value: 'archived',
                      },
                    ]}
                    onChange={(value) => setFilter(value as GoalListFilter)}
                  />
                  <ActionIcon
                    icon={ListIcon}
                    size={'small'}
                    style={{ alignSelf: 'center' }}
                    title={t('goalPage.view.list')}
                    variant={viewMode === 'list' ? 'filled' : 'borderless'}
                    onClick={() => setViewMode('list')}
                  />
                  <ActionIcon
                    icon={LayoutGridIcon}
                    size={'small'}
                    style={{ alignSelf: 'center' }}
                    title={t('goalPage.view.card')}
                    variant={viewMode === 'card' ? 'filled' : 'borderless'}
                    onClick={() => setViewMode('card')}
                  />
                </ScrollShadow>
              </Flexbox>
              <div className={viewMode === 'card' ? styles.list : styles.listRows}>
                {isTabError ? (
                  <GoalLoadError onRetry={() => void refreshGoals(scopeId)} />
                ) : isTabPending ? (
                  <GoalListRowsSkeleton />
                ) : isFilterEmpty ? (
                  <Block padding={32} variant={'outlined'}>
                    <Empty
                      description={t('goalPage.filteredEmptyDescription')}
                      title={t('goalPage.filteredEmptyTitle')}
                    />
                  </Block>
                ) : isSearchEmpty ? (
                  <Block padding={32} variant={'outlined'}>
                    <Empty
                      description={t('goalPage.searchEmptyDescription')}
                      icon={SearchXIcon}
                      title={t('goalPage.searchEmptyTitle', { keyword: keyword.trim() })}
                    />
                  </Block>
                ) : (
                  searchedGoals
                    .slice(0, visibleLimit)
                    .map((item) => (
                      <GoalItem
                        globalScopeId={isGlobal ? scopeId : undefined}
                        goal={item}
                        key={item.goal.id}
                        projectId={projectId}
                      />
                    ))
                )}
              </div>
              {hasMoreGoals && <div aria-hidden className={styles.sentinel} ref={sentinelRef} />}
              {isSearchPartial && (
                <Text align={'center'} fontSize={12} type={'secondary'}>
                  {t('goalPage.searchPartial', {
                    loaded: filteredGoals.length,
                    total: tabTotal,
                  })}
                </Text>
              )}
            </Flexbox>
          </>
        )}
      </WideScreenContainer>
    </Flexbox>
  );
});

AgentGoalsPage.displayName = 'AgentGoalsPage';

export default AgentGoalsPage;