Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/src/components/DiffPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { resolveThreadRouteRef } from "../threadRoutes";
import { useClientSettings } from "../hooks/useSettings";
import { formatShortTimestamp } from "../timestampFormat";
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableFileDiff";
import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView";
import { ToggleGroup, Toggle } from "./ui/toggle-group";
import { Switch } from "./ui/switch";
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import type {
FileDiffMetadata,
SelectedLineRange,
} from "@pierre/diffs";
import {
CodeView,
type CodeViewHandle,
type CodeViewProps,
FileDiff,
type FileDiffProps,
} from "@pierre/diffs/react";
import { CodeView, type CodeViewHandle, type CodeViewProps } from "@pierre/diffs/react";
import type { ScopedThreadRef } from "@t3tools/contracts";
import { useCallback, useMemo, useState, type ReactNode, type Ref } from "react";

Expand Down Expand Up @@ -76,178 +70,6 @@ function appendAnnotationEntry(
);
}

interface AnnotatableFileDiffProps {
fileDiff: FileDiffMetadata;
filePath: string;
sectionId: string;
sectionTitle: string;
composerDraftTarget: ScopedThreadRef | DraftId;
options: FileDiffProps<DiffCommentAnnotationGroup>["options"];
renderHeaderPrefix: (fileDiff: FileDiffMetadata) => ReactNode;
}

export function AnnotatableFileDiff({
fileDiff,
filePath,
sectionId,
sectionTitle,
composerDraftTarget,
options,
renderHeaderPrefix,
}: AnnotatableFileDiffProps) {
const addReviewComment = useComposerDraftStore((store) => store.addReviewComment);
const removeReviewComment = useComposerDraftStore((store) => store.removeReviewComment);
const reviewComments = useComposerDraftStore(
(store) => store.getComposerDraft(composerDraftTarget)?.reviewComments ?? EMPTY_REVIEW_COMMENTS,
);
const [selectedRange, setSelectedRange] = useState<SelectedLineRange | null>(null);
const [draftAnnotation, setDraftAnnotation] = useState<DiffCommentLineAnnotation | null>(null);
const persistedAnnotations = useMemo(
() =>
reviewComments
.filter(
(comment) =>
comment.sectionId === sectionId &&
comment.filePath === filePath &&
(comment.fenceLanguage ?? "diff") === "diff",
)
.reduce<DiffCommentLineAnnotation[]>((annotations, comment) => {
const range = restoreDiffReviewCommentRange(fileDiff, comment);
if (!range) return annotations;
return appendAnnotationEntry(annotations, range, {
id: comment.id,
kind: "comment",
range,
rangeLabel: comment.rangeLabel,
text: comment.text,
});
}, []),
[fileDiff, filePath, reviewComments, sectionId],
);
const lineAnnotations = useMemo(
() => (draftAnnotation ? [...persistedAnnotations, draftAnnotation] : persistedAnnotations),
[draftAnnotation, persistedAnnotations],
);

const removeAnnotationEntry = useCallback(
(entryId: string) => {
setSelectedRange(null);
if (
draftAnnotation?.metadata.entries.some(
(entry) => entry.id === entryId && entry.kind === "draft",
)
) {
setDraftAnnotation(null);
return;
}
removeReviewComment(composerDraftTarget, entryId);
},
[composerDraftTarget, draftAnnotation, removeReviewComment],
);

const submitAnnotationEntry = useCallback(
(entryId: string, text: string) => {
const entry = draftAnnotation?.metadata.entries.find((candidate) => candidate.id === entryId);
if (!entry) return;

const comment = buildDiffReviewComment({
id: entry.id,
sectionId,
sectionTitle,
filePath,
fileDiff,
range: entry.range,
text,
});
if (comment) {
addReviewComment(composerDraftTarget, comment);
}
setSelectedRange(null);
setDraftAnnotation(null);
},
[
addReviewComment,
composerDraftTarget,
fileDiff,
filePath,
draftAnnotation,
sectionId,
sectionTitle,
],
);

const beginComment = useCallback(
(range: SelectedLineRange) => {
const id = nextFileCommentId();
const comment = buildDiffReviewComment({
id,
sectionId,
sectionTitle,
filePath,
fileDiff,
range,
text: "",
});
if (!comment) return;

const draftEntry: DiffCommentAnnotationEntry = {
id,
kind: "draft",
range,
rangeLabel: comment.rangeLabel,
text: "",
};
setDraftAnnotation({
side: annotationSide(range),
lineNumber: range.end,
metadata: { entries: [draftEntry] },
});
},
[fileDiff, filePath, sectionId, sectionTitle],
);

const hasOpenCommentForm = draftAnnotation !== null;
const handleLineSelectionEnd = useCallback(
(range: SelectedLineRange | null) => {
setSelectedRange(range);
if (range) beginComment(range);
},
[beginComment],
);

return (
<FileDiff<DiffCommentAnnotationGroup>
fileDiff={fileDiff}
renderHeaderPrefix={renderHeaderPrefix}
options={{
...options,
enableGutterUtility: !hasOpenCommentForm,
enableLineSelection: !hasOpenCommentForm,
onGutterUtilityClick: setSelectedRange,
onLineSelectionChange: setSelectedRange,
onLineSelectionEnd: handleLineSelectionEnd,
}}
selectedLines={selectedRange}
lineAnnotations={lineAnnotations}
renderAnnotation={(annotation) => (
<div className="py-1">
{annotation.metadata.entries.map((entry) => (
<LocalCommentAnnotation
key={entry.id}
kind={entry.kind}
rangeLabel={entry.rangeLabel}
text={entry.text}
onCancel={() => removeAnnotationEntry(entry.id)}
onComment={(text) => submitAnnotationEntry(entry.id, text)}
onDelete={() => removeAnnotationEntry(entry.id)}
/>
))}
</div>
)}
/>
);
}

interface AnnotatableCodeViewProps {
files: ReadonlyArray<{
fileDiff: FileDiffMetadata;
Expand Down
Loading