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

232 lines
7.5 KiB
TypeScript
Raw Normal View History

2022-09-20 22:23:01 +02:00
import { emojis } from '@components/Emojis'
import EmojisContext from '@components/Emojis/Context'
import Icon from '@components/Icon'
import { MAX_MEDIA_ATTACHMENTS } from '@components/mediaSelector'
2021-01-13 01:03:46 +01:00
import { useActionSheet } from '@expo/react-native-action-sheet'
import { androidActionSheetStyles } from '@utils/helpers/androidActionSheetStyles'
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'
import React, { useContext } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
import { Keyboard, Pressable, StyleSheet, View } from 'react-native'
2021-01-19 01:13:45 +01:00
import ComposeContext from '../utils/createContext'
2022-05-02 22:31:22 +02:00
import chooseAndUploadAttachment 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)
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'screenCompose'])
2022-12-19 22:36:30 +01:00
const { colors } = useTheme()
2020-12-03 22:03:06 +01:00
const attachmentColor = () => {
2022-02-12 14:51:01 +01:00
if (composeState.poll.active) return colors.disabled
2020-12-06 21:42:19 +01:00
2020-12-07 12:31:40 +01:00
if (composeState.attachments.uploads.length) {
2022-02-12 14:51:01 +01:00
return colors.primaryDefault
2020-12-06 21:42:19 +01:00
} else {
2022-02-12 14:51:01 +01:00
return colors.secondary
2020-12-06 21:42:19 +01:00
}
}
const attachmentOnPress = () => {
2020-12-26 14:40:10 +01:00
if (composeState.poll.active) return
2020-12-06 21:42:19 +01:00
if (composeState.attachments.uploads.length < MAX_MEDIA_ATTACHMENTS) {
return chooseAndUploadAttachment({ composeDispatch, showActionSheetWithOptions })
2020-12-06 21:42:19 +01:00
}
}
2020-12-06 21:42:19 +01:00
const pollColor = () => {
2022-02-12 14:51:01 +01:00
if (composeState.attachments.uploads.length) return colors.disabled
2020-12-26 14:40:10 +01:00
2020-12-07 12:31:40 +01:00
if (composeState.poll.active) {
2022-02-12 14:51:01 +01:00
return colors.primaryDefault
2020-12-06 21:42:19 +01:00
} else {
2022-02-12 14:51:01 +01:00
return colors.secondary
2020-12-06 21:42:19 +01:00
}
}
const pollOnPress = () => {
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-06 21:42:19 +01:00
const visibilityIcon = () => {
2020-12-10 19:19:56 +01:00
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
}
}
const visibilityOnPress = () => {
2020-12-26 00:40:27 +01:00
if (!composeState.visibilityLock) {
2021-01-13 01:03:46 +01:00
showActionSheetWithOptions(
2020-12-10 19:19:56 +01:00
{
2022-12-23 15:53:40 +01:00
title: t('screenCompose:content.root.actions.visibility.title'),
2021-01-19 01:13:45 +01:00
options: [
2022-12-23 15:53:40 +01:00
t('screenCompose:content.root.actions.visibility.options.public'),
t('screenCompose:content.root.actions.visibility.options.unlisted'),
t('screenCompose:content.root.actions.visibility.options.private'),
t('screenCompose:content.root.actions.visibility.options.direct'),
t('common:buttons.cancel')
2021-01-19 01:13:45 +01:00
],
2022-02-12 14:51:01 +01:00
cancelButtonIndex: 4,
2022-12-19 22:36:30 +01:00
...androidActionSheetStyles(colors)
2020-12-10 19:19:56 +01:00
},
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
const spoilerOnPress = () => {
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 }
})
}
2020-12-10 19:19:56 +01:00
const { emojisState, emojisDispatch } = useContext(EmojisContext)
const emojiColor = () => {
2022-09-20 22:23:01 +02:00
if (!emojis.current?.length) return colors.disabled
2020-12-26 14:40:10 +01:00
if (emojisState.targetIndex !== -1) {
2022-02-12 14:51:01 +01:00
return colors.primaryDefault
2020-12-06 22:32:36 +01:00
} else {
2022-02-12 14:51:01 +01:00
return colors.secondary
2020-12-06 22:32:36 +01:00
}
}
const emojiOnPress = () => {
if (emojisState.targetIndex === -1) {
Keyboard.dismiss()
2022-09-19 22:22:52 +02:00
const focusedPropsIndex = emojisState.inputProps?.findIndex(props => props.isFocused.current)
if (focusedPropsIndex === -1) return
emojisDispatch({ type: 'target', payload: focusedPropsIndex })
} else {
emojisDispatch({ type: 'target', payload: -1 })
return
2020-12-06 22:32:36 +01:00
}
}
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
2021-04-09 21:43:12 +02:00
accessibilityRole='toolbar'
2022-05-10 23:19:26 +02:00
style={{
height: 45,
borderTopWidth: StyleSheet.hairlineWidth,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
backgroundColor: colors.backgroundDefault,
borderTopColor: colors.border
}}
2020-12-03 22:03:06 +01:00
>
<Pressable
2021-04-09 21:43:12 +02:00
accessibilityRole='button'
2022-12-23 15:53:40 +01:00
accessibilityLabel={t('screenCompose:content.root.actions.attachment.accessibilityLabel')}
accessibilityHint={t('screenCompose:content.root.actions.attachment.accessibilityHint')}
2021-04-09 21:43:12 +02:00
accessibilityState={{
disabled: composeState.poll.active
}}
style={styles.button}
2020-12-06 21:42:19 +01:00
onPress={attachmentOnPress}
children={<Icon name='Camera' size={24} color={attachmentColor()} />}
2020-12-03 22:03:06 +01:00
/>
<Pressable
2021-04-09 21:43:12 +02:00
accessibilityRole='button'
2022-12-23 15:53:40 +01:00
accessibilityLabel={t('screenCompose:content.root.actions.poll.accessibilityLabel')}
accessibilityHint={t('screenCompose:content.root.actions.poll.accessibilityHint')}
2021-04-09 21:43:12 +02:00
accessibilityState={{
disabled: composeState.attachments.uploads.length ? true : false,
expanded: composeState.poll.active
}}
style={styles.button}
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
2021-04-09 21:43:12 +02:00
accessibilityRole='button'
2022-12-23 15:53:40 +01:00
accessibilityLabel={t('screenCompose:content.root.actions.visibility.accessibilityLabel', {
visibility: composeState.visibility
})}
2021-04-09 21:43:12 +02:00
accessibilityState={{ disabled: composeState.visibilityLock }}
style={styles.button}
2020-12-10 19:19:56 +01:00
onPress={visibilityOnPress}
children={
<Icon
name={visibilityIcon()}
size={24}
color={composeState.visibilityLock ? colors.disabled : colors.secondary}
/>
}
2020-12-03 22:03:06 +01:00
/>
<Pressable
2021-04-09 21:43:12 +02:00
accessibilityRole='button'
2022-12-23 15:53:40 +01:00
accessibilityLabel={t('screenCompose:content.root.actions.spoiler.accessibilityLabel')}
2021-04-09 21:43:12 +02:00
accessibilityState={{ expanded: composeState.spoiler.active }}
style={styles.button}
2020-12-10 19:19:56 +01:00
onPress={spoilerOnPress}
children={
<Icon
name='AlertTriangle'
size={24}
color={composeState.spoiler.active ? colors.primaryDefault : colors.secondary}
/>
}
2020-12-06 23:51:13 +01:00
/>
<Pressable
2021-04-09 21:43:12 +02:00
accessibilityRole='button'
2022-12-23 15:53:40 +01:00
accessibilityLabel={t('screenCompose:content.root.actions.emoji.accessibilityLabel')}
accessibilityHint={t('screenCompose:content.root.actions.emoji.accessibilityHint')}
2021-04-09 21:43:12 +02:00
accessibilityState={{
2022-09-20 22:23:01 +02:00
disabled: emojis.current?.length ? false : true,
expanded: emojisState.targetIndex !== -1
2021-04-09 21:43:12 +02:00
}}
style={styles.button}
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({
2021-04-09 21:43:12 +02:00
button: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
height: '100%'
2020-12-03 22:03:06 +01:00
}
})
export default ComposeActions