tooot/src/screens/Compose.tsx

422 lines
14 KiB
TypeScript
Raw Normal View History

import { handleError } from '@api/helpers'
import { ComponentEmojis } from '@components/Emojis'
import { EmojisState } from '@components/Emojis/helpers/EmojisContext'
2022-04-30 17:44:39 +02:00
import { HeaderLeft, HeaderRight } from '@components/Header'
import { createNativeStackNavigator } from '@react-navigation/native-stack'
2020-12-30 14:33:33 +01:00
import haptics from '@root/components/haptics'
2022-04-30 23:47:52 +02:00
import { useAppDispatch } from '@root/store'
2021-01-30 01:29:15 +01:00
import ComposeRoot from '@screens/Compose/Root'
2022-12-07 23:56:00 +01:00
import { formatText } from '@screens/Compose/utils/processText'
2021-08-29 15:25:38 +02:00
import { RootStackScreenProps } from '@utils/navigation/navigators'
2022-10-31 21:54:24 +01:00
import { useTimelineMutation } from '@utils/queryHooks/timeline'
2021-01-18 00:23:40 +01:00
import { updateStoreReview } from '@utils/slices/contextsSlice'
import {
2021-02-20 19:12:44 +01:00
getInstanceAccount,
2021-11-15 22:34:43 +01:00
getInstanceConfigurationStatusMaxChars,
2021-02-20 19:12:44 +01:00
removeInstanceDraft,
updateInstanceDraft
} from '@utils/slices/instancesSlice'
2020-12-29 16:19:04 +01:00
import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager'
2021-02-07 00:39:11 +01:00
import { filter } from 'lodash'
2022-09-20 22:23:01 +02:00
import React, { useCallback, useEffect, useMemo, useReducer, useState } from 'react'
2021-01-19 01:13:45 +01:00
import { useTranslation } from 'react-i18next'
import { Alert, Keyboard, Platform } from 'react-native'
import { useQueryClient } from '@tanstack/react-query'
2022-04-30 23:47:52 +02:00
import { useSelector } from 'react-redux'
2021-02-07 00:39:11 +01:00
import ComposeDraftsList from './Compose/DraftsList'
2020-12-30 00:56:25 +01:00
import ComposeEditAttachment from './Compose/EditAttachment'
2022-05-02 22:31:22 +02:00
import { uploadAttachment } from './Compose/Root/Footer/addAttachment'
2021-01-01 17:52:14 +01:00
import ComposeContext from './Compose/utils/createContext'
2020-12-30 00:56:25 +01:00
import composeInitialState from './Compose/utils/initialState'
import composeParseState from './Compose/utils/parseState'
2021-01-01 23:10:47 +01:00
import composePost from './Compose/utils/post'
2020-12-30 00:56:25 +01:00
import composeReducer from './Compose/utils/reducer'
2021-01-30 01:29:15 +01:00
2020-11-15 20:29:43 +01:00
const Stack = createNativeStackNavigator()
2021-08-29 15:25:38 +02:00
const ScreenCompose: React.FC<RootStackScreenProps<'Screen-Compose'>> = ({
2021-01-07 22:18:14 +01:00
route: { params },
navigation
}) => {
2021-03-28 23:31:10 +02:00
const { t } = useTranslation('screenCompose')
2022-02-12 14:51:01 +01:00
const { colors } = useTheme()
2020-12-20 17:53:24 +01:00
const queryClient = useQueryClient()
2020-12-06 23:51:13 +01:00
2020-11-15 23:33:01 +01:00
const [hasKeyboard, setHasKeyboard] = useState(false)
useEffect(() => {
const keyboardShown = Keyboard.addListener('keyboardWillShow', () => setHasKeyboard(true))
const keyboardHidden = Keyboard.addListener('keyboardWillHide', () => setHasKeyboard(false))
2020-11-15 23:33:01 +01:00
return () => {
2021-10-10 21:58:36 +02:00
keyboardShown.remove()
keyboardHidden.remove()
2020-11-15 23:33:01 +01:00
}
}, [])
2021-02-20 19:12:44 +01:00
const localAccount = useSelector(getInstanceAccount, (prev, next) =>
2021-02-10 00:40:44 +01:00
prev?.preferences && next?.preferences
? prev?.preferences['posting:default:visibility'] ===
next?.preferences['posting:default:visibility']
: true
)
2021-02-07 00:39:11 +01:00
const initialReducerState = useMemo(() => {
if (params) {
return composeParseState(params)
} else {
return {
...composeInitialState,
timestamp: Date.now(),
attachments: {
...composeInitialState.attachments,
sensitive:
localAccount?.preferences && localAccount?.preferences['posting:default:sensitive']
? localAccount?.preferences['posting:default:sensitive']
: false
},
2021-02-07 00:39:11 +01:00
visibility:
localAccount?.preferences && localAccount.preferences['posting:default:visibility']
2021-02-07 00:39:11 +01:00
? localAccount.preferences['posting:default:visibility']
: 'public'
}
}
}, [])
const [composeState, composeDispatch] = useReducer(composeReducer, initialReducerState)
2020-12-07 12:31:40 +01:00
const maxTootChars = useSelector(getInstanceConfigurationStatusMaxChars, () => true)
2021-02-07 00:39:11 +01:00
const totalTextCount =
(composeState.spoiler.active ? composeState.spoiler.count : 0) + composeState.text.count
2021-02-07 00:39:11 +01:00
// If compose state is dirty, then disallow add back drafts
useEffect(() => {
composeDispatch({
type: 'dirty',
payload:
totalTextCount !== 0 ||
composeState.attachments.uploads.length !== 0 ||
(composeState.poll.active === true &&
filter(composeState.poll.options, o => {
return o !== undefined && o.length > 0
}).length > 0)
})
}, [
totalTextCount,
composeState.attachments.uploads.length,
composeState.poll.active,
composeState.poll.options
])
2020-12-07 12:31:40 +01:00
useEffect(() => {
switch (params?.type) {
2022-05-02 22:31:22 +02:00
case 'share':
if (params.text) {
formatText({
textInput: 'text',
composeDispatch,
content: params.text,
disableDebounce: true
})
}
2022-06-05 17:58:18 +02:00
if (params.media?.length) {
for (const m of params.media) {
uploadAttachment({
composeDispatch,
2022-08-07 01:18:10 +02:00
media: { uri: m.uri, fileName: 'temp.jpg', type: m.mime }
2022-06-05 17:58:18 +02:00
})
}
2022-05-02 22:31:22 +02:00
}
break
2020-12-07 12:31:40 +01:00
case 'edit':
2022-04-30 17:44:39 +02:00
case 'deleteEdit':
2020-12-07 12:31:40 +01:00
if (params.incomingStatus.spoiler_text) {
formatText({
textInput: 'spoiler',
2020-12-07 12:31:40 +01:00
composeDispatch,
content: params.incomingStatus.spoiler_text,
disableDebounce: true
})
}
formatText({
textInput: 'text',
2020-12-07 12:31:40 +01:00
composeDispatch,
content: params.incomingStatus.text!,
disableDebounce: true
})
break
case 'reply':
const actualStatus = params.incomingStatus.reblog || params.incomingStatus
if (actualStatus.spoiler_text) {
formatText({
textInput: 'spoiler',
composeDispatch,
content: actualStatus.spoiler_text,
disableDebounce: true
})
}
2021-03-11 22:27:38 +01:00
params.accts.length && // When replying to myself only, do not add space or even format text
formatText({
textInput: 'text',
composeDispatch,
content: params.accts.map(acct => `@${acct}`).join(' ') + ' ',
disableDebounce: true
})
break
2020-12-21 21:47:15 +01:00
case 'conversation':
formatText({
textInput: 'text',
composeDispatch,
2022-11-17 21:48:22 +01:00
content:
2022-11-28 14:42:01 +01:00
(params.text ? `${params.text}\n` : '') +
2022-11-17 21:48:22 +01:00
params.accts.map(acct => `@${acct}`).join(' ') +
' ',
2020-12-21 21:47:15 +01:00
disableDebounce: true
})
break
2020-12-07 12:31:40 +01:00
}
}, [params?.type])
2020-11-15 22:33:09 +01:00
2021-02-07 00:39:11 +01:00
const saveDraft = () => {
dispatch(
2021-02-20 19:12:44 +01:00
updateInstanceDraft({
2021-02-07 00:39:11 +01:00
timestamp: composeState.timestamp,
spoiler: composeState.spoiler.raw,
text: composeState.text.raw,
poll: composeState.poll,
attachments: composeState.attachments,
visibility: composeState.visibility,
visibilityLock: composeState.visibilityLock,
replyToStatus: composeState.replyToStatus
})
)
}
const removeDraft = useCallback(() => {
2021-02-20 19:12:44 +01:00
dispatch(removeInstanceDraft(composeState.timestamp))
2021-02-07 00:39:11 +01:00
}, [composeState.timestamp])
useEffect(() => {
const autoSave = composeState.dirty
? setInterval(() => {
saveDraft()
2021-04-01 18:39:53 +02:00
}, 1000)
2021-02-07 00:39:11 +01:00
: removeDraft()
return () => autoSave && clearInterval(autoSave)
}, [composeState])
2020-12-06 23:51:13 +01:00
2020-12-26 00:53:49 +01:00
const headerLeft = useCallback(
() => (
<HeaderLeft
2020-12-26 23:27:53 +01:00
type='text'
2022-12-12 20:43:45 +01:00
content={t('common:buttons.cancel')}
2021-01-17 22:37:05 +01:00
onPress={() => {
2021-02-07 00:39:11 +01:00
if (!composeState.dirty) {
2021-01-17 22:37:05 +01:00
navigation.goBack()
return
} else {
2021-01-19 01:13:45 +01:00
Alert.alert(t('heading.left.alert.title'), undefined, [
2021-01-17 22:37:05 +01:00
{
2021-02-07 00:39:11 +01:00
text: t('heading.left.alert.buttons.delete'),
2021-01-17 22:37:05 +01:00
style: 'destructive',
2021-01-24 02:25:43 +01:00
onPress: () => {
2021-02-07 00:39:11 +01:00
removeDraft()
navigation.goBack()
}
},
{
text: t('heading.left.alert.buttons.save'),
onPress: () => {
saveDraft()
2021-01-24 02:25:43 +01:00
navigation.goBack()
}
2021-01-17 22:37:05 +01:00
},
2021-01-19 01:13:45 +01:00
{
2022-12-12 20:43:45 +01:00
text: t('common:buttons.cancel'),
2022-11-29 23:44:11 +01:00
style: 'cancel'
2021-01-19 01:13:45 +01:00
}
2021-01-17 22:37:05 +01:00
])
}
}}
2020-12-26 00:53:49 +01:00
/>
),
2021-02-07 00:39:11 +01:00
[composeState]
2020-12-26 00:53:49 +01:00
)
2022-04-30 23:47:52 +02:00
const dispatch = useAppDispatch()
2021-02-08 00:23:32 +01:00
const headerRightDisabled = useMemo(() => {
if (totalTextCount > maxTootChars) {
return true
}
if (composeState.attachments.uploads.filter(upload => upload.uploading).length > 0) {
2021-02-08 00:23:32 +01:00
return true
}
if (composeState.attachments.uploads.length === 0 && composeState.text.raw.length === 0) {
2021-02-08 00:23:32 +01:00
return true
}
return false
}, [totalTextCount, composeState.attachments.uploads, composeState.text.raw])
2022-04-30 17:44:39 +02:00
const mutateTimeline = useTimelineMutation({ onMutate: true })
2020-12-26 00:53:49 +01:00
const headerRight = useCallback(
2020-12-26 23:27:53 +01:00
() => (
<HeaderRight
type='text'
2022-12-05 22:11:45 +01:00
content={t(
`heading.right.button.${
(params?.type &&
(params.type === 'conversation'
? params.visibility === 'direct'
? params.type
: 'default'
: params.type)) ||
'default'
}`
)}
2020-12-30 00:56:25 +01:00
onPress={() => {
2021-01-14 22:53:01 +01:00
composeDispatch({ type: 'posting', payload: true })
2021-01-01 23:10:47 +01:00
composePost(params, composeState)
2022-04-30 17:44:39 +02:00
.then(res => {
2020-12-30 14:33:33 +01:00
haptics('Success')
if (Platform.OS === 'ios' && Platform.constants.osVersion === '13.3') {
2021-03-19 21:33:03 +01:00
// https://github.com/tooot-app/app/issues/59
} else {
dispatch(updateStoreReview(1))
}
2021-01-14 22:53:01 +01:00
2021-01-24 02:25:43 +01:00
switch (params?.type) {
case 'edit':
2022-04-30 17:44:39 +02:00
mutateTimeline.mutate({
type: 'editItem',
queryKey: params.queryKey,
rootQueryKey: params.rootQueryKey,
status: res.body
})
break
case 'deleteEdit':
2021-01-24 02:25:43 +01:00
case 'reply':
if (params?.queryKey && params.queryKey[1].page === 'Toot') {
queryClient.invalidateQueries(params.queryKey)
}
break
2021-01-04 14:55:34 +01:00
}
2021-02-08 00:13:35 +01:00
removeDraft()
2020-12-30 00:56:25 +01:00
navigation.goBack()
})
2021-01-22 01:34:20 +01:00
.catch(error => {
2022-05-17 23:12:43 +02:00
if (error?.removeReply) {
2021-03-09 00:47:40 +01:00
Alert.alert(
t('heading.right.alert.removeReply.title'),
t('heading.right.alert.removeReply.description'),
[
{
text: t('common:buttons.cancel'),
2021-03-09 00:47:40 +01:00
onPress: () => {
composeDispatch({ type: 'posting', payload: false })
},
style: 'destructive'
},
{
text: t('heading.right.alert.removeReply.confirm'),
onPress: () => {
composeDispatch({ type: 'removeReply' })
composeDispatch({ type: 'posting', payload: false })
},
style: 'default'
}
]
)
} else {
haptics('Error')
handleError({ message: 'Posting error', captureResponse: true })
2021-03-09 00:47:40 +01:00
composeDispatch({ type: 'posting', payload: false })
Alert.alert(t('heading.right.alert.default.title'), undefined, [
{
text: t('heading.right.alert.default.button')
}
])
}
2020-12-30 00:56:25 +01:00
})
}}
2021-01-14 22:53:01 +01:00
loading={composeState.posting}
2021-02-08 00:23:32 +01:00
disabled={headerRightDisabled}
2020-12-26 23:27:53 +01:00
/>
),
2021-02-07 00:39:11 +01:00
[totalTextCount, composeState]
2020-12-29 16:19:04 +01:00
)
2021-03-18 23:32:31 +01:00
const headerContent = useMemo(() => {
2022-12-12 20:43:45 +01:00
return `${totalTextCount} / ${maxTootChars}`
2021-03-18 23:32:31 +01:00
}, [totalTextCount, maxTootChars, composeState.dirty])
const inputProps: EmojisState['inputProps'] = [
{
value: [
composeState.text.raw,
2022-09-20 22:23:01 +02:00
content => {
formatText({ textInput: 'text', composeDispatch, content })
}
],
selection: [
composeState.text.selection,
selection => composeDispatch({ type: 'text', payload: { selection } })
],
2022-09-19 22:01:13 +02:00
isFocused: composeState.textInputFocus.isFocused.text,
2022-09-23 00:21:41 +02:00
maxLength: maxTootChars - (composeState.spoiler.active ? composeState.spoiler.count : 0),
ref: composeState.textInputFocus.refs.text
2022-09-19 22:01:13 +02:00
},
{
value: [
composeState.spoiler.raw,
content => formatText({ textInput: 'spoiler', composeDispatch, content })
],
selection: [
composeState.spoiler.selection,
selection => composeDispatch({ type: 'spoiler', payload: { selection } })
],
isFocused: composeState.textInputFocus.isFocused.spoiler,
2022-09-23 00:21:41 +02:00
maxLength: maxTootChars - composeState.text.count,
ref: composeState.textInputFocus.refs.spoiler
}
]
return (
<ComponentEmojis
inputProps={inputProps}
customButton
customBehavior={Platform.OS === 'ios' ? 'padding' : undefined}
customEdges={hasKeyboard ? ['top'] : ['top', 'bottom']}
2021-01-14 00:43:35 +01:00
>
<ComposeContext.Provider value={{ composeState, composeDispatch }}>
<Stack.Navigator initialRouteName='Screen-Compose-Root'>
<Stack.Screen
name='Screen-Compose-Root'
component={ComposeRoot}
options={{
title: headerContent,
headerTitleStyle: {
fontWeight:
totalTextCount > maxTootChars
? StyleConstants.Font.Weight.Bold
: StyleConstants.Font.Weight.Normal,
fontSize: StyleConstants.Font.Size.M
},
headerTintColor: totalTextCount > maxTootChars ? colors.red : colors.secondary,
headerLeft,
headerRight
}}
/>
<Stack.Screen
name='Screen-Compose-DraftsList'
component={ComposeDraftsList}
2022-12-10 23:11:41 +01:00
options={{ presentation: 'modal' }}
/>
<Stack.Screen
name='Screen-Compose-EditAttachment'
component={ComposeEditAttachment}
2022-12-10 23:11:41 +01:00
options={{ presentation: 'modal' }}
/>
</Stack.Navigator>
</ComposeContext.Provider>
</ComponentEmojis>
)
}
2021-01-30 01:29:15 +01:00
export default ScreenCompose