Skip to content

Commit

Permalink
fix: store errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1230 committed Apr 20, 2024
1 parent a13388c commit 84bc87c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
8 changes: 4 additions & 4 deletions client/src/app/(auth)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useUser } from "@/hooks/useUser";
import { Redirect, Stack, useLocalSearchParams } from "expo-router";

import { useUserData } from "@/hooks/stores/useUserData";

export default function AuthLayout() {
const params = useLocalSearchParams();
const { loading, data } = useUser();
const session = useUserData.use.session();

if (loading) return null;
if (data?.session) return <Redirect href={{ pathname: "/(main)", params }} />;
if (session) return <Redirect href={{ pathname: "/(main)", params }} />;
return (
<Stack screenOptions={{ headerShown: false }}>
<Stack.Screen name="index" />
Expand Down
10 changes: 5 additions & 5 deletions client/src/app/(main)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Drawer from "@/components/DrawerNav/Drawer";
import { useUser } from "@/hooks/useUser";
import { Redirect, useLocalSearchParams } from "expo-router";

import Drawer from "@/components/DrawerNav/Drawer";
import { useUserData } from "@/hooks/stores/useUserData";

export default function HomeLayout() {
const params = useLocalSearchParams();
const { loading, data } = useUser();
const session = useUserData.use.session();

if (loading) return null;
if (!data?.session) return <Redirect href={{ pathname: "/(auth)", params }} />;
if (!session) return <Redirect href={{ pathname: "/(auth)", params }} />;
return <Drawer />;
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/hooks/fetchers/User/useUserPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { User } from "@/types";
type PostOpts = { email: string; password: string };

const postUser = (opts: PostOpts, apiKey: string) =>
fetcher<User>(`/user/`, {
fetcher<User>(`/user`, {
method: "POST",
apiKey,
body: JSON.stringify(opts),
Expand Down
32 changes: 26 additions & 6 deletions client/src/hooks/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useUserData } from "./stores/useUserData";
import Toast from "react-native-toast-message";
import { useIsRestoring } from "@tanstack/react-query";
import { useEffect } from "react";
import { isFetchError } from "@/lib/fetcher";

export function useUser() {
const { session, setSession, setUser } = useUserData();
Expand All @@ -13,10 +14,21 @@ export function useUser() {
useEffect(() => {
if (userQuery.isPending) return;
if (userQuery.isError) {
if (isFetchError(userQuery.error)) {
if (userQuery.error.status === 401) {
setUser(null);
setSession(null);
return Toast.show({
type: "error",
text1: "User Error",
text2: "Unauthorized.",
});
}
}
return Toast.show({
type: "error",
text1: "User Error",
text2: "Failed to fetch user.",
text2: userQuery.error.message,
});
}
if (userQuery.data) {
Expand All @@ -27,10 +39,20 @@ export function useUser() {
useEffect(() => {
if (userSessionQuery.isPending) return;
if (userSessionQuery.isError) {
if (isFetchError(userQuery.error)) {
if (userQuery.error.status === 401) {
setSession(null);
return Toast.show({
type: "error",
text1: "User Error",
text2: "Unauthorized.",
});
}
}
return Toast.show({
type: "error",
text1: "Session Error",
text2: "Failed to fetch user session.",
text2: userSessionQuery.error.message,
});
}
if (userSessionQuery.data) {
Expand All @@ -42,18 +64,16 @@ export function useUser() {
return { error: "No session" };
}

if (loading) return { loading: true } as const;

if (userQuery.isError) {
console.log("User Query Error", userQuery.error);
return { error: userQuery.error };
}

if (userSessionQuery.isError) {
console.log("User Session Query Error", userSessionQuery.error);
return { error: userSessionQuery.error };
}

if (loading) return { loading: true } as const;

if (!userQuery.data || !userSessionQuery.data) {
return { loading: true } as const;
}
Expand Down
13 changes: 8 additions & 5 deletions client/src/providers/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ const queryClient = new QueryClient({
}),
mutationCache: new MutationCache({
onError: (error) => {
Toast.show({
type: "error",
text1: error.name,
text2: error.message,
});
if (!isFetchError(error)) return;
if (error.status && error.status >= 500) {
Toast.show({
type: "error",
text1: error.name,
text2: error.message,
});
}
},
}),
});
Expand Down
6 changes: 3 additions & 3 deletions client/src/views/chat/ChatHeader/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useState } from "react";

import { useUser } from "@/hooks/useUser";
import { useAction } from "@/hooks/useAction";
import { useThreadQuery } from "@/hooks/fetchers/Thread/useThreadQuery";
import { useAgentQuery } from "@/hooks/fetchers/Agent/useAgentQuery";
import { useMessagesQuery } from "@/hooks/fetchers/Message/useMessagesQuery";
import { useUserQuery } from "@/hooks/fetchers/User/useUserQuery";

import {
DropdownMenu,
Expand All @@ -28,8 +28,8 @@ export function Dropdown({
threadId: string | null;
}) {
const threadQuery = useThreadQuery(threadId);
const { data } = useUser();
const currentAgent = threadId ? threadQuery.data?.agent : data?.user?.defaultAgent;
const { data } = useUserQuery();
const currentAgent = threadId ? threadQuery.data?.agent : data?.defaultAgent;
const agentQuery = useAgentQuery(currentAgent?.id || "");

const [open, setOpen] = useState(false);
Expand Down

0 comments on commit 84bc87c

Please sign in to comment.