55 serializeAssistantCitation ,
66} from "@t3tools/shared/assistantCitations" ;
77import { Link , useNavigate } from "@tanstack/react-router" ;
8- import { ArrowUpRightIcon , PencilIcon , QuoteIcon } from "lucide-react" ;
8+ import { PencilIcon , QuoteIcon } from "lucide-react" ;
99import {
1010 useEffect ,
1111 useEffectEvent ,
@@ -24,24 +24,93 @@ import {
2424import { cn } from "~/lib/utils" ;
2525import { useQuietThreadShell } from "~/state/entities" ;
2626import { ContextChip , ContextChipAction , ContextChipLabel } from "../ContextChip" ;
27- import { ContextChipPopover } from "../contextChipParts" ;
28- import { Button } from "../ui/button" ;
29- import { Popover , PopoverClose , PopoverPopup , PopoverTrigger } from "../ui/popover" ;
30- import { getVirtualizedScrollFadeClassName } from "../ui/scroll-area" ;
27+ import { Popover , PopoverPopup , PopoverTrigger } from "../ui/popover" ;
3128import { AssistantCitationCommentEditor } from "./AssistantCitationCommentEditor" ;
3229import { resolveAssistantCitationCommentDismissal } from "./assistantCitationCommentDismissal" ;
3330import { observeAssistantCitationCommentSource } from "./AssistantCitationSource" ;
3431import { composerFloatingLayerProps } from "./composerEventScope" ;
35- import { observeResize } from "~/lib/observeResize" ;
3632import { textDirection } from "~/textDirection" ;
3733
34+ function useCitationSourceLinkProps ( citation : AssistantCitation ) {
35+ const navigate = useNavigate ( ) ;
36+ const sourceThread = useQuietThreadShell (
37+ scopeThreadRef ( citation . environmentId , citation . threadId ) ,
38+ ) ;
39+ return {
40+ ...assistantCitationRoute ( citation , sourceThread ) ,
41+ "data-markdown-copy" : serializeAssistantCitation ( citation ) ,
42+ onClick : ( event : ReactMouseEvent < HTMLAnchorElement > ) => {
43+ if ( event . button !== 0 || event . metaKey || event . ctrlKey || event . shiftKey || event . altKey ) {
44+ return ;
45+ }
46+ event . preventDefault ( ) ;
47+ void navigate ( assistantCitationNavigation ( citation , sourceThread ) ) ;
48+ } ,
49+ } ;
50+ }
51+
52+ /**
53+ * A quoted response inside a message. Clicking it goes to the source; its comment follows as
54+ * text. `leading` and `trailing` say it starts or ends its block, which already has spacing.
55+ */
56+ export function AssistantCitationEmbed ( {
57+ citation,
58+ leading,
59+ trailing,
60+ } : {
61+ citation : AssistantCitation ;
62+ leading : boolean ;
63+ trailing : boolean ;
64+ } ) {
65+ const wholeResponse = citation . prefix === "" && citation . suffix === "" ;
66+ const flat = ( text : string ) => text . replace ( / \s + / g, " " ) ;
67+ const { comment, ...quoted } = citation ;
68+ const linkProps = useCitationSourceLinkProps ( citation ) ;
69+ return (
70+ // One copy boundary for the card and its comment, so a copied quote carries the comment once.
71+ < span
72+ data-markdown-copy = { linkProps [ "data-markdown-copy" ] }
73+ className = { cn ( "block" , ! leading && "mt-[0.65rem]" , ! trailing && "mb-[0.65rem]" ) }
74+ >
75+ < Link
76+ { ...linkProps }
77+ // A selection inside the card's boundary copies the quote here and the comment as text.
78+ data-markdown-copy = { serializeAssistantCitation ( quoted ) }
79+ // The quote is findable in its source message; here it is clamped and mostly hidden.
80+ data-thread-find-ignore
81+ aria-label = { `Go to quoted response: ${ assistantCitationLabel ( quoted ) } ` }
82+ // The message's markdown styles colour and underline links; a quote is neither.
83+ className = "block min-w-0 rounded-lg bg-black/10 px-2.5 py-1.5 text-start text-sm text-muted-foreground! no-underline! wrap-break-word hover:bg-black/14 focus-visible:outline-2 focus-visible:outline-ring dark:bg-black/35 dark:hover:bg-black/50"
84+ >
85+ < span className = "line-clamp-2" dir = { textDirection ( citation . text ) } >
86+ { wholeResponse ? (
87+ flat ( citation . text )
88+ ) : (
89+ < >
90+ { citation . prefix ? `…${ flat ( citation . prefix ) } ` : null }
91+ < mark className = "rounded-sm bg-foreground/12 px-0.5 text-foreground" >
92+ { flat ( citation . text ) }
93+ </ mark >
94+ { citation . suffix ? `${ flat ( citation . suffix ) } …` : null }
95+ </ >
96+ ) }
97+ </ span >
98+ </ Link >
99+ { comment ? (
100+ < span className = "mt-1.5 block whitespace-pre-wrap" dir = { textDirection ( comment ) } >
101+ { comment }
102+ </ span >
103+ ) : null }
104+ </ span >
105+ ) ;
106+ }
107+
108+ /** The citation chip in the composer, with its comment editor. */
38109export function AssistantCitationChip ( {
39110 citation,
40- composer = false ,
41111 commentEditor,
42112} : {
43113 citation : AssistantCitation ;
44- composer ?: boolean ;
45114 commentEditor ?: {
46115 open : boolean ;
47116 sourceAnchor ?: AssistantCitationSourceAnchor | undefined ;
@@ -53,10 +122,7 @@ export function AssistantCitationChip({
53122 onRestoreFocus ?: ( ) => void ;
54123 } ;
55124} ) {
56- const navigate = useNavigate ( ) ;
57- const sourceThread = useQuietThreadShell (
58- scopeThreadRef ( citation . environmentId , citation . threadId ) ,
59- ) ;
125+ const sourceLinkProps = useCitationSourceLinkProps ( citation ) ;
60126 const commentInputRef = useRef < HTMLTextAreaElement > ( null ) ;
61127 const commentPopupRef = useRef < HTMLDivElement > ( null ) ;
62128 const draftCommentRef = useRef < string | null > ( null ) ;
@@ -116,17 +182,6 @@ export function AssistantCitationChip({
116182 }
117183 : undefined ;
118184 const label = assistantCitationLabel ( citation ) ;
119- const sourceLinkProps = {
120- ...assistantCitationRoute ( citation , sourceThread ) ,
121- "data-markdown-copy" : serializeAssistantCitation ( citation ) ,
122- onClick : ( event : ReactMouseEvent < HTMLAnchorElement > ) => {
123- if ( event . button !== 0 || event . metaKey || event . ctrlKey || event . shiftKey || event . altKey ) {
124- return ;
125- }
126- event . preventDefault ( ) ;
127- void navigate ( assistantCitationNavigation ( citation , sourceThread ) ) ;
128- } ,
129- } ;
130185 const composerSourceLink = (
131186 < Link
132187 { ...sourceLinkProps }
@@ -139,27 +194,6 @@ export function AssistantCitationChip({
139194 </ ContextChipLabel >
140195 </ Link >
141196 ) ;
142- if ( ! composer ) {
143- return (
144- < ContextChipPopover
145- kind = "citation"
146- icon = { < QuoteIcon /> }
147- label = { label }
148- accessibleLabel = { `Quoted assistant text: ${ label } ` }
149- copyMarkdown = { serializeAssistantCitation ( citation ) }
150- >
151- < div className = "flex max-h-[calc(var(--available-height)_-_1rem_-_2px)] flex-col items-start gap-3 p-1 text-sm" >
152- < AssistantCitationQuote citation = { citation } />
153- < PopoverClose
154- render = { < Button variant = "outline" size = "sm" render = { < Link { ...sourceLinkProps } /> } /> }
155- >
156- < ArrowUpRightIcon aria-hidden = "true" />
157- Go to source
158- </ PopoverClose >
159- </ div >
160- </ ContextChipPopover >
161- ) ;
162- }
163197 return (
164198 < ContextChip
165199 kind = "citation"
@@ -262,34 +296,3 @@ export function AssistantCitationChip({
262296 </ ContextChip >
263297 ) ;
264298}
265-
266- function AssistantCitationQuote ( { citation } : { citation : AssistantCitation } ) {
267- const [ fade , setFade ] = useState ( { top : false , bottom : false } ) ;
268- const updateFade = ( element : HTMLElement ) => {
269- const top = element . scrollTop > 1 ;
270- const bottom = element . scrollHeight - element . clientHeight - element . scrollTop > 1 ;
271- setFade ( ( current ) =>
272- current . top === top && current . bottom === bottom ? current : { top, bottom } ,
273- ) ;
274- } ;
275- const observeFade = ( element : HTMLDivElement | null ) =>
276- element ? observeResize ( element , ( ) => updateFade ( element ) ) : undefined ;
277- return (
278- < div
279- ref = { observeFade }
280- onScroll = { ( event ) => updateFade ( event . currentTarget ) }
281- className = { cn (
282- "max-h-64 min-h-0 space-y-3 self-stretch overflow-y-auto whitespace-pre-wrap wrap-break-word" ,
283- getVirtualizedScrollFadeClassName ( fade ) ,
284- ) }
285- >
286- < blockquote
287- className = "border-l-2 border-border pl-3 text-muted-foreground"
288- dir = { textDirection ( citation . text ) }
289- >
290- { citation . text }
291- </ blockquote >
292- { citation . comment ? < p dir = { textDirection ( citation . comment ) } > { citation . comment } </ p > : null }
293- </ div >
294- ) ;
295- }
0 commit comments