Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions apps/web/src/components/ComposerCitationNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type ComposerCitationCommentRequest = {
export type ComposerCitationCommentTarget = {
nodeKey: NodeKey;
sourceAnchor?: AssistantCitationSourceAnchor;
removeOnCancel?: boolean;
};

export const ComposerCitationCommentContext = createContext<{
Expand All @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion apps/web/src/components/chat/AssistantCitationChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -168,7 +169,13 @@ export function AssistantCitationChip({
},
}
: {})}
onCancel={() => commentEditor.onOpenChange(false)}
onCancel={() => {
if (commentEditor.onCancel) {
commentEditor.onCancel();
} else {
commentEditor.onOpenChange(false);
}
}}
/>
</PopoverPopup>
) : null}
Expand Down
Loading