tooot/src/screens/Tabs/Shared/Search.tsx

284 lines
9.3 KiB
TypeScript
Raw Normal View History

2021-01-19 01:13:45 +01:00
import ComponentAccount from '@components/Account'
import ComponentHashtag from '@components/Hashtag'
2022-12-03 16:50:54 +01:00
import { HeaderLeft } from '@components/Header'
2021-01-19 01:13:45 +01:00
import ComponentSeparator from '@components/Separator'
import CustomText from '@components/Text'
2021-02-08 23:47:20 +01:00
import TimelineDefault from '@components/Timeline/Default'
2021-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2021-01-11 21:36:57 +01:00
import { useSearchQuery } from '@utils/queryHooks/search'
import { useTrendsQuery } from '@utils/queryHooks/trends'
2021-01-01 16:48:16 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2022-12-03 16:50:54 +01:00
import { debounce } from 'lodash'
import React, { useEffect } from 'react'
2021-01-19 01:13:45 +01:00
import { Trans, useTranslation } from 'react-i18next'
2022-12-03 16:50:54 +01:00
import {
KeyboardAvoidingView,
Platform,
SectionList,
StyleSheet,
TextInput,
View
} from 'react-native'
2021-02-08 23:47:20 +01:00
import { Circle } from 'react-native-animated-spinkit'
2020-11-24 00:18:47 +01:00
2022-11-29 23:44:11 +01:00
const TabSharedSearch: React.FC<TabSharedStackScreenProps<'Tab-Shared-Search'>> = ({
2022-12-03 16:50:54 +01:00
navigation,
2021-03-14 00:47:55 +01:00
route: {
params: { text }
}
}) => {
2021-03-28 23:31:10 +02:00
const { t } = useTranslation('screenTabs')
2022-12-03 16:50:54 +01:00
const { colors, mode } = useTheme()
useEffect(() => {
navigation.setOptions({
...(Platform.OS === 'ios'
? {
headerLeft: () => <HeaderLeft onPress={() => navigation.goBack()} />
}
: { headerLeft: () => null }),
headerTitle: () => {
return (
<View
style={{
flexBasis: '80%',
flexDirection: 'row',
alignItems: 'center'
}}
>
<TextInput
editable={false}
style={{
fontSize: StyleConstants.Font.Size.M,
color: colors.primaryDefault
}}
defaultValue={t('shared.search.header.prefix')}
/>
<TextInput
accessibilityRole='search'
keyboardAppearance={mode}
style={{
fontSize: StyleConstants.Font.Size.M,
flex: 1,
color: colors.primaryDefault,
paddingLeft: StyleConstants.Spacing.XS
}}
autoFocus
value={text}
onChangeText={debounce((text: string) => navigation.setParams({ text }), 1000, {
trailing: true
})}
2022-12-03 16:50:54 +01:00
autoCapitalize='none'
autoCorrect={false}
clearButtonMode='never'
keyboardType='web-search'
onSubmitEditing={({ nativeEvent: { text } }) => navigation.setParams({ text })}
placeholder={t('shared.search.header.placeholder')}
placeholderTextColor={colors.secondary}
returnKeyType='go'
/>
</View>
)
}
})
}, [text, mode])
const trendsTags = useTrendsQuery({ type: 'tags' })
2020-12-19 01:57:57 +01:00
2021-01-19 01:13:45 +01:00
const mapKeyToTranslations = {
2021-03-28 23:31:10 +02:00
accounts: t('shared.search.sections.accounts'),
hashtags: t('shared.search.sections.hashtags'),
statuses: t('shared.search.sections.statuses')
2021-01-19 01:13:45 +01:00
}
2021-12-18 19:59:38 +01:00
const { status, data } = useSearchQuery<
{
title: string
translation: string
data: any[]
}[]
>({
2021-03-14 00:47:55 +01:00
term: text,
options: {
enabled: text !== undefined,
select: data =>
2020-12-19 01:57:57 +01:00
Object.keys(data as Mastodon.Results)
2020-12-18 19:02:18 +01:00
.map(key => ({
title: key,
// @ts-ignore
2021-01-19 01:13:45 +01:00
translation: mapKeyToTranslations[key],
// @ts-ignore
2020-12-18 19:02:18 +01:00
data: data[key]
}))
.sort((a, b) => {
if (!a.data.length) {
return 1
} else if (!b.data.length) {
return -1
} else {
return 0
}
})
2020-12-19 01:57:57 +01:00
}
2021-03-14 00:47:55 +01:00
})
2020-12-18 19:02:18 +01:00
const listEmpty = () => {
2021-01-19 01:13:45 +01:00
return (
<View style={{ paddingVertical: StyleConstants.Spacing.Global.PagePadding }}>
{status === 'loading' ? (
<View style={{ flex: 1, alignItems: 'center' }}>
<Circle size={StyleConstants.Font.Size.M * 1.25} color={colors.secondary} />
</View>
) : (
<>
<View style={{ paddingHorizontal: StyleConstants.Spacing.Global.PagePadding }}>
<CustomText
fontStyle='S'
style={{
marginBottom: StyleConstants.Spacing.L,
color: colors.primaryDefault
}}
2021-01-04 18:29:02 +01:00
>
2021-01-19 01:13:45 +01:00
<Trans
2021-03-28 23:31:10 +02:00
i18nKey='screenTabs:shared.search.empty.general'
components={{
2022-05-10 23:19:26 +02:00
bold: <CustomText fontWeight='Bold' />
}}
2021-01-19 01:13:45 +01:00
/>
</CustomText>
<CustomText
style={[styles.emptyAdvanced, { color: colors.primaryDefault }]}
fontWeight='Bold'
>
2021-03-28 23:31:10 +02:00
{t('shared.search.empty.advanced.header')}
</CustomText>
2022-11-29 23:44:11 +01:00
<CustomText style={[styles.emptyAdvanced, { color: colors.primaryDefault }]}>
<CustomText style={{ color: colors.secondary }}>@username@domain</CustomText>
2021-01-04 18:29:02 +01:00
{' '}
2021-03-28 23:31:10 +02:00
{t('shared.search.empty.advanced.example.account')}
</CustomText>
2022-11-29 23:44:11 +01:00
<CustomText style={[styles.emptyAdvanced, { color: colors.primaryDefault }]}>
<CustomText style={{ color: colors.secondary }}>#example</CustomText>
2021-01-19 01:13:45 +01:00
{' '}
2021-03-28 23:31:10 +02:00
{t('shared.search.empty.advanced.example.hashtag')}
</CustomText>
2022-11-29 23:44:11 +01:00
<CustomText style={[styles.emptyAdvanced, { color: colors.primaryDefault }]}>
<CustomText style={{ color: colors.secondary }}>URL</CustomText>
2021-01-19 01:13:45 +01:00
{' '}
2021-03-28 23:31:10 +02:00
{t('shared.search.empty.advanced.example.statusLink')}
</CustomText>
2022-11-29 23:44:11 +01:00
<CustomText style={[styles.emptyAdvanced, { color: colors.primaryDefault }]}>
<CustomText style={{ color: colors.secondary }}>URL</CustomText>
2021-01-19 01:13:45 +01:00
{' '}
2021-03-28 23:31:10 +02:00
{t('shared.search.empty.advanced.example.accountLink')}
</CustomText>
</View>
<CustomText
style={{
color: colors.primaryDefault,
marginTop: StyleConstants.Spacing.M,
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding
}}
fontWeight='Bold'
>
{t('shared.search.empty.trending.tags')}
</CustomText>
<View>
{trendsTags.data?.map((tag, index) => {
const hashtag = tag as Mastodon.Tag
return (
<React.Fragment key={index}>
{index !== 0 ? <ComponentSeparator /> : null}
<ComponentHashtag
hashtag={hashtag}
onPress={() => navigation.setParams({ text: `#${hashtag.name}` })}
/>
</React.Fragment>
)
})}
</View>
</>
)}
</View>
2021-01-19 01:13:45 +01:00
)
}
2020-12-18 19:02:18 +01:00
return (
2021-01-14 00:43:35 +01:00
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={{ flex: 1 }}
>
2021-01-04 18:29:02 +01:00
<SectionList
style={{ minHeight: '100%' }}
renderItem={({ item, section }: { item: any; section: any }) => {
switch (section.title) {
case 'accounts':
return <ComponentAccount account={item} />
case 'hashtags':
return <ComponentHashtag hashtag={item} />
case 'statuses':
return <TimelineDefault item={item} disableDetails />
default:
return null
}
}}
2021-01-04 18:29:02 +01:00
stickySectionHeadersEnabled
2021-03-14 00:47:55 +01:00
sections={data || []}
ListEmptyComponent={listEmpty()}
2021-01-04 18:29:02 +01:00
keyboardShouldPersistTaps='always'
2022-08-07 01:18:10 +02:00
renderSectionHeader={({ section: { translation } }) => (
<View
style={{
padding: StyleConstants.Spacing.M,
backgroundColor: colors.backgroundDefault
}}
>
<CustomText
fontStyle='M'
style={{
textAlign: 'center',
color: colors.primaryDefault
}}
fontWeight='Bold'
>
{translation}
</CustomText>
</View>
)}
renderSectionFooter={({ section: { data, translation } }) =>
!data.length ? (
<View
style={{
padding: StyleConstants.Spacing.S,
backgroundColor: colors.backgroundDefault
}}
>
2022-11-29 23:44:11 +01:00
<CustomText fontStyle='S' style={{ textAlign: 'center', color: colors.secondary }}>
2022-08-07 01:18:10 +02:00
<Trans
i18nKey='screenTabs:shared.search.notFound'
values={{ searchTerm: text, type: translation }}
components={{
bold: <CustomText fontWeight='Bold' />
}}
/>
</CustomText>
</View>
) : null
}
2021-01-04 18:29:02 +01:00
keyExtractor={(item, index) => item + index}
SectionSeparatorComponent={ComponentSeparator}
ItemSeparatorComponent={ComponentSeparator}
/>
</KeyboardAvoidingView>
2020-12-18 19:02:18 +01:00
)
2020-11-24 00:18:47 +01:00
}
2020-12-18 19:02:18 +01:00
const styles = StyleSheet.create({
emptyAdvanced: {
marginBottom: StyleConstants.Spacing.S
}
})
2021-01-30 01:29:15 +01:00
export default TabSharedSearch