-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSourcesDisplay.tsx
381 lines (362 loc) · 14.5 KB
/
SourcesDisplay.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { useState, useContext } from "react";
import { ThemeContext, removeUndefined } from "../../theme/ThemeContext";
import { ResetWrapper } from "../../utils/ResetWrapper";
import { useSources} from "../../hooks";
import { TrashIcon } from '@heroicons/react/24/outline';
import { MagnifyingGlassIcon } from '@heroicons/react/24/outline';
import {addOpacity, scaleFontSize} from "../../utils/scaleFontSize.tsx";
export interface SourcesDisplayStyles extends React.CSSProperties {
backgroundColor?: string;
color?: string;
padding?: string;
fontFamily?: string;
fontSize?: string;
borderRadius?: string;
inputBackgroundColor?: string;
inputBorderColor?: string;
buttonBackground?: string;
buttonTextColor?: string;
buttonBorderRadius?: string;
activeSourceBackground?: string;
activeSourceBorderColor?: string;
selectedSourceBackground?: string;
selectedSourceBorderColor?: string;
inactiveSourceBackground?: string;
inactiveSourceBorderColor?: string;
inputFocusRingColor?: string;
metadataTagBackground?: string;
metadataTagColor?: string;
relevanceScoreColor?: string;
sourceTypeBackground?: string;
sourceTypeColor?: string;
}
/**
* Props for the SourcesDisplay component
* @see {@link SourcesDisplay}
*/
export interface SourcesDisplayProps {
/**
* Unique key for the component which can be used to identify the source of UserAction's if multiple SourcesDisplay components are used.
* The default is 'SourcesDisplay', if key is provided it will be appended to the default key as following 'SourcesDisplay-${key}'.
*/
componentKey?: string;
/**
* The title displayed above the sources list
* @default "Retrieved Sources"
*/
title?: string;
/**
* Placeholder text for the search input field
* @default "Search sources..."
*/
searchPlaceholder?: string;
/**
* Controls visibility of the search functionality
* @default true
*/
showSearch?: boolean;
/**
* Controls visibility of relevance scores
* @default true
*/
showRelevanceScore?: boolean;
/**
* Controls visibility of metadata tags
* @default true
*/
showMetadata?: boolean;
/**
* Style customization options
*/
styleOverrides?: SourcesDisplayStyles;
}
/**
* A component for displaying, searching, and selecting sources.
*
* `SourcesDisplay` shows a list of available sources with search functionality,
* allowing users to browse, filter, and select sources for viewing or reference.
*
* @component
*
* Features:
* - Source listing with titles and metadata
* - Search functionality for filtering sources
* - Source selection for viewing content
* - Relevance score display
* - Metadata tag display
* - Clear sources functionality
* - Responsive design
*
* @example
*
* ```tsx
* <SourcesDisplay
* componentKey="main-sources"
* title="Knowledge Base"
* searchPlaceholder="Search documents..."
* showSearch={true}
* showRelevanceScore={true}
* showMetadata={true}
* styleOverrides={{
* backgroundColor: '#ffffff',
* borderRadius: '8px',
* boxShadow: '0 2px 4px rgba(0,0,0,0.1)',
* padding: '1rem',
* }}
* />
* ```
*/
const SourcesDisplay: React.FC<SourcesDisplayProps> = ({
componentKey,
title = "Retrieved Sources",
searchPlaceholder = "Search knowledge base...",
showSearch = true,
showRelevanceScore = true,
showMetadata = true,
styleOverrides = {},
}) => {
const { sources, activeSources, selectedSourceId, setSelectedSource, searchSources, clearSources } = useSources(componentKey ? `SourcesDisplay-${componentKey}` : 'SourcesDisplay'); // todo: make use of new API
const [searchQuery, setSearchQuery] = useState("");
// use theme
const theme = useContext(ThemeContext);
if (!theme) {
throw new Error('ThemeContext is undefined');
}
const { colors, typography, componentDefaults } = theme.theme;
// Merge theme defaults + overrides
const style: SourcesDisplayStyles = {
backgroundColor: colors.background,
color: colors.text,
padding: componentDefaults.padding,
borderRadius: componentDefaults.borderRadius,
fontSize: typography.fontSizeBase,
inputBackgroundColor: 'white',
inputBorderColor: colors.primary,
inputFocusRingColor: colors.primary,
buttonBackground: colors.primary,
buttonTextColor: colors.contrast,
buttonBorderRadius: componentDefaults.borderRadius,
activeSourceBackground: colors.secondaryBackground,
activeSourceBorderColor: colors.primary,
selectedSourceBackground: colors.secondaryBackground,
selectedSourceBorderColor: colors.secondary,
inactiveSourceBackground: colors.secondaryBackground,
inactiveSourceBorderColor: 'transparent',
metadataTagBackground: colors.background,
metadataTagColor: colors.secondaryText,
relevanceScoreColor: colors.primary,
sourceTypeBackground: addOpacity(colors.secondary, 0.17),
sourceTypeColor: colors.secondary,
...removeUndefined(styleOverrides),
};
const handleSearch = () => {
if (searchQuery.trim()) {
searchSources(searchQuery);
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
handleSearch();
}
};
return (
<ResetWrapper>
<div className="w-full h-full flex flex-col" style={{
backgroundColor: style.backgroundColor,
color: style.color,
borderRadius: style.borderRadius,
fontFamily: style.fontFamily,
fontSize: style.fontSize,
}}>
{/* Header */}
<div className="flex justify-between items-center p-4">
<h2 className="font-semibold" style={{ color: style.color, fontSize: `calc(${style.fontSize} * 1.15)` }}>
{title}
</h2>
<button
onClick={clearSources}
className="p-2 rounded-full hover:bg-gray-100 transition-colors disabled:opacity-40 disabled:hover:bg-transparent disabled:cursor-not-allowed"
style={{
color: style.color,
backgroundColor: 'transparent',
border: 'none',
cursor: sources.length > 0 ? 'pointer' : 'not-allowed',
}}
disabled={sources.length === 0}
title={sources.length > 0 ? "Clear all sources" : "No sources to clear"}
aria-label={sources.length > 0 ? "Clear all sources" : "No sources to clear"}
>
<TrashIcon className="size-5" />
</button>
</div>
{/* Scrollable Sources List */}
<div className="flex-1 overflow-y-auto px-4">
{sources.length === 0 ? (
<p style={{ color: style.color, fontStyle: 'italic', fontSize: style.fontSize }}>No sources available</p>
) : (
<ul className="space-y-3 mb-4">
{sources.map((source, index) => (
<li
key={index}
className="p-4 shadow-sm border transition-all cursor-pointer"
style={{
backgroundColor: source.id === selectedSourceId
? style.selectedSourceBackground
: activeSources && activeSources.includes(source)
? style.activeSourceBackground
: activeSources && activeSources.length > 0
? style.inactiveSourceBackground
: style.inactiveSourceBackground,
borderColor: source.id === selectedSourceId
? style.selectedSourceBorderColor
: activeSources && activeSources.includes(source)
? style.selectedSourceBorderColor
: style.inactiveSourceBorderColor,
opacity: activeSources && activeSources.length > 0 && !activeSources.includes(source) ? 0.6 : 1,
borderRadius: style.borderRadius,
fontSize: style.fontSize,
}}
onClick={() => setSelectedSource(source.id)}
>
<div className="flex items-start justify-between">
<div className="overflow-hidden flex-1 mr-2">
<p className="font-medium truncate" style={{
color: style.color,
fontSize: scaleFontSize(style.fontSize || '16px', 1.0)
}}>
{source.title}
</p>
{source.description && (
<p className="text-gray-500 line-clamp-2" style={{
color: addOpacity(style.color || colors.text, 0.6),
fontSize: scaleFontSize(style.fontSize || '14px', 0.95)
}}>
{source.description}
</p>
)}
{showRelevanceScore && source.relevance !== undefined && (
<div className="mt-2 flex items-center group relative">
<span style={{
color: addOpacity(style.color || colors.text, 0.6),
fontSize: scaleFontSize(style.fontSize || '12px', 0.85),
}}>Relevance:</span>
<div
className="ml-2 h-2 w-24 rounded-full"
style={{
backgroundColor: style.metadataTagBackground,
}}
>
<div
className="h-2 rounded-full"
style={{
backgroundColor: style.relevanceScoreColor,
width: `${Math.round(source.relevance * 100)}%`
}}
/>
</div>
<div className="absolute bottom-full left-1/3 transform -translate-x-1 mb-1 px-2 py-1 rounded text-xs whitespace-nowrap opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-opacity duration-200"
style={{
backgroundColor: style.backgroundColor,
color: style.color,
border: `1px solid ${style.metadataTagBackground}`,
fontSize: scaleFontSize(style.fontSize || '12px', 0.75),
}}>
{Math.round(source.relevance * 100)}%
</div>
</div>
)}
</div>
{source.type && (
<span className="inline-block px-2 py-1 font-medium rounded-full flex-shrink-0" style={{
backgroundColor: style.sourceTypeBackground,
color: style.sourceTypeColor,
fontSize: scaleFontSize(style.fontSize || '12px', 0.8)
}}>
{source.type}
</span>
)}
</div>
{showMetadata && source.metadata && Object.keys(source.metadata).length > 0 && (
<div className="pt-2 border-t" style={{ borderColor: style.inactiveSourceBorderColor }}>
<div className="flex flex-wrap gap-2">
{Object.entries(source.metadata)
.filter(([key]) => typeof key === "string" && !key.startsWith("_"))
.map(([key, value]) => (
<span
key={key}
className="inline-flex items-center px-2 py-1 rounded-md"
style={{
backgroundColor: style.metadataTagBackground,
color: style.metadataTagColor,
fontSize: scaleFontSize(style.fontSize || '12px', 0.85),
lineHeight: '1.2',
}}
>
{key}: {value}
</span>
))}
</div>
</div>
)}
</li>
))}
</ul>
)}
</div>
{/* Search Section with proper focus behavior */}
{showSearch && (
<div className="border-t px-4 py-3" style={{
borderColor: style.inputBorderColor + '20',
backgroundColor: style.backgroundColor
}}>
<div className="flex items-center gap-3">
<div
className="flex-1 flex items-center gap-2 group
bg-white border shadow-[inset_0_1px_2px_rgba(0,0,0,0.02)]
focus-within:ring-1 focus-within:ring-opacity-20 transition-all"
style={{
borderColor: `${style.inputBorderColor}30`,
borderRadius: style.borderRadius,
}}
>
<MagnifyingGlassIcon
className="ml-3 size-4 transition-colors group-focus-within:text-primary-600"
style={{
color: style.buttonBackground + '60',
}}
/>
<input
type="text"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={searchPlaceholder}
className="w-full py-2 pr-3 border-0 focus:ring-0 focus:outline-none"
style={{
color: style.color,
backgroundColor: 'transparent',
fontSize: style.fontSize,
}}
/>
</div>
<button
onClick={handleSearch}
className="rounded-md px-4 py-2 transition-all hover:opacity-90 active:transform active:scale-[0.98]"
style={{
backgroundColor: style.buttonBackground,
color: style.buttonTextColor,
fontSize: style.fontSize,
fontWeight: '500',
borderRadius: style.buttonBorderRadius,
}}
>
Search
</button>
</div>
</div>
)}
</div>
</ResetWrapper>
);
};
export { SourcesDisplay }