-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathsearch.tsx
More file actions
313 lines (272 loc) · 13.9 KB
/
Copy pathsearch.tsx
File metadata and controls
313 lines (272 loc) · 13.9 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
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
import APAvatar from '@components/global/ap-avatar';
import ActivityItem from '@components/activities/activity-item';
import FollowButton from '@components/global/follow-button';
import ProfilePreviewHoverCard from '../global/profile-preview-hover-card';
import React, {useEffect, useMemo, useRef, useState} from 'react';
import {ActorProperties} from '@tryghost/admin-x-framework/api/activitypub';
import {Button, Input, LoadingIndicator, NoValueLabel, NoValueLabelIcon} from '@tryghost/shade/components';
import {H4} from '@tryghost/shade/primitives';
import {LucideIcon} from '@tryghost/shade/utils';
import {SuggestedProfiles} from '../global/suggested-profiles';
import {useAccountForUser, useSearchForUser, useSuggestedProfilesForUser, useTopicsForUser} from '@hooks/use-activity-pub-queries';
import {useDebounce} from 'use-debounce';
import {useNavigateWithBasePath} from '@src/hooks/use-navigate-with-base-path';
interface AccountSearchResult {
id: string;
name: string;
handle: string;
avatarUrl: string;
followedByMe: boolean;
blockedByMe: boolean;
domainBlockedByMe: boolean;
}
interface TopicSearchResult {
slug: string;
name: string;
}
type SearchResult =
| {type: 'topic'; data: TopicSearchResult}
| {type: 'account'; data: AccountSearchResult};
interface SearchProps {
onOpenChange?: (open: boolean) => void;
query: string;
setQuery: (query: string) => void;
}
const STICKY_HEADER_HEIGHT = 80;
const Search: React.FC<SearchProps> = ({onOpenChange, query, setQuery}) => {
const queryInputRef = useRef<HTMLInputElement>(null);
const itemRefs = useRef<(HTMLDivElement | null)[]>([]);
const lastKeyPressRef = useRef(0);
const navigate = useNavigateWithBasePath();
const [debouncedQuery] = useDebounce(query, 300);
const shouldSearch = query.length >= 2;
const {searchQuery, updateAccountSearchResult: updateResult} = useSearchForUser('index', shouldSearch ? debouncedQuery : '');
const {data, isFetching, isFetched} = searchQuery;
const {suggestedProfilesQuery} = useSuggestedProfilesForUser('index', 5);
const {data: suggestedProfilesData, isLoading: isLoadingSuggestedProfiles} = suggestedProfilesQuery;
const hasSuggestedProfiles = isLoadingSuggestedProfiles || (suggestedProfilesData && suggestedProfilesData.length > 0);
const {topicsQuery} = useTopicsForUser();
const {data: topicsData} = topicsQuery;
const currentAccountQuery = useAccountForUser('index', 'me');
const {data: currentUser} = currentAccountQuery;
const [displayResults, setDisplayResults] = useState<AccountSearchResult[]>([]);
const [lastResultState, setLastResultState] = useState<'results' | 'none' | null>(null);
const [selectedIndex, setSelectedIndex] = useState(0);
// Filter topics client-side
const matchingTopics = useMemo(() => {
const topics = topicsData?.topics || [];
if (!shouldSearch || topics.length === 0) {
return [];
}
const normalizedQuery = query.toLowerCase();
return topics.filter((topic) => {
if (topic.slug === 'following') {
return false;
}
return topic.name.toLowerCase().startsWith(normalizedQuery) ||
topic.slug.toLowerCase().startsWith(normalizedQuery);
});
}, [query, shouldSearch, topicsData?.topics]);
// Merge topics and accounts into a single list
const searchResults: SearchResult[] = useMemo(() => [
...matchingTopics.map(topic => ({type: 'topic' as const, data: topic})),
...displayResults.map(account => ({type: 'account' as const, data: account}))
], [matchingTopics, displayResults]);
useEffect(() => {
if (!shouldSearch) {
setDisplayResults([]);
setLastResultState(null);
return;
}
if (!isFetched) {
return;
}
if (data?.accounts && data.accounts.length > 0) {
setDisplayResults(data.accounts);
setLastResultState('results');
setSelectedIndex(0);
} else {
setDisplayResults([]);
setLastResultState('none');
setSelectedIndex(0);
}
}, [data?.accounts, isFetched, shouldSearch]);
const showLoading = isFetching && shouldSearch;
const showSuggested = query.length < 2 || (!lastResultState && shouldSearch && matchingTopics.length === 0);
const showNoResults = !showSuggested && lastResultState === 'none' && matchingTopics.length === 0;
const showSearchResults = !showSuggested && (displayResults.length > 0 || matchingTopics.length > 0);
// Focus input on mount
useEffect(() => {
queryInputRef.current?.focus();
}, []);
// Scroll selected item into view
useEffect(() => {
const element = itemRefs.current[selectedIndex];
if (!element) {
return;
}
const container = element.closest('[data-radix-scroll-area-viewport]') || element.closest('.overflow-y-auto');
if (!container) {
element.scrollIntoView({block: 'nearest'});
return;
}
const containerRect = container.getBoundingClientRect();
const elementRect = element.getBoundingClientRect();
if (elementRect.top < containerRect.top + STICKY_HEADER_HEIGHT) {
container.scrollTo({
top: container.scrollTop - (containerRect.top + STICKY_HEADER_HEIGHT - elementRect.top),
behavior: 'smooth'
});
} else if (elementRect.bottom > containerRect.bottom - STICKY_HEADER_HEIGHT) {
container.scrollTo({
top: container.scrollTop + (elementRect.bottom - containerRect.bottom + STICKY_HEADER_HEIGHT),
behavior: 'smooth'
});
}
}, [selectedIndex]);
const handleKeyDown = (e: React.KeyboardEvent) => {
if (!showSearchResults || searchResults.length === 0) {
return;
}
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
// Throttle rapid key presses
const now = Date.now();
if (now - lastKeyPressRef.current < 50) {
return;
}
lastKeyPressRef.current = now;
if (e.key === 'ArrowDown') {
setSelectedIndex(prev => (prev + 1) % searchResults.length);
} else {
setSelectedIndex(prev => (prev - 1 + searchResults.length) % searchResults.length);
}
} else if (e.key === 'Enter') {
const item = searchResults[selectedIndex];
if (!item) {
return;
}
e.preventDefault();
onOpenChange?.(false);
if (item.type === 'topic') {
navigate(`/explore/${item.data.slug}`);
} else {
navigate(`/profile/${item.data.handle}`);
}
}
};
return (
<>
<div className='sticky -top-6 z-30 -mt-6 flex h-[72px] shrink-0 items-center gap-2 bg-white pt-3 pb-2 before:pointer-events-none before:absolute before:-inset-x-6 before:bottom-0 before:h-0 before:border-b before:border-b-gray-200 before:content-[""] dark:bg-surface-elevated-2 dark:before:border-b-gray-950'>
<LucideIcon.Search className='text-gray-600' size={18} strokeWidth={1.5} />
<Input
ref={queryInputRef}
autoComplete='off'
className='flex h-10 w-full items-center rounded-lg border-0 bg-transparent px-0 py-1.5 text-lg !shadow-none !outline-none focus-visible:!border-0 focus-visible:bg-transparent focus-visible:!shadow-none focus-visible:!ring-0 focus-visible:!outline-0 dark:bg-surface-elevated-2 dark:text-white dark:placeholder:text-gray-800'
placeholder='Search by name, handle, or URL...'
title="Search"
type='text'
value={query}
onChange={e => setQuery(e.target.value)}
onKeyDown={handleKeyDown}
/>
{showLoading && (
<LoadingIndicator className='absolute! right-0 mr-0.5 shrink-0' size='sm' />
)}
</div>
<div className='h-full'>
{showNoResults && (
<div className='flex h-full items-center justify-center pb-14'>
<NoValueLabel>
<NoValueLabelIcon><LucideIcon.UserRound /></NoValueLabelIcon>
No users matching this handle or account URL
</NoValueLabel>
</div>
)}
{showSearchResults && (
<div className='mt-[-14px] pb-2'>
{searchResults.map((item, index) => {
const isSelected = index === selectedIndex;
if (item.type === 'topic') {
return (
<div
key={item.data.slug}
ref={el => itemRefs.current[index] = el}
>
<ActivityItem
isSelected={isSelected}
onClick={() => {
onOpenChange?.(false);
navigate(`/explore/${item.data.slug}`);
}}
>
<div className='flex size-10 shrink-0 items-center justify-center rounded-full bg-gray-100 dark:bg-gray-900'>
<LucideIcon.Globe className='text-gray-700 dark:text-gray-500' size={18} strokeWidth={1.5} />
</div>
<div className='flex flex-col'>
<span className='font-semibold text-black dark:text-white'>{item.data.name}</span>
<span className='text-sm text-gray-700 dark:text-gray-600'>Topic</span>
</div>
</ActivityItem>
</div>
);
}
const account = item.data;
const isCurrentUser = account.handle === currentUser?.handle;
return (
<ProfilePreviewHoverCard
key={account.id}
actor={account as unknown as ActorProperties}
align='center'
isCurrentUser={isCurrentUser}
side='left'
>
<div ref={el => itemRefs.current[index] = el}>
<ActivityItem
isSelected={isSelected}
onClick={() => {
onOpenChange?.(false);
navigate(`/profile/${account.handle}`);
}}
>
<APAvatar author={{
icon: {url: account.avatarUrl},
name: account.name,
handle: account.handle
}}/>
<div className='break-anywhere flex flex-col'>
<span className='line-clamp-1 font-semibold text-black dark:text-white'>{account.name}</span>
<span className='line-clamp-1 text-sm text-gray-700 dark:text-gray-600'>{account.handle}</span>
</div>
{account.blockedByMe || account.domainBlockedByMe ? (
<Button className='pointer-events-none ml-auto min-w-[90px]' variant='destructive'>Blocked</Button>
) : !isCurrentUser ? (
<FollowButton
className='ml-auto'
following={account.followedByMe}
handle={account.handle}
type='secondary'
onFollow={() => updateResult(account.id, {followedByMe: true})}
onUnfollow={() => updateResult(account.id, {followedByMe: false})}
/>
) : null}
</ActivityItem>
</div>
</ProfilePreviewHoverCard>
);
})}
</div>
)}
{showSuggested && hasSuggestedProfiles && (
<>
<H4>More people to follow</H4>
<SuggestedProfiles
onOpenChange={onOpenChange}
/>
</>
)}
</div>
</>
);
};
export default Search;