tooot/src/screens/Shared/Compose/Actions.tsx

206 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-12-03 22:03:06 +01:00
import { Feather } from '@expo/vector-icons'
2020-12-06 21:42:19 +01:00
import React, { Dispatch, useCallback, useMemo } from 'react'
2020-12-03 22:03:06 +01:00
import {
ActionSheetIOS,
Keyboard,
Pressable,
StyleSheet,
Text,
TextInput
} from 'react-native'
import { StyleConstants } from 'src/utils/styles/constants'
import { useTheme } from 'src/utils/styles/ThemeManager'
import { PostAction, PostState } from '../Compose'
import addAttachments from './addAttachments'
export interface Props {
textInputRef: React.RefObject<TextInput>
postState: PostState
postDispatch: Dispatch<PostAction>
}
const ComposeActions: React.FC<Props> = ({
textInputRef,
postState,
postDispatch
}) => {
const { theme } = useTheme()
const getVisibilityIcon = () => {
switch (postState.visibility) {
case 'public':
return 'globe'
case 'unlisted':
return 'unlock'
case 'private':
return 'lock'
case 'direct':
return 'mail'
}
}
2020-12-06 21:42:19 +01:00
const attachmentColor = useMemo(() => {
if (postState.poll.active) return theme.disabled
if (postState.attachmentUploadProgress) return theme.primary
if (postState.attachments.length) {
return theme.primary
} else {
return theme.secondary
}
}, [
postState.poll.active,
postState.attachments,
postState.attachmentUploadProgress
])
const attachmentOnPress = useCallback(async () => {
if (postState.poll.active) return
if (postState.attachmentUploadProgress) return
if (!postState.attachments.length) {
return await addAttachments({ postState, postDispatch })
}
}, [
postState.poll.active,
postState.attachments,
postState.attachmentUploadProgress
])
const pollColor = useMemo(() => {
if (postState.attachments.length) return theme.disabled
if (postState.attachmentUploadProgress) return theme.disabled
if (postState.poll.active) {
return theme.primary
} else {
return theme.secondary
}
}, [
postState.poll.active,
postState.attachments,
postState.attachmentUploadProgress
])
const pollOnPress = useCallback(() => {
if (!postState.attachments.length && !postState.attachmentUploadProgress) {
postDispatch({
type: 'poll',
payload: { ...postState.poll, active: !postState.poll.active }
})
}
if (postState.poll.active) {
textInputRef.current?.focus()
}
}, [
postState.poll.active,
postState.attachments,
postState.attachmentUploadProgress
])
2020-12-06 22:32:36 +01:00
const emojiColor = useMemo(() => {
if (!postState.emoji.emojis) return theme.disabled
if (postState.emoji.active) {
return theme.primary
} else {
return theme.secondary
}
}, [postState.emoji.active, postState.emoji.emojis])
const emojiOnPress = useCallback(() => {
if (postState.emoji.emojis) {
if (postState.emoji.active) {
postDispatch({
type: 'emoji',
payload: { ...postState.emoji, active: false }
})
} else {
postDispatch({
type: 'emoji',
payload: { ...postState.emoji, active: true }
})
}
}
}, [postState.emoji.active, postState.emoji.emojis])
2020-12-03 22:03:06 +01:00
return (
<Pressable
style={[
styles.additions,
{ backgroundColor: theme.background, borderTopColor: theme.border }
]}
onPress={() => Keyboard.dismiss()}
>
<Feather
name='aperture'
size={24}
2020-12-06 21:42:19 +01:00
color={attachmentColor}
onPress={attachmentOnPress}
2020-12-03 22:03:06 +01:00
/>
<Feather
name='bar-chart-2'
size={24}
2020-12-06 21:42:19 +01:00
color={pollColor}
onPress={pollOnPress}
2020-12-03 22:03:06 +01:00
/>
<Feather
name={getVisibilityIcon()}
size={24}
2020-12-06 12:52:29 +01:00
color={theme.secondary}
2020-12-03 22:03:06 +01:00
onPress={() =>
ActionSheetIOS.showActionSheetWithOptions(
{
options: ['公开', '不公开', '仅关注着', '私信', '取消'],
cancelButtonIndex: 4
},
buttonIndex => {
switch (buttonIndex) {
case 0:
postDispatch({ type: 'visibility', payload: 'public' })
break
case 1:
postDispatch({ type: 'visibility', payload: 'unlisted' })
break
case 2:
postDispatch({ type: 'visibility', payload: 'private' })
break
case 3:
postDispatch({ type: 'visibility', payload: 'direct' })
break
}
}
)
}
/>
<Feather
name='smile'
size={24}
2020-12-06 22:32:36 +01:00
color={emojiColor}
onPress={emojiOnPress}
2020-12-03 22:03:06 +01:00
/>
<Text
style={[
styles.count,
2020-12-06 12:52:29 +01:00
{ color: postState.text.count < 0 ? theme.error : theme.secondary }
2020-12-03 22:03:06 +01:00
]}
>
{postState.text.count}
</Text>
</Pressable>
)
}
const styles = StyleSheet.create({
additions: {
height: 45,
borderTopWidth: StyleSheet.hairlineWidth,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center'
},
count: {
textAlign: 'center',
fontSize: StyleConstants.Font.Size.M,
fontWeight: StyleConstants.Font.Weight.Bold
}
})
export default ComposeActions