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

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-12-30 00:56:25 +01:00
import { store } from '@root/store'
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-01-24 02:25:43 +01:00
const composeParseState = (
2021-02-07 00:39:11 +01:00
params: NonNullable<Nav.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':
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
}))
}
}),
visibility:
2021-01-24 02:25:43 +01:00
params.incomingStatus.visibility ||
getInstanceAccount(store.getState())?.preferences[
2020-12-30 00:56:25 +01:00
'posting:default:visibility'
2021-01-07 19:13:09 +01:00
] ||
'public',
2021-01-24 02:25:43 +01:00
...(params.incomingStatus.visibility === 'direct' && {
visibilityLock: true
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 }
}),
visibility: actualStatus.visibility,
visibilityLock: actualStatus.visibility === 'direct',
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(),
2020-12-30 00:56:25 +01:00
visibility: 'direct',
visibilityLock: true
}
}
}
export default composeParseState