import { discardConfirmation } from '@components/discardConfirmation' import haptics from '@components/haptics' import { HeaderLeft, HeaderRight } from '@components/Header' import { ModalScrollView } from '@components/ModalScrollView' import CustomText from '@components/Text' import apiInstance from '@utils/api/instance' import { ScreenComposeStackScreenProps } from '@utils/navigation/navigators' import { StyleConstants } from '@utils/styles/constants' import { useTheme } from '@utils/styles/ThemeManager' import { Image } from 'expo-image' import React, { useContext, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { Alert, TextInput, View } from 'react-native' import { DEFAULT_WIDTH } from './Root/Footer/Attachments' import ComposeContext from './utils/createContext' const ComposeEditAttachment: React.FC< ScreenComposeStackScreenProps<'Screen-Compose-EditAttachment'> > = ({ navigation, route: { params: { index } } }) => { const { colors } = useTheme() const { t } = useTranslation(['common', 'screenCompose']) const { composeState, composeDispatch } = useContext(ComposeContext) const [isSubmitting, setIsSubmitting] = useState(false) const theAttachment = composeState.attachments.uploads[index].remote if (!theAttachment) { navigation.goBack() return null } const [altText, setAltText] = useState(theAttachment.description) useEffect(() => { navigation.setOptions({ headerLeft: () => ( { discardConfirmation({ condition: theAttachment.description != altText, action: () => navigation.goBack() }) }} /> ), headerRight: () => ( { if (composeState.type === 'edit') { composeDispatch({ type: 'attachment/edit', payload: { ...theAttachment, description: altText } }) navigation.goBack() return } theAttachment?.id && apiInstance({ method: 'put', url: `media/${theAttachment.id}`, body: { description: altText } }) .then(res => { setIsSubmitting(false) haptics('Success') composeDispatch({ type: 'attachment/edit', payload: res.body }) navigation.goBack() }) .catch(() => { setIsSubmitting(false) haptics('Error') Alert.alert( t('screenCompose:content.editAttachment.header.right.failed.title'), undefined, [ { text: t('screenCompose:content.editAttachment.header.right.failed.button'), style: 'cancel' } ] ) }) }} /> ) }) }, [theAttachment, altText]) return ( {t('screenCompose:content.editAttachment.content.altText.heading')} setAltText(e)} placeholder={t('screenCompose:content.editAttachment.content.altText.placeholder')} placeholderTextColor={colors.secondary} /> {theAttachment.description?.length || 0} / 1500 ) } export default ComposeEditAttachment