MessageChangeSummaryPanel.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { useMemo, useState, type CSSProperties } from "react";
  2. import type {
  3. ZCodePersistedFileChange,
  4. ZCodeTaskChangeSummary,
  5. ZCodeTurnFileState,
  6. } from "@zcode/shared";
  7. import { TID_CHAT_CHANGE_SUMMARY_TOGGLE_FILES_BUTTON, testId } from "@zcode/shared";
  8. import { ChevronRightIcon, Undo2 } from "lucide-react";
  9. import {
  10. Collapsible,
  11. CollapsibleContent,
  12. CollapsibleTrigger,
  13. } from "@/components/ui/collapsible.js";
  14. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  15. import type { CodeViewerSource } from "@/lib/codeViewer.js";
  16. import { FileDisplayInline } from "@/lib/fileDisplay.js";
  17. import { buildTurnChangeSummary, toWorkspaceRelativePath } from "@/lib/taskChangeSummary.js";
  18. import { Button } from "@/components/ui/button.js";
  19. import { OpenSplitButton } from "@/OpenSplitButton.js";
  20. import { cn } from "@/components/lib/utils.js";
  21. import {
  22. buildChangeSummaryDiffViewerSource,
  23. buildChangeSummaryFilePreviewSource,
  24. openChangeSummaryDiffViewer,
  25. } from "@/messageChangeSummaryPreview.js";
  26. export {
  27. buildChangeSummaryDiffViewerSource,
  28. buildChangeSummaryFilePreviewSource,
  29. openChangeSummaryDiffViewer,
  30. } from "@/messageChangeSummaryPreview.js";
  31. interface MessageChangeSummaryPanelProps {
  32. summary: ZCodeTaskChangeSummary;
  33. workspacePath: string;
  34. workspaceIdentity?: string;
  35. workspaceRemoteSessionId?: string;
  36. changeDetails?: ZCodePersistedFileChange | null;
  37. readonlyDiffPatchesByPath?: ReadonlyMap<string, string>;
  38. fileState?: ZCodeTurnFileState;
  39. messageId?: string;
  40. compactForRemoteControl?: boolean;
  41. animationDelayMs?: number;
  42. // M5 ③-4:onPreviewFilesRewind/onApplyFilesRewind 已随 session/previewFileRewind、
  43. // session/applyFileRewind 旧协议词删除(全仓无 caller 传入);文件撤销回退只剩
  44. // onToggleFiles 回调(数据面与协议无关)。
  45. onToggleFiles?: () => void;
  46. onOpenCodeViewer?: (source: CodeViewerSource) => void;
  47. }
  48. export function MessageChangeSummaryPanel({
  49. summary,
  50. workspacePath,
  51. workspaceIdentity,
  52. workspaceRemoteSessionId,
  53. changeDetails,
  54. readonlyDiffPatchesByPath,
  55. fileState = "applied",
  56. messageId,
  57. compactForRemoteControl = false,
  58. animationDelayMs = 0,
  59. onToggleFiles,
  60. onOpenCodeViewer,
  61. }: MessageChangeSummaryPanelProps) {
  62. const { intl } = useZCodeIntl();
  63. const [isFileListOpen, setIsFileListOpen] = useState(false);
  64. const turnSummary = useMemo(() => {
  65. // Bugfix: 某些链路传进来的 summary 可能还是 task/session 级汇总。
  66. // 面板已经拿到了当前轮 changeDetails,优先从单轮快照重算,避免历史轮次文件混进当前消息。
  67. return buildTurnChangeSummary(changeDetails) ?? summary;
  68. }, [changeDetails, summary]);
  69. const filesChangedLabel = intl.formatMessage(
  70. {
  71. id:
  72. turnSummary.fileCount === 1
  73. ? "chat.changeSummary.filesChanged.one"
  74. : "chat.changeSummary.filesChanged.other",
  75. },
  76. { count: String(turnSummary.fileCount) },
  77. );
  78. // Bugfix: 变更摘要以前只有聚合计数,文件行点击后拿不到对应的 before/after。
  79. // 这里按 path 建索引,把同一轮持久化快照重新接回 UI,文件列表才能展开真正的 diff。
  80. const snapshotsByPath = useMemo(
  81. () => new Map((changeDetails?.snapshots ?? []).map((snapshot) => [snapshot.path, snapshot])),
  82. [changeDetails],
  83. );
  84. const isFileRewindApplied = fileState === "reverted";
  85. const toggleActionLabel = intl.formatMessage({
  86. id: isFileRewindApplied
  87. ? "chat.changeSummary.reverted"
  88. : "chat.changeSummary.rewind",
  89. });
  90. const panelToggleLabel = intl.formatMessage({
  91. id: isFileListOpen ? "chat.changeSummary.collapse" : "chat.changeSummary.expand",
  92. });
  93. const reviewDiffLabel = intl.formatMessage({ id: "chat.changeSummary.review" });
  94. const canRequestFileRewind = Boolean(onToggleFiles);
  95. return (
  96. <>
  97. <Collapsible
  98. open={isFileListOpen}
  99. onOpenChange={setIsFileListOpen}
  100. // Bugfix: 历史消息里的文件卡会等二次校验后再出现;
  101. // summary 如果没有同样的渐入,会在文件卡稳定前后显得像突兀跳出。
  102. data-zcode-stream-animate="true"
  103. className="overflow-hidden rounded-xl border border-border bg-card shadow-none"
  104. style={
  105. {
  106. "--zcode-stream-animation-delay": `${animationDelayMs}ms`,
  107. } as CSSProperties
  108. }
  109. >
  110. {/* Bugfix: 展开/收起触发器之前只给内部小按钮加 hover 背景,
  111. 收起态看起来像 header 里漂着一块。hover 视觉提升到整行 header,
  112. 但仍只让左侧标题区域负责 toggle,避免右侧撤销按钮误触发折叠。 */}
  113. <div className="flex h-10 items-center justify-between gap-3 px-2 transition-colors hover:bg-hover">
  114. <CollapsibleTrigger asChild>
  115. <button
  116. type="button"
  117. aria-label={panelToggleLabel}
  118. title={panelToggleLabel}
  119. className="flex h-full min-w-0 flex-1 items-center gap-2 px-1 text-left"
  120. >
  121. <ChevronRightIcon
  122. className={`size-3.5 shrink-0 text-foreground-subtlest transition-transform ${
  123. isFileListOpen ? "rotate-90" : ""
  124. }`}
  125. aria-hidden="true"
  126. />
  127. <span className="min-w-0 truncate text-ui-base font-medium text-foreground">
  128. {filesChangedLabel}
  129. </span>
  130. <span className="shrink-0 tabular-nums text-ui-base">
  131. <span className="text-diff-added">+{turnSummary.added}</span>{" "}
  132. <span className="text-diff-removed">-{turnSummary.removed}</span>
  133. </span>
  134. </button>
  135. </CollapsibleTrigger>
  136. {canRequestFileRewind && turnSummary.fileCount > 0 && changeDetails ? (
  137. <div className="flex items-center gap-1.5 text-ui-base text-foreground-subtle">
  138. <Button
  139. type="button"
  140. variant="ghost"
  141. size="sm"
  142. data-testid={
  143. messageId
  144. ? testId(TID_CHAT_CHANGE_SUMMARY_TOGGLE_FILES_BUTTON, messageId)
  145. : TID_CHAT_CHANGE_SUMMARY_TOGGLE_FILES_BUTTON
  146. }
  147. data-message-id={messageId}
  148. aria-label={toggleActionLabel}
  149. title={toggleActionLabel}
  150. disabled={isFileRewindApplied}
  151. onClick={() => onToggleFiles?.()}
  152. className="h-7 gap-1.5 px-2 text-ui-base text-foreground-subtle hover:text-foreground"
  153. >
  154. <span>{toggleActionLabel}</span>
  155. <Undo2 className="size-3.5" />
  156. </Button>
  157. </div>
  158. ) : null}
  159. </div>
  160. <CollapsibleContent>
  161. {/* Bugfix: 变更摘要整体之前没有折叠态,完成任务后会默认展开所有文件行。
  162. 默认闭合可避免大批文件变更把最新回复向上挤开;单文件 diff 仍保持按需展开。 */}
  163. {turnSummary.files.map((file) => {
  164. const relativePath = toWorkspaceRelativePath(workspacePath, file.path);
  165. const snapshot = snapshotsByPath.get(file.path);
  166. const readonlyPatch = readonlyDiffPatchesByPath?.get(file.path);
  167. const diffViewerSource = buildChangeSummaryDiffViewerSource({
  168. patch: readonlyPatch,
  169. path: file.path,
  170. snapshot,
  171. relativePath,
  172. workspacePath,
  173. workspaceIdentity,
  174. workspaceRemoteSessionId,
  175. });
  176. const filePreviewSource = buildChangeSummaryFilePreviewSource({
  177. path: file.path,
  178. relativePath,
  179. workspacePath,
  180. workspaceIdentity,
  181. workspaceRemoteSessionId,
  182. });
  183. const canOpenDiff = Boolean(diffViewerSource && onOpenCodeViewer);
  184. const openDiffViewer = () => {
  185. openChangeSummaryDiffViewer(diffViewerSource, onOpenCodeViewer);
  186. };
  187. return (
  188. <div key={file.path} className="w-full bg-background/50 overflow-hidden">
  189. <div
  190. aria-disabled={!canOpenDiff}
  191. className={cn(
  192. "flex w-full items-center gap-1 px-2 py-2 text-left transition-colors",
  193. canOpenDiff ? "cursor-pointer hover:bg-hover/30" : "cursor-default",
  194. )}
  195. onClick={openDiffViewer}
  196. onKeyDown={(event) => {
  197. if (event.key !== "Enter" && event.key !== " ") {
  198. return;
  199. }
  200. event.preventDefault();
  201. openDiffViewer();
  202. }}
  203. role="button"
  204. tabIndex={canOpenDiff ? 0 : -1}
  205. title={file.path}
  206. >
  207. <div className="flex min-w-0 flex-1 items-center gap-2">
  208. <div className="min-w-0 flex items-center">
  209. <FileDisplayInline
  210. path={file.path}
  211. options={{
  212. basePath: workspacePath,
  213. showFilePath: true,
  214. className: "inline-flex min-w-0 max-w-full items-center gap-1.5",
  215. fileNameClassName: "truncate text-ui-base font-medium text-foreground",
  216. filePathClassName: "truncate text-ui-base text-foreground-subtlest",
  217. }}
  218. />
  219. </div>
  220. <span className="flex shrink-0 items-center gap-2 tabular-nums text-ui-base">
  221. {file.added > 0 ? <span className="text-diff-added">+{file.added}</span> : null}
  222. {file.removed > 0 ? (
  223. <span className="text-diff-removed">-{file.removed}</span>
  224. ) : null}
  225. </span>
  226. </div>
  227. <div className="flex shrink-0 items-center gap-1.5">
  228. <Button
  229. type="button"
  230. variant="outline"
  231. size="default"
  232. aria-label={reviewDiffLabel}
  233. title={reviewDiffLabel}
  234. disabled={!canOpenDiff}
  235. className="h-7 gap-1.5 rounded-lg bg-input px-2 text-ui-base"
  236. onClick={(event) => {
  237. event.stopPropagation();
  238. openDiffViewer();
  239. }}
  240. onPointerDown={(event) => event.stopPropagation()}
  241. >
  242. <span>{reviewDiffLabel}</span>
  243. </Button>
  244. <OpenSplitButton
  245. target={{
  246. type: "file",
  247. path: file.path,
  248. title: relativePath,
  249. label: relativePath,
  250. previewSource: filePreviewSource,
  251. }}
  252. onOpenCodeViewer={onOpenCodeViewer}
  253. // Bugfix: 手机远控只需要在应用内预览 diff 文件,不需要桌面端的“选择 App 打开”菜单。
  254. // 隐藏下拉区可以减少窄屏误触,同时保留主“打开”按钮的预览能力。
  255. hideOpenWithMenu={compactForRemoteControl}
  256. stopPropagation
  257. />
  258. </div>
  259. </div>
  260. </div>
  261. );
  262. })}
  263. </CollapsibleContent>
  264. </Collapsible>
  265. </>
  266. );
  267. }