fix(web): remove inserted citations on cancel (#10518)

This commit is contained in:
Exotic authored and GitHub committed 2026-09-07 10:29:02 -05:00
1 parent 8b2838e0e8
commit fe07ffe7c0
2 files changed
+19 -7

No files matched your search

@@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = {
export type ComposerCitationCommentTarget = {
nodeKey: NodeKey;
sourceAnchor?: AssistantCitationSourceAnchor;
removeOnCancel?: boolean;
};
export const ComposerCitationCommentContext = createContext<{
@@ -68,7 +69,11 @@ export function $consumeComposerCitationCommentRequest(requestRef: {
let offset = 0;
for (const node of paragraph.getChildren()) {
if (offset === request.citationStart && node instanceof ComposerCitationNode) {
return { nodeKey: node.getKey(), sourceAnchor: request.sourceAnchor };
return {
nodeKey: node.getKey(),
sourceAnchor: request.sourceAnchor,
removeOnCancel: true,
};
}
offset += node.getTextContentSize();
}
@@ -78,6 +83,8 @@ export function $consumeComposerCitationCommentRequest(requestRef: {
function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey: NodeKey }) {
const [editor] = useLexicalComposerContext();
const commentContext = use(ComposerCitationCommentContext);
const commentTarget =
commentContext.openComment?.nodeKey === props.nodeKey ? commentContext.openComment : null;
const onSaveComment = (comment: string): boolean => {
if (!editor.isEditable()) return false;
let accepted = false;
@@ -118,15 +125,13 @@ function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey
<AssistantCitationChip
citation={props.citation}
commentEditor={{
open: commentContext.openComment?.nodeKey === props.nodeKey,
sourceAnchor:
commentContext.openComment?.nodeKey === props.nodeKey
? commentContext.openComment.sourceAnchor
: undefined,
open: commentTarget !== null,
sourceAnchor: commentTarget?.sourceAnchor,
onOpenChange: (open) => {
if (open && !editor.isEditable()) return;
commentContext.onOpenChange(props.nodeKey, open);
},
...(commentTarget?.removeOnCancel ? { onCancel: onRemove } : {}),
onSave: onSaveComment,
onSaveAndSend: (comment) => {
if (!onSaveComment(comment)) return false;
@@ -41,6 +41,7 @@ export function AssistantCitationChip({
open: boolean;
sourceAnchor?: AssistantCitationSourceAnchor | undefined;
onOpenChange: (open: boolean) => void;
onCancel?: () => void;
onSave: (comment: string) => boolean;
onSaveAndSend?: (comment: string) => boolean;
};
@@ -168,7 +169,13 @@ export function AssistantCitationChip({
},
}
: {})}
onCancel={() => commentEditor.onOpenChange(false)}
onCancel={() => {
if (commentEditor.onCancel) {
commentEditor.onCancel();
} else {
commentEditor.onOpenChange(false);
}
}}
/>
</PopoverPopup>
) : null}