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

Lots of updates

This commit is contained in:
Zhiyuan Zheng
2021-01-07 19:13:09 +01:00
parent dcb36a682d
commit 4b99813bb7
104 changed files with 2463 additions and 1619 deletions

View File

@ -0,0 +1,27 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Account', { id: Mastodon.Account['id'] }]
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { id } = queryKey[1]
return client<Mastodon.Account>({
method: 'get',
instance: 'local',
url: `accounts/${id}`
})
}
const hookAccount = <TData = Mastodon.Account>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Account, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Account', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookAccount

View File

@ -0,0 +1,35 @@
import client from '@api/client'
import { InstancesState } from '@utils/slices/instancesSlice'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = [
'AccountCheck',
{
id: Mastodon.Account['id']
index: NonNullable<InstancesState['local']['activeIndex']>
}
]
const queryFunction = async ({ queryKey }: { queryKey: QueryKey }) => {
const { id, index } = queryKey[1]
return client<Mastodon.Account>({
method: 'get',
instance: 'local',
localIndex: index,
url: `accounts/${id}`
})
}
const hookAccountCheck = <TData = Mastodon.Account>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Account, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['AccountCheck', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookAccountCheck

View File

@ -0,0 +1,32 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
type QueryKey = ['Announcements', { showAll?: boolean }]
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { showAll } = queryKey[1]
return client<Mastodon.Announcement[]>({
method: 'get',
instance: 'local',
url: `announcements`,
...(showAll && {
params: {
with_dismissed: 'true'
}
})
})
}
const hookAnnouncement = <TData = Mastodon.Announcement[]>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Announcement[], AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Announcements', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookAnnouncement

View File

@ -0,0 +1,34 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Apps', { instanceDomain?: string }]
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { instanceDomain } = queryKey[1]
const formData = new FormData()
formData.append('client_name', 'test_dudu')
formData.append('redirect_uris', 'exp://127.0.0.1:19000')
formData.append('scopes', 'read write follow push')
return client<Mastodon.Apps>({
method: 'post',
instance: 'remote',
instanceDomain,
url: `apps`,
body: formData
})
}
const hookApps = <TData = Mastodon.Apps>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Apps, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Apps', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookApps

View File

@ -0,0 +1,24 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
type QueryKey = ['Emojis']
const queryFunction = () => {
return client<Mastodon.Emoji[]>({
method: 'get',
instance: 'local',
url: 'custom_emojis'
})
}
const hookEmojis = <TData = Mastodon.Emoji[]>({
options
}: {
options?: UseQueryOptions<Mastodon.Emoji[], AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Emojis']
return useQuery(queryKey, queryFunction, options)
}
export default hookEmojis

View File

@ -0,0 +1,28 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Instance', { instanceDomain?: string }]
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { instanceDomain } = queryKey[1]
return client<Mastodon.Instance>({
method: 'get',
instance: 'remote',
instanceDomain,
url: `instance`
})
}
const hookInstance = <TData = Mastodon.Instance>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Instance, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Instance', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookInstance

View File

@ -0,0 +1,24 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Lists']
const queryFunction = () => {
return client<Mastodon.List[]>({
method: 'get',
instance: 'local',
url: 'lists'
})
}
const hookLists = <TData = Mastodon.List[]>({
options
}: {
options?: UseQueryOptions<Mastodon.List[], AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Lists']
return useQuery(queryKey, queryFunction, options)
}
export default hookLists

View File

@ -0,0 +1,30 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Relationship', { id: Mastodon.Account['id'] }]
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { id } = queryKey[1]
return client<Mastodon.Relationship>({
method: 'get',
instance: 'local',
url: `accounts/relationships`,
params: {
'id[]': id
}
})
}
const hookRelationship = <TData = Mastodon.Relationship>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Relationship, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Relationship', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookRelationship

View File

@ -0,0 +1,46 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useInfiniteQuery, UseInfiniteQueryOptions } from 'react-query'
export type QueryKey = [
'Relationships',
{ type: 'following' | 'followers'; id: Mastodon.Account['id'] }
]
const queryFunction = ({
queryKey,
pageParam
}: {
queryKey: QueryKey
pageParam?: { direction: 'next'; id: Mastodon.Status['id'] }
}) => {
const { type, id } = queryKey[1]
let params: { [key: string]: string } = {}
if (pageParam) {
switch (pageParam.direction) {
case 'next':
params.max_id = pageParam.id
break
}
}
return client<Mastodon.Account[]>({
method: 'get',
instance: 'local',
url: `accounts/${id}/${type}`,
params
})
}
const hookRelationships = <TData = Mastodon.Account[]>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseInfiniteQueryOptions<Mastodon.Account[], AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Relationships', { ...queryKeyParams }]
return useInfiniteQuery(queryKey, queryFunction, options)
}
export default hookRelationships

View File

@ -0,0 +1,41 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = [
'Search',
{
type?: 'accounts' | 'hashtags' | 'statuses'
term?: string
limit?: number
}
]
type SearchResult = {
accounts: Mastodon.Account[]
hashtags: Mastodon.Tag[]
statuses: Mastodon.Status[]
}
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { type, term, limit = 20 } = queryKey[1]
return client<SearchResult>({
version: 'v2',
method: 'get',
instance: 'local',
url: 'search',
params: { ...(type && { type }), ...(term && { q: term }), limit }
})
}
const hookSearch = <TData = SearchResult>({
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<SearchResult, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Search', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
export default hookSearch

View File

@ -0,0 +1,291 @@
import client from '@api/client'
import { AxiosError } from 'axios'
import { uniqBy } from 'lodash'
import { useInfiniteQuery, UseInfiniteQueryOptions } from 'react-query'
export type QueryKeyTimeline = [
'Timeline',
{
page: App.Pages
hashtag?: Mastodon.Tag['name']
list?: Mastodon.List['id']
toot?: Mastodon.Status['id']
account?: Mastodon.Account['id']
}
]
const queryFunction = async ({
queryKey,
pageParam
}: {
queryKey: QueryKeyTimeline
pageParam?: { direction: 'prev' | 'next'; id: Mastodon.Status['id'] }
}) => {
const { page, account, hashtag, list, toot } = queryKey[1]
let res
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
}
}
switch (page) {
case 'Following':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: 'timelines/home',
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Local':
params.local = 'true'
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: 'timelines/public',
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'LocalPublic':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: 'timelines/public',
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'RemotePublic':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'remote',
url: 'timelines/public',
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Notifications':
res = await client<Mastodon.Notification[]>({
method: 'get',
instance: 'local',
url: 'notifications',
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Account_Default':
if (pageParam && pageParam.direction === 'next') {
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `accounts/${account}/statuses`,
params: {
exclude_replies: 'true',
...params
}
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
} else {
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `accounts/${account}/statuses`,
params: {
pinned: 'true'
}
})
const pinnedLength = res.length
let toots = res
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `accounts/${account}/statuses`,
params: {
exclude_replies: 'true'
}
})
toots = uniqBy([...toots, ...res], 'id')
return Promise.resolve({
toots: toots,
pointer: undefined,
pinnedLength
})
}
case 'Account_All':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `accounts/${account}/statuses`,
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Account_Media':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `accounts/${account}/statuses`,
params: {
only_media: 'true',
...params
}
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Hashtag':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `timelines/tag/${hashtag}`,
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Conversations':
res = await client<Mastodon.Conversation[]>({
method: 'get',
instance: 'local',
url: `conversations`,
params
})
if (pageParam) {
// Bug in pull to refresh in conversations
res = res.filter(b => b.id !== pageParam.id)
}
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Bookmarks':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `bookmarks`,
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Favourites':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `favourites`,
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'List':
res = await client<Mastodon.Status[]>({
method: 'get',
instance: 'local',
url: `timelines/list/${list}`,
params
})
return Promise.resolve({
toots: res,
pointer: undefined,
pinnedLength: undefined
})
case 'Toot':
res = await client<Mastodon.Status>({
method: 'get',
instance: 'local',
url: `statuses/${toot}`
})
const theToot = res
res = await client<{
ancestors: Mastodon.Status[]
descendants: Mastodon.Status[]
}>({
method: 'get',
instance: 'local',
url: `statuses/${toot}/context`
})
return Promise.resolve({
toots: [...res.ancestors, theToot, ...res.descendants],
pointer: res.ancestors.length,
pinnedLength: undefined
})
default:
return Promise.reject()
}
}
type Unpromise<T extends Promise<any>> = T extends Promise<infer U> ? U : never
const hookTimeline = <TData = Unpromise<ReturnType<typeof queryFunction>>>({
options,
...queryKeyParams
}: QueryKeyTimeline[1] & {
options?: UseInfiniteQueryOptions<any, AxiosError, TData>
}) => {
const queryKey: QueryKeyTimeline = ['Timeline', { ...queryKeyParams }]
return useInfiniteQuery(queryKey, queryFunction, options)
}
export default hookTimeline