messageChangeSummaryPreview.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import type { ZCodePersistedFileChange } from "@zcode/shared";
  2. import type { CodeViewerSource } from "@/lib/codeViewer.js";
  3. import type { CodeViewerWorkspaceScope } from "@/lib/codeViewerWorkspaceScope.js";
  4. export function buildChangeSummaryDiffViewerSource({
  5. patch,
  6. path,
  7. snapshot,
  8. relativePath,
  9. workspacePath,
  10. workspaceIdentity,
  11. workspaceRemoteSessionId,
  12. }: {
  13. patch?: string;
  14. path?: string;
  15. snapshot: ZCodePersistedFileChange["snapshots"][number] | undefined;
  16. relativePath: string;
  17. } & CodeViewerWorkspaceScope): CodeViewerSource | undefined {
  18. const workspaceScope = {
  19. ...(workspacePath ? { workspacePath } : {}),
  20. ...(workspaceIdentity ? { workspaceIdentity } : {}),
  21. ...(workspaceRemoteSessionId ? { workspaceRemoteSessionId } : {}),
  22. };
  23. if (patch) {
  24. return {
  25. type: "patch",
  26. title: relativePath,
  27. path: path ?? snapshot?.path,
  28. patch,
  29. ...workspaceScope,
  30. };
  31. }
  32. if (!snapshot) {
  33. return undefined;
  34. }
  35. return {
  36. type: "multi-file-diff",
  37. title: relativePath,
  38. path: snapshot.path,
  39. beforeContent: snapshot.beforeContent ?? "",
  40. afterContent: snapshot.afterContent,
  41. ...workspaceScope,
  42. };
  43. }
  44. export function buildChangeSummaryFilePreviewSource(input: {
  45. path: string;
  46. relativePath: string;
  47. workspacePath: string;
  48. } & CodeViewerWorkspaceScope): CodeViewerSource {
  49. const { path, relativePath, workspacePath, workspaceIdentity, workspaceRemoteSessionId } = input;
  50. return {
  51. type: "file",
  52. title: relativePath,
  53. path,
  54. workspacePath,
  55. ...(workspaceIdentity ? { workspaceIdentity } : {}),
  56. ...(workspaceRemoteSessionId ? { workspaceRemoteSessionId } : {}),
  57. };
  58. }
  59. export function openChangeSummaryDiffViewer(
  60. source: CodeViewerSource | undefined,
  61. onOpenCodeViewer: ((source: CodeViewerSource) => void) | undefined,
  62. ) {
  63. if (!source || !onOpenCodeViewer) {
  64. return false;
  65. }
  66. onOpenCodeViewer(source);
  67. return true;
  68. }