import ComponentAccount from '@components/Account' import ComponentHashtag from '@components/Hashtag' import { HeaderLeft } from '@components/Header' import ComponentSeparator from '@components/Separator' import CustomText from '@components/Text' import TimelineDefault from '@components/Timeline/Default' import { TabSharedStackScreenProps } from '@utils/navigation/navigators' import { useSearchQuery } from '@utils/queryHooks/search' import { StyleConstants } from '@utils/styles/constants' import { useTheme } from '@utils/styles/ThemeManager' import { debounce } from 'lodash' import React, { useEffect, useRef, useState } from 'react' import { Trans, useTranslation } from 'react-i18next' import { KeyboardAvoidingView, Platform, SectionList, TextInput, View } from 'react-native' import SearchEmpty from './Empty' const TabSharedSearch: React.FC> = ({ navigation }) => { const { t } = useTranslation('screenTabs') const { colors, mode } = useTheme() const inputRef = useRef(null) const [searchTerm, setSearchTerm] = useState('') useEffect(() => { navigation.setOptions({ ...(Platform.OS === 'ios' ? { headerLeft: () => navigation.goBack()} /> } : { headerLeft: () => null }), headerTitle: () => { return ( { setSearchTerm(text) refetch() }, 1000, { trailing: true } )} autoCapitalize='none' autoCorrect={false} clearButtonMode='always' keyboardType='web-search' placeholder={t('shared.search.header.placeholder')} placeholderTextColor={colors.secondary} returnKeyType='search' /> ) } }) }, [mode]) const mapKeyToTranslations = { accounts: t('shared.search.sections.accounts'), hashtags: t('shared.search.sections.hashtags'), statuses: t('shared.search.sections.statuses') } const { isFetching, data, refetch } = useSearchQuery< { title: string translation: string data: any[] }[] >({ term: searchTerm, options: { enabled: !!searchTerm.length, select: data => Object.keys(data as Mastodon.Results) .map(key => ({ title: key, // @ts-ignore translation: mapKeyToTranslations[key], // @ts-ignore data: data[key] })) .filter(d => d.data.length) .sort((a, b) => { if (!a.data.length) { return 1 } else if (!b.data.length) { return -1 } else { return 0 } }) } }) return ( { switch (section.title) { case 'accounts': return case 'hashtags': return case 'statuses': return default: return null } }} stickySectionHeadersEnabled ListEmptyComponent={ } keyboardShouldPersistTaps='always' renderSectionHeader={({ section: { translation } }) => ( {translation} )} renderSectionFooter={({ section: { data, translation } }) => !data.length ? ( }} /> ) : null } keyExtractor={(item, index) => item + index} SectionSeparatorComponent={ComponentSeparator} ItemSeparatorComponent={ComponentSeparator} /> ) } export default TabSharedSearch