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

187 lines
5.3 KiB
TypeScript
Raw Normal View History

2020-12-03 22:03:06 +01:00
import { Feather } from '@expo/vector-icons'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
import { ComposeContext } from '@screens/Shared/Compose'
2020-12-30 00:56:25 +01:00
import addAttachment from '@root/screens/Shared/Compose/addAttachment'
import React, { useCallback, useContext, useMemo } from 'react'
import { ActionSheetIOS, StyleSheet, View } from 'react-native'
import layoutAnimation from '@root/utils/styles/layoutAnimation'
2020-12-03 22:03:06 +01:00
const ComposeActions: React.FC = () => {
const { composeState, composeDispatch } = useContext(ComposeContext)
2020-12-03 22:03:06 +01:00
const { theme } = useTheme()
2020-12-06 21:42:19 +01:00
const attachmentColor = useMemo(() => {
2020-12-26 14:40:10 +01:00
if (composeState.poll.active) return theme.disabled
2020-12-06 21:42:19 +01:00
2020-12-07 12:31:40 +01:00
if (composeState.attachments.uploads.length) {
2020-12-06 21:42:19 +01:00
return theme.primary
} else {
return theme.secondary
}
2020-12-30 00:56:25 +01:00
}, [composeState.poll.active, composeState.attachments.uploads])
2020-12-06 21:42:19 +01:00
const attachmentOnPress = useCallback(async () => {
2020-12-26 14:40:10 +01:00
if (composeState.poll.active) return
2020-12-06 21:42:19 +01:00
2020-12-30 00:56:25 +01:00
if (composeState.attachments.uploads.length < 4) {
return await addAttachment({ composeDispatch })
2020-12-06 21:42:19 +01:00
}
2020-12-30 00:56:25 +01:00
}, [composeState.poll.active, composeState.attachments.uploads])
2020-12-06 21:42:19 +01:00
const pollColor = useMemo(() => {
2020-12-26 14:40:10 +01:00
if (composeState.attachments.uploads.length) return theme.disabled
2020-12-07 12:31:40 +01:00
if (composeState.poll.active) {
2020-12-06 21:42:19 +01:00
return theme.primary
} else {
return theme.secondary
}
2020-12-30 00:56:25 +01:00
}, [composeState.poll.active, composeState.attachments.uploads])
2020-12-06 21:42:19 +01:00
const pollOnPress = useCallback(() => {
2020-12-30 00:56:25 +01:00
if (!composeState.attachments.uploads.length) {
layoutAnimation()
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-06 21:42:19 +01:00
type: 'poll',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.poll, active: !composeState.poll.active }
2020-12-06 21:42:19 +01:00
})
}
2020-12-07 12:31:40 +01:00
if (composeState.poll.active) {
composeState.textInputFocus.refs.text.current?.focus()
2020-12-06 21:42:19 +01:00
}
2020-12-30 00:56:25 +01:00
}, [composeState.poll.active, composeState.attachments.uploads])
2020-12-06 21:42:19 +01:00
2020-12-10 19:19:56 +01:00
const visibilityIcon = useMemo(() => {
switch (composeState.visibility) {
case 'public':
return 'globe'
case 'unlisted':
return 'unlock'
case 'private':
return 'lock'
case 'direct':
return 'mail'
}
}, [composeState.visibility])
2020-12-26 00:40:27 +01:00
const visibilityOnPress = useCallback(() => {
if (!composeState.visibilityLock) {
2020-12-10 19:19:56 +01:00
ActionSheetIOS.showActionSheetWithOptions(
{
options: ['公开', '不公开', '仅关注着', '私信', '取消'],
cancelButtonIndex: 4
},
buttonIndex => {
switch (buttonIndex) {
case 0:
composeDispatch({ type: 'visibility', payload: 'public' })
break
case 1:
composeDispatch({ type: 'visibility', payload: 'unlisted' })
break
case 2:
composeDispatch({ type: 'visibility', payload: 'private' })
break
case 3:
composeDispatch({ type: 'visibility', payload: 'direct' })
break
}
}
2020-12-26 00:40:27 +01:00
)
}
}, [])
2020-12-10 19:19:56 +01:00
2020-12-26 00:40:27 +01:00
const spoilerOnPress = useCallback(() => {
if (composeState.spoiler.active) {
composeState.textInputFocus.refs.text.current?.focus()
}
2020-12-30 00:56:25 +01:00
layoutAnimation()
2020-12-26 00:40:27 +01:00
composeDispatch({
type: 'spoiler',
payload: { active: !composeState.spoiler.active }
})
}, [composeState.spoiler.active, composeState.textInputFocus])
2020-12-10 19:19:56 +01:00
2020-12-06 22:32:36 +01:00
const emojiColor = useMemo(() => {
2020-12-26 14:40:10 +01:00
if (!composeState.emoji.emojis) return theme.disabled
2020-12-07 12:31:40 +01:00
if (composeState.emoji.active) {
2020-12-06 22:32:36 +01:00
return theme.primary
} else {
return theme.secondary
}
2020-12-07 12:31:40 +01:00
}, [composeState.emoji.active, composeState.emoji.emojis])
2020-12-06 22:32:36 +01:00
const emojiOnPress = useCallback(() => {
2020-12-07 12:31:40 +01:00
if (composeState.emoji.emojis) {
if (composeState.emoji.active) {
2020-12-30 00:56:25 +01:00
layoutAnimation()
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-06 22:32:36 +01:00
type: 'emoji',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.emoji, active: false }
2020-12-06 22:32:36 +01:00
})
} else {
2020-12-30 00:56:25 +01:00
layoutAnimation()
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-06 22:32:36 +01:00
type: 'emoji',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.emoji, active: true }
2020-12-06 22:32:36 +01:00
})
}
}
2020-12-07 12:31:40 +01:00
}, [composeState.emoji.active, composeState.emoji.emojis])
2020-12-06 22:32:36 +01:00
2020-12-03 22:03:06 +01:00
return (
2020-12-10 19:19:56 +01:00
<View
2020-12-03 22:03:06 +01:00
style={[
styles.additions,
{ backgroundColor: theme.background, borderTopColor: theme.border }
]}
>
<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
2020-12-10 19:19:56 +01:00
name={visibilityIcon}
2020-12-03 22:03:06 +01:00
size={24}
2020-12-26 14:40:10 +01:00
color={composeState.visibilityLock ? theme.disabled : theme.secondary}
2020-12-10 19:19:56 +01:00
onPress={visibilityOnPress}
2020-12-03 22:03:06 +01:00
/>
2020-12-06 23:51:13 +01:00
<Feather
name='alert-triangle'
size={24}
2020-12-07 12:31:40 +01:00
color={composeState.spoiler.active ? theme.primary : theme.secondary}
2020-12-10 19:19:56 +01:00
onPress={spoilerOnPress}
2020-12-06 23:51:13 +01:00
/>
2020-12-03 22:03:06 +01:00
<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
/>
2020-12-10 19:19:56 +01:00
</View>
2020-12-03 22:03:06 +01:00
)
}
const styles = StyleSheet.create({
additions: {
height: 45,
borderTopWidth: StyleSheet.hairlineWidth,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center'
},
count: {
textAlign: 'center',
...StyleConstants.FontStyle.M,
2020-12-03 22:03:06 +01:00
fontWeight: StyleConstants.Font.Weight.Bold
}
})
export default ComposeActions