-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathSearchResults.tsx
279 lines (251 loc) Β· 8.63 KB
/
SearchResults.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
import React, { PropsWithChildren, useCallback, useEffect, useState } from 'react';
import clsx from 'clsx';
import { SearchIcon } from './icons';
import { ChannelPreview } from '../ChannelPreview';
import { ChannelOrUserResponse, isChannel } from './utils';
import { Avatar } from '../Avatar';
import { useChatContext, useTranslationContext } from '../../context';
import type { DefaultStreamChatGenerics } from '../../types/types';
const DefaultSearchEmpty = () => {
const { t } = useTranslationContext('SearchResults');
return (
<div aria-live='polite' className='str-chat__channel-search-container-empty'>
<SearchIcon />
{t<string>('No results found')}
</div>
);
};
export type SearchResultsHeaderProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = Pick<SearchResultsProps<StreamChatGenerics>, 'results'>;
const DefaultSearchResultsHeader = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>({
results,
}: SearchResultsHeaderProps<StreamChatGenerics>) => {
const { t } = useTranslationContext('SearchResultsHeader');
return (
<div
className='str-chat__channel-search-results-header'
data-testid='channel-search-results-header'
>
{t<string>('searchResultsCount', {
count: results.length,
})}
</div>
);
};
export type SearchResultsListProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = Pick<
SearchResultsProps<StreamChatGenerics>,
'results' | 'SearchResultItem' | 'selectResult'
> & {
focusedUser?: number;
};
const DefaultSearchResultsList = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
props: SearchResultsListProps<StreamChatGenerics>,
) => {
const { focusedUser, results, SearchResultItem = DefaultSearchResultItem, selectResult } = props;
return (
<>
{results.map((result, index) => (
<SearchResultItem
focusedUser={focusedUser}
index={index}
key={index}
result={result}
selectResult={selectResult}
/>
))}
</>
);
};
export type SearchResultItemProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = Pick<SearchResultsProps<StreamChatGenerics>, 'selectResult'> & {
index: number;
result: ChannelOrUserResponse<StreamChatGenerics>;
focusedUser?: number;
};
const DefaultSearchResultItem = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
props: SearchResultItemProps<StreamChatGenerics>,
) => {
const { focusedUser, index, result, selectResult } = props;
const focused = focusedUser === index;
const { themeVersion } = useChatContext();
const className = clsx(
'str-chat__channel-search-result',
focused && 'str-chat__channel-search-result--focused focused',
);
if (isChannel(result)) {
const channel = result;
return themeVersion === '2' ? (
<ChannelPreview
channel={channel}
className={className}
onSelect={() => selectResult(channel)}
/>
) : (
<button
aria-label={`Select Channel: ${channel.data?.name || ''}`}
className={className}
data-testid='channel-search-result-channel'
onClick={() => selectResult(channel)}
role='option'
>
<div className='result-hashtag'>#</div>
<p className='channel-search__result-text'>{channel.data?.name}</p>
</button>
);
} else {
return (
<button
aria-label={`Select User Channel: ${result.name || ''}`}
className={className}
data-testid='channel-search-result-user'
onClick={() => selectResult(result)}
role='option'
>
<Avatar
image={result.image}
name={result.name || result.id}
size={themeVersion === '2' ? 40 : undefined}
user={result}
/>
<div className='str-chat__channel-search-result--display-name'>
{result.name || result.id}
</div>
</button>
);
}
};
const ResultsContainer = ({
children,
popupResults,
}: PropsWithChildren<{ popupResults?: boolean }>) => {
const { t } = useTranslationContext('ResultsContainer');
return (
<div
aria-label={t('aria/Channel search results')}
className={clsx(
`str-chat__channel-search-container str-chat__channel-search-result-list`,
popupResults ? 'popup' : 'inline',
)}
>
{children}
</div>
);
};
export type SearchResultsController<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = {
results: Array<ChannelOrUserResponse<StreamChatGenerics>> | [];
searching: boolean;
selectResult: (result: ChannelOrUserResponse<StreamChatGenerics>) => Promise<void> | void;
};
export type AdditionalSearchResultsProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = {
/** Display search results as an absolutely positioned popup, defaults to false and shows inline */
popupResults?: boolean;
/** Custom UI component to display empty search results */
SearchEmpty?: React.ComponentType;
/** Custom UI component to display the search loading state */
SearchLoading?: React.ComponentType;
/** Custom UI component to display a search result list item, defaults to and accepts the same props as: [DefaultSearchResultItem](https://github.com/GetStream/stream-chat-react/blob/master/src/components/ChannelSearch/SearchResults.tsx) */
SearchResultItem?: React.ComponentType<SearchResultItemProps<StreamChatGenerics>>;
/** Custom UI component to display the search results header */
SearchResultsHeader?: React.ComponentType;
/** Custom UI component to display all the search results, defaults to and accepts the same props as: [DefaultSearchResultsList](https://github.com/GetStream/stream-chat-react/blob/master/src/components/ChannelSearch/SearchResults.tsx) */
SearchResultsList?: React.ComponentType<SearchResultsListProps<StreamChatGenerics>>;
};
export type SearchResultsProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = AdditionalSearchResultsProps<StreamChatGenerics> & SearchResultsController<StreamChatGenerics>;
export const SearchResults = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
props: SearchResultsProps<StreamChatGenerics>,
) => {
const {
popupResults,
results,
searching,
SearchEmpty = DefaultSearchEmpty,
SearchResultsHeader = DefaultSearchResultsHeader,
SearchLoading,
SearchResultItem = DefaultSearchResultItem,
SearchResultsList = DefaultSearchResultsList,
selectResult,
} = props;
const { t } = useTranslationContext('SearchResults');
const [focusedResult, setFocusedResult] = useState<number>();
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (event.key === 'ArrowUp') {
setFocusedResult((prevFocused) => {
if (prevFocused === undefined) return 0;
return prevFocused === 0 ? results.length - 1 : prevFocused - 1;
});
}
if (event.key === 'ArrowDown') {
setFocusedResult((prevFocused) => {
if (prevFocused === undefined) return 0;
return prevFocused === results.length - 1 ? 0 : prevFocused + 1;
});
}
if (event.key === 'Enter') {
event.preventDefault();
if (focusedResult !== undefined) {
selectResult(results[focusedResult]);
return setFocusedResult(undefined);
}
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[focusedResult],
);
useEffect(() => {
document.addEventListener('keydown', handleKeyDown, false);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [handleKeyDown]);
if (searching) {
return (
<ResultsContainer popupResults={popupResults}>
{SearchLoading ? (
<SearchLoading />
) : (
<div
className='str-chat__channel-search-container-searching'
data-testid='search-in-progress-indicator'
>
{t<string>('Searching...')}
</div>
)}
</ResultsContainer>
);
}
if (!results.length) {
return (
<ResultsContainer popupResults={popupResults}>
<SearchEmpty />
</ResultsContainer>
);
}
return (
<ResultsContainer popupResults={popupResults}>
<SearchResultsHeader results={results} />
<SearchResultsList
focusedUser={focusedResult}
results={results}
SearchResultItem={SearchResultItem}
selectResult={selectResult}
/>
</ResultsContainer>
);
};