tooot/src/screens/Shared/Compose/EditAttachment.tsx

182 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-01-01 17:52:14 +01:00
import client from '@api/client'
2021-01-24 02:25:43 +01:00
import analytics from '@components/analytics'
2021-01-01 17:52:14 +01:00
import haptics from '@components/haptics'
import { HeaderLeft, HeaderRight } from '@components/Header'
2020-12-06 16:06:38 +01:00
import React, {
useCallback,
2020-12-30 00:56:25 +01:00
useContext,
2020-12-06 16:06:38 +01:00
useEffect,
useRef,
useState
} from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
2021-01-14 00:43:35 +01:00
import { Alert, KeyboardAvoidingView, Platform } from 'react-native'
2020-12-06 12:52:29 +01:00
import { SafeAreaView } from 'react-native-safe-area-context'
import { createNativeStackNavigator } from 'react-native-screens/native-stack'
2020-12-30 00:56:25 +01:00
import ComposeEditAttachmentRoot from './EditAttachment/Root'
2021-01-01 17:52:14 +01:00
import ComposeContext from './utils/createContext'
2020-12-06 12:52:29 +01:00
const Stack = createNativeStackNavigator()
export interface Props {
route: {
params: {
2020-12-30 00:56:25 +01:00
index: number
2020-12-06 12:52:29 +01:00
}
}
navigation: any
2020-12-06 12:52:29 +01:00
}
const ComposeEditAttachment: React.FC<Props> = ({
route: {
2020-12-30 00:56:25 +01:00
params: { index }
},
navigation
2020-12-06 12:52:29 +01:00
}) => {
2020-12-30 00:56:25 +01:00
const { composeState, composeDispatch } = useContext(ComposeContext)
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('sharedCompose')
2020-12-30 00:56:25 +01:00
const theAttachment = composeState.attachments.uploads[index].remote!
const [isSubmitting, setIsSubmitting] = useState(false)
2020-12-06 12:52:29 +01:00
2020-12-06 16:06:38 +01:00
const [altText, setAltText] = useState<string | undefined>(
2020-12-30 00:56:25 +01:00
theAttachment.description
2020-12-06 16:06:38 +01:00
)
const focus = useRef({ x: 0, y: 0 })
2020-12-06 12:52:29 +01:00
2020-12-06 16:06:38 +01:00
useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', () => {
let needUpdate = false
2020-12-30 00:56:25 +01:00
if (theAttachment.description !== altText) {
theAttachment.description = altText
2020-12-06 16:06:38 +01:00
needUpdate = true
}
2020-12-30 00:56:25 +01:00
if (theAttachment.type === 'image') {
2020-12-06 21:42:19 +01:00
if (focus.current.x !== 0 || focus.current.y !== 0) {
2021-01-22 01:34:20 +01:00
theAttachment.meta &&
(theAttachment.meta.focus = {
x: focus.current.x > 1 ? 1 : focus.current.x,
y: focus.current.y > 1 ? 1 : focus.current.y
})
2020-12-06 21:42:19 +01:00
needUpdate = true
2020-12-06 16:06:38 +01:00
}
}
if (needUpdate) {
2020-12-30 00:56:25 +01:00
composeDispatch({ type: 'attachment/edit', payload: theAttachment })
2020-12-06 16:06:38 +01:00
}
})
2020-12-06 12:52:29 +01:00
2020-12-06 16:06:38 +01:00
return unsubscribe
2020-12-30 00:56:25 +01:00
}, [focus, altText])
const headerLeft = useCallback(
() => (
<HeaderLeft
type='text'
2021-01-19 01:13:45 +01:00
content={t('content.editAttachment.header.left')}
2020-12-30 00:56:25 +01:00
onPress={() => navigation.goBack()}
2020-12-06 21:42:19 +01:00
/>
2020-12-30 00:56:25 +01:00
),
[]
)
const headerRight = useCallback(
() => (
<HeaderRight
type='text'
2021-01-19 01:13:45 +01:00
content={t('content.editAttachment.header.right.button')}
2020-12-30 00:56:25 +01:00
loading={isSubmitting}
onPress={() => {
2021-01-24 02:25:43 +01:00
analytics('editattachment_confirm_press')
2020-12-30 00:56:25 +01:00
if (!altText && focus.current.x === 0 && focus.current.y === 0) {
navigation.goBack()
return
}
setIsSubmitting(true)
const formData = new FormData()
if (altText) {
formData.append('description', altText)
}
if (focus.current.x !== 0 || focus.current.y !== 0) {
formData.append('focus', `${focus.current.x},${focus.current.y}`)
}
2020-12-06 12:52:29 +01:00
2021-01-07 19:13:09 +01:00
client<Mastodon.Attachment>({
2020-12-30 00:56:25 +01:00
method: 'put',
instance: 'local',
url: `media/${theAttachment.id}`,
body: formData
})
.then(() => {
2020-12-30 14:33:33 +01:00
haptics('Success')
2021-01-19 01:13:45 +01:00
Alert.alert(
t('content.editAttachment.header.right.succeed.title'),
undefined,
[
{
text: t(
'content.editAttachment.header.right.succeed.button'
),
onPress: () => {
navigation.goBack()
}
2020-12-30 00:56:25 +01:00
}
2021-01-19 01:13:45 +01:00
]
)
2020-12-30 00:56:25 +01:00
})
.catch(() => {
setIsSubmitting(false)
2020-12-30 14:33:33 +01:00
haptics('Error')
2021-01-19 01:13:45 +01:00
Alert.alert(
t('content.editAttachment.header.right.failed.title'),
undefined,
[
{
text: t(
'content.editAttachment.header.right.failed.button'
),
style: 'cancel'
}
]
)
2020-12-30 00:56:25 +01:00
})
}}
/>
),
2020-12-06 12:52:29 +01:00
2020-12-30 00:56:25 +01:00
[]
)
const children = useCallback(
() => (
<ComposeEditAttachmentRoot
index={index}
focus={focus}
altText={altText}
setAltText={setAltText}
/>
),
[]
)
2020-12-06 12:52:29 +01:00
return (
2021-01-14 00:43:35 +01:00
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={{ flex: 1 }}
>
2020-12-30 00:56:25 +01:00
<SafeAreaView style={{ flex: 1 }} edges={['left', 'right', 'bottom']}>
2021-01-13 01:03:46 +01:00
<Stack.Navigator screenOptions={{ headerTopInsetEnabled: false }}>
2020-12-06 12:52:29 +01:00
<Stack.Screen
name='Screen-Shared-Compose-EditAttachment-Root'
2020-12-30 00:56:25 +01:00
children={children}
options={{ headerLeft, headerRight, headerCenter: () => null }}
/>
2020-12-06 12:52:29 +01:00
</Stack.Navigator>
</SafeAreaView>
</KeyboardAvoidingView>
)
}
export default ComposeEditAttachment