Skip to content

Commit 843cebe

Browse files
committed
fix(web): remove inserted citations on cancel
1 parent 8b2838e commit 843cebe

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎apps/web/src/components/ComposerCitationNode.tsx‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = {
4242
export type ComposerCitationCommentTarget = {
4343
nodeKey: NodeKey;
4444
sourceAnchor?: AssistantCitationSourceAnchor;
45+
removeOnCancel?: boolean;
4546
};
4647

4748
export const ComposerCitationCommentContext = createContext<{
@@ -68,7 +69,11 @@ export function $consumeComposerCitationCommentRequest(requestRef: {
6869
let offset = 0;
6970
for (const node of paragraph.getChildren()) {
7071
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+
};
7277
}
7378
offset += node.getTextContentSize();
7479
}
@@ -78,6 +83,8 @@ export function $consumeComposerCitationCommentRequest(requestRef: {
7883
function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey: NodeKey }) {
7984
const [editor] = useLexicalComposerContext();
8085
const commentContext = use(ComposerCitationCommentContext);
86+
const commentTarget =
87+
commentContext.openComment?.nodeKey === props.nodeKey ? commentContext.openComment : null;
8188
const onSaveComment = (comment: string): boolean => {
8289
if (!editor.isEditable()) return false;
8390
let accepted = false;
@@ -118,15 +125,13 @@ function ComposerCitationDecorator(props: { citation: AssistantCitation; nodeKey
118125
<AssistantCitationChip
119126
citation={props.citation}
120127
commentEditor={{
121-
open: commentContext.openComment?.nodeKey === props.nodeKey,
122-
sourceAnchor:
123-
commentContext.openComment?.nodeKey === props.nodeKey
124-
? commentContext.openComment.sourceAnchor
125-
: undefined,
128+
open: commentTarget !== null,
129+
sourceAnchor: commentTarget?.sourceAnchor,
126130
onOpenChange: (open) => {
127131
if (open && !editor.isEditable()) return;
128132
commentContext.onOpenChange(props.nodeKey, open);
129133
},
134+
...(commentTarget?.removeOnCancel ? { onCancel: onRemove } : {}),
130135
onSave: onSaveComment,
131136
onSaveAndSend: (comment) => {
132137
if (!onSaveComment(comment)) return false;

‎apps/web/src/components/chat/AssistantCitationChip.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function AssistantCitationChip({
4141
open: boolean;
4242
sourceAnchor?: AssistantCitationSourceAnchor | undefined;
4343
onOpenChange: (open: boolean) => void;
44+
onCancel?: () => void;
4445
onSave: (comment: string) => boolean;
4546
onSaveAndSend?: (comment: string) => boolean;
4647
};
@@ -168,7 +169,13 @@ export function AssistantCitationChip({
168169
},
169170
}
170171
: {})}
171-
onCancel={() => commentEditor.onOpenChange(false)}
172+
onCancel={() => {
173+
if (commentEditor.onCancel) {
174+
commentEditor.onCancel();
175+
} else {
176+
commentEditor.onOpenChange(false);
177+
}
178+
}}
172179
/>
173180
</PopoverPopup>
174181
) : null}

0 commit comments

Comments
 (0)