2022-09-09 07:24:25 -07:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
import React, { useState } from 'react';
|
2022-12-30 13:49:29 -08:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
2022-12-08 11:37:04 -08:00
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-12-08 11:37:04 -08:00
|
|
|
import { Icon, Input, Stack } from 'soapbox/components/ui';
|
2022-11-02 12:28:16 -07:00
|
|
|
import { ChatWidgetScreens, useChatContext } from 'soapbox/contexts/chat-context';
|
2022-12-20 08:34:53 -08:00
|
|
|
import { useDebounce } from 'soapbox/hooks';
|
2022-09-09 07:24:25 -07:00
|
|
|
import { useChats } from 'soapbox/queries/chats';
|
|
|
|
import { queryClient } from 'soapbox/queries/client';
|
|
|
|
import useAccountSearch from 'soapbox/queries/search';
|
2022-12-20 08:34:53 -08:00
|
|
|
import toast from 'soapbox/toast';
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-10-05 12:15:16 -07:00
|
|
|
import { ChatKeys } from '../../../../queries/chats';
|
2022-09-13 08:55:13 -07:00
|
|
|
|
|
|
|
import Blankslate from './blankslate';
|
|
|
|
import EmptyResultsBlankslate from './empty-results-blankslate';
|
|
|
|
import Results from './results';
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-12-30 13:49:29 -08:00
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: { id: 'chat_search.placeholder', defaultMessage: 'Type a name' },
|
|
|
|
});
|
|
|
|
|
2022-12-08 11:37:04 -08:00
|
|
|
interface IChatSearch {
|
|
|
|
isMainPage?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChatSearch = (props: IChatSearch) => {
|
2022-12-30 13:49:29 -08:00
|
|
|
const intl = useIntl();
|
2022-12-08 11:37:04 -08:00
|
|
|
const { isMainPage = false } = props;
|
2022-09-14 07:35:32 -07:00
|
|
|
|
2022-09-09 07:24:25 -07:00
|
|
|
const debounce = useDebounce;
|
2022-12-08 11:37:04 -08:00
|
|
|
const history = useHistory();
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-12-08 11:37:04 -08:00
|
|
|
const { changeScreen } = useChatContext();
|
2022-09-09 07:24:25 -07:00
|
|
|
const { getOrCreateChatByAccountId } = useChats();
|
|
|
|
|
2022-12-01 09:40:30 -08:00
|
|
|
const [value, setValue] = useState<string>('');
|
2022-09-09 07:24:25 -07:00
|
|
|
const debouncedValue = debounce(value as string, 300);
|
|
|
|
|
2022-12-08 11:37:04 -08:00
|
|
|
const accountSearchResult = useAccountSearch(debouncedValue);
|
|
|
|
const { data: accounts, isFetching } = accountSearchResult;
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-09-13 08:55:13 -07:00
|
|
|
const hasSearchValue = debouncedValue && debouncedValue.length > 0;
|
|
|
|
const hasSearchResults = (accounts || []).length > 0;
|
2022-09-09 07:24:25 -07:00
|
|
|
|
|
|
|
const handleClickOnSearchResult = useMutation((accountId: string) => {
|
|
|
|
return getOrCreateChatByAccountId(accountId);
|
|
|
|
}, {
|
|
|
|
onError: (error: AxiosError) => {
|
|
|
|
const data = error.response?.data as any;
|
2022-12-20 08:34:53 -08:00
|
|
|
toast.error(data?.error);
|
2022-09-09 07:24:25 -07:00
|
|
|
},
|
|
|
|
onSuccess: (response) => {
|
2022-12-08 11:37:04 -08:00
|
|
|
if (isMainPage) {
|
|
|
|
history.push(`/chats/${response.data.id}`);
|
|
|
|
} else {
|
|
|
|
changeScreen(ChatWidgetScreens.CHAT, response.data.id);
|
|
|
|
}
|
|
|
|
|
2022-10-05 12:15:16 -07:00
|
|
|
queryClient.invalidateQueries(ChatKeys.chatSearch());
|
2022-09-09 07:24:25 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-09-13 08:55:13 -07:00
|
|
|
const renderBody = () => {
|
|
|
|
if (hasSearchResults) {
|
|
|
|
return (
|
|
|
|
<Results
|
2022-12-08 11:37:04 -08:00
|
|
|
accountSearchResult={accountSearchResult}
|
2022-09-13 08:55:13 -07:00
|
|
|
onSelect={(id) => {
|
|
|
|
handleClickOnSearchResult.mutate(id);
|
|
|
|
clearValue();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (hasSearchValue && !hasSearchResults && !isFetching) {
|
|
|
|
return <EmptyResultsBlankslate />;
|
|
|
|
} else {
|
|
|
|
return <Blankslate />;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-09 07:24:25 -07:00
|
|
|
const clearValue = () => {
|
|
|
|
if (hasSearchValue) {
|
|
|
|
setValue('');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-02-01 14:13:42 -08:00
|
|
|
<Stack space={4} className='h-full grow'>
|
2022-12-08 11:37:04 -08:00
|
|
|
<div className='px-4'>
|
|
|
|
<Input
|
|
|
|
data-testid='search'
|
|
|
|
type='text'
|
|
|
|
autoFocus
|
2022-12-30 13:49:29 -08:00
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
2022-12-08 11:37:04 -08:00
|
|
|
value={value || ''}
|
|
|
|
onChange={(event) => setValue(event.target.value)}
|
|
|
|
outerClassName='mt-0'
|
|
|
|
theme='search'
|
|
|
|
append={
|
|
|
|
<button onClick={clearValue}>
|
2022-09-09 07:24:25 -07:00
|
|
|
<Icon
|
2022-12-08 11:37:04 -08:00
|
|
|
src={hasSearchValue ? require('@tabler/icons/x.svg') : require('@tabler/icons/search.svg')}
|
|
|
|
className='h-4 w-4 text-gray-700 dark:text-gray-600'
|
|
|
|
aria-hidden='true'
|
2022-09-09 07:24:25 -07:00
|
|
|
/>
|
|
|
|
</button>
|
2022-12-08 11:37:04 -08:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2023-02-01 14:13:42 -08:00
|
|
|
<Stack className='grow'>
|
2022-12-08 11:37:04 -08:00
|
|
|
{renderBody()}
|
|
|
|
</Stack>
|
|
|
|
</Stack>
|
2022-09-09 07:24:25 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-30 13:49:29 -08:00
|
|
|
export default ChatSearch;
|