import { useCallback, type ReactNode } from 'react'; import type { DiffLineAnnotation } from '@pierre/diffs'; import type { DiffComment } from '@/lib/diff/types'; import { CommentForm } from './comment-form'; import { CommentDisplay } from './comment-display'; import { HunkActionBar } from './hunk-action-bar'; type AnnotationMetadata = { type: 'comment' | 'new-comment-form' | 'hunk-actions'; comment?: DiffComment; isEditing?: boolean; changeBlockId?: string; }; type UseAnnotationRendererOpts = { handleRevertBlock: (changeBlockId: string) => Promise; onButtonEnter: () => void; onButtonLeave: () => void; handleCommentSubmit: (content: string) => void; handleCommentUpdate: (commentId: string, content: string) => void; handleCommentDelete: (commentId: string) => void; setShowCommentForm: (show: boolean) => void; setSelectedLines: (lines: null) => void; setEditingComment: (id: string | null) => void; }; export type { AnnotationMetadata }; export function useAnnotationRenderer(opts: UseAnnotationRendererOpts) { const { handleRevertBlock, onButtonEnter, onButtonLeave, handleCommentSubmit, handleCommentUpdate, handleCommentDelete, setShowCommentForm, setSelectedLines, setEditingComment, } = opts; return useCallback( (annotation: DiffLineAnnotation): ReactNode => { const { type, comment, isEditing, changeBlockId } = annotation.metadata; if (type === 'hunk-actions' && changeBlockId) { return ( handleRevertBlock(changeBlockId)} onMouseEnter={onButtonEnter} onMouseLeave={onButtonLeave} /> ); } if (type === 'new-comment-form') { return (
{ setShowCommentForm(false); setSelectedLines(null); }} />
); } if (isEditing && comment) { return (
handleCommentUpdate(comment.id, content)} onCancel={() => setEditingComment(null)} isEditing />
); } if (comment) { return (
handleCommentDelete(comment.id)} onEdit={() => setEditingComment(comment.id)} showCode={false} />
); } return null; }, [setEditingComment, handleCommentDelete, handleCommentUpdate, handleCommentSubmit, handleRevertBlock, onButtonEnter, onButtonLeave, setShowCommentForm, setSelectedLines] ); }