-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathSearchAIOverview.tsx
More file actions
260 lines (245 loc) · 8.14 KB
/
SearchAIOverview.tsx
File metadata and controls
260 lines (245 loc) · 8.14 KB
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
import Popover from '@mui/material/Popover';
import React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { View } from '@/application/types';
import { ReactComponent as AISearchingIcon } from '@/assets/icons/ai_searching_icon.svg';
import { ReactComponent as ChatAIPageIcon } from '@/assets/icons/chat_ai_page.svg';
import { ReactComponent as HomeAIChatIcon } from '@/assets/icons/m_home_ai_chat_icon.svg';
import { ReactComponent as ToolbarLinkIcon } from '@/assets/icons/m_toolbar_link.svg';
import PageIcon from '@/components/_shared/view-icon/PageIcon';
import { useToView } from '@/components/app/app.hooks';
import { cn } from '@/lib/utils';
export interface SearchOverviewSource {
id: string;
name: string;
targetViewId: string;
targetRowId?: string | null;
ragId: string;
view?: View;
}
interface SearchAIOverviewProps {
askingAI: boolean;
loading: boolean;
query: string;
sources: SearchOverviewSource[];
summary?: {
content: string;
highlights?: string;
} | null;
onAskAI: (sourceIds?: string[]) => void;
onClose: () => void;
}
function escapeRegExp(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function HighlightedSummary({
content,
highlights,
query,
sources,
onClose,
}: {
content: string;
highlights?: string;
query: string;
sources: SearchOverviewSource[];
onClose: () => void;
}) {
const { t } = useTranslation();
const [expanded, setExpanded] = useState(false);
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
const highlightText = (highlights || query).trim();
const isLong = content.length > 520;
const parts = useMemo(() => {
if (!highlightText) return [content];
return content.split(new RegExp(`(${escapeRegExp(highlightText)})`, 'ig'));
}, [content, highlightText]);
const showReference = sources.length > 0 && (!isLong || expanded);
return (
<>
<div
className='whitespace-pre-wrap break-words text-sm leading-[22px] text-text-primary'
style={
!expanded && isLong
? {
display: '-webkit-box',
WebkitBoxOrient: 'vertical',
WebkitLineClamp: 5,
overflow: 'hidden',
}
: undefined
}
>
{parts.map((part, index) =>
highlightText && part.toLowerCase() === highlightText.toLowerCase() ? (
<mark key={`${part}-${index}`} className='bg-fill-theme-select px-0.5 text-text-primary'>
{part}
</mark>
) : (
<React.Fragment key={`${part}-${index}`}>{part}</React.Fragment>
)
)}
{showReference && (
<button
type='button'
aria-label={t('commandPalette.aiOverviewSource', { defaultValue: 'Reference sources' })}
className='ml-1 inline-flex h-[15px] w-[21px] items-center justify-center rounded-[6px] bg-secondary align-middle text-icon-primary hover:bg-secondary'
onClick={(event) => setAnchorEl(event.currentTarget)}
>
<ToolbarLinkIcon className='h-2.5 w-2.5' />
</button>
)}
</div>
{isLong && !expanded && (
<button
type='button'
className='mt-2 text-sm text-text-secondary hover:underline'
onClick={() => setExpanded(true)}
>
{` ...${t('search.seeMore', { defaultValue: 'See more' })}`}
</button>
)}
{sources.length > 0 && (
<ReferenceSources
anchorEl={anchorEl}
onClosePopover={() => setAnchorEl(null)}
onCloseSearch={onClose}
sources={sources}
/>
)}
</>
);
}
function ReferenceSources({
anchorEl,
onClosePopover,
onCloseSearch,
sources,
}: {
anchorEl: HTMLElement | null;
onClosePopover: () => void;
onCloseSearch: () => void;
sources: SearchOverviewSource[];
}) {
const { t } = useTranslation();
const navigateToView = useToView();
const open = Boolean(anchorEl);
return (
<Popover
open={open}
anchorEl={anchorEl}
onClose={onClosePopover}
anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }}
transformOrigin={{ vertical: 'top', horizontal: 'left' }}
slotProps={{
paper: {
className: 'mt-1 max-h-[420px] w-[360px] overflow-hidden rounded-[8px] bg-surface-primary p-2 shadow-menu',
},
}}
>
<div className='px-2 py-1 text-sm font-medium text-text-secondary'>
{t('commandPalette.aiOverviewSource', { defaultValue: 'Reference sources' })}
</div>
<div className='appflowy-scroller max-h-[360px] overflow-y-auto'>
{sources.map((source) => (
<button
key={source.id}
type='button'
className='flex w-full items-start gap-2 rounded-[8px] px-2 py-3 text-left hover:bg-fill-content-hover'
onClick={() => {
onClosePopover();
onCloseSearch();
void navigateToView(source.targetViewId, source.targetRowId || undefined);
}}
>
<span className='flex h-5 w-5 shrink-0 items-center justify-center'>
{source.view ? (
<PageIcon view={source.view} className='h-5 w-5' />
) : (
<ToolbarLinkIcon className='h-4 w-4 text-icon-primary' />
)}
</span>
<span className='min-w-0 flex-1 truncate text-sm text-text-primary'>{source.name}</span>
</button>
))}
</div>
</Popover>
);
}
function AskAIButton({ askingAI, query, onAskAI }: { askingAI: boolean; query: string; onAskAI: () => void }) {
const { t } = useTranslation();
return (
<button
type='button'
disabled={askingAI}
className='flex w-full items-center gap-2 rounded-[8px] p-3 text-left hover:bg-fill-content-hover disabled:cursor-default disabled:opacity-60'
onClick={onAskAI}
>
<HomeAIChatIcon className='h-5 w-5 shrink-0' />
<span className='min-w-0 flex-1 truncate text-sm leading-[22px] text-text-primary'>
{query ? (
<>
{t('search.askAIFor', { defaultValue: 'Ask AI for' })} <span className='font-medium'>{`"${query}"`}</span>
</>
) : (
t('search.askAIAnything', { defaultValue: 'Ask AI anything' })
)}
</span>
</button>
);
}
export function SearchAIOverview({
askingAI,
loading,
query,
sources,
summary,
onAskAI,
onClose,
}: SearchAIOverviewProps) {
const { t } = useTranslation();
if (loading) {
return (
<div className='px-2 py-1'>
<div className='flex items-center gap-2 p-3 text-sm leading-[22px] text-text-secondary'>
<HomeAIChatIcon className='h-5 w-5 shrink-0' />
<span>{t('search.searching', { defaultValue: 'Searching...' })}</span>
</div>
</div>
);
}
if (!summary?.content) {
return (
<div className='px-2 py-1'>
<AskAIButton askingAI={askingAI} query={query} onAskAI={() => onAskAI()} />
</div>
);
}
return (
<div className='px-2 pb-5 pt-3'>
<div className='mb-3 flex items-center gap-2 text-sm font-medium leading-[22px] tracking-[0.2px] text-text-secondary'>
<AISearchingIcon className='h-5 w-5 shrink-0' />
<span>{t('commandPalette.aiOverview', { defaultValue: 'AI overview' })}</span>
</div>
<HighlightedSummary
content={summary.content}
highlights={summary.highlights}
query={query}
sources={sources}
onClose={onClose}
/>
<button
type='button'
disabled={askingAI}
className={cn(
'mt-3 inline-flex h-8 w-36 items-center justify-center gap-1.5 rounded-[16px] border border-border-primary px-3 py-1.5 text-sm font-medium leading-[22px] text-text-primary',
'hover:bg-fill-content-hover disabled:cursor-default disabled:opacity-60'
)}
onClick={() => onAskAI(sources.map((source) => source.ragId))}
>
<ChatAIPageIcon className='h-5 w-5 shrink-0 text-icon-primary' />
{t('commandPalette.aiAskFollowUp', { defaultValue: 'Ask follow-up' })}
</button>
</div>
);
}