Skip to content

Commit

Permalink
fix(frontend): added conditional fetch in useSWR to prevent request w…
Browse files Browse the repository at this point in the history
…hen user is null
  • Loading branch information
bekossy committed Sep 24, 2024
1 parent 467ccab commit 4b85af6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions agenta-web/src/contexts/app.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useSWR from "swr"
import {dynamicContext} from "@/lib/helpers/dynamic"
import {HookAPI} from "antd/es/modal/useModal"
import {useLocalStorage} from "usehooks-ts"
import {useProfileData} from "./profile.context"

type AppContextType = {
currentApp: ListAppsItem | null
Expand All @@ -31,6 +32,7 @@ const initialValues: AppContextType = {

const useApps = () => {
const [useOrgData, setUseOrgData] = useState<Function>(() => () => "")
const {user} = useProfileData()

useEffect(() => {
dynamicContext("org.context", {useOrgData}).then((context) => {
Expand All @@ -40,11 +42,13 @@ const useApps = () => {

const {selectedOrg, loading} = useOrgData()
const {data, error, isLoading, mutate} = useSWR(
`${getAgentaApiUrl()}/api/apps/` +
(isDemo()
? `?org_id=${selectedOrg?.id}&workspace_id=${selectedOrg?.default_workspace.id}`
: ""),
isDemo() ? (selectedOrg?.id ? axiosFetcher : () => {}) : axiosFetcher,
!!user
? `${getAgentaApiUrl()}/api/apps/` +
(isDemo()
? `?org_id=${selectedOrg?.id}&workspace_id=${selectedOrg?.default_workspace.id}`
: "")
: null,
!!user ? (isDemo() ? (selectedOrg?.id ? axiosFetcher : () => {}) : axiosFetcher) : null,
{
shouldRetryOnError: false,
},
Expand Down
2 changes: 1 addition & 1 deletion agenta-web/src/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useSession: () => {loading: boolean; doesSessionExist: boolean; log
.then(() => {
posthog.reset()
reset()
router.push("/auth").then(() => router.reload())
router.push("/auth")
})
.catch(console.error)
},
Expand Down

0 comments on commit 4b85af6

Please sign in to comment.