feat: integrate LoadingContainer
- Added LoadingContainer component to RootLayout for improved loading state management. - Refactored DebugPanel to include useEffect and removed unused code for better clarity and performance. These changes enhance user experience by providing a loading indicator and streamlining the debug interface. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import { atom } from "jotai"
|
||||
|
||||
export const loadingVisibleAtom = atom(false)
|
||||
|
||||
export const loadingAtom = atom<{
|
||||
finish: null | (() => any)
|
||||
cancel: null | (() => any)
|
||||
thenable: null | Promise<any>
|
||||
}>({
|
||||
finish: null,
|
||||
cancel: null,
|
||||
thenable: null,
|
||||
})
|
||||
@@ -0,0 +1,100 @@
|
||||
import { useAtom } from "jotai"
|
||||
import { useCallback, useEffect, useRef, useState } from "react"
|
||||
import { Modal, Text, TouchableOpacity, View } from "react-native"
|
||||
import Animated, {
|
||||
Easing,
|
||||
useAnimatedStyle,
|
||||
useSharedValue,
|
||||
withRepeat,
|
||||
withTiming,
|
||||
} from "react-native-reanimated"
|
||||
|
||||
import { loadingAtom, loadingVisibleAtom } from "@/src/atoms/app"
|
||||
import { Loading3CuteReIcon } from "@/src/icons/loading_3_cute_re"
|
||||
|
||||
export const LoadingContainer = () => {
|
||||
const rotate = useSharedValue(0)
|
||||
|
||||
const [visible, setVisible] = useAtom(loadingVisibleAtom)
|
||||
const [showCancelButton, setShowCancelButton] = useState(false)
|
||||
|
||||
const [loadingCaller, setLoadingCaller] = useAtom(loadingAtom)
|
||||
|
||||
const resetLoadingCaller = useCallback(() => {
|
||||
setLoadingCaller({
|
||||
finish: null,
|
||||
cancel: null,
|
||||
thenable: null,
|
||||
})
|
||||
}, [setLoadingCaller])
|
||||
|
||||
useEffect(() => {
|
||||
rotate.value = withRepeat(
|
||||
withTiming(360, { duration: 1000, easing: Easing.linear }),
|
||||
Infinity,
|
||||
false,
|
||||
)
|
||||
return () => {
|
||||
rotate.value = 0
|
||||
}
|
||||
}, [rotate])
|
||||
|
||||
useEffect(() => {
|
||||
if (loadingCaller.thenable) {
|
||||
loadingCaller.thenable.finally(() => {
|
||||
setVisible(false)
|
||||
setShowCancelButton(false)
|
||||
|
||||
resetLoadingCaller()
|
||||
|
||||
loadingCaller.finish?.()
|
||||
})
|
||||
}
|
||||
}, [loadingCaller.thenable])
|
||||
|
||||
const cancelTimerRef = useRef<NodeJS.Timeout | null>(null)
|
||||
useEffect(() => {
|
||||
cancelTimerRef.current = setTimeout(() => {
|
||||
setShowCancelButton(true)
|
||||
}, 3000)
|
||||
return () => {
|
||||
if (cancelTimerRef.current) {
|
||||
clearTimeout(cancelTimerRef.current)
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
const rotateStyle = useAnimatedStyle(() => ({
|
||||
transform: [{ rotate: `${rotate.value}deg` }],
|
||||
}))
|
||||
|
||||
const cancel = () => {
|
||||
setVisible(false)
|
||||
setShowCancelButton(false)
|
||||
|
||||
if (loadingCaller.cancel) {
|
||||
loadingCaller.cancel()
|
||||
}
|
||||
resetLoadingCaller()
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal visible={visible} animationType="fade" transparent>
|
||||
<View className="flex-1 items-center justify-center bg-black/30">
|
||||
<View className="border-system-fill/40 rounded-2xl border bg-black/50 p-12 drop-shadow dark:bg-white/5">
|
||||
<Animated.View style={rotateStyle}>
|
||||
<Loading3CuteReIcon height={36} width={36} color="#fff" />
|
||||
</Animated.View>
|
||||
</View>
|
||||
|
||||
{showCancelButton && (
|
||||
<View className="absolute inset-x-0 bottom-24 flex-row justify-center">
|
||||
<TouchableOpacity onPress={cancel}>
|
||||
<Text className="text-center text-lg text-accent">Cancel</Text>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { useSetAtom } from "jotai"
|
||||
import { useCallback } from "react"
|
||||
|
||||
import { loadingAtom, loadingVisibleAtom } from "../atoms/app"
|
||||
|
||||
export const useLoadingCallback = () => {
|
||||
const setLoadingCaller = useSetAtom(loadingAtom)
|
||||
const setVisible = useSetAtom(loadingVisibleAtom)
|
||||
|
||||
return useCallback(
|
||||
(thenable: Promise<any>, options: { finish: () => any; cancel: () => any }) => {
|
||||
setLoadingCaller({
|
||||
thenable,
|
||||
finish: options.finish,
|
||||
cancel: options.cancel,
|
||||
})
|
||||
setVisible(true)
|
||||
},
|
||||
[setLoadingCaller, setVisible],
|
||||
)
|
||||
}
|
||||
@@ -19,27 +19,11 @@ import { clearSessionToken, getSessionToken, setSessionToken } from "@/src/lib/c
|
||||
|
||||
export default function DebugPanel() {
|
||||
const insets = useSafeAreaInsets()
|
||||
// const isExpanded = useSharedValue(false)
|
||||
|
||||
return (
|
||||
<ScrollView className="flex-1 bg-black" style={{ paddingTop: insets.top }}>
|
||||
<Text className="mt-4 px-8 text-2xl font-medium text-white">Users</Text>
|
||||
|
||||
{/* <Button
|
||||
title="Toggle"
|
||||
onPress={() => {
|
||||
isExpanded.value = !isExpanded.value
|
||||
}}
|
||||
/>
|
||||
<AccordionItem isExpanded={isExpanded} viewKey="users">
|
||||
{Array.from({ length: 100 }).map((_, index) => {
|
||||
return (
|
||||
<Text key={index} className="text-white">
|
||||
{index}
|
||||
</Text>
|
||||
)
|
||||
})}
|
||||
</AccordionItem> */}
|
||||
|
||||
<View style={styles.container}>
|
||||
<View style={styles.itemContainer}>
|
||||
<UserSessionSetting />
|
||||
|
||||
@@ -3,6 +3,7 @@ import "../global.css"
|
||||
import { Stack } from "expo-router"
|
||||
import { useColorScheme } from "nativewind"
|
||||
|
||||
import { LoadingContainer } from "../components/common/LoadingContainer"
|
||||
import { DebugButton } from "../modules/debug"
|
||||
import { RootProviders } from "../providers"
|
||||
import { usePrefetchSessionUser } from "../store/user/hooks"
|
||||
@@ -16,6 +17,7 @@ export default function RootLayout() {
|
||||
return (
|
||||
<RootProviders>
|
||||
<Session />
|
||||
<LoadingContainer />
|
||||
<Stack
|
||||
screenOptions={{
|
||||
contentStyle: { backgroundColor: systemBackgroundColor },
|
||||
|
||||
Reference in New Issue
Block a user