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.3 KB
Raw Blame
'use client';

import type { DeviceGitLinkedPullRequest } from '@lobechat/types';
import { Button, Center, Empty, Flexbox, toast } from '@lobehub/ui';
import { SkillsIcon } from '@lobehub/ui/icons';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import {
  ArrowDownIcon,
  ArrowUpIcon,
  BoxesIcon,
  ClipboardListIcon,
  FilesIcon,
  FileTextIcon,
  GitBranchIcon,
  GitForkIcon,
  LaptopIcon,
} from 'lucide-react';
import { memo, useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import AsyncError from '@/components/AsyncError';
import RingLoadingIcon from '@/components/RingLoading';
import ThreadListSection from '@/features/AgentSidebar/Topic/TopicListContent/ThreadList/ThreadListSection';
import { TopicBackgroundActivity } from '@/features/BackgroundActivity/TopicSection';
import BranchSwitcher from '@/features/ChatInput/ControlBar/BranchSwitcher';
import WorktreeSwitcher from '@/features/ChatInput/ControlBar/WorktreeSwitcher';
import { getAllWorkSummaries } from '@/features/Conversation/store/slices/data/workSummaries';
import type { TopicPullRequestView } from '@/features/TopicPullRequests/pullRequestView';
import WorkSummaryCard from '@/features/Work/WorkSummaryCard';
import { gitService } from '@/services/git';
import { useAgentStore } from '@/store/agent';
import { agentSelectors } from '@/store/agent/selectors';
import { useChatStore } from '@/store/chat';
import { dbMessageSelectors, topicSelectors } from '@/store/chat/selectors';
import {
  useFetchGitAheadBehind,
  useFetchGitBranch,
  useFetchGitWorktrees,
  useReviewPatches,
} from '@/store/device';
import { useUserStore } from '@/store/user';
import { labPreferSelectors } from '@/store/user/selectors';
import { sidebarRow } from '@/styles/density.stylex';

import { SidebarRowSkeleton } from '../DeferredSkeleton';
import GoalSection from '../GoalSection';
import ProgressSection from '../ProgressSection';
import { toLinkedPullRequest } from '../PullRequest/useSidebarPullRequests';
import { BackgroundTasksSection } from './BackgroundTasks';
import DeviceStatusSection from './DeviceStatusSection';
import KeptWorktreeRows, { RemovedWorktreeRow, useTopicWorktrees } from './KeptWorktreeRows';
import { LinkPullRequestRow } from './LinkPullRequestRow';
import {
  collectChangeStats,
  isLinkedWorktreeCheckout,
  shouldShowGitSkeleton,
} from './overviewData';
import OverviewHeader from './OverviewHeader';
import { ChevronRight, OverviewRow, PickerGlyph, rowStyles } from './OverviewRow';
import PortSwitcher from './PortSwitcher';
import PullRequestRow from './PullRequestRow';
import PullRequestRowMenu from './PullRequestRowMenu';
import { SectionHeader } from './SectionHeader';
import { sectionProps } from './sectionStyles';
import { TerminalRow } from './TerminalRow';

const styles = stylex.create({
  body: {
    overflowY: 'auto',
    paddingBlockStart: sidebarRow.paneBlockStart,
    paddingBlockEnd: sidebarRow.paneBlockEnd,
    paddingInline: sidebarRow.paneInline,
  },
});

interface OverviewProps {
  active: boolean;
  agentId?: string;
  /** Routes file / git RPCs; empty for "This device", which reads over IPC. */
  deviceId?: string;
  environmentAvailable: boolean;
  /** Remove one of {@link pullRequests} from the topic. */
  onDismissPullRequest?: (pullRequest: TopicPullRequestView) => Promise<void>;
  /** Link a PR by URL; resolves to its `owner/repo#n`. */
  onLinkPullRequest?: (url: string) => Promise<string | undefined>;
  /** Open one of {@link pullRequests}. */
  onOpenPullRequest?: (pullRequest: TopicPullRequestView) => void;
  onOpenTab: (tab: string) => void;
  onRefreshPullRequest: () => Promise<unknown>;
  /** Parent-owned so the Overview row and the on-demand PR tab cannot disagree. */
  pullRequest?: DeviceGitLinkedPullRequest | null;
  /**
   * Every PR the topic's Work rows link, listed
   * under Changes in place of the single branch PR.
   */
  pullRequests?: TopicPullRequestView[];
  repoType?: string;
  sourcePath?: string;
  /** The bound machine whose live status to show, local desktop included. */
  statusDeviceId?: string;
  workingDirectory?: string;
}

const pathBasename = (path: string) => path.replaceAll('\\', '/').split('/').findLast(Boolean);

const Overview = memo<OverviewProps>(
  ({
    active,
    agentId,
    deviceId,
    environmentAvailable,
    onDismissPullRequest,
    onLinkPullRequest,
    onOpenPullRequest,
    onOpenTab,
    onRefreshPullRequest,
    pullRequest,
    pullRequests,
    repoType,
    sourcePath,
    statusDeviceId,
    workingDirectory,
  }) => {
    const { t } = useTranslation('chat');
    const { t: tDevice } = useTranslation('device');
    const isHetero = useAgentStore(agentSelectors.isCurrentAgentHeterogeneous);
    const tunnelsEnabled = useUserStore(labPreferSelectors.enableDeviceTunnel);
    const topicId = useChatStore((s) => s.activeTopicId);
    const topicStatus = useChatStore((s) => topicSelectors.currentActiveTopic(s)?.status);
    const topicMetadata = useChatStore((s) => topicSelectors.currentActiveTopic(s)?.metadata);
    const threadId = useChatStore((s) => s.activeThreadId);
    const works = useChatStore((s) =>
      getAllWorkSummaries(dbMessageSelectors.activeDbMessages(s), threadId),
    );

    const topicWorktrees = useTopicWorktrees({
      metadata: topicMetadata,
      status: topicStatus,
      topicId: topicId ?? undefined,
      workingDirectory,
    });
    // A worktree the cleanup removed has no folder left to read git from.
    const worktreeRemoved = !!topicWorktrees.removed;
    const gitPath = active && repoType && !worktreeRemoved ? workingDirectory : undefined;
    const isGithub = repoType === 'github';
    const {
      data: branchData,
      error: branchError,
      isConfirmed: branchConfirmed,
      isLoading: branchLoading,
      mutate: mutateBranch,
    } = useFetchGitBranch(deviceId, gitPath);
    const branch = branchData?.branch;
    const detached = branchData?.detached;
    const {
      data: aheadBehind,
      error: aheadBehindError,
      isConfirmed: aheadBehindConfirmed,
      mutate: mutateAheadBehind,
    } = useFetchGitAheadBehind(deviceId, gitPath);
    const {
      data: reviewData,
      error: reviewError,
      isLoading: reviewLoading,
      mutate: mutateReview,
    } = useReviewPatches(gitPath, 'unstaged', undefined, deviceId, active);
    const {
      data: worktreesData,
      isConfirmed: worktreesConfirmed,
      isLoading: worktreesLoading,
      mutate: mutateWorktrees,
    } = useFetchGitWorktrees(deviceId, gitPath);
    const worktrees = worktreesData ?? [];
    const [switcherOpen, setSwitcherOpen] = useState(false);
    const [pulling, setPulling] = useState(false);
    const [pushing, setPushing] = useState(false);

    const changeStats = useMemo(() => collectChangeStats(reviewData), [reviewData]);

    const gitError = branchError || aheadBehindError || reviewError;
    // Stale-first: the small git reads are replicas (`@/store/device/gitStatus`),
    // so after a reload they paint from the persisted copy while the rest (e.g.
    // the diff patches) are still in flight. Only fall back to the skeleton when
    // nothing at all is there yet; rows whose own read is pending render a
    // placeholder instead.
    const isGitLoading = shouldShowGitSkeleton({
      hasData:
        branchData !== undefined ||
        aheadBehind !== undefined ||
        reviewData !== undefined ||
        worktreesData !== undefined,
      loading: branchLoading || reviewLoading || worktreesLoading,
    });
    const changesPending = reviewData === undefined && reviewLoading;
    const visibleWorks = works.slice(0, 3);
    const directoryName = workingDirectory ? pathBasename(workingDirectory) : undefined;
    const hasWorkspace = environmentAvailable && !!workingDirectory;
    const showEmptyWorkspace = environmentAvailable && !workingDirectory;

    const isLinkedWorktree = isLinkedWorktreeCheckout(workingDirectory, worktrees);

    const refreshGit = useCallback(async () => {
      await Promise.all([
        mutateBranch(),
        mutateAheadBehind(),
        mutateReview(),
        mutateWorktrees(),
        onRefreshPullRequest(),
      ]);
    }, [mutateBranch, mutateAheadBehind, mutateReview, mutateWorktrees, onRefreshPullRequest]);

    // Flip the displayed branch instantly on checkout; the switcher's
    // onAfterCheckout reconciles once the checkout lands (same as GitStatus).
    const handleOptimisticCheckout = useCallback(
      (nextBranch: string) => {
        void mutateBranch({ branch: nextBranch, detached: false }, { revalidate: false });
      },
      [mutateBranch],
    );

    const syncBusy = pulling || pushing;
    // The branch and its counts may be stale-first paints from the last
    // session. Checkout, pull and push assume both still describe the live
    // branch, so they stay inert until this session confirms both reads — a
    // faster branch probe must not enable sync against the previous branch's
    // ahead/behind snapshot.
    const actionsBlocked = syncBusy || !branchConfirmed || !aheadBehindConfirmed;

    const handlePull = useCallback(async () => {
      if (syncBusy || !workingDirectory) return;
      setPulling(true);
      try {
        const result = await gitService.pullGitBranch({ deviceId, path: workingDirectory });
        if (result.success) {
          if (result.noop) {
            toast.info(tDevice('workingDirectory.pullNoop'));
          } else {
            toast.success(tDevice('workingDirectory.pullSuccess'));
          }
          await refreshGit();
        } else {
          toast.error(result.error || tDevice('workingDirectory.pullFailed'));
        }
      } finally {
        setPulling(false);
      }
    }, [deviceId, refreshGit, syncBusy, tDevice, workingDirectory]);

    const handlePush = useCallback(async () => {
      if (syncBusy || !workingDirectory) return;
      setPushing(true);
      try {
        const result = await gitService.pushGitBranch({ deviceId, path: workingDirectory });
        if (result.success) {
          if (result.noop) {
            toast.info(tDevice('workingDirectory.pushNoop'));
          } else {
            toast.success(tDevice('workingDirectory.pushSuccess'));
          }
          await refreshGit();
        } else {
          toast.error(result.error || tDevice('workingDirectory.pushFailed'));
        }
      } finally {
        setPushing(false);
      }
    }, [deviceId, refreshGit, syncBusy, tDevice, workingDirectory]);

    const showAhead = !!aheadBehind?.hasUpstream && aheadBehind.ahead > 0;
    const showBehind = !!aheadBehind?.hasUpstream && aheadBehind.behind > 0;

    // The branch picker routes through the worktree list, so both reads must be
    // confirmed before either picker may act on them.
    const pickersReady = branchConfirmed && worktreesConfirmed;
    const canSwitchBranch = !detached && !!agentId && !!workingDirectory && pickersReady;
    const branchRow = (
      <OverviewRow
        icon={GitBranchIcon}
        interactive={canSwitchBranch}
        title={detached ? tDevice('workingDirectory.detachedHead', { sha: branch ?? '' }) : branch}
        trailing={canSwitchBranch ? <PickerGlyph /> : undefined}
        value={
          detached ? (
            <>
              <span {...stylex.props(rowStyles.num)}>{branch}</span>
              {t('workingPanel.overview.branch.detached')}
            </>
          ) : (
            branch
          )
        }
      />
    );

    // A topic's PRs are server records: they stay listed with
    // their last known state even when the working directory cannot be read.
    const topicPullRequestRows = pullRequests && (
      <>
        {pullRequests.map((view) => (
          <PullRequestRow
            key={view.identifier}
            pullRequest={toLinkedPullRequest(view)}
            tooltip={`${view.repoFullName}#${view.number} ${view.title}`}
            unvouched={!view.vouched}
            actions={
              <PullRequestRowMenu
                topicId={topicId}
                view={view}
                onDismiss={
                  onDismissPullRequest &&
                  (() =>
                    void onDismissPullRequest(view).then(
                      () =>
                        toast.info(
                          t('workingPanel.overview.pr.dismissed', { name: `#${view.number}` }),
                        ),
                      () =>
                        toast.error(
                          t('workingPanel.overview.pr.dismissFailed', { name: `#${view.number}` }),
                        ),
                    ))
                }
              />
            }
            onClick={() => onOpenPullRequest?.(view)}
          />
        ))}
        {onLinkPullRequest && <LinkPullRequestRow onLink={onLinkPullRequest} />}
      </>
    );

    const gitRows = (
      <>
        {branch &&
          (canSwitchBranch ? (
            <BranchSwitcher
              agentId={agentId}
              currentBranch={branch}
              deviceId={deviceId}
              isGithub={isGithub}
              open={switcherOpen}
              path={workingDirectory}
              placement={'bottomLeft'}
              sourcePath={sourcePath ?? workingDirectory}
              worktrees={worktrees}
              onAfterCheckout={() => void refreshGit()}
              onExternalRefresh={refreshGit}
              onOpenChange={setSwitcherOpen}
              onOptimisticCheckout={handleOptimisticCheckout}
            >
              {branchRow}
            </BranchSwitcher>
          ) : (
            branchRow
          ))}

        {showBehind && (
          <OverviewRow
            icon={ArrowDownIcon}
            iconColor={cssVar.colorError}
            value={t('workingPanel.overview.sync.behind', { count: aheadBehind!.behind })}
            trailing={
              pulling ? (
                <RingLoadingIcon size={12} />
              ) : (
                <span {...stylex.props(rowStyles.rowAction)}>
                  {t('workingPanel.overview.sync.pull')}
                </span>
              )
            }
            onClick={actionsBlocked ? undefined : handlePull}
          />
        )}
        {showAhead && (
          <OverviewRow
            icon={ArrowUpIcon}
            iconColor={cssVar.colorInfo}
            value={t('workingPanel.overview.sync.ahead', { count: aheadBehind!.ahead })}
            trailing={
              pushing ? (
                <RingLoadingIcon size={12} />
              ) : (
                <span {...stylex.props(rowStyles.rowAction)}>
                  {t('workingPanel.overview.sync.push')}
                </span>
              )
            }
            onClick={actionsBlocked ? undefined : handlePush}
          />
        )}

        {isLinkedWorktree && branch && workingDirectory && agentId && (
          <WorktreeSwitcher
            agentId={agentId}
            currentBranch={branch}
            detached={detached}
            deviceId={deviceId}
            disabled={!pickersReady}
            isGithub={isGithub}
            path={workingDirectory}
            placement={'bottomLeft'}
            sourcePath={sourcePath ?? workingDirectory}
            worktrees={worktrees}
            onWorktreesChange={mutateWorktrees}
          >
            <OverviewRow
              icon={GitForkIcon}
              interactive={pickersReady}
              title={sourcePath ?? workingDirectory}
              trailing={pickersReady ? <PickerGlyph /> : undefined}
              value={t('workingPanel.overview.worktree.of', {
                name: pathBasename(sourcePath ?? workingDirectory),
              })}
            />
          </WorktreeSwitcher>
        )}

        <OverviewRow
          icon={ClipboardListIcon}
          trailing={
            changesPending ? (
              <RingLoadingIcon size={12} />
            ) : changeStats.files > 0 ? (
              <>
                <span {...stylex.props(rowStyles.changeAdditions)}>+{changeStats.additions}</span>
                <span {...stylex.props(rowStyles.changeDeletions)}>−{changeStats.deletions}</span>
              </>
            ) : (
              t('workingPanel.overview.changes.none')
            )
          }
          value={
            changeStats.files > 0
              ? t('workingPanel.pr.reason.localDirty', { count: changeStats.files })
              : t('workingPanel.overview.changes')
          }
          onClick={() => onOpenTab('review')}
        />

        {!pullRequests && pullRequest && (
          <PullRequestRow pullRequest={pullRequest} onClick={() => onOpenTab('pr')} />
        )}
      </>
    );

    const workspaceSection = repoType ? (
      isGitLoading ? (
        <SidebarRowSkeleton rows={3} />
      ) : gitError ? (
        <AsyncError
          align={'start'}
          error={gitError}
          title={t('workingPanel.overview.environmentError')}
          variant={'inline'}
          onRetry={() => void refreshGit()}
        />
      ) : (
        gitRows
      )
    ) : null;

    return (
      <Flexbox className={stylex.props(styles.body).className}>
        {hasWorkspace && (
          <>
            <OverviewHeader
              deviceId={deviceId}
              error={!!gitError}
              name={directoryName!}
              path={workingDirectory}
              repoType={repoType}
              onClick={() => onOpenTab('files')}
            />
            <Flexbox {...sectionProps()}>
              {/* A worktree the archive cleanup kept for its uncommitted work. */}
              <KeptWorktreeRows
                entries={topicWorktrees.entries}
                topicId={topicId ?? undefined}
                onChange={topicWorktrees.mutate}
              />
              {topicWorktrees.removed ? (
                <RemovedWorktreeRow entry={topicWorktrees.removed} />
              ) : (
                workspaceSection
              )}
              {(repoType || !!pullRequests?.length) && topicPullRequestRows}
              {/* Outside the git rows: a dev server runs in plain folders too. */}
              {tunnelsEnabled && deviceId && (
                <PortSwitcher
                  active={active}
                  deviceId={deviceId}
                  workingDirectory={workingDirectory}
                />
              )}
            </Flexbox>
          </>
        )}
        {!hasWorkspace && !!pullRequests?.length && (
          <Flexbox {...sectionProps()}>{topicPullRequestRows}</Flexbox>
        )}

        {/* The bound machine's live state, as its device card shows it. */}
        {statusDeviceId && <DeviceStatusSection active={active} deviceId={statusDeviceId} />}

        <TopicBackgroundActivity topicId={topicId} />
        {showEmptyWorkspace && (
          <Center paddingBlock={24}>
            <Empty
              description={t('workingPanel.overview.workspace.emptyDesc')}
              icon={LaptopIcon}
              title={t('workingPanel.overview.workspace.empty')}
            />
          </Center>
        )}

        <GoalSection className={sectionProps(showEmptyWorkspace).className} />
        <ProgressSection className={sectionProps(showEmptyWorkspace).className} />

        {visibleWorks.length > 0 && (
          <Flexbox {...sectionProps(showEmptyWorkspace)}>
            <SectionHeader
              title={t('workingPanel.overview.outputs')}
              actions={
                <Button size={'small'} type={'text'} onClick={() => onOpenTab('works')}>
                  {t('workingPanel.overview.viewAll')}
                </Button>
              }
            />
            {visibleWorks.map((work) => (
              <WorkSummaryCard item={work} key={work.id} variant={'inline'} />
            ))}
          </Flexbox>
        )}

        {!environmentAvailable && !topicId && visibleWorks.length === 0 && (
          <Center {...sectionProps()} paddingBlock={24}>
            <Empty
              description={t('workingPanel.overview.empty')}
              icon={BoxesIcon}
              title={t('workingPanel.overview.emptyTitle')}
            />
          </Center>
        )}

        {/* Claude Code background tasks; async subagents stay in the thread list below. */}
        <BackgroundTasksSection
          afterBreak={showEmptyWorkspace}
          deviceId={deviceId}
          outputReadable={environmentAvailable}
        />

        {/* Thread rows open in the Portal; the list labels itself as subagents and
            sits in its own section so the resource rows below aren't grouped under it. */}
        {topicId && <ThreadListSection afterBreak={showEmptyWorkspace} topicId={topicId} />}

        <Flexbox {...sectionProps(showEmptyWorkspace)}>
          {hasWorkspace && (
            <OverviewRow
              icon={FilesIcon}
              trailing={<ChevronRight />}
              value={t('workingPanel.overview.files')}
              onClick={() => onOpenTab('files')}
            />
          )}
          <OverviewRow
            icon={SkillsIcon}
            trailing={<ChevronRight />}
            value={t('workingPanel.resources.filter.skills')}
            onClick={() => onOpenTab('skills')}
          />
          {!isHetero && (
            <OverviewRow
              icon={FileTextIcon}
              trailing={<ChevronRight />}
              value={t('workingPanel.resources.filter.documents')}
              onClick={() => onOpenTab('documents')}
            />
          )}
          <TerminalRow />
        </Flexbox>
      </Flexbox>
    );
  },
);

Overview.displayName = 'WorkingSidebarOverview';

export default Overview;