TypeScript
fix(web): restore file comment focus in editable preview (#9061)
Shpetim13427ecd8ba5
diff --git a/apps/web/src/components/diffs/DiffCommentAnnotation.tsx b/apps/web/src/components/diffs/DiffCommentAnnotation.tsxindex d210732b6b6..bce9f7e55f5 100644--- a/apps/web/src/components/diffs/DiffCommentAnnotation.tsx+++ b/apps/web/src/components/diffs/DiffCommentAnnotation.tsx@@ -1,5 +1,5 @@import { MessageCircle, Trash2 } from "lucide-react";-import { useState, type ReactNode } from "react";+import { useLayoutEffect, useRef, useState, type ReactNode } from "react";import { Button } from "~/components/ui/button";import { Textarea } from "~/components/ui/textarea";@@ -25,6 +25,7 @@ interface DiffCommentAnnotationProps {submitLabel?: string;pending?: boolean;secondaryAction?: DiffCommentSecondaryAction;+ focusOnMount?: boolean;}/** The shared inline comment treatment for file previews, thread diffs, and pull-request diffs. */@@ -40,10 +41,20 @@ export function DiffCommentAnnotation({submitLabel = "Comment",pending = false,secondaryAction,+ focusOnMount = true,}: DiffCommentAnnotationProps) {const [localDraftText, setLocalDraftText] = useState("");const displayedText = kind === "draft" && !onTextChange ? localDraftText : text;const trimmedText = displayedText.trim();+ const textareaRef = useRef<HTMLTextAreaElement | null>(null);++ useLayoutEffect(() => {+ if (kind !== "draft" || !focusOnMount) return;+ const frame = window.requestAnimationFrame(() => {+ textareaRef.current?.focus({ preventScroll: true });+ });+ return () => window.cancelAnimationFrame(frame);+ }, [focusOnMount, kind]);if (kind === "comment") {return (@@ -78,9 +89,10 @@ export function DiffCommentAnnotation({onPointerDown={(event) => event.stopPropagation()}><Textarea- autoFocus+ ref={textareaRef}+ autoFocus={focusOnMount}unstyled- className="relative inline-flex w-full rounded-md border border-border/50 bg-background/20 font-sans text-foreground transition-colors focus-within:border-border/70 [&_[data-slot=textarea]]:min-h-12 [&_[data-slot=textarea]]:cursor-text [&_[data-slot=textarea]]:px-2.5 [&_[data-slot=textarea]]:py-1.5 [&_[data-slot=textarea]]:font-sans [&_[data-slot=textarea]]:text-xs [&_[data-slot=textarea]]:leading-5 max-sm:[&_[data-slot=textarea]]:min-h-12"+ className="relative inline-flex w-full rounded-md border border-border/50 bg-background/20 font-sans text-foreground transition-colors focus-within:border-border/70 [&_[data-slot=textarea]]:min-h-12 [&_[data-slot=textarea]]:cursor-text [&_[data-slot=textarea]]:caret-foreground [&_[data-slot=textarea]]:px-2.5 [&_[data-slot=textarea]]:py-1.5 [&_[data-slot=textarea]]:font-sans [&_[data-slot=textarea]]:text-xs [&_[data-slot=textarea]]:leading-5 max-sm:[&_[data-slot=textarea]]:min-h-12"size="sm"value={displayedText}placeholder={placeholder}diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsxindex a702c4c1abd..5e11abe8577 100644--- a/apps/web/src/components/files/FilePreviewPanel.tsx+++ b/apps/web/src/components/files/FilePreviewPanel.tsx@@ -774,42 +774,47 @@ function EditableFileSurface({],);- const beginComment = useCallback((range: SelectedLineRange) => {- const { startLine, endLine } = normalizeFileCommentRange(range);- const draftEntry: FileCommentAnnotationEntry = {- id: nextFileCommentId(),- kind: "draft",- startLine,- endLine,- text: "",- };- setLineAnnotations((current) => {- const withoutDraft = current.flatMap((annotation) => {- const entries = annotation.metadata.entries.filter((entry) => entry.kind !== "draft");- return entries.length > 0 ? [{ ...annotation, metadata: { entries } }] : [];+ const beginComment = useCallback(+ (range: SelectedLineRange) => {+ editor.setSelections([]);+ editor.blur();+ const { startLine, endLine } = normalizeFileCommentRange(range);+ const draftEntry: FileCommentAnnotationEntry = {+ id: nextFileCommentId(),+ kind: "draft",+ startLine,+ endLine,+ text: "",+ };+ setLineAnnotations((current) => {+ const withoutDraft = current.flatMap((annotation) => {+ const entries = annotation.metadata.entries.filter((entry) => entry.kind !== "draft");+ return entries.length > 0 ? [{ ...annotation, metadata: { entries } }] : [];+ });+ const existingIndex = withoutDraft.findIndex(+ (annotation) => annotation.lineNumber === endLine,+ );+ if (existingIndex < 0) {+ return [+ ...withoutDraft,+ {+ lineNumber: endLine,+ metadata: { entries: [draftEntry] },+ },+ ];+ }+ return withoutDraft.map((annotation, index) =>+ index === existingIndex+ ? {+ ...annotation,+ metadata: { entries: [...annotation.metadata.entries, draftEntry] },+ }+ : annotation,+ );});- const existingIndex = withoutDraft.findIndex(- (annotation) => annotation.lineNumber === endLine,- );- if (existingIndex < 0) {- return [- ...withoutDraft,- {- lineNumber: endLine,- metadata: { entries: [draftEntry] },- },- ];- }- return withoutDraft.map((annotation, index) =>- index === existingIndex- ? {- ...annotation,- metadata: { entries: [...annotation.metadata.entries, draftEntry] },- }- : annotation,- );- });- }, []);+ },+ [editor],+ );const hasOpenCommentForm = lineAnnotations.some((annotation) =>annotation.metadata.entries.some((entry) => entry.kind === "draft"),);