1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00
This commit is contained in:
Zhiyuan Zheng
2021-02-11 01:33:31 +01:00
parent a40a645337
commit d1bddc696a
50 changed files with 271 additions and 269 deletions

View File

@ -28,29 +28,10 @@ const queryFunction = ({
pageParam
}: {
queryKey: QueryKeyTimeline
pageParam?: { direction: 'prev' | 'next'; id: Mastodon.Status['id'] }
pageParam?: { [key: string]: string }
}) => {
const { page, account, hashtag, list, toot } = queryKey[1]
let params: { [key: string]: string } = {}
if (pageParam) {
switch (pageParam.direction) {
case 'prev':
if (page === 'Bookmarks' || page === 'Favourites') {
params.max_id = pageParam.id
} else {
params.min_id = pageParam.id
}
break
case 'next':
if (page === 'Bookmarks' || page === 'Favourites') {
params.min_id = pageParam.id
} else {
params.max_id = pageParam.id
}
break
}
}
let params: { [key: string]: string } = { ...pageParam }
switch (page) {
case 'Following':
@ -89,7 +70,7 @@ const queryFunction = ({
})
case 'Account_Default':
if (pageParam && pageParam.direction === 'next') {
if (pageParam && pageParam.pointer === 'max_id') {
return client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
@ -108,10 +89,8 @@ const queryFunction = ({
pinned: 'true'
}
}).then(async res1 => {
let toots = res1.map(status => {
status.isPinned = true
return status
})
let pinned: Mastodon.Status['id'][] = []
res1.body.forEach(status => pinned.push(status.id))
const res2 = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
@ -120,7 +99,11 @@ const queryFunction = ({
exclude_replies: 'true'
}
})
return uniqBy([...toots, ...res2], 'id')
return {
body: uniqBy([...res1.body, ...res2.body], 'id'),
...(res2.links.next && { links: { max_id: res2.links.next } }),
pinned
}
})
}
@ -197,7 +180,9 @@ const queryFunction = ({
instance: 'local',
url: `statuses/${toot}/context`
})
return [...res2.ancestors, res1, ...res2.descendants]
return {
body: [...res2.body.ancestors, res1.body, ...res2.body.descendants]
}
})
default:
return Promise.reject()
@ -210,7 +195,18 @@ const useTimelineQuery = <TData = TimelineData>({
options,
...queryKeyParams
}: QueryKeyTimeline[1] & {
options?: UseInfiniteQueryOptions<any, AxiosError, TData>
options?: UseInfiniteQueryOptions<
{
body:
| Mastodon.Status[]
| Mastodon.Notification[]
| Mastodon.Conversation[]
links?: { prev?: string; next?: string }
pinned?: Mastodon.Status['id'][]
},
AxiosError,
TData
>
}) => {
const queryKey: QueryKeyTimeline = ['Timeline', { ...queryKeyParams }]
return useInfiniteQuery(queryKey, queryFunction, options)
@ -354,7 +350,7 @@ const mutationFunction = async (params: MutationVarsTimeline) => {
}
type MutationOptionsTimeline = MutationOptions<
Mastodon.Conversation | Mastodon.Notification | Mastodon.Status,
{ body: Mastodon.Conversation | Mastodon.Notification | Mastodon.Status },
AxiosError,
MutationVarsTimeline
>
@ -373,7 +369,7 @@ const useTimelineMutation = ({
onSuccess?: MutationOptionsTimeline['onSuccess'] | boolean
}) => {
return useMutation<
Mastodon.Conversation | Mastodon.Notification | Mastodon.Status,
{ body: Mastodon.Conversation | Mastodon.Notification | Mastodon.Status },
AxiosError,
MutationVarsTimeline
>(mutationFunction, {