History

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

Exotic committed 2 months ago

Commit fe07ffe1 parent 8b2838e2 files · +19 −7Browse files

Unified · Split

2 files
  1. apps/web/src/components/ComposerCitationNode.tsx +11 −6
  2. apps/web/src/components/chat/AssistantCitationChip.tsx +8 −1
apps/web/src/components/ComposerCitationNode.tsx+11−6
@@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = { · 41 unmodified lines
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: { · 20 unmodified lines
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: { · 3 unmodified lines
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 · 34 unmodified lines
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+8−1
@@ -41,6 +41,7 @@ export function AssistantCitationChip({ · 40 unmodified lines
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({ · 121 unmodified lines
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}