mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Fix status interactions
This commit is contained in:
@ -41,6 +41,7 @@ export type RootStackParamList = {
|
||||
accts: Mastodon.Account['acct'][]
|
||||
visibility: ComposeState['visibility']
|
||||
text?: string // For contacting tooot only
|
||||
navigationState: (QueryKeyTimeline | undefined)[]
|
||||
}
|
||||
| {
|
||||
type: 'share'
|
||||
|
@ -16,6 +16,7 @@ import { AxiosError } from 'axios'
|
||||
import { uniqBy } from 'lodash'
|
||||
import { searchLocalStatus } from './search'
|
||||
import deleteItem from './timeline/deleteItem'
|
||||
import editItem from './timeline/editItem'
|
||||
import updateStatusProperty from './timeline/updateStatusProperty'
|
||||
|
||||
export type QueryKeyTimeline = [
|
||||
@ -284,8 +285,13 @@ export type MutationVarsTimelineUpdateAccountProperty = {
|
||||
}
|
||||
}
|
||||
|
||||
export type MutationVarsTimelineEditItem = {
|
||||
type: 'editItem'
|
||||
status: Mastodon.Status
|
||||
navigationState: (QueryKeyTimeline | undefined)[]
|
||||
}
|
||||
|
||||
export type MutationVarsTimelineDeleteItem = {
|
||||
// This is for deleting status and conversation
|
||||
type: 'deleteItem'
|
||||
source: 'statuses' | 'conversations'
|
||||
id: Mastodon.Status['id']
|
||||
@ -300,6 +306,7 @@ export type MutationVarsTimelineDomainBlock = {
|
||||
export type MutationVarsTimeline =
|
||||
| MutationVarsTimelineUpdateStatusProperty
|
||||
| MutationVarsTimelineUpdateAccountProperty
|
||||
| MutationVarsTimelineEditItem
|
||||
| MutationVarsTimelineDeleteItem
|
||||
| MutationVarsTimelineDomainBlock
|
||||
|
||||
@ -365,6 +372,8 @@ const mutationFunction = async (params: MutationVarsTimeline) => {
|
||||
}
|
||||
})
|
||||
}
|
||||
case 'editItem':
|
||||
return { body: params.status }
|
||||
case 'deleteItem':
|
||||
return apiInstance<Mastodon.Conversation>({
|
||||
method: 'delete',
|
||||
@ -418,6 +427,10 @@ const useTimelineMutation = ({
|
||||
case 'updateStatusProperty':
|
||||
updateStatusProperty(params, navigationState)
|
||||
break
|
||||
case 'editItem':
|
||||
console.log('YES!!!')
|
||||
editItem(params)
|
||||
break
|
||||
case 'deleteItem':
|
||||
deleteItem(params, navigationState)
|
||||
break
|
||||
|
@ -10,20 +10,17 @@ const deleteItem = (
|
||||
if (!key) continue
|
||||
|
||||
queryClient.setQueryData<InfiniteData<TimelineData> | undefined>(key, old => {
|
||||
if (old) {
|
||||
let foundToot: boolean = false
|
||||
old.pages = old.pages.map(page => {
|
||||
if (foundToot) return page
|
||||
if (!old) return old
|
||||
|
||||
page.body = (page.body as Mastodon.Status[]).filter(
|
||||
(item: Mastodon.Status) => item.id !== id
|
||||
return {
|
||||
...old,
|
||||
pages: old.pages.map(page => ({
|
||||
...page,
|
||||
body: (page.body as Mastodon.Status[]).filter(
|
||||
status => status.id !== id && status.reblog?.id !== id
|
||||
)
|
||||
|
||||
return page
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
return old
|
||||
})
|
||||
}
|
||||
}
|
||||
|
46
src/utils/queryHooks/timeline/editItem.ts
Normal file
46
src/utils/queryHooks/timeline/editItem.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { InfiniteData } from '@tanstack/react-query'
|
||||
import { queryClient } from '@utils/queryHooks'
|
||||
import { MutationVarsTimelineEditItem, TimelineData } from '../timeline'
|
||||
|
||||
const editItem = ({ status, navigationState }: MutationVarsTimelineEditItem) => {
|
||||
for (const key of navigationState) {
|
||||
if (!key) continue
|
||||
|
||||
queryClient.setQueryData<InfiniteData<TimelineData>>(key, old => {
|
||||
if (!old) return old
|
||||
|
||||
let updated: boolean = false
|
||||
return {
|
||||
...old,
|
||||
pages: old.pages.map(page => {
|
||||
if (updated) return page
|
||||
|
||||
if (typeof (page.body as Mastodon.Notification[])[0].type === 'string') {
|
||||
;(page.body as Mastodon.Notification[]).forEach(no => {
|
||||
if (no.status?.reblog?.id === status.id) {
|
||||
updated = true
|
||||
no.status.reblog = { ...status }
|
||||
} else if (no.status?.id === status.id) {
|
||||
updated = true
|
||||
no.status = { ...status }
|
||||
}
|
||||
})
|
||||
} else {
|
||||
;(page.body as Mastodon.Status[]).forEach(toot => {
|
||||
if (toot.reblog?.id === status.id) {
|
||||
updated = true
|
||||
toot.reblog = { ...status }
|
||||
} else if (toot.id === status.id) {
|
||||
updated = true
|
||||
toot = { ...status }
|
||||
}
|
||||
})
|
||||
}
|
||||
return page
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default editItem
|
@ -39,10 +39,13 @@ const updateStatusProperty = (
|
||||
for (const key of navigationState) {
|
||||
if (!key) continue
|
||||
|
||||
queryClient.setQueryData<InfiniteData<TimelineData> | undefined>(key, old => {
|
||||
if (old) {
|
||||
let updated: boolean = false
|
||||
old.pages = old.pages.map(page => {
|
||||
queryClient.setQueryData<InfiniteData<TimelineData>>(key, old => {
|
||||
if (!old) return old
|
||||
|
||||
let updated: boolean = false
|
||||
return {
|
||||
...old,
|
||||
pages: old.pages.map(page => {
|
||||
if (updated) return page
|
||||
|
||||
if (typeof (page.body as Mastodon.Conversation[])[0].unread === 'boolean') {
|
||||
@ -73,7 +76,6 @@ const updateStatusProperty = (
|
||||
return page
|
||||
})
|
||||
}
|
||||
return old
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,4 @@ export const infinitePageParams = {
|
||||
}
|
||||
|
||||
export const flattenPages = <T>(data: InfiniteData<PagedResponse<T[]>> | undefined): T[] | [] =>
|
||||
data?.pages.map(page => page.body).flat() || []
|
||||
data?.pages.flatMap(page => page.body) || []
|
||||
|
Reference in New Issue
Block a user