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

113 lines
3.5 KiB
TypeScript
Raw Normal View History

import CustomText from '@components/Text'
2021-02-08 23:47:20 +01:00
import AttachmentVideo from '@components/Timeline/Shared/Attachment/Video'
2020-12-30 00:56:25 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-03-18 23:32:31 +01:00
import React, { useContext, useMemo, useRef } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
import { ScrollView, StyleSheet, TextInput, View } from 'react-native'
2021-01-01 17:52:14 +01:00
import ComposeContext from '../utils/createContext'
2020-12-30 00:56:25 +01:00
import ComposeEditAttachmentImage from './Image'
export interface Props {
index: number
}
2021-03-09 14:43:31 +01:00
const ComposeEditAttachmentRoot: React.FC<Props> = ({ index }) => {
2021-03-28 23:31:10 +02:00
const { t } = useTranslation('screenCompose')
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2021-03-09 14:43:31 +01:00
const { composeState, composeDispatch } = useContext(ComposeContext)
const theAttachment = composeState.attachments.uploads[index].remote!
2020-12-30 00:56:25 +01:00
const mediaDisplay = useMemo(() => {
2021-01-22 01:34:20 +01:00
if (theAttachment) {
switch (theAttachment.type) {
case 'image':
2021-03-09 14:43:31 +01:00
return <ComposeEditAttachmentImage index={index} />
2021-01-22 01:34:20 +01:00
case 'video':
case 'gifv':
const video = composeState.attachments.uploads[index]
return (
<AttachmentVideo
total={1}
index={0}
sensitiveShown={false}
video={
video.local
? ({
2022-06-09 21:33:10 +02:00
url: video.local.uri,
2021-01-22 01:34:20 +01:00
preview_url: video.local.local_thumbnail,
blurhash: video.remote?.blurhash
} as Mastodon.AttachmentVideo)
: (video.remote as Mastodon.AttachmentVideo)
}
/>
)
}
2020-12-30 00:56:25 +01:00
}
return null
}, [])
2021-03-09 14:43:31 +01:00
const onChangeText = (e: any) =>
composeDispatch({
type: 'attachment/edit',
payload: {
...theAttachment,
description: e
}
})
2021-03-18 23:32:31 +01:00
const scrollViewRef = useRef<ScrollView>(null)
2020-12-30 00:56:25 +01:00
return (
2021-03-18 23:32:31 +01:00
<ScrollView ref={scrollViewRef}>
2020-12-30 00:56:25 +01:00
{mediaDisplay}
<View style={{ padding: StyleConstants.Spacing.Global.PagePadding }}>
<CustomText
fontStyle='M'
2022-05-10 23:19:26 +02:00
style={{ color: colors.primaryDefault }}
fontWeight='Bold'
2022-02-12 14:51:01 +01:00
>
2021-01-19 01:13:45 +01:00
{t('content.editAttachment.content.altText.heading')}
</CustomText>
2020-12-30 00:56:25 +01:00
<TextInput
style={{
height: 200,
...StyleConstants.FontStyle.M,
marginTop: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S,
padding: StyleConstants.Spacing.Global.PagePadding,
paddingTop: StyleConstants.Spacing.S * 1.5,
borderWidth: StyleSheet.hairlineWidth,
borderColor: colors.border,
color: colors.primaryDefault
}}
2021-03-18 23:32:31 +01:00
onFocus={() => scrollViewRef.current?.scrollToEnd()}
2020-12-30 00:56:25 +01:00
autoCapitalize='none'
autoCorrect={false}
maxLength={1500}
multiline
2021-03-09 14:43:31 +01:00
onChangeText={onChangeText}
2021-01-19 01:13:45 +01:00
placeholder={t('content.editAttachment.content.altText.placeholder')}
2022-02-12 14:51:01 +01:00
placeholderTextColor={colors.secondary}
2020-12-30 00:56:25 +01:00
scrollEnabled
2021-03-09 14:43:31 +01:00
value={theAttachment.description}
keyboardAppearance={mode}
2020-12-30 00:56:25 +01:00
/>
<CustomText
fontStyle='S'
style={{
textAlign: 'right',
marginRight: StyleConstants.Spacing.S,
marginBottom: StyleConstants.Spacing.M,
color: colors.secondary
}}
>
2021-03-09 14:43:31 +01:00
{theAttachment.description?.length || 0} / 1500
</CustomText>
2020-12-30 00:56:25 +01:00
</View>
</ScrollView>
)
}
export default ComposeEditAttachmentRoot