import { emojis } from '@components/Emojis' import EmojisContext from '@components/Emojis/helpers/EmojisContext' import Icon from '@components/Icon' import { useActionSheet } from '@expo/react-native-action-sheet' import { getInstanceConfigurationStatusMaxAttachments } from '@utils/slices/instancesSlice' import layoutAnimation from '@utils/styles/layoutAnimation' import { useTheme } from '@utils/styles/ThemeManager' import React, { useContext, useMemo } from 'react' import { useTranslation } from 'react-i18next' import { Keyboard, Pressable, StyleSheet, View } from 'react-native' import { useSelector } from 'react-redux' import ComposeContext from '../utils/createContext' import chooseAndUploadAttachment from './Footer/addAttachment' const ComposeActions: React.FC = () => { const { showActionSheetWithOptions } = useActionSheet() const { composeState, composeDispatch } = useContext(ComposeContext) const { t } = useTranslation('screenCompose') const { colors, mode } = useTheme() const instanceConfigurationStatusMaxAttachments = useSelector( getInstanceConfigurationStatusMaxAttachments, () => true ) const attachmentColor = useMemo(() => { if (composeState.poll.active) return colors.disabled if (composeState.attachments.uploads.length) { return colors.primaryDefault } else { return colors.secondary } }, [composeState.poll.active, composeState.attachments.uploads]) const attachmentOnPress = () => { if (composeState.poll.active) return if (composeState.attachments.uploads.length < instanceConfigurationStatusMaxAttachments) { return chooseAndUploadAttachment({ composeDispatch, showActionSheetWithOptions }) } } const pollColor = useMemo(() => { if (composeState.attachments.uploads.length) return colors.disabled if (composeState.poll.active) { return colors.primaryDefault } else { return colors.secondary } }, [composeState.poll.active, composeState.attachments.uploads]) const pollOnPress = () => { if (!composeState.attachments.uploads.length) { layoutAnimation() composeDispatch({ type: 'poll', payload: { ...composeState.poll, active: !composeState.poll.active } }) } if (composeState.poll.active) { composeState.textInputFocus.refs.text.current?.focus() } } const visibilityIcon = useMemo(() => { switch (composeState.visibility) { case 'public': return 'Globe' case 'unlisted': return 'Unlock' case 'private': return 'Lock' case 'direct': return 'Mail' } }, [composeState.visibility]) const visibilityOnPress = () => { if (!composeState.visibilityLock) { showActionSheetWithOptions( { 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') ], cancelButtonIndex: 4, userInterfaceStyle: mode }, 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 } } ) } } const spoilerOnPress = () => { if (composeState.spoiler.active) { composeState.textInputFocus.refs.text.current?.focus() } layoutAnimation() composeDispatch({ type: 'spoiler', payload: { active: !composeState.spoiler.active } }) } const { emojisState, emojisDispatch } = useContext(EmojisContext) const emojiColor = useMemo(() => { if (!emojis.current?.length) return colors.disabled if (emojisState.targetIndex !== -1) { return colors.primaryDefault } else { return colors.secondary } }, [emojis.current?.length, emojisState.targetIndex]) const emojiOnPress = () => { if (emojisState.targetIndex === -1) { Keyboard.dismiss() 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 } } return ( } /> } /> } /> } /> } /> ) } const styles = StyleSheet.create({ button: { flex: 1, justifyContent: 'center', alignItems: 'center', height: '100%' } }) export default ComposeActions