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
42export type ComposerCitationCommentTarget = {42export type ComposerCitationCommentTarget = {
43 nodeKey: NodeKey;43 nodeKey: NodeKey;
44 sourceAnchor?: AssistantCitationSourceAnchor;44 sourceAnchor?: AssistantCitationSourceAnchor;
45 removeOnCancel?: boolean;
45};46};
46 47
47export const ComposerCitationCommentContext = createContext<{48export const ComposerCitationCommentContext = createContext<{
@@ -68,7 +69,11 @@ export function $consumeComposerCitationCommentRequest(requestRef: { · 20 unmodified lines
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: { · 3 unmodified lines
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 · 34 unmodified lines
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({ · 40 unmodified lines
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({ · 121 unmodified lines
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}