authorgravatar for 118054752+extoci@users.noreply.github.comExotic <118054752+extoci@users.noreply.github.com> 2026-09-07 18:29:02+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2026-09-07 10:29:02-05:00
logfe07ffe7c0bf1858ba4d1cd1ce0a67b10802a747
tree04b6c8e11fa6fa1dfe7d1aa3142348e738b6223d
parent8b2838e0e8a73d3fa6476940445c372e47b99db4
signature Signed by PGP key B5690EEEBB952194

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


2 files changed, 19 insertions(+), 7 deletions(-)

apps/web/src/components/ComposerCitationNode.tsx+11-6
...@@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = {...@@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = {
42export type ComposerCitationCommentTarget = {42export type ComposerCitationCommentTarget = {
43 nodeKey: NodeKey;43 nodeKey: NodeKey;
44 sourceAnchor?: AssistantCitationSourceAnchor;44 sourceAnchor?: AssistantCitationSourceAnchor;
45 removeOnCancel?: boolean;
45};46};
4647
47export const ComposerCitationCommentContext = createContext<{48export const ComposerCitationCommentContext = createContext<{
...@@ -68,7 +69,11 @@ export function $consumeComposerCitationCommentRequest(requestRef: {...@@ -68,7 +69,11 @@ export function $consumeComposerCitationCommentRequest(requestRef: {
68 let offset = 0;69 let offset = 0;
69 for (const node of paragraph.getChildren()) {70 for (const node of paragraph.getChildren()) {
70 if (offset === request.citationStart && node instanceof ComposerCitationNode) {71 if (offset === request.citationStart && node instanceof ComposerCitationNode) {
71 return { nodeKey: node.getKey(), sourceAnchor: request.sourceAnchor };72 return {
73 nodeKey: node.getKey(),
74 sourceAnchor: request.sourceAnchor,
75 removeOnCancel: true,
76 };
72 }77 }
73 offset += node.getTextContentSize();78 offset += node.getTextContentSize();
74 }79 }
...@@ -78,6 +83,8 @@ export function $consumeComposerCitationCommentRequest(requestRef: {...@@ -78,6 +83,8 @@ export function $consumeComposerCitationCommentRequest(requestRef: {
78function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey: NodeKey }) {83function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey: NodeKey }) {
79 const [editor] = useLexicalComposerContext();84 const [editor] = useLexicalComposerContext();
80 const commentContext = use(ComposerCitationCommentContext);85 const commentContext = use(ComposerCitationCommentContext);
86 const commentTarget =
87 commentContext.openComment?.nodeKey === props.nodeKey ? commentContext.openComment : null;
81 const onSaveComment = (comment: string): boolean => {88 const onSaveComment = (comment: string): boolean => {
82 if (!editor.isEditable()) return false;89 if (!editor.isEditable()) return false;
83 let accepted = false;90 let accepted = false;
...@@ -118,15 +125,13 @@ function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey...@@ -118,15 +125,13 @@ function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey
118 <AssistantCitationChip125 <AssistantCitationChip
119 citation={props.citation}126 citation={props.citation}
120 commentEditor={{127 commentEditor={{
121 open: commentContext.openComment?.nodeKey === props.nodeKey,128 open: commentTarget !== null,
122 sourceAnchor:129 sourceAnchor: commentTarget?.sourceAnchor,
123 commentContext.openComment?.nodeKey === props.nodeKey
124 ? commentContext.openComment.sourceAnchor
125 : undefined,
126 onOpenChange: (open) => {130 onOpenChange: (open) => {
127 if (open && !editor.isEditable()) return;131 if (open && !editor.isEditable()) return;
128 commentContext.onOpenChange(props.nodeKey, open);132 commentContext.onOpenChange(props.nodeKey, open);
129 },133 },
134 ...(commentTarget?.removeOnCancel ? { onCancel: onRemove } : {}),
130 onSave: onSaveComment,135 onSave: onSaveComment,
131 onSaveAndSend: (comment) => {136 onSaveAndSend: (comment) => {
132 if (!onSaveComment(comment)) return false;137 if (!onSaveComment(comment)) return false;
apps/web/src/components/chat/AssistantCitationChip.tsx+8-1
...@@ -41,6 +41,7 @@ export function AssistantCitationChip({...@@ -41,6 +41,7 @@ export function AssistantCitationChip({
41 open: boolean;41 open: boolean;
42 sourceAnchor?: AssistantCitationSourceAnchor | undefined;42 sourceAnchor?: AssistantCitationSourceAnchor | undefined;
43 onOpenChange: (open: boolean) => void;43 onOpenChange: (open: boolean) => void;
44 onCancel?: () => void;
44 onSave: (comment: string) => boolean;45 onSave: (comment: string) => boolean;
45 onSaveAndSend?: (comment: string) => boolean;46 onSaveAndSend?: (comment: string) => boolean;
46 };47 };
...@@ -168,7 +169,13 @@ export function AssistantCitationChip({...@@ -168,7 +169,13 @@ export function AssistantCitationChip({
168 },169 },
169 }170 }
170 : {})}171 : {})}
171 onCancel={() => commentEditor.onOpenChange(false)}172 onCancel={() => {
173 if (commentEditor.onCancel) {
174 commentEditor.onCancel();
175 } else {
176 commentEditor.onOpenChange(false);
177 }
178 }}
172 />179 />
173 </PopoverPopup>180 </PopoverPopup>
174 ) : null}181 ) : null}