Skip to content

Open source alternative to Semrush and Ahrefs

Open the last project from / without waiting for the project list (#920)

Carter Smitha15ac02ca997
diff --git a/src/client/components/Sidebar.tsx b/src/client/components/Sidebar.tsx
index 294493be..0ecbee14 100644
--- a/src/client/components/Sidebar.tsx
+++ b/src/client/components/Sidebar.tsx
@@ -14,6 +14,7 @@ import {
} from "lucide-react";
import { organizationContextQueryOptions } from "@/client/features/team/organizationQueries";
import { switchOrganization } from "@/serverFunctions/organization";
+import { clearLastProjectId } from "@/client/lib/active-project";
import {
connectNavGroup,
getProjectNavGroups,
@@ -218,6 +219,9 @@ function AccountFooter({ ready }: { ready: boolean }) {
setIsSwitching(true);
try {
await switchOrganization({ data: { organizationId } });
+ // The remembered project belongs to the old workspace; without this,
+ // "/" would open it before the access check bounces back.
+ clearLastProjectId();
window.location.assign("/");
} catch (error) {
toast.error(getStandardErrorMessage(error));
diff --git a/src/client/lib/active-project.ts b/src/client/lib/active-project.ts
index 5905fe8f..4b0a317b 100644
--- a/src/client/lib/active-project.ts
+++ b/src/client/lib/active-project.ts
@@ -1,7 +1,7 @@
// Remembers the last project the user was looking at so the app can return them
// there on the next visit. Browser-local only (per-device); the server never
-// trusts it — landing and the route guard always re-validate the id against the
-// org's project list.
+// trusts it — the project route's access check re-validates the id and clears
+// it when it isn't one of the org's projects.
const LAST_PROJECT_KEY = "openseo:lastProjectId";
export function getLastProjectId(): string | null {
diff --git a/src/routes/_app/index.tsx b/src/routes/_app/index.tsx
index 0559b900..9240e9b3 100644
--- a/src/routes/_app/index.tsx
+++ b/src/routes/_app/index.tsx
@@ -2,10 +2,7 @@ import { createFileRoute, useNavigate } from "@tanstack/react-router";
import { projectsQueryOptions } from "@/client/features/projects/projectQueries";
import { useEffect } from "react";
import { useQuery } from "@tanstack/react-query";
-import {
- clearLastProjectId,
- getLastProjectId,
-} from "@/client/lib/active-project";
+import { getLastProjectId } from "@/client/lib/active-project";
import {
getErrorCode,
UNEXPECTED_ERROR_MESSAGE,
@@ -23,6 +20,11 @@ export const Route = createFileRoute("/_app/")({
function IndexRedirect() {
const navigate = useNavigate();
+ // Open the remembered project at once instead of waiting on the project
+ // list. localStorage is untrusted, but the project layout checks access and,
+ // if the id isn't one of the org's projects, clears it and sends the user
+ // back here to fall back to the list.
+ const lastProjectId = getLastProjectId();
const { data, error, isError, isFetching, refetch } = useQuery({
...projectsQueryOptions(),
retry: false,
@@ -30,22 +32,10 @@ function IndexRedirect() {
useEffect(() => {
// getProjects always returns at least one project.
- if (!data) return;
-
- // localStorage is untrusted — only honor the remembered project if it's
- // actually in the org's list; otherwise fall back to the most recent and
- // clear the stale id.
- const lastProjectId = getLastProjectId();
- const target = data.find((project) => project.id === lastProjectId);
- if (lastProjectId && !target) {
- clearLastProjectId();
- }
-
- void navigate({
- to: "/p/$projectId",
- params: { projectId: (target ?? data[0]).id },
- });
- }, [data, navigate]);
+ const projectId = lastProjectId ?? data?.[0]?.id;
+ if (!projectId) return;
+ void navigate({ to: "/p/$projectId", params: { projectId } });
+ }, [lastProjectId, data, navigate]);
useEffect(() => {
if (getErrorCode(error) !== "PAYMENT_REQUIRED") {
diff --git a/src/routes/_app/p/$projectId/route.tsx b/src/routes/_app/p/$projectId/route.tsx
index ed7d5fd6..2e3906f8 100644
--- a/src/routes/_app/p/$projectId/route.tsx
+++ b/src/routes/_app/p/$projectId/route.tsx
@@ -6,7 +6,11 @@ import {
} from "@tanstack/react-router";
import { useQuery } from "@tanstack/react-query";
import { useEffect } from "react";
-import { setLastProjectId } from "@/client/lib/active-project";
+import {
+ clearLastProjectId,
+ getLastProjectId,
+ setLastProjectId,
+} from "@/client/lib/active-project";
import { getErrorCode } from "@/client/lib/error-messages";
import {
getCurrentAuthRedirectFromHref,
@@ -50,15 +54,20 @@ function useProjectAccessRedirect(projectId: string) {
});
return;
}
+ // Forget it if it's the remembered project, or the landing redirect
+ // would bring the user straight back here.
+ if (getLastProjectId() === projectId) clearLastProjectId();
void navigate({ to: "/", replace: true });
- }, [error, navigate]);
+ }, [error, navigate, projectId]);
+ return access.isSuccess;
}
function ProjectLayout() {
const { projectId } = Route.useParams();
- useProjectAccessRedirect(projectId);
+ const hasAccess = useProjectAccessRedirect(projectId);
- // Remember this as the last-visited project for the landing redirect.
+ // Remember this as the last-visited project for the landing redirect, once
+ // the access check confirms it's one of the org's projects.
// Settings and its sub-pages are excluded: editing another project's
// settings is administration, not a context switch, so it shouldn't change
// which project the app opens next time. (An explicit choice still counts:
@@ -70,9 +79,9 @@ function ProjectLayout() {
select: () => true,
}) ?? false;
useEffect(() => {
- if (isSettingsPage) return;
+ if (!hasAccess || isSettingsPage) return;
setLastProjectId(projectId);
- }, [projectId, isSettingsPage]);
+ }, [projectId, hasAccess, isSettingsPage]);
return <Outlet />;
}
diff --git a/src/routes/accept-invitation.$id.tsx b/src/routes/accept-invitation.$id.tsx
index 1fbca611..96990ae7 100644
--- a/src/routes/accept-invitation.$id.tsx
+++ b/src/routes/accept-invitation.$id.tsx
@@ -5,6 +5,7 @@ import { Button } from "@/client/components/ui/button";
import { Link, createFileRoute, notFound } from "@tanstack/react-router";
import { useState } from "react";
import { AuthPageCard, AuthPageShell } from "@/client/features/auth/AuthPage";
+import { clearLastProjectId } from "@/client/lib/active-project";
import { captureClientEvent } from "@/client/lib/posthog";
import { authClient, signOutAndRedirect, useSession } from "@/lib/auth-client";
import { isHostedClientAuthMode } from "@/lib/auth-mode";
@@ -118,8 +119,9 @@ function InvitationCard({
organizationId: accepted.data.invitation.organizationId,
});
captureClientEvent("team:invitation_accept");
- // Full navigation: every cached query in this tab belongs to the old
- // workspace.
+ // Full navigation: every cached query in this tab, and the remembered
+ // project, belong to the old workspace.
+ clearLastProjectId();
window.location.assign("/");
} catch {
setActionError("We couldn't accept the invitation. Please try again.");