tooot/src/screens/Compose/EditAttachment.tsx

154 lines
5.0 KiB
TypeScript
Raw Normal View History

2023-02-12 18:38:06 +01:00
import { discardConfirmation } from '@components/discardConfirmation'
2022-12-10 23:11:41 +01:00
import haptics from '@components/haptics'
import { HeaderLeft, HeaderRight } from '@components/Header'
2023-01-26 13:59:42 +01:00
import { ModalScrollView } from '@components/ModalScrollView'
import CustomText from '@components/Text'
import apiInstance from '@utils/api/instance'
2021-08-29 15:25:38 +02:00
import { ScreenComposeStackScreenProps } from '@utils/navigation/navigators'
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2023-02-12 18:38:06 +01:00
import { Image } from 'expo-image'
2022-12-10 23:11:41 +01:00
import React, { useContext, useEffect, useState } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
2023-02-12 18:38:06 +01:00
import { Alert, TextInput, View } from 'react-native'
import { DEFAULT_WIDTH } from './Root/Footer/Attachments'
2022-12-10 23:11:41 +01:00
import ComposeContext from './utils/createContext'
2020-12-06 12:52:29 +01:00
2022-12-10 23:11:41 +01:00
const ComposeEditAttachment: React.FC<
ScreenComposeStackScreenProps<'Screen-Compose-EditAttachment'>
> = ({
navigation,
2020-12-06 12:52:29 +01:00
route: {
2020-12-30 00:56:25 +01:00
params: { index }
2022-12-10 23:11:41 +01:00
}
2020-12-06 12:52:29 +01:00
}) => {
const { colors } = useTheme()
2023-02-12 18:38:06 +01:00
const { t } = useTranslation(['common', 'screenCompose'])
2020-12-30 00:56:25 +01:00
const { composeState, composeDispatch } = useContext(ComposeContext)
2022-12-10 23:11:41 +01:00
const [isSubmitting, setIsSubmitting] = useState(false)
const theAttachment = composeState.attachments.uploads[index].remote
if (!theAttachment) {
navigation.goBack()
return null
}
2022-12-10 23:11:41 +01:00
2023-02-12 18:38:06 +01:00
const [altText, setAltText] = useState<string | undefined>(theAttachment.description)
2022-12-10 23:11:41 +01:00
useEffect(() => {
navigation.setOptions({
2023-02-12 18:38:06 +01:00
title: t('screenCompose:content.editAttachment.header.title'),
headerLeft: () => (
<HeaderLeft
content='chevron-down'
onPress={() => {
discardConfirmation({
condition: theAttachment.description != altText,
action: () => navigation.goBack()
})
}}
/>
),
2022-12-10 23:11:41 +01:00
headerRight: () => (
<HeaderRight
2023-02-12 18:38:06 +01:00
accessibilityLabel={t(
'screenCompose:content.editAttachment.header.right.accessibilityLabel'
)}
content='save'
2022-12-10 23:11:41 +01:00
loading={isSubmitting}
onPress={() => {
if (composeState.type === 'edit') {
2023-02-12 18:38:06 +01:00
composeDispatch({
type: 'attachment/edit',
payload: { ...theAttachment, description: altText }
})
navigation.goBack()
return
}
2022-12-10 23:11:41 +01:00
theAttachment?.id &&
apiInstance<Mastodon.Attachment>({
method: 'put',
url: `media/${theAttachment.id}`,
2023-02-12 18:38:06 +01:00
body: { description: altText }
2022-12-10 23:11:41 +01:00
})
2023-02-12 18:38:06 +01:00
.then(res => {
setIsSubmitting(false)
2022-12-10 23:11:41 +01:00
haptics('Success')
2023-02-12 18:38:06 +01:00
composeDispatch({
type: 'attachment/edit',
payload: res.body
})
2022-12-10 23:11:41 +01:00
navigation.goBack()
})
.catch(() => {
setIsSubmitting(false)
haptics('Error')
2023-02-12 18:38:06 +01:00
Alert.alert(
t('screenCompose:content.editAttachment.header.right.failed.title'),
undefined,
[
{
text: t('screenCompose:content.editAttachment.header.right.failed.button'),
style: 'cancel'
}
]
)
2022-12-10 23:11:41 +01:00
})
}}
/>
)
})
2023-02-12 18:38:06 +01:00
}, [theAttachment, altText])
2022-12-10 23:11:41 +01:00
return (
2023-01-26 13:59:42 +01:00
<ModalScrollView>
2023-02-12 18:38:06 +01:00
<View style={{ alignItems: 'center', marginBottom: StyleConstants.Spacing.M }}>
<Image
style={{
width: DEFAULT_WIDTH,
height: DEFAULT_WIDTH,
borderRadius: StyleConstants.BorderRadius
}}
source={theAttachment.preview_url}
/>
</View>
2023-01-26 13:59:42 +01:00
<CustomText fontStyle='M' style={{ color: colors.primaryDefault }} fontWeight='Bold'>
2023-02-12 18:38:06 +01:00
{t('screenCompose:content.editAttachment.content.altText.heading')}
2023-01-26 13:59:42 +01:00
</CustomText>
<TextInput
style={{
height: StyleConstants.Font.Size.M * 11 + StyleConstants.Spacing.Global.PagePadding * 2,
...StyleConstants.FontStyle.M,
marginTop: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S,
padding: StyleConstants.Spacing.Global.PagePadding,
borderWidth: 1,
borderColor: colors.border,
color: colors.primaryDefault
}}
maxLength={1500}
multiline
2023-02-12 18:38:06 +01:00
value={altText}
onChangeText={e => setAltText(e)}
placeholder={t('screenCompose:content.editAttachment.content.altText.placeholder')}
2023-01-26 13:59:42 +01:00
placeholderTextColor={colors.secondary}
/>
<CustomText
fontStyle='S'
style={{
textAlign: 'right',
marginRight: StyleConstants.Spacing.S,
marginBottom: StyleConstants.Spacing.M,
color: colors.secondary
}}
>
{theAttachment.description?.length || 0} / 1500
</CustomText>
</ModalScrollView>
2022-12-10 23:11:41 +01:00
)
}
2020-12-06 12:52:29 +01:00
export default ComposeEditAttachment