tooot/src/screens/Compose/Root.tsx

130 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-01-16 00:00:31 +01:00
import ComponentSeparator from '@components/Separator'
2021-01-11 21:36:57 +01:00
import { useEmojisQuery } from '@utils/queryHooks/emojis'
import { useSearchQuery } from '@utils/queryHooks/search'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2020-12-04 01:17:10 +01:00
import { forEach, groupBy, sortBy } from 'lodash'
2021-01-07 19:13:09 +01:00
import React, { useCallback, useContext, useEffect, useMemo } from 'react'
2021-02-27 16:33:54 +01:00
import { FlatList, Image, StyleSheet, View } from 'react-native'
2021-02-08 23:47:20 +01:00
import { Circle } from 'react-native-animated-spinkit'
2021-01-19 01:13:45 +01:00
import ComposeActions from './Root/Actions'
2021-01-14 22:53:01 +01:00
import ComposePosting from './Posting'
2021-01-01 17:52:14 +01:00
import ComposeRootFooter from './Root/Footer'
import ComposeRootHeader from './Root/Header'
2021-01-07 19:13:09 +01:00
import ComposeRootSuggestion from './Root/Suggestion'
2021-01-01 17:52:14 +01:00
import ComposeContext from './utils/createContext'
2021-02-07 00:39:11 +01:00
import ComposeDrafts from './Root/Drafts'
2020-12-04 01:17:10 +01:00
2021-02-27 16:33:54 +01:00
const prefetchEmojis = (
sortedEmojis: { title: string; data: Mastodon.Emoji[] }[]
) => {
let requestedIndex = 0
sortedEmojis.map(sorted => {
sorted.data.map(emoji => {
if (requestedIndex > 40) {
return
}
Image.prefetch(emoji.url)
requestedIndex++
})
})
}
const ComposeRoot: React.FC = () => {
const { theme } = useTheme()
const { composeState, composeDispatch } = useContext(ComposeContext)
2021-01-11 21:36:57 +01:00
const { isFetching, data, refetch } = useSearchQuery({
2021-01-07 19:13:09 +01:00
type:
composeState.tag?.type === 'accounts' ||
composeState.tag?.type === 'hashtags'
? composeState.tag.type
: undefined,
term: composeState.tag?.text.substring(1),
options: { enabled: false }
})
2020-12-04 01:17:10 +01:00
useEffect(() => {
2021-01-07 19:13:09 +01:00
if (
(composeState.tag?.type === 'accounts' ||
composeState.tag?.type === 'hashtags') &&
composeState.tag?.text
) {
2020-12-04 19:04:23 +01:00
refetch()
}
2021-01-07 19:13:09 +01:00
}, [composeState.tag])
2020-12-04 01:17:10 +01:00
2021-01-11 21:36:57 +01:00
const { data: emojisData } = useEmojisQuery({})
2020-11-15 20:29:43 +01:00
useEffect(() => {
if (emojisData && emojisData.length) {
2020-12-04 01:17:10 +01:00
let sortedEmojis: { title: string; data: Mastodon.Emoji[] }[] = []
forEach(
groupBy(sortBy(emojisData, ['category', 'shortcode']), 'category'),
(value, key) => sortedEmojis.push({ title: key, data: value })
)
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-04 01:17:10 +01:00
type: 'emoji',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.emoji, emojis: sortedEmojis }
2020-12-04 01:17:10 +01:00
})
2021-02-27 16:33:54 +01:00
prefetchEmojis(sortedEmojis)
2020-11-15 20:29:43 +01:00
}
}, [emojisData])
2020-12-04 01:17:10 +01:00
const listEmpty = useMemo(() => {
if (isFetching) {
return (
2020-12-30 00:56:25 +01:00
<View key='listEmpty' style={styles.loading}>
2021-02-08 23:47:20 +01:00
<Circle
size={StyleConstants.Font.Size.M * 1.25}
color={theme.secondary}
/>
</View>
)
2020-12-04 01:17:10 +01:00
}
}, [isFetching])
2020-12-10 19:19:56 +01:00
const listItem = useCallback(
2021-02-07 00:39:11 +01:00
({ item }) => (
2021-01-07 19:13:09 +01:00
<ComposeRootSuggestion
2020-12-30 00:56:25 +01:00
item={item}
composeState={composeState}
composeDispatch={composeDispatch}
/>
),
[composeState]
2020-12-10 19:19:56 +01:00
)
2020-12-30 00:56:25 +01:00
2020-12-04 01:17:10 +01:00
return (
<View style={styles.base}>
<FlatList
2021-01-07 19:13:09 +01:00
renderItem={listItem}
ListEmptyComponent={listEmpty}
2020-12-06 22:32:36 +01:00
keyboardShouldPersistTaps='handled'
2020-12-30 00:56:25 +01:00
ListHeaderComponent={ComposeRootHeader}
ListFooterComponent={ComposeRootFooter}
2021-01-16 00:00:31 +01:00
ItemSeparatorComponent={ComponentSeparator}
2021-01-07 19:13:09 +01:00
// @ts-ignore
data={data ? data[composeState.tag?.type] : undefined}
2021-02-07 00:39:11 +01:00
keyExtractor={() => Math.random().toString()}
2020-12-04 01:17:10 +01:00
/>
<ComposeActions />
2021-02-07 00:39:11 +01:00
<ComposeDrafts />
2021-01-14 22:53:01 +01:00
<ComposePosting />
2020-11-15 23:33:01 +01:00
</View>
2020-11-15 20:29:43 +01:00
)
}
const styles = StyleSheet.create({
2020-11-20 01:41:46 +01:00
base: {
2020-11-15 20:29:43 +01:00
flex: 1
},
2020-12-03 22:03:06 +01:00
contentView: { flex: 1 },
loading: {
flex: 1,
alignItems: 'center'
2020-11-15 20:29:43 +01:00
}
})
2020-12-03 22:03:06 +01:00
export default ComposeRoot