diff --git a/src/app/(loading-group)/[organizationSlug]/page.tsx b/src/app/(loading-group)/[organizationSlug]/page.tsx index d9d6d7d6..ea946647 100644 --- a/src/app/(loading-group)/[organizationSlug]/page.tsx +++ b/src/app/(loading-group)/[organizationSlug]/page.tsx @@ -172,6 +172,10 @@ const OrganizationHomePage: FunctionComponent = () => { mutate(); }; + useEffect(() => { + localStorage.setItem("lastActiveOrg", activeOrg.slug); + }, [activeOrg.slug]); + useEffect(() => { // trigger a sync on page load - if the org has an external entity provider if (activeOrg.externalEntityProviderId) { diff --git a/src/app/page.tsx b/src/app/page.tsx index 31cb45d3..e62372d3 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,18 +1,34 @@ "use client"; -import { redirect } from "next/navigation"; +import { useRouter } from "next/navigation"; import { useSession } from "../context/SessionContext"; +import { useEffect } from "react"; const Index = () => { - // decide where to go based on if the user has any orgs const { organizations } = useSession(); - if (organizations.length > 0) { - // redirect to the first org - redirect(`/${organizations[0].slug}`); - } else { - // redirect to the onboarding page - redirect("/setup"); - } + const router = useRouter(); + + useEffect(() => { + if (organizations.length > 0) { + const lastActiveOrg = localStorage.getItem("lastActiveOrg"); + if (lastActiveOrg) { + const orgExists = organizations.some( + (org) => org.slug === lastActiveOrg, + ); + if (orgExists) { + router.replace(`/${lastActiveOrg}`); + } else { + router.replace(`/${organizations[0].slug}`); + } + } else { + router.replace(`/${organizations[0].slug}`); + } + } else { + router.replace("/setup"); + } + }, [organizations, router]); + + return null; }; export default Index;