diff --git a/front/apps/mobile/app/_layout.tsx b/front/apps/mobile/app/_layout.tsx index 19ada4e9..7b18f754 100644 --- a/front/apps/mobile/app/_layout.tsx +++ b/front/apps/mobile/app/_layout.tsx @@ -21,7 +21,7 @@ import { PortalProvider } from "@gorhom/portal"; import { ThemeSelector } from "@kyoo/primitives"; import { NavbarRight, NavbarTitle } from "@kyoo/ui"; -import { AccountProvider, createQueryClient, useAccounts } from "@kyoo/models"; +import { AccountProvider, createQueryClient, useAccount, useAccounts } from "@kyoo/models"; import { QueryClientProvider } from "@tanstack/react-query"; import i18next from "i18next"; import { Stack } from "expo-router"; @@ -92,53 +92,53 @@ const ThemedStack = ({ onLayout }: { onLayout?: () => void }) => { ); }; -const AuthGuard = ({ selected }: { selected: number | null }) => { +const AuthGuard = () => { const router = useRouter(); + // TODO: support guest accounts on mobile too. + const account = useAccount(); useEffect(() => { - if (selected === null) + if (account === null) router.replace("/login", undefined, { experimental: { nativeBehavior: "stack-replace", isNestedNavigator: false }, }); - }, [selected, router]); + }, [account, router]); return null; }; -let rendered: boolean = false; SplashScreen.preventAutoHideAsync(); export default function Root() { const [queryClient] = useState(() => createQueryClient()); const theme = useColorScheme(); const [fontsLoaded] = useFonts({ Poppins_300Light, Poppins_400Regular, Poppins_900Black }); - const info = useAccounts(); - const isReady = fontsLoaded && (rendered || info.type !== "loading"); useEffect(() => { - if (isReady) SplashScreen.hideAsync(); - }, [isReady]); + if (fontsLoaded) SplashScreen.hideAsync(); + }, [fontsLoaded]); - if (!isReady) return null; - rendered = true; + if (!fontsLoaded) return null; return ( - - - - {info.type === "loading" ? : } - {info.type === "error" && } - {info.type === "ok" && } - - - + + + + <> + + + + > + + + ); } diff --git a/front/apps/mobile/app/connection-error.tsx b/front/apps/mobile/app/connection-error.tsx index b4b57ec8..2a81145d 100644 --- a/front/apps/mobile/app/connection-error.tsx +++ b/front/apps/mobile/app/connection-error.tsx @@ -18,7 +18,7 @@ * along with Kyoo. If not, see . */ -import { AccountContext } from "@kyoo/models"; +import { ConnectionErrorContext } from "@kyoo/models"; import { Button, H1, P, ts } from "@kyoo/primitives"; import { useRouter } from "expo-router"; import { useContext } from "react"; @@ -30,12 +30,12 @@ const ConnectionError = () => { const { css } = useYoshiki(); const { t } = useTranslation(); const router = useRouter(); - const { error, retry } = useContext(AccountContext); + const { error, retry } = useContext(ConnectionErrorContext); return ( {t("errors.connection")} - {error ?? t("error.unknown")} + {error?.errors[0] ?? t("error.unknown")} {t("errors.connection-tips")} ; const AccountContext = createContext<(Account & { select: () => void; remove: () => void })[]>([]); +export const ConnectionErrorContext = createContext<{ + error: KyooErrors | null; + retry?: () => void; +}>({ error: null }); export const AccountProvider = ({ children }: { children: ReactNode }) => { const [accStr] = useMMKVString("accounts"); @@ -80,15 +85,27 @@ export const AccountProvider = ({ children }: { children: ReactNode }) => { const oldSelectedId = useRef(selected?.id); useEffect(() => { // if the user change account (or connect/disconnect), reset query cache. - if (selected?.id !== oldSelectedId.current) - queryClient.invalidateQueries(); + if (selected?.id !== oldSelectedId.current) queryClient.invalidateQueries(); oldSelectedId.current = selected?.id; // update cookies for ssr (needs to contains token, theme, language...) if (Platform.OS === "web") setAccountCookie(selected); }, [selected, queryClient]); - return {children}; + return ( + + { + queryClient.invalidateQueries({ queryKey: ["auth", "me"] }); + }, + }} + > + {children} + + + ); }; export const useAccount = () => {
{error ?? t("error.unknown")}
{error?.errors[0] ?? t("error.unknown")}
{t("errors.connection-tips")}