fix(web): keep citation comment when popover is dismissed #10831
Mergedflamboh(GitHub profile, opens in a new tab)merged 8 commits into
main fromrefs/pull/10831/headmerged by shivamhwp(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/web/src/components/chat/AssistantCitationChip.tsx lines 67 to 73OutdatedResolvedby flamboh(GitHub profile, opens in a new tab)@@ -51,4 +54,20 @@54+ const draftCommentRef = useRef<string | null>(null);5155 const commentOpen = commentEditor?.open ?? false;5256 const sourceAnchor = commentEditor?.sourceAnchor;57+ useEffect(() => {58+ if (!commentOpen) draftCommentRef.current = null;59+ }, [commentOpen]);60+ // Saves the draft if it can be saved. Returns false when the popover must stay open.61+ const settleDraftOnClose = (reason: string): boolean => {62+ const dismissal = resolveAssistantCitationCommentDismissal({63+ reason,64+ draft: draftCommentRef.current,65+ savedComment: citation.comment,66+ });67+ if (dismissal.kind === "commit") commentEditor?.onSave(dismissal.comment);68+ return dismissal.kind !== "keep-open";69+ };5370 const onSourceUnavailable = useEffectEvent(() => {54- if (sourceAnchor) commentEditor?.onOpenChange(false);71+ if (!sourceAnchor) return;72+ settleDraftOnClose("none");73+ commentEditor?.onOpenChange(false);
- apps/web/src/components/chat/AssistantCitationChip.tsx lines 67 to 68OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -48,5 +49,20 @@4849 }) {4950 const navigate = useNavigate();5051 const commentInputRef = useRef<HTMLTextAreaElement>(null);52+ // Unsaved comment text, held outside the editor so a dismissal can commit it53+ // after Base UI has already decided to close the popover.54+ const draftCommentRef = useRef<string | null>(null);5155 const commentOpen = commentEditor?.open ?? false;5256 const sourceAnchor = commentEditor?.sourceAnchor;57+ useEffect(() => {58+ if (!commentOpen) draftCommentRef.current = null;59+ }, [commentOpen]);60+ // Saves the draft if it can be saved. Returns false when the popover must stay open.61+ const settleDraftOnClose = (reason: string): boolean => {62+ const dismissal = resolveAssistantCitationCommentDismissal({63+ reason,64+ draft: draftCommentRef.current,65+ savedComment: citation.comment,66+ });67+ if (dismissal.kind === "commit") commentEditor?.onSave(dismissal.comment);68+ return dismissal.kind !== "keep-open";apps/web/src/components/chat/AssistantCitationChip.tsx lines 72 to 73OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -51,4 +54,20 @@54+ const draftCommentRef = useRef<string | null>(null);5155 const commentOpen = commentEditor?.open ?? false;5256 const sourceAnchor = commentEditor?.sourceAnchor;57+ useEffect(() => {58+ if (!commentOpen) draftCommentRef.current = null;59+ }, [commentOpen]);60+ // Saves the draft if it can be saved. Returns false when the popover must stay open.61+ const settleDraftOnClose = (reason: string): boolean => {62+ const dismissal = resolveAssistantCitationCommentDismissal({63+ reason,64+ draft: draftCommentRef.current,65+ savedComment: citation.comment,66+ });67+ if (dismissal.kind === "commit") commentEditor?.onSave(dismissal.comment);68+ return dismissal.kind !== "keep-open";69+ };5370 const onSourceUnavailable = useEffectEvent(() => {54- if (sourceAnchor) commentEditor?.onOpenChange(false);71+ if (!sourceAnchor) return;72+ settleDraftOnClose("none");73+ commentEditor?.onOpenChange(false);apps/web/src/components/chat/assistantCitationCommentDismissal.ts line 24OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +4,21 @@4+ | { kind: "commit"; comment: string }5+ | { kind: "close" }6+ | { kind: "keep-open" };7+8+/**9+ * Decides what happens to unsaved comment text when the citation popover closes10+ * without Save or Cancel: clicking away, focus leaving, or toggling the pencil.11+ * Typed text is committed rather than dropped. Escape stays an explicit discard,12+ * and a draft over the length limit keeps the popover open so the error is visible.13+ */14+export function resolveAssistantCitationCommentDismissal({15+ reason,16+ draft,17+ savedComment,18+}: {19+ reason: string;20+ draft: string | null;21+ savedComment: string | undefined;22+}): AssistantCitationCommentDismissal {23+ if (reason === "escape-key" || draft === null) return { kind: "close" };24+ if (draft.trim() === (savedComment ?? "")) return { kind: "close" };
- apps/web/src/components/chat/assistantCitationCommentDismissal.ts lines 18 to 19Resolvedby flamboh(GitHub profile, opens in a new tab)@@ -0,0 +5,21 @@5+ | { kind: "close" }6+ | { kind: "keep-open" };7+8+/**9+ * Decides what happens to unsaved comment text when the citation popover closes10+ * without Save or Cancel: clicking away, focus leaving, or toggling the pencil.11+ * Typed text is committed rather than dropped. Escape stays an explicit discard,12+ * and a draft over the length limit keeps the popover open so the error is visible.13+ */14+export function resolveAssistantCitationCommentDismissal({15+ reason,16+ draft,17+ savedComment,18+}: {19+ reason: string;20+ draft: string | null;21+ savedComment: string | undefined;22+}): AssistantCitationCommentDismissal {23+ if (reason === "escape-key" || draft === null) return { kind: "close" };24+ if (draft.trim() === (savedComment ?? "").trim()) return { kind: "close" };25+ if (draft.length > ASSISTANT_CITATION_MAX_COMMENT_LENGTH) return { kind: "keep-open" };
- apps/web/src/components/chat/AssistantCitationChip.tsx line 78Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -51,4 +58,20 @@58+ const draftCommentRef = useRef<string | null>(null);59+ const [unavailableSourceAnchor, setUnavailableSourceAnchor] =60+ useState<AssistantCitationSourceAnchor | null>(null);5161 const commentOpen = commentEditor?.open ?? false;5262 const sourceAnchor = commentEditor?.sourceAnchor;63+ const activeSourceAnchor = sourceAnchor === unavailableSourceAnchor ? undefined : sourceAnchor;64+ useEffect(() => {65+ if (!commentOpen) draftCommentRef.current = null;66+ }, [commentOpen]);67+ const settleDraftOnClose = (reason: string): boolean => {68+ const dismissal = resolveAssistantCitationCommentDismissal({69+ reason,70+ draft: draftCommentRef.current,71+ savedComment: citation.comment,72+ });73+ if (dismissal.kind === "commit") return commentEditor?.onSave(dismissal.comment) ?? true;74+ return dismissal.kind !== "keep-open";75+ };5376 const onSourceUnavailable = useEffectEvent(() => {54- if (sourceAnchor) commentEditor?.onOpenChange(false);77+ if (!sourceAnchor) return;
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed