feat(mobile): enhance invitations screen with loading state and error handling
- Replaced the user avatar icon in the UserAvatar component for better visual consistency. - Added a new GroupedInsetActivityIndicatorCell to display a loading indicator while fetching invitations. - Updated the Invitations screen to handle loading states and improved error handling with toast notifications. - Refactored invitation data fetching to ensure proper data structure and loading management. These changes improve user experience by providing feedback during data loading and enhancing error visibility. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { cn } from "@follow/utils/src/utils"
|
||||
import { Text, View } from "react-native"
|
||||
|
||||
import { User4CuteReIcon } from "@/src/icons/user_4_cute_re"
|
||||
import { User4CuteFiIcon } from "@/src/icons/user_4_cute_fi"
|
||||
|
||||
import { Galeria } from "../image/galeria"
|
||||
import { Image } from "../image/Image"
|
||||
@@ -43,7 +43,7 @@ export const UserAvatar = ({
|
||||
{name.slice(0, 2)}
|
||||
</Text>
|
||||
) : (
|
||||
<User4CuteReIcon width={size} height={size} color={color} />
|
||||
<User4CuteFiIcon width={size} height={size} color={color} />
|
||||
)}
|
||||
</View>
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { FC, PropsWithChildren } from "react"
|
||||
import * as React from "react"
|
||||
import { Fragment } from "react"
|
||||
import type { PressableProps, ViewProps } from "react-native"
|
||||
import { Pressable, StyleSheet, Text, View } from "react-native"
|
||||
import { ActivityIndicator, Pressable, StyleSheet, Text, View } from "react-native"
|
||||
import Animated, { FadeIn, FadeOut } from "react-native-reanimated"
|
||||
|
||||
import { CheckFilledIcon } from "@/src/icons/check_filled"
|
||||
@@ -340,3 +340,11 @@ export const GroupedPlainButtonCell: FC<
|
||||
</GroupedInsetListBaseCell>
|
||||
)
|
||||
}
|
||||
|
||||
export const GroupedInsetActivityIndicatorCell: FC = () => {
|
||||
return (
|
||||
<GroupedInsetListBaseCell className="flex-1 items-center justify-center py-4">
|
||||
<ActivityIndicator />
|
||||
</GroupedInsetListBaseCell>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { FetchError } from "ofetch"
|
||||
|
||||
import { toast } from "./toast"
|
||||
|
||||
export const getFetchErrorInfo = (
|
||||
error: Error,
|
||||
): {
|
||||
message: string
|
||||
code?: number
|
||||
} => {
|
||||
if (error instanceof FetchError) {
|
||||
try {
|
||||
const json = JSON.parse(error.response?._data)
|
||||
|
||||
const { reason, code, message } = json
|
||||
const i18nKey = `errors:${code}` as any
|
||||
const i18nMessage = t(i18nKey) === i18nKey ? message : t(i18nKey)
|
||||
return {
|
||||
message: `${i18nMessage}${reason ? `: ${reason}` : ""}`,
|
||||
code,
|
||||
}
|
||||
} catch {
|
||||
return { message: error.message }
|
||||
}
|
||||
}
|
||||
|
||||
return { message: error.message }
|
||||
}
|
||||
|
||||
export const getFetchErrorMessage = (error: Error) => {
|
||||
const { message } = getFetchErrorInfo(error)
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Just a wrapper around `toastFetchError` to create a function that can be used as a callback.
|
||||
*/
|
||||
export const createErrorToaster = (title?: string) => (err: Error) =>
|
||||
toastFetchError(err, { title })
|
||||
|
||||
export const toastFetchError = (error: Error, { title: _title }: { title?: string } = {}) => {
|
||||
let message = ""
|
||||
let _reason = ""
|
||||
let code: number | undefined
|
||||
|
||||
if (error instanceof FetchError) {
|
||||
try {
|
||||
const json =
|
||||
typeof error.response?._data === "string"
|
||||
? JSON.parse(error.response?._data)
|
||||
: error.response?._data
|
||||
|
||||
const { reason, code: _code, message: _message } = json
|
||||
code = _code
|
||||
message = _message
|
||||
|
||||
const i18nMessage = message
|
||||
|
||||
message = i18nMessage
|
||||
|
||||
if (reason) {
|
||||
_reason = reason
|
||||
}
|
||||
} catch {
|
||||
message = error.message
|
||||
}
|
||||
}
|
||||
|
||||
// 2fa errors are handled by the form
|
||||
if (code === 4007 || code === 4008) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!_reason) {
|
||||
const title = _title || message
|
||||
return toast.error(title)
|
||||
} else {
|
||||
return toast.error(message || _title || "Unknown error")
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { cn } from "@follow/utils"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { useMutation, useQuery } from "@tanstack/react-query"
|
||||
import dayjs from "dayjs"
|
||||
import { setStringAsync } from "expo-clipboard"
|
||||
import { Clipboard, Pressable, Text, View } from "react-native"
|
||||
|
||||
import { useServerConfigs } from "@/src/atoms/server-configs"
|
||||
@@ -13,6 +14,7 @@ import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar"
|
||||
import { ContextMenu } from "@/src/components/ui/context-menu"
|
||||
import {
|
||||
GroupedInformationCell,
|
||||
GroupedInsetActivityIndicatorCell,
|
||||
GroupedInsetListBaseCell,
|
||||
GroupedInsetListCard,
|
||||
GroupedInsetListSectionHeader,
|
||||
@@ -21,16 +23,20 @@ import { MonoText } from "@/src/components/ui/typography/MonoText"
|
||||
import { LoveCuteFiIcon } from "@/src/icons/love_cute_fi"
|
||||
import { PowerIcon } from "@/src/icons/power"
|
||||
import { apiClient } from "@/src/lib/api-fetch"
|
||||
import type { DialogComponent } from "@/src/lib/dialog"
|
||||
import { Dialog } from "@/src/lib/dialog"
|
||||
import { toastFetchError } from "@/src/lib/error-parser"
|
||||
import type { NavigationControllerView } from "@/src/lib/navigation/types"
|
||||
import { queryClient } from "@/src/lib/query-client"
|
||||
import { toast } from "@/src/lib/toast"
|
||||
import { accentColor } from "@/src/theme/colors"
|
||||
|
||||
const invitationQueryKey = ["invitations"]
|
||||
const useInvitationsQuery = () => {
|
||||
const { data } = useQuery({
|
||||
queryKey: ["invitations"],
|
||||
return useQuery({
|
||||
queryKey: invitationQueryKey,
|
||||
queryFn: () => apiClient.invitations.$get(),
|
||||
})
|
||||
return data?.data
|
||||
}
|
||||
const useInvitationsLimitationQuery = () => {
|
||||
const { data } = useQuery({
|
||||
@@ -39,25 +45,12 @@ const useInvitationsLimitationQuery = () => {
|
||||
})
|
||||
return data?.data
|
||||
}
|
||||
// export const invitations = {
|
||||
// list: () =>
|
||||
// defineQuery(["invitations"], async () => {
|
||||
// const res = await apiClient.invitations.$get()
|
||||
// return res.data
|
||||
// }),
|
||||
|
||||
// limitation: () =>
|
||||
// defineQuery(["invitations", "limitation"], async () => {
|
||||
// const res = await apiClient.invitations.limitation.$get()
|
||||
// return res.data
|
||||
// }),
|
||||
// }
|
||||
|
||||
const numberFormatter = new Intl.NumberFormat("en-US")
|
||||
export const InvitationsScreen: NavigationControllerView = () => {
|
||||
const serverConfigs = useServerConfigs()
|
||||
|
||||
const invitations = useInvitationsQuery()
|
||||
const { data: invitations, isLoading } = useInvitationsQuery()
|
||||
const limitation = useInvitationsLimitationQuery()
|
||||
const handleCopyCode = (code: string) => {
|
||||
Clipboard.setString(code)
|
||||
@@ -92,15 +85,19 @@ export const InvitationsScreen: NavigationControllerView = () => {
|
||||
|
||||
<GroupedInsetListSectionHeader label="Invitations" />
|
||||
<GroupedInsetListCard>
|
||||
{invitations?.map((invitation) => (
|
||||
{isLoading && <GroupedInsetActivityIndicatorCell />}
|
||||
{invitations?.data?.map((invitation) => (
|
||||
<ContextMenu.Root key={invitation.code}>
|
||||
<ContextMenu.Trigger>
|
||||
<GroupedInsetListBaseCell className="bg-secondary-system-grouped-background flex-1">
|
||||
<View className="mr-2 shrink flex-row items-center gap-4">
|
||||
<UserAvatar size={26} image={invitation.users?.image} preview={false} />
|
||||
<View className="min-w-0 shrink">
|
||||
<Text className="text-label" numberOfLines={1}>
|
||||
{invitation.users?.name}
|
||||
<Text
|
||||
className={cn("text-label", !invitation.users && "text-secondary-label")}
|
||||
numberOfLines={1}
|
||||
>
|
||||
{invitation.users?.name || (!invitation.users ? "Not used" : "")}
|
||||
</Text>
|
||||
<Text className="text-secondary-label text-sm">
|
||||
Created at {dayjs(invitation.createdAt).format("YYYY/MM/DD")}
|
||||
@@ -131,10 +128,15 @@ export const InvitationsScreen: NavigationControllerView = () => {
|
||||
|
||||
const GenerateButton = () => {
|
||||
const limitation = useInvitationsLimitationQuery()
|
||||
const invitations = useInvitationsQuery()
|
||||
const disabled = !limitation || (invitations && invitations?.length >= limitation)
|
||||
const { data: invitations } = useInvitationsQuery()
|
||||
const disabled = !limitation || (invitations && invitations?.data?.length >= limitation)
|
||||
return (
|
||||
<UINavigationHeaderActionButton disabled={disabled}>
|
||||
<UINavigationHeaderActionButton
|
||||
disabled={disabled}
|
||||
onPress={() => {
|
||||
Dialog.show(ConfirmGenerateDialog)
|
||||
}}
|
||||
>
|
||||
<Text
|
||||
className={cn(
|
||||
"text-base font-semibold",
|
||||
@@ -146,3 +148,62 @@ const GenerateButton = () => {
|
||||
</UINavigationHeaderActionButton>
|
||||
)
|
||||
}
|
||||
|
||||
const ConfirmGenerateDialog: DialogComponent = () => {
|
||||
const serverConfigs = useServerConfigs()
|
||||
const { dismiss } = Dialog.useDialogContext()!
|
||||
|
||||
const newInvitation = useMutation({
|
||||
mutationKey: ["newInvitation"],
|
||||
mutationFn: (values: Parameters<typeof apiClient.invitations.new.$post>[0]["json"]) =>
|
||||
apiClient.invitations.new.$post({ json: values }),
|
||||
onError(err) {
|
||||
toastFetchError(err)
|
||||
},
|
||||
onSuccess(data) {
|
||||
dismiss()
|
||||
|
||||
toast.success("Generate successfully, code is copied to clipboard")
|
||||
|
||||
type Invitation = {
|
||||
code: string
|
||||
createdAt: string | null
|
||||
}
|
||||
const old = queryClient.getQueryData<Invitation[]>(invitationQueryKey)
|
||||
|
||||
queryClient.setQueryData<Invitation[]>(invitationQueryKey, () => {
|
||||
return [
|
||||
{
|
||||
code: data.data,
|
||||
createdAt: dayjs().toISOString(),
|
||||
},
|
||||
...(old ?? []),
|
||||
]
|
||||
})
|
||||
setStringAsync(data.data)
|
||||
},
|
||||
})
|
||||
|
||||
return (
|
||||
<View>
|
||||
<Text>
|
||||
You can spend {serverConfigs?.INVITATION_PRICE}{" "}
|
||||
<View style={{ transform: [{ translateY: 2 }] }}>
|
||||
<PowerIcon color={accentColor} height={16} width={16} />
|
||||
</View>{" "}
|
||||
Power to generate an invitation code for your friends.
|
||||
</Text>
|
||||
|
||||
<Dialog.DialogConfirm
|
||||
onPress={() => {
|
||||
newInvitation.mutateAsync({})
|
||||
}}
|
||||
/>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
ConfirmGenerateDialog.id = "ConfirmGenerateDialog"
|
||||
ConfirmGenerateDialog.confirmText = "Generate"
|
||||
ConfirmGenerateDialog.title = "Generate Invitation Code"
|
||||
ConfirmGenerateDialog.headerIcon = <PowerIcon color={accentColor} height={24} width={24} />
|
||||
|
||||
Reference in New Issue
Block a user