2022-12-10 14:47:19 +01:00
|
|
|
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
2022-12-28 23:41:36 +01:00
|
|
|
import apiInstance from '@utils/api/instance'
|
|
|
|
import { getAccountStorage, setAccountStorage } from '@utils/storage/actions'
|
|
|
|
import { AxiosError } from 'axios'
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2021-12-18 19:59:38 +01:00
|
|
|
type QueryKeyEmojis = ['Emojis']
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2021-12-18 19:59:38 +01:00
|
|
|
const queryFunction = async () => {
|
|
|
|
const res = await apiInstance<Mastodon.Emoji[]>({
|
2021-01-07 19:13:09 +01:00
|
|
|
method: 'get',
|
|
|
|
url: 'custom_emojis'
|
2021-12-18 19:59:38 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 23:41:36 +01:00
|
|
|
const useEmojisQuery = (params?: { options?: UseQueryOptions<Mastodon.Emoji[], AxiosError> }) => {
|
2021-12-18 19:59:38 +01:00
|
|
|
const queryKey: QueryKeyEmojis = ['Emojis']
|
2022-12-28 23:41:36 +01:00
|
|
|
return useQuery(queryKey, queryFunction, {
|
|
|
|
...params?.options,
|
|
|
|
staleTime: Infinity,
|
|
|
|
cacheTime: Infinity,
|
|
|
|
onSuccess: data => {
|
|
|
|
if (!data.length) return
|
|
|
|
|
|
|
|
const currEmojis = getAccountStorage.object('emojis_frequent')
|
|
|
|
if (!Array.isArray(currEmojis)) {
|
|
|
|
setAccountStorage([{ key: 'emojis_frequent', value: [] }])
|
|
|
|
} else {
|
|
|
|
setAccountStorage([
|
|
|
|
{
|
|
|
|
key: 'emojis_frequent',
|
|
|
|
value: currEmojis?.filter(emoji =>
|
|
|
|
data.find(e => e.shortcode === emoji.emoji.shortcode)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
export { useEmojisQuery }
|