Open History to see the last commit
1import type { AssistantCitation } from "@t3tools/contracts";
2import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
3import { Link, useNavigate } from "@tanstack/react-router";
4import { PencilIcon, QuoteIcon, XIcon } from "lucide-react";
5import { useEffect, useEffectEvent, useRef, type MouseEvent as ReactMouseEvent } from "react";
6import {
7 findAssistantCitationSourceAnchor,
8 type AssistantCitationSourceAnchor,
9} from "~/lib/assistantTextSelection";
10import { cn } from "~/lib/utils";
11import {
12 assistantCitationHash,
13 assistantCitationNavigation,
14} from "../../lib/assistantCitationNavigation";
15import {
16 CHAT_INLINE_CHIP_CLASS_NAME,
17 COMPOSER_INLINE_CHIP_CLASS_NAME,
18 COMPOSER_INLINE_CHIP_DISMISS_BUTTON_CLASS_NAME,
19 COMPOSER_INLINE_CHIP_ICON_CLASS_NAME,
20 COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME,
21} from "../composerInlineChip";
22import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
23import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
24import { AssistantCitationCommentEditor } from "./AssistantCitationCommentEditor";
25import { observeAssistantCitationCommentSource } from "./AssistantCitationSource";
26import { composerFloatingLayerProps } from "./composerEventScope";
27
28const CITATION_ACTION_BUTTON_CLASS_NAME = cn(
29 COMPOSER_INLINE_CHIP_DISMISS_BUTTON_CLASS_NAME,
30 "text-primary/80 hover:bg-primary/10 hover:text-primary",
31);
32
33export function AssistantCitationChip({
34 citation,
35 onRemove,
36 commentEditor,
37}: {
38 citation: AssistantCitation;
39 onRemove?: () => void;
40 commentEditor?: {
41 open: boolean;
42 sourceAnchor?: AssistantCitationSourceAnchor | undefined;
43 onOpenChange: (open: boolean) => void;
44 onCancel?: () => void;
45 onSave: (comment: string) => boolean;
46 onSaveAndSend?: (comment: string) => boolean;
47 };
48}) {
49 const navigate = useNavigate();
50 const commentInputRef = useRef<HTMLTextAreaElement>(null);
51 const commentOpen = commentEditor?.open ?? false;
52 const sourceAnchor = commentEditor?.sourceAnchor;
53 const onSourceUnavailable = useEffectEvent(() => {
54 if (sourceAnchor) commentEditor?.onOpenChange(false);
55 });
56 useEffect(() => {
57 if (!commentOpen) return;
58 const anchor = sourceAnchor ?? findAssistantCitationSourceAnchor(document, citation);
59 if (!anchor) return;
60 return observeAssistantCitationCommentSource({
61 anchor,
62 citation,
63 onUnavailable: onSourceUnavailable,
64 });
65 }, [citation, commentOpen, sourceAnchor]);
66 // A multi-line selection's bounding box spans the full message width; anchor
67 // the bubble to the selection's last line, where the pointer released.
68 const popupAnchor = sourceAnchor
69 ? {
70 contextElement: sourceAnchor.source,
71 getBoundingClientRect: () => {
72 const rects = sourceAnchor.range.getClientRects();
73 return rects.item(rects.length - 1) ?? sourceAnchor.range.getBoundingClientRect();
74 },
75 }
76 : undefined;
77 const preview = (citation.comment?.trim() || citation.text).replace(/\s+/g, " ");
78 const label = preview.length > 64 ? `${preview.slice(0, 64)}…` : preview;
79 const sourceLinkProps = {
80 to: "/$environmentId/$threadId" as const,
81 params: { environmentId: citation.environmentId, threadId: citation.threadId },
82 hash: assistantCitationHash(citation),
83 "data-markdown-copy": serializeAssistantCitation(citation),
84 resetScroll: false,
85 onClick: (event: ReactMouseEvent<HTMLAnchorElement>) => {
86 if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
87 return;
88 }
89 event.preventDefault();
90 void navigate(assistantCitationNavigation(citation));
91 },
92 };
93 const composerSourceLink = (
94 <Link
95 {...sourceLinkProps}
96 className="inline-flex h-full min-w-0 items-center gap-[0.33em] rounded-sm text-inherit no-underline focus-visible:outline-2 focus-visible:outline-primary"
97 aria-label={`View cited assistant text: ${label}`}
98 >
99 <QuoteIcon aria-hidden="true" className={COMPOSER_INLINE_CHIP_ICON_CLASS_NAME} />
100 <span className={cn(COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME, "max-w-[16em]")}>{label}</span>
101 </Link>
102 );
103 const chatSourceLink = (
104 <Link
105 {...sourceLinkProps}
106 className="inline-flex h-full min-w-0 items-center gap-[0.33em] rounded-sm text-inherit no-underline hover:bg-primary/10 focus-visible:outline-2 focus-visible:outline-primary"
107 aria-label={`View cited assistant text: ${label}`}
108 >
109 <QuoteIcon aria-hidden="true" className={COMPOSER_INLINE_CHIP_ICON_CLASS_NAME} />
110 <span className={cn(COMPOSER_INLINE_CHIP_LABEL_CLASS_NAME, "max-w-[16em]")}>{label}</span>
111 </Link>
112 );
113 return (
114 <span
115 className={cn(
116 onRemove ? COMPOSER_INLINE_CHIP_CLASS_NAME : CHAT_INLINE_CHIP_CLASS_NAME,
117 "border-primary/20 bg-primary/8 text-primary",
118 )}
119 contentEditable={false}
120 data-assistant-citation-chip="true"
121 data-markdown-copy={serializeAssistantCitation(citation)}
122 >
123 {onRemove ? (
124 composerSourceLink
125 ) : (
126 <Tooltip>
127 <TooltipTrigger render={chatSourceLink} />
128 <TooltipPopup side="top">View source</TooltipPopup>
129 </Tooltip>
130 )}
131 {commentEditor ? (
132 <Popover open={commentEditor.open} onOpenChange={commentEditor.onOpenChange}>
133 <PopoverTrigger
134 aria-label={citation.comment ? "Edit citation comment" : "Add comment to citation"}
135 className={CITATION_ACTION_BUTTON_CLASS_NAME}
136 >
137 <PencilIcon aria-hidden="true" className="size-[0.85em]" />
138 </PopoverTrigger>
139 {commentEditor.open ? (
140 <PopoverPopup
141 {...composerFloatingLayerProps}
142 side={sourceAnchor ? "bottom" : "top"}
143 align="end"
144 anchor={popupAnchor}
145 initialFocus={() => {
146 commentInputRef.current?.focus({ preventScroll: true });
147 return false;
148 }}
149 aria-label="Edit citation comment"
150 className="w-72 max-w-[calc(100vw-1rem)]"
151 viewportClassName="p-3"
152 onPointerDown={(event) => event.stopPropagation()}
153 >
154 <AssistantCitationCommentEditor
155 key={serializeAssistantCitation(citation)}
156 citation={citation}
157 inputRef={commentInputRef}
158 onSubmit={(comment) => {
159 if (!commentEditor.onSave(comment)) return false;
160 commentEditor.onOpenChange(false);
161 return true;
162 }}
163 {...(commentEditor.onSaveAndSend
164 ? {
165 onSubmitAndSend: (comment: string) => {
166 if (!commentEditor.onSaveAndSend?.(comment)) return false;
167 commentEditor.onOpenChange(false);
168 return true;
169 },
170 }
171 : {})}
172 onCancel={() => {
173 if (commentEditor.onCancel) {
174 commentEditor.onCancel();
175 } else {
176 commentEditor.onOpenChange(false);
177 }
178 }}
179 />
180 </PopoverPopup>
181 ) : null}
182 </Popover>
183 ) : null}
184 {onRemove ? (
185 <button
186 type="button"
187 onClick={onRemove}
188 aria-label="Remove assistant citation"
189 className={cn(
190 COMPOSER_INLINE_CHIP_DISMISS_BUTTON_CLASS_NAME,
191 "text-primary/85 hover:bg-primary/10 hover:text-primary",
192 )}
193 >
194 <XIcon aria-hidden="true" className="size-[0.85em]" />
195 </button>
196 ) : null}
197 </span>
198 );
199}