mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Added push server error messaging
Also clean up <Message> component
This commit is contained in:
@ -7,7 +7,6 @@ import { useAppDispatch } from '@root/store'
|
||||
import * as Sentry from '@sentry/react-native'
|
||||
import { getExpoToken, retrieveExpoToken } from '@utils/slices/appSlice'
|
||||
import { disableAllPushes, getInstances } from '@utils/slices/instancesSlice'
|
||||
import { useTheme } from '@utils/styles/ThemeManager'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { useEffect } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
@ -16,7 +15,6 @@ import { useSelector } from 'react-redux'
|
||||
|
||||
const pushUseConnect = () => {
|
||||
const { t } = useTranslation('screens')
|
||||
const { theme } = useTheme()
|
||||
|
||||
const dispatch = useAppDispatch()
|
||||
useEffect(() => {
|
||||
@ -39,8 +37,7 @@ const pushUseConnect = () => {
|
||||
Notifications.setBadgeCountAsync(0)
|
||||
if (error?.status == 404) {
|
||||
displayMessage({
|
||||
theme,
|
||||
type: 'error',
|
||||
type: 'danger',
|
||||
duration: 'long',
|
||||
message: t('pushError.message'),
|
||||
description: t('pushError.description'),
|
||||
|
@ -2,15 +2,13 @@ import apiInstance from '@api/instance'
|
||||
import haptics from '@components/haptics'
|
||||
import { displayMessage } from '@components/Message'
|
||||
import queryClient from '@helpers/queryClient'
|
||||
import { Theme } from '@utils/styles/themes'
|
||||
import { AxiosError } from 'axios'
|
||||
import i18next from 'i18next'
|
||||
import { RefObject } from 'react'
|
||||
import FlashMessage from 'react-native-flash-message'
|
||||
import { useMutation, useQuery, UseQueryOptions } from 'react-query'
|
||||
|
||||
type AccountWithSource = Mastodon.Account &
|
||||
Required<Pick<Mastodon.Account, 'source'>>
|
||||
type AccountWithSource = Mastodon.Account & Required<Pick<Mastodon.Account, 'source'>>
|
||||
|
||||
type QueryKeyProfile = ['Profile']
|
||||
const queryKey: QueryKeyProfile = ['Profile']
|
||||
@ -52,7 +50,6 @@ type MutationVarsProfileBase =
|
||||
}
|
||||
|
||||
type MutationVarsProfile = MutationVarsProfileBase & {
|
||||
theme: Theme
|
||||
messageRef: RefObject<FlashMessage>
|
||||
message: {
|
||||
text: string
|
||||
@ -92,73 +89,70 @@ const mutationFunction = async ({ type, data }: MutationVarsProfile) => {
|
||||
}
|
||||
|
||||
const useProfileMutation = () => {
|
||||
return useMutation<
|
||||
{ body: AccountWithSource },
|
||||
AxiosError,
|
||||
MutationVarsProfile
|
||||
>(mutationFunction, {
|
||||
onMutate: async variables => {
|
||||
await queryClient.cancelQueries(queryKey)
|
||||
return useMutation<{ body: AccountWithSource }, AxiosError, MutationVarsProfile>(
|
||||
mutationFunction,
|
||||
{
|
||||
onMutate: async variables => {
|
||||
await queryClient.cancelQueries(queryKey)
|
||||
|
||||
const oldData = queryClient.getQueryData<AccountWithSource>(queryKey)
|
||||
const oldData = queryClient.getQueryData<AccountWithSource>(queryKey)
|
||||
|
||||
queryClient.setQueryData<AccountWithSource | undefined>(queryKey, old => {
|
||||
if (old) {
|
||||
switch (variables.type) {
|
||||
case 'source[privacy]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, privacy: variables.data }
|
||||
}
|
||||
case 'source[sensitive]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, sensitive: variables.data }
|
||||
}
|
||||
case 'locked':
|
||||
return { ...old, locked: variables.data }
|
||||
case 'bot':
|
||||
return { ...old, bot: variables.data }
|
||||
default:
|
||||
return old
|
||||
queryClient.setQueryData<AccountWithSource | undefined>(queryKey, old => {
|
||||
if (old) {
|
||||
switch (variables.type) {
|
||||
case 'source[privacy]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, privacy: variables.data }
|
||||
}
|
||||
case 'source[sensitive]':
|
||||
return {
|
||||
...old,
|
||||
source: { ...old.source, sensitive: variables.data }
|
||||
}
|
||||
case 'locked':
|
||||
return { ...old, locked: variables.data }
|
||||
case 'bot':
|
||||
return { ...old, bot: variables.data }
|
||||
default:
|
||||
return old
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return oldData
|
||||
},
|
||||
onError: (err, variables, context) => {
|
||||
queryClient.setQueryData(queryKey, context)
|
||||
haptics('Error')
|
||||
if (variables.message.failed) {
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.failed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
...(err && { description: err.message }),
|
||||
theme: variables.theme,
|
||||
type: 'error'
|
||||
})
|
||||
return oldData
|
||||
},
|
||||
onError: (err, variables, context) => {
|
||||
queryClient.setQueryData(queryKey, context)
|
||||
haptics('Error')
|
||||
if (variables.message.failed) {
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.failed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
...(err && { description: err.message }),
|
||||
type: 'danger'
|
||||
})
|
||||
}
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
if (variables.message.succeed) {
|
||||
haptics('Success')
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.succeed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
type: 'success'
|
||||
})
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(queryKey)
|
||||
}
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
if (variables.message.succeed) {
|
||||
haptics('Success')
|
||||
displayMessage({
|
||||
ref: variables.messageRef,
|
||||
message: i18next.t('screenTabs:me.profile.feedback.succeed', {
|
||||
type: i18next.t(`screenTabs:${variables.message.text}`)
|
||||
}),
|
||||
theme: variables.theme,
|
||||
type: 'success'
|
||||
})
|
||||
}
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(queryKey)
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export { useProfileQuery, useProfileMutation }
|
||||
|
@ -1,12 +1,15 @@
|
||||
import apiInstance from '@api/instance'
|
||||
import apiTooot, { TOOOT_API_DOMAIN } from '@api/tooot'
|
||||
import { displayMessage } from '@components/Message'
|
||||
import i18n from '@root/i18n/i18n'
|
||||
import { RootState } from '@root/store'
|
||||
import * as Sentry from '@sentry/react-native'
|
||||
import { InstanceLatest } from '@utils/migrations/instances/migration'
|
||||
import { getInstance } from '@utils/slices/instancesSlice'
|
||||
import { Theme } from '@utils/styles/themes'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import * as Random from 'expo-random'
|
||||
import i18next from 'i18next'
|
||||
import { Platform } from 'react-native'
|
||||
import base64 from 'react-native-base64'
|
||||
import androidDefaults from './androidDefaults'
|
||||
@ -74,6 +77,12 @@ const pushRegister = async (
|
||||
})
|
||||
|
||||
if (!res.body.server_key?.length) {
|
||||
displayMessage({
|
||||
type: 'danger',
|
||||
duration: 'long',
|
||||
message: i18next.t('screenTabs:me.push.missingServerKey.message'),
|
||||
description: i18next.t('screenTabs:me.push.missingServerKey.description')
|
||||
})
|
||||
Sentry.setContext('Push server key', {
|
||||
instance: instanceUri,
|
||||
resBody: res.body
|
||||
|
Reference in New Issue
Block a user