tooot/src/screens/Compose/utils/parseState.ts

98 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-12-30 00:56:25 +01:00
import { store } from '@root/store'
2021-08-29 15:25:38 +02:00
import { RootStackParamList } from '@utils/navigation/navigators'
2021-02-20 19:12:44 +01:00
import { getInstanceAccount } from '@utils/slices/instancesSlice'
2020-12-30 00:56:25 +01:00
import composeInitialState from './initialState'
import { ComposeState } from './types'
2021-03-11 22:22:17 +01:00
const assignVisibility = (
target: ComposeState['visibility']
): Pick<ComposeState, 'visibility' | 'visibilityLock'> => {
const accountPreference = getInstanceAccount(store.getState())?.preferences[
'posting:default:visibility'
]
switch (target) {
case 'direct':
return { visibility: 'direct', visibilityLock: true }
case 'private':
return { visibility: 'private', visibilityLock: false }
case 'unlisted':
if (accountPreference === 'private') {
return { visibility: 'private', visibilityLock: false }
} else {
return { visibility: 'unlisted', visibilityLock: false }
}
case 'public':
switch (accountPreference) {
case 'private':
return { visibility: 'private', visibilityLock: false }
case 'unlisted':
return { visibility: 'unlisted', visibilityLock: false }
default:
return { visibility: 'public', visibilityLock: false }
}
}
}
2021-01-24 02:25:43 +01:00
const composeParseState = (
2021-08-29 15:25:38 +02:00
params: NonNullable<RootStackParamList['Screen-Compose']>
2021-01-24 02:25:43 +01:00
): ComposeState => {
switch (params.type) {
2020-12-30 00:56:25 +01:00
case 'edit':
2022-04-30 17:44:39 +02:00
case 'deleteEdit':
2020-12-30 00:56:25 +01:00
return {
...composeInitialState,
2021-02-07 00:39:11 +01:00
dirty: true,
timestamp: Date.now(),
2021-01-24 02:25:43 +01:00
...(params.incomingStatus.spoiler_text && {
2021-01-01 16:48:16 +01:00
spoiler: { ...composeInitialState.spoiler, active: true }
}),
2021-01-24 02:25:43 +01:00
...(params.incomingStatus.poll && {
2020-12-30 00:56:25 +01:00
poll: {
active: true,
2021-01-24 02:25:43 +01:00
total: params.incomingStatus.poll.options.length,
2020-12-30 00:56:25 +01:00
options: {
2021-01-24 02:25:43 +01:00
'0': params.incomingStatus.poll.options[0]?.title || undefined,
'1': params.incomingStatus.poll.options[1]?.title || undefined,
'2': params.incomingStatus.poll.options[2]?.title || undefined,
'3': params.incomingStatus.poll.options[3]?.title || undefined
2020-12-30 00:56:25 +01:00
},
2021-01-24 02:25:43 +01:00
multiple: params.incomingStatus.poll.multiple,
2020-12-30 00:56:25 +01:00
expire: '86400' // !!!
}
}),
2021-01-24 02:25:43 +01:00
...(params.incomingStatus.media_attachments && {
2020-12-30 00:56:25 +01:00
attachments: {
2021-01-24 02:25:43 +01:00
sensitive: params.incomingStatus.sensitive,
uploads: params.incomingStatus.media_attachments.map(media => ({
2020-12-30 00:56:25 +01:00
remote: media
}))
}
}),
2021-03-11 22:22:17 +01:00
...assignVisibility(params.incomingStatus.visibility),
2021-03-09 00:47:40 +01:00
...(params.replyToStatus && { replyToStatus: params.replyToStatus })
2020-12-30 00:56:25 +01:00
}
case 'reply':
2021-01-24 02:25:43 +01:00
const actualStatus = params.incomingStatus.reblog || params.incomingStatus
2020-12-30 00:56:25 +01:00
return {
...composeInitialState,
2021-02-07 00:39:11 +01:00
dirty: true,
timestamp: Date.now(),
...(actualStatus.spoiler_text && {
spoiler: { ...composeInitialState.spoiler, active: true }
}),
2021-03-11 22:22:17 +01:00
...assignVisibility(actualStatus.visibility),
2020-12-30 00:56:25 +01:00
replyToStatus: actualStatus
}
case 'conversation':
return {
...composeInitialState,
2021-02-07 00:39:11 +01:00
dirty: true,
timestamp: Date.now(),
2021-03-11 22:22:17 +01:00
...assignVisibility('direct')
2020-12-30 00:56:25 +01:00
}
}
}
export default composeParseState