Refactor inline comment architecture across plan/files/diff and fix diff overlay rendering (#461)
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import type { AnnotationSide, DiffLineAnnotation, SelectedLineRange } from '@pierre/diffs';
|
||||
import type { InlineCommentDraft } from '@/stores/useInlineCommentDraftStore';
|
||||
|
||||
export type PierreAnnotationData =
|
||||
| { type: 'saved' | 'edit'; draft: InlineCommentDraft }
|
||||
| { type: 'new'; selection: SelectedLineRange };
|
||||
|
||||
export const toPierreAnnotationId = (meta: PierreAnnotationData): string => {
|
||||
if (meta.type === 'new') {
|
||||
const start = Math.min(meta.selection.start, meta.selection.end);
|
||||
const end = Math.max(meta.selection.start, meta.selection.end);
|
||||
const side = meta.selection.side ?? 'additions';
|
||||
return `new-comment-${side}-${start}-${end}`;
|
||||
}
|
||||
|
||||
return `draft-${meta.draft.id}`;
|
||||
};
|
||||
|
||||
interface BuildPierreLineAnnotationsOptions {
|
||||
drafts: InlineCommentDraft[];
|
||||
editingDraftId: string | null;
|
||||
selection: SelectedLineRange | null;
|
||||
}
|
||||
|
||||
export const buildPierreLineAnnotations = (
|
||||
options: BuildPierreLineAnnotationsOptions
|
||||
): DiffLineAnnotation<PierreAnnotationData>[] => {
|
||||
const { drafts, editingDraftId, selection } = options;
|
||||
const annotations: DiffLineAnnotation<PierreAnnotationData>[] = [];
|
||||
|
||||
for (const draft of drafts) {
|
||||
const side: AnnotationSide = draft.side === 'original' ? 'deletions' : 'additions';
|
||||
annotations.push({
|
||||
lineNumber: draft.endLine,
|
||||
side,
|
||||
metadata: {
|
||||
type: draft.id === editingDraftId ? 'edit' : 'saved',
|
||||
draft,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (selection && !editingDraftId) {
|
||||
annotations.push({
|
||||
lineNumber: Math.max(selection.start, selection.end),
|
||||
side: selection.side ?? 'additions',
|
||||
metadata: { type: 'new', selection },
|
||||
});
|
||||
}
|
||||
|
||||
return annotations;
|
||||
};
|
||||
Reference in New Issue
Block a user