import { useMutation } from '@tanstack/react-query'; import { AxiosError } from 'axios'; import React, { useState } from 'react'; import snackbar from 'soapbox/actions/snackbar'; import { Avatar, HStack, Icon, Input, Stack, Text } from 'soapbox/components/ui'; import VerificationBadge from 'soapbox/components/verification_badge'; import { useChatContext } from 'soapbox/contexts/chat-context'; import { useAppDispatch, useDebounce } from 'soapbox/hooks'; import { useChats } from 'soapbox/queries/chats'; import { queryClient } from 'soapbox/queries/client'; import useAccountSearch from 'soapbox/queries/search'; import ChatPaneHeader from './chat-pane-header'; import { Pane } from './ui'; const ChatSearch = () => { const debounce = useDebounce; const dispatch = useAppDispatch(); const { isOpen, setChat, setSearching, toggleChatPane } = useChatContext(); const { getOrCreateChatByAccountId } = useChats(); const [value, setValue] = useState(); const debouncedValue = debounce(value as string, 300); const { data: accounts } = useAccountSearch(debouncedValue); const hasSearchValue = value && value.length > 0; const handleClickOnSearchResult = useMutation((accountId: string) => { return getOrCreateChatByAccountId(accountId); }, { onError: (error: AxiosError) => { const data = error.response?.data as any; dispatch(snackbar.error(data?.error)); }, onSuccess: (response) => { setChat(response.data); queryClient.invalidateQueries(['chats']); }, }); const clearValue = () => { if (hasSearchValue) { setValue(''); } }; return ( Messages } isOpen={isOpen} isToggleable={false} onToggle={toggleChatPane} /> {isOpen ? (
setValue(event.target.value)} isSearch append={ } />
{(accounts || []).map((account: any) => ( ))}
) : null}
); }; export default ChatSearch;