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

228 lines
7.0 KiB
TypeScript
Raw Normal View History

2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
import Icon from '@components/Icon'
2021-01-13 01:03:46 +01:00
import { useActionSheet } from '@expo/react-native-action-sheet'
2020-12-13 14:04:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
2021-01-01 17:52:14 +01:00
import layoutAnimation from '@utils/styles/layoutAnimation'
2020-12-13 14:04:25 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2020-12-30 00:56:25 +01:00
import React, { useCallback, useContext, useMemo } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
2021-01-13 01:03:46 +01:00
import { Pressable, StyleSheet, View } from 'react-native'
2021-01-19 01:13:45 +01:00
import ComposeContext from '../utils/createContext'
import addAttachment from './Footer/addAttachment'
2020-12-03 22:03:06 +01:00
const ComposeActions: React.FC = () => {
2021-01-13 01:03:46 +01:00
const { showActionSheetWithOptions } = useActionSheet()
const { composeState, composeDispatch } = useContext(ComposeContext)
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('sharedCompose')
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) {
return theme.primaryDefault
2020-12-06 21:42:19 +01:00
} 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) {
2021-01-24 02:25:43 +01:00
analytics('compose_actions_attachment_press', {
count: composeState.attachments.uploads.length
})
2021-01-13 01:03:46 +01:00
return await addAttachment({
composeDispatch,
showActionSheetWithOptions
})
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) {
return theme.primaryDefault
2020-12-06 21:42:19 +01:00
} 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) {
2021-01-24 02:25:43 +01:00
analytics('compose_actions_poll_press', {
current: composeState.poll.active
})
2020-12-30 00:56:25 +01:00
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'
2020-12-10 19:19:56 +01:00
case 'unlisted':
return 'Unlock'
2020-12-10 19:19:56 +01:00
case 'private':
return 'Lock'
2020-12-10 19:19:56 +01:00
case 'direct':
return 'Mail'
2020-12-10 19:19:56 +01:00
}
}, [composeState.visibility])
2020-12-26 00:40:27 +01:00
const visibilityOnPress = useCallback(() => {
if (!composeState.visibilityLock) {
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-10 19:19:56 +01:00
{
2021-01-19 01:13:45 +01:00
title: t('content.root.actions.visibility.title'),
options: [
t('content.root.actions.visibility.options.public'),
t('content.root.actions.visibility.options.unlisted'),
t('content.root.actions.visibility.options.private'),
t('content.root.actions.visibility.options.direct'),
t('content.root.actions.visibility.options.cancel')
],
2020-12-10 19:19:56 +01:00
cancelButtonIndex: 4
},
buttonIndex => {
switch (buttonIndex) {
case 0:
2021-01-24 02:25:43 +01:00
analytics('compose_actions_visibility_press', {
current: composeState.visibility,
new: 'public'
})
2020-12-10 19:19:56 +01:00
composeDispatch({ type: 'visibility', payload: 'public' })
break
case 1:
2021-01-24 02:25:43 +01:00
analytics('compose_actions_visibility_press', {
current: composeState.visibility,
new: 'unlisted'
})
2020-12-10 19:19:56 +01:00
composeDispatch({ type: 'visibility', payload: 'unlisted' })
break
case 2:
2021-01-24 02:25:43 +01:00
analytics('compose_actions_visibility_press', {
current: composeState.visibility,
new: 'private'
})
2020-12-10 19:19:56 +01:00
composeDispatch({ type: 'visibility', payload: 'private' })
break
case 3:
2021-01-24 02:25:43 +01:00
analytics('compose_actions_visibility_press', {
current: composeState.visibility,
new: 'direct'
})
2020-12-10 19:19:56 +01:00
composeDispatch({ type: 'visibility', payload: 'direct' })
break
}
}
2020-12-26 00:40:27 +01:00
)
}
2021-01-24 02:25:43 +01:00
}, [composeState.visibility])
2020-12-10 19:19:56 +01:00
2020-12-26 00:40:27 +01:00
const spoilerOnPress = useCallback(() => {
2021-01-24 02:25:43 +01:00
analytics('compose_actions_spoiler_press', {
current: composeState.spoiler.active
})
2020-12-26 00:40:27 +01:00
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) {
return theme.primaryDefault
2020-12-06 22:32:36 +01:00
} 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(() => {
2021-01-24 02:25:43 +01:00
analytics('compose_actions_emojis_press', {
current: composeState.emoji.active
})
2020-12-07 12:31:40 +01:00
if (composeState.emoji.emojis) {
2021-01-24 02:25:43 +01:00
layoutAnimation()
composeDispatch({
type: 'emoji',
payload: { ...composeState.emoji, active: !composeState.emoji.active }
})
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.backgroundDefault, borderTopColor: theme.border }
2020-12-03 22:03:06 +01:00
]}
>
<Pressable
2020-12-06 21:42:19 +01:00
onPress={attachmentOnPress}
children={<Icon name='Aperture' size={24} color={attachmentColor} />}
2020-12-03 22:03:06 +01:00
/>
<Pressable
2020-12-06 21:42:19 +01:00
onPress={pollOnPress}
children={<Icon name='BarChart2' size={24} color={pollColor} />}
2020-12-03 22:03:06 +01:00
/>
<Pressable
2020-12-10 19:19:56 +01:00
onPress={visibilityOnPress}
children={
<Icon
name={visibilityIcon}
size={24}
color={
composeState.visibilityLock ? theme.disabled : theme.secondary
}
/>
}
2020-12-03 22:03:06 +01:00
/>
<Pressable
2020-12-10 19:19:56 +01:00
onPress={spoilerOnPress}
children={
<Icon
name='AlertTriangle'
size={24}
color={
composeState.spoiler.active ? theme.primaryDefault : theme.secondary
}
/>
}
2020-12-06 23:51:13 +01:00
/>
<Pressable
2020-12-06 22:32:36 +01:00
onPress={emojiOnPress}
children={<Icon name='Smile' size={24} color={emojiColor} />}
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