previewPaneCodeReview.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import type {
  2. CodeCommentPreview,
  3. CodeCommentRange,
  4. } from "@/lib/codeCommentContext.js";
  5. import type { CodeReviewCodeViewerSource } from "@/lib/codeViewer.js";
  6. export interface CodeReviewContentProjection {
  7. focusedRange: CodeCommentRange | null;
  8. inlineComments: readonly CodeCommentPreview[];
  9. targetLineOutOfRange: boolean;
  10. topComment: CodeCommentPreview | null;
  11. }
  12. export function resolveCodeReviewContentProjection(
  13. source: CodeReviewCodeViewerSource,
  14. content: string,
  15. ): CodeReviewContentProjection {
  16. const { review } = source;
  17. const startLine = review.startLine;
  18. const endLine = review.endLine ?? startLine;
  19. const hasRange =
  20. Number.isInteger(startLine) &&
  21. Number.isInteger(endLine) &&
  22. startLine !== undefined &&
  23. endLine !== undefined &&
  24. startLine > 0 &&
  25. endLine >= startLine;
  26. const lineCount = content.split(/\r\n|\r|\n/).length;
  27. const rangeIsVisible = hasRange && endLine <= lineCount;
  28. const comment: CodeCommentPreview = {
  29. id: review.requestId,
  30. sourcePath: source.path,
  31. sourceTitle: source.title,
  32. startLine: hasRange ? startLine : 1,
  33. endLine: hasRange ? endLine : 1,
  34. selectedText: "",
  35. comment: review.body,
  36. };
  37. return {
  38. focusedRange: rangeIsVisible ? { startLine, endLine } : null,
  39. inlineComments: rangeIsVisible ? [comment] : [],
  40. targetLineOutOfRange: hasRange && !rangeIsVisible,
  41. topComment: rangeIsVisible ? null : comment,
  42. };
  43. }