1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Simplify update toot logic

This commit is contained in:
xmflsct
2023-01-01 18:37:05 +01:00
parent 56d1090ca9
commit 2705b4b804
12 changed files with 114 additions and 310 deletions

View File

@ -87,9 +87,9 @@ const TimelineDefault: React.FC<Props> = ({
const main = () => (
<>
{item.reblog ? (
<TimelineActioned action='reblog' />
<TimelineActioned action='reblog' rootStatus={item} />
) : item._pinned ? (
<TimelineActioned action='pinned' />
<TimelineActioned action='pinned' rootStatus={item} />
) : null}
<View
@ -167,7 +167,6 @@ const TimelineDefault: React.FC<Props> = ({
queryKey,
rootQueryKey,
status,
reblogStatus: item.reblog ? item : undefined,
ownAccount,
spoilerHidden,
rawContent,

View File

@ -61,6 +61,7 @@ const TimelineNotifications: React.FC<Props> = ({ notification, queryKey }) => {
action={notification.type}
isNotification
account={notification.account}
rootStatus={notification.status}
/>
) : null}

View File

@ -14,11 +14,12 @@ export interface Props {
action: Mastodon.Notification['type'] | 'reblog' | 'pinned'
isNotification?: boolean
account?: Mastodon.Account // For notification
rootStatus?: Mastodon.Status
}
const TimelineActioned: React.FC<Props> = ({ action, isNotification, ...rest }) => {
const { status, reblogStatus } = useContext(StatusContext)
const account = rest.account || (reblogStatus ? reblogStatus.account : status?.account)
const { status } = useContext(StatusContext)
const account = rest.account || (rest.rootStatus || status)?.account
if (!account) return null
const { t } = useTranslation('componentTimeline')

View File

@ -22,7 +22,7 @@ import { Pressable, StyleSheet, View } from 'react-native'
import StatusContext from './Context'
const TimelineActions: React.FC = () => {
const { queryKey, rootQueryKey, status, reblogStatus, ownAccount, highlighted, disableDetails } =
const { queryKey, rootQueryKey, status, ownAccount, highlighted, disableDetails } =
useContext(StatusContext)
if (!queryKey || !status || disableDetails) return null
@ -38,16 +38,16 @@ const TimelineActions: React.FC = () => {
const theParams = params as MutationVarsTimelineUpdateStatusProperty
if (
// Un-bookmark from bookmarks page
(queryKey[1].page === 'Bookmarks' && theParams.payload.property === 'bookmarked') ||
(queryKey[1].page === 'Bookmarks' && theParams.payload.type === 'bookmarked') ||
// Un-favourite from favourites page
(queryKey[1].page === 'Favourites' && theParams.payload.property === 'favourited')
(queryKey[1].page === 'Favourites' && theParams.payload.type === 'favourited')
) {
queryClient.invalidateQueries(queryKey)
} else if (theParams.payload.property === 'favourited') {
} else if (theParams.payload.type === 'favourited') {
// When favourited, update favourited page
const tempQueryKey: QueryKeyTimeline = ['Timeline', { page: 'Favourites' }]
queryClient.invalidateQueries(tempQueryKey)
} else if (theParams.payload.property === 'bookmarked') {
} else if (theParams.payload.type === 'bookmarked') {
// When bookmarked, update bookmark page
const tempQueryKey: QueryKeyTimeline = ['Timeline', { page: 'Bookmarks' }]
queryClient.invalidateQueries(tempQueryKey)
@ -60,7 +60,7 @@ const TimelineActions: React.FC = () => {
type: 'error',
message: t('common:message.error.message', {
function: t(
`componentTimeline:shared.actions.${correctParam.payload.property}.function` as any
`componentTimeline:shared.actions.${correctParam.payload.type}.function` as any
)
}),
...(err.status &&
@ -111,14 +111,9 @@ const TimelineActions: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
fetchRemoteURI: status._remote ? status.uri : undefined,
status,
payload: {
property: 'reblogged',
currentValue: status.reblogged,
propertyCount: 'reblogs_count',
countValue: status.reblogs_count,
type: 'reblogged',
visibility: 'public'
}
})
@ -128,14 +123,9 @@ const TimelineActions: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
fetchRemoteURI: status._remote ? status.uri : undefined,
status,
payload: {
property: 'reblogged',
currentValue: status.reblogged,
propertyCount: 'reblogs_count',
countValue: status.reblogs_count,
type: 'reblogged',
visibility: 'unlisted'
}
})
@ -148,14 +138,9 @@ const TimelineActions: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
fetchRemoteURI: status._remote ? status.uri : undefined,
status,
payload: {
property: 'reblogged',
currentValue: status.reblogged,
propertyCount: 'reblogs_count',
countValue: status.reblogs_count,
type: 'reblogged',
visibility: 'public'
}
})
@ -166,14 +151,9 @@ const TimelineActions: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
fetchRemoteURI: status._remote ? status.uri : undefined,
status,
payload: {
property: 'favourited',
currentValue: status.favourited,
propertyCount: 'favourites_count',
countValue: status.favourites_count
type: 'favourited'
}
})
}
@ -182,14 +162,9 @@ const TimelineActions: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
fetchRemoteURI: status._remote ? status.uri : undefined,
status,
payload: {
property: 'bookmarked',
currentValue: status.bookmarked,
propertyCount: undefined,
countValue: undefined
type: 'bookmarked'
}
})
}

View File

@ -9,7 +9,6 @@ type StatusContextType = {
status?: Mastodon.Status
reblogStatus?: Mastodon.Status // When it is a reblog, pass the root status
ownAccount?: boolean
spoilerHidden?: boolean
rawContent?: React.MutableRefObject<string[]> // When highlighted, for translate, edit history

View File

@ -20,15 +20,8 @@ import { Pressable, View } from 'react-native'
import StatusContext from './Context'
const TimelinePoll: React.FC = () => {
const {
queryKey,
rootQueryKey,
status,
reblogStatus,
ownAccount,
spoilerHidden,
disableDetails
} = useContext(StatusContext)
const { queryKey, rootQueryKey, status, ownAccount, spoilerHidden, disableDetails } =
useContext(StatusContext)
if (!queryKey || !status || !status.poll) return null
const poll = status.poll
@ -45,10 +38,9 @@ const TimelinePoll: React.FC = () => {
rootQueryKey && queryClient.cancelQueries(rootQueryKey)
haptics('Success')
switch (theParams.payload.property) {
switch (theParams.payload.type) {
case 'poll':
theParams.payload.data = body as unknown as Mastodon.Poll
updateStatusProperty(theParams)
updateStatusProperty({ ...theParams, poll: body as unknown as Mastodon.Poll })
break
}
},
@ -84,12 +76,10 @@ const TimelinePoll: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
status,
payload: {
property: 'poll',
id: poll.id,
type: 'vote',
type: 'poll',
action: 'vote',
options: allOptions
}
})
@ -110,12 +100,10 @@ const TimelinePoll: React.FC = () => {
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
isReblog: !!reblogStatus,
status,
payload: {
property: 'poll',
id: poll.id,
type: 'refresh'
type: 'poll',
action: 'refresh'
}
})
}
@ -230,13 +218,9 @@ const TimelinePoll: React.FC = () => {
const pollVoteCounts = () => {
if (poll.voters_count !== null) {
return (
t('componentTimeline:shared.poll.meta.count.voters', { count: poll.voters_count }) + ' • '
)
return t('componentTimeline:shared.poll.meta.count.voters', { count: poll.voters_count })
} else if (poll.votes_count !== null) {
return (
t('componentTimeline:shared.poll.meta.count.votes', { count: poll.votes_count }) + ' • '
)
return t('componentTimeline:shared.poll.meta.count.votes', { count: poll.votes_count })
}
}
@ -246,11 +230,14 @@ const TimelinePoll: React.FC = () => {
} else {
if (poll.expires_at) {
return (
<Trans
ns='componentTimeline'
i18nKey='shared.poll.meta.expiration.until'
components={[<RelativeTime time={poll.expires_at} />]}
/>
<>
{' • '}
<Trans
ns='componentTimeline'
i18nKey='shared.poll.meta.expiration.until'
components={[<RelativeTime time={poll.expires_at} />]}
/>
</>
)
}
}

View File

@ -35,7 +35,7 @@ const menuStatus = ({
onMutate: true,
onError: (err: any, params, oldData) => {
const theFunction = (params as MutationVarsTimelineUpdateStatusProperty).payload
? (params as MutationVarsTimelineUpdateStatusProperty).payload.property
? (params as MutationVarsTimelineUpdateStatusProperty).payload.type
: 'delete'
displayMessage({
theme,
@ -195,10 +195,9 @@ const menuStatus = ({
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
status,
payload: {
property: 'muted',
currentValue: status.muted
type: 'muted'
}
}),
disabled: false,
@ -220,12 +219,9 @@ const menuStatus = ({
type: 'updateStatusProperty',
queryKey,
rootQueryKey,
id: status.id,
status,
payload: {
property: 'pinned',
currentValue: status.pinned,
propertyCount: undefined,
countValue: undefined
type: 'pinned'
}
}),
disabled: false,