|
| 1 | +import { Tooltip } from "@components/ui/Tooltip"; |
| 2 | +import { useGitQueries } from "@features/git-interaction/hooks/useGitQueries"; |
| 3 | +import { computeDiffStats } from "@features/git-interaction/utils/diffStats"; |
| 4 | +import { useCwd } from "@features/sidebar/hooks/useCwd"; |
| 5 | +import { useCloudChangedFiles } from "@features/task-detail/hooks/useCloudChangedFiles"; |
| 6 | +import { useWorkspace } from "@features/workspace/hooks/useWorkspace"; |
| 7 | +import { GitDiff } from "@phosphor-icons/react"; |
| 8 | +import { Flex, Text } from "@radix-ui/themes"; |
| 9 | +import { |
| 10 | + formatHotkey, |
| 11 | + SHORTCUTS, |
| 12 | +} from "@renderer/constants/keyboard-shortcuts"; |
| 13 | +import { useReviewNavigationStore } from "@renderer/features/code-review/stores/reviewNavigationStore"; |
| 14 | +import type { Task } from "@shared/types"; |
| 15 | +import { useMemo } from "react"; |
| 16 | + |
| 17 | +interface DiffStatsBadgeProps { |
| 18 | + task: Task; |
| 19 | +} |
| 20 | + |
| 21 | +function useChangedFileStats(task: Task) { |
| 22 | + const taskId = task.id; |
| 23 | + const workspace = useWorkspace(taskId); |
| 24 | + const isCloud = |
| 25 | + workspace?.mode === "cloud" || task.latest_run?.environment === "cloud"; |
| 26 | + const repoPath = useCwd(taskId); |
| 27 | + |
| 28 | + const { diffStats: localDiffStats } = useGitQueries( |
| 29 | + isCloud ? undefined : repoPath, |
| 30 | + ); |
| 31 | + |
| 32 | + const { changedFiles: cloudFiles } = useCloudChangedFiles(taskId, task); |
| 33 | + |
| 34 | + return useMemo(() => { |
| 35 | + if (isCloud) { |
| 36 | + const stats = computeDiffStats(cloudFiles); |
| 37 | + return { |
| 38 | + filesChanged: stats.filesChanged, |
| 39 | + linesAdded: stats.linesAdded, |
| 40 | + linesRemoved: stats.linesRemoved, |
| 41 | + }; |
| 42 | + } |
| 43 | + return { |
| 44 | + filesChanged: localDiffStats.filesChanged, |
| 45 | + linesAdded: localDiffStats.linesAdded, |
| 46 | + linesRemoved: localDiffStats.linesRemoved, |
| 47 | + }; |
| 48 | + }, [isCloud, cloudFiles, localDiffStats]); |
| 49 | +} |
| 50 | + |
| 51 | +export function DiffStatsBadge({ task }: DiffStatsBadgeProps) { |
| 52 | + const taskId = task.id; |
| 53 | + const { filesChanged, linesAdded, linesRemoved } = useChangedFileStats(task); |
| 54 | + const reviewMode = useReviewNavigationStore( |
| 55 | + (s) => s.reviewModes[taskId] ?? "closed", |
| 56 | + ); |
| 57 | + const setReviewMode = useReviewNavigationStore((s) => s.setReviewMode); |
| 58 | + |
| 59 | + const hasChanges = filesChanged > 0; |
| 60 | + |
| 61 | + const isOpen = reviewMode !== "closed"; |
| 62 | + |
| 63 | + const handleClick = () => { |
| 64 | + setReviewMode(taskId, isOpen ? "closed" : "split"); |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <Tooltip |
| 69 | + content={isOpen ? "Close review panel" : "Open review panel"} |
| 70 | + shortcut={formatHotkey(SHORTCUTS.TOGGLE_REVIEW_PANEL)} |
| 71 | + side="bottom" |
| 72 | + > |
| 73 | + <button |
| 74 | + type="button" |
| 75 | + onClick={handleClick} |
| 76 | + className="no-drag hover:bg-[var(--gray-a3)]" |
| 77 | + style={{ |
| 78 | + display: "inline-flex", |
| 79 | + alignItems: "center", |
| 80 | + gap: "4px", |
| 81 | + padding: "0 6px", |
| 82 | + height: "24px", |
| 83 | + borderRadius: "var(--radius-1)", |
| 84 | + border: "none", |
| 85 | + cursor: "pointer", |
| 86 | + fontSize: "11px", |
| 87 | + fontFamily: "var(--code-font-family)", |
| 88 | + background: isOpen ? "var(--gray-a3)" : "transparent", |
| 89 | + color: "var(--gray-11)", |
| 90 | + transition: "background 0.1s", |
| 91 | + }} |
| 92 | + > |
| 93 | + <GitDiff size={14} style={{ flexShrink: 0 }} /> |
| 94 | + {hasChanges ? ( |
| 95 | + <Flex align="center" gap="1"> |
| 96 | + {linesAdded > 0 && ( |
| 97 | + <Text style={{ color: "var(--green-9)", fontSize: "11px" }}> |
| 98 | + +{linesAdded} |
| 99 | + </Text> |
| 100 | + )} |
| 101 | + {linesRemoved > 0 && ( |
| 102 | + <Text style={{ color: "var(--red-9)", fontSize: "11px" }}> |
| 103 | + -{linesRemoved} |
| 104 | + </Text> |
| 105 | + )} |
| 106 | + </Flex> |
| 107 | + ) : ( |
| 108 | + <Text style={{ color: "var(--gray-9)", fontSize: "11px" }}>0</Text> |
| 109 | + )} |
| 110 | + </button> |
| 111 | + </Tooltip> |
| 112 | + ); |
| 113 | +} |
0 commit comments