Skip to content
TypeScript

fix(web): project settings no longer dead-ends on environments without the project (#18692)

Samuel Soelleracf7043b72a5
diff --git a/apps/web/src/components/settings/ProjectSettingsPanel.browser.test.tsx b/apps/web/src/components/settings/ProjectSettingsPanel.browser.test.tsx
index 53df16f2cf6..acae49cafc6 100644
--- a/apps/web/src/components/settings/ProjectSettingsPanel.browser.test.tsx
+++ b/apps/web/src/components/settings/ProjectSettingsPanel.browser.test.tsx
@@ -6,6 +6,7 @@ import type {
import {
AuthOrchestrationOperateScope,
AuthSettingsWriteScope,
+ DEFAULT_SERVER_SETTINGS,
EnvironmentId,
ProjectId,
ProviderInstanceId,
@@ -26,7 +27,7 @@ import { AsyncResult, Atom } from "effect/reactivity";
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
-import { userEvent } from "vite-plus/test/browser";
+import { page, userEvent } from "vite-plus/test/browser";
import type { Project } from "../../types";
@@ -105,7 +106,7 @@ vi.mock(import("~/lib/composerDraftUploads"), async (original) => ({
...(await original()),
releaseProjectDraftUploads: state.releaseUploads,
}));
-// Sections with their own data sources and tests; this file covers project permissions.
+// These sections have separate data sources and their own tests.
vi.mock(import("./ProjectDefaultsSettings"), async (original) => ({
...(await original()),
ProjectDefaultsSettings: (() => null) as never,
@@ -115,11 +116,18 @@ vi.mock(import("./ProjectActionsSettings"), async (original) => ({
ProjectActionsSettings: (() => null) as never,
}));
+vi.mock(import("../files/projectFilesQueryState"), async (original) => ({
+ ...(await original()),
+ getProjectFileQueryAtom: () => missingProjectFile,
+}));
+
import { useComposerDraftStore } from "../../composerDraftStore";
import { appAtomRegistry } from "../../rpc/atomRegistry";
import { buildSidebarProjectSnapshots } from "../../sidebarProjectGrouping";
import { ProjectSettingsPanel } from "./ProjectSettingsPanel";
-import type { SettingsScopeSearch } from "./settingsScope";
+import { ProjectsSettings } from "./ProjectsSettings";
+import { SettingsScopeProvider } from "./SettingsScopeContext";
+import { validateSettingsScopeSearch, type SettingsScopeSearch } from "./settingsScope";
import { retainSettingsScope, validateSettingsRouteSearch } from "./settingsScopeNavigation";
const primaryId = EnvironmentId.make("primary");
@@ -129,7 +137,11 @@ const environments = [primaryId, remoteId].map((environmentId) => ({
environmentId,
label: environmentId,
connection: { phase: "connected" },
- serverConfig: { keybindings: DEFAULT_RESOLVED_KEYBINDINGS },
+ serverConfig: {
+ keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
+ settings: DEFAULT_SERVER_SETTINGS,
+ environment: { platform: { machine: "server" } },
+ },
}));
const session = (scopes: ReadonlyArray<AuthEnvironmentScope>): AuthSessionState => ({
authenticated: true,
@@ -142,6 +154,7 @@ const session = (scopes: ReadonlyArray<AuthEnvironmentScope>): AuthSessionState
},
});
const writable = () => AsyncResult.success(session([AuthOrchestrationOperateScope]));
+const missingProjectFile = Atom.make(AsyncResult.initial<never, never>());
function deferred<T>() {
let resolve!: (value: T) => void;
@@ -253,6 +266,38 @@ async function mountPanel(search: Omit<SettingsScopeSearch, "project"> = {}) {
return router;
}
+async function mountSettings(search: SettingsScopeSearch) {
+ const searchParams = new URLSearchParams(
+ Object.entries(search).flatMap(([key, value]) => (value === undefined ? [] : [[key, value]])),
+ );
+ const route = createRootRoute({
+ validateSearch: validateSettingsScopeSearch,
+ component: () => (
+ <SettingsScopeProvider
+ search={route.useSearch()}
+ onChange={(next) => void router.navigate({ to: "/settings/projects", search: () => next })}
+ >
+ <ProjectsSettings />
+ </SettingsScopeProvider>
+ ),
+ });
+ const router = createRouter({
+ routeTree: route,
+ history: createMemoryHistory({
+ initialEntries: [`/settings/projects?${searchParams.toString()}`],
+ }),
+ });
+ await router.load();
+ await act(async () =>
+ root.render(
+ <RegistryContext.Provider value={appAtomRegistry}>
+ <RouterProvider router={router} />
+ </RegistryContext.Provider>,
+ ),
+ );
+ return router;
+}
+
const nameInput = () => container.querySelector<HTMLInputElement>('[aria-label="Project name"]')!;
const removeCheckout = (root: string) =>
container.querySelector<HTMLButtonElement>(`[aria-label="Remove checkout ${root}"]`)!;
@@ -385,6 +430,82 @@ describe("project settings permissions", () => {
);
});
+describe("project settings scope", () => {
+ it("offers project checkout environments while keeping unselected and unknown projects recoverable", async () => {
+ state.projects = state.projects.filter((project) => project.environmentId === primaryId);
+ const key = projectKey();
+ const router = await mountSettings({});
+ const environmentMenu = () => page.getByRole("button", { name: /^Environment scope:/ });
+ const primaryOption = () => page.getByRole("menuitemradio", { name: /^primary/ });
+ const remoteOption = () => page.getByRole("menuitemradio", { name: /^remote/ });
+
+ await act(async () => userEvent.click(environmentMenu()));
+ await expect.element(primaryOption()).toBeVisible();
+ await expect.element(remoteOption()).toBeVisible();
+ await act(async () => userEvent.keyboard("{Escape}"));
+
+ await act(async () =>
+ router.navigate({ to: "/settings/projects", search: () => ({ project: key }) }),
+ );
+ await expect.element(page.getByRole("textbox", { name: "Project name" })).toBeVisible();
+ await act(async () => userEvent.click(environmentMenu()));
+ await expect.element(primaryOption()).toBeVisible();
+ await expect.element(remoteOption()).not.toBeInTheDocument();
+ await act(async () => userEvent.keyboard("{Escape}"));
+
+ await act(async () =>
+ router.navigate({ to: "/settings/projects", search: () => ({ project: "removed-project" }) }),
+ );
+ await act(async () => userEvent.click(environmentMenu()));
+ await expect.element(primaryOption()).toBeVisible();
+ await expect.element(remoteOption()).toBeVisible();
+ await act(async () => userEvent.click(remoteOption()));
+ await vi.waitFor(() =>
+ expect(router.state.location.search).toEqual({
+ project: "removed-project",
+ machine: remoteId,
+ }),
+ );
+ });
+
+ it.each([
+ [undefined, "This project has no checkout on this environment."],
+ [
+ "removed-checkout",
+ "This checkout is no longer available in the selected project and environment.",
+ ],
+ ])("recovers a stale environment link with checkout %s", async (checkout, message) => {
+ state.projects = state.projects.filter((project) => project.environmentId === primaryId);
+ const key = projectKey();
+ const router = await mountSettings({
+ project: key,
+ machine: remoteId,
+ ...(checkout ? { checkout } : {}),
+ });
+
+ await expect.element(page.getByText(message, { exact: true })).toBeVisible();
+ await expect.element(page.getByRole("button", { name: /^Project scope:/ })).toBeVisible();
+ await expect
+ .element(page.getByRole("button", { name: "Environment scope: remote" }))
+ .toBeVisible();
+ await act(async () =>
+ userEvent.click(page.getByRole("button", { name: "Show all environments" })),
+ );
+
+ await expect
+ .element(page.getByRole("textbox", { name: "Project name" }))
+ .toHaveValue("Shared repo");
+ expect(router.state.location.search).toEqual({ project: key });
+ await expect
+ .element(page.getByRole("button", { name: "Environment scope: All environments" }))
+ .toBeVisible();
+ await expect.element(page.getByText(message, { exact: true })).not.toBeInTheDocument();
+ await expect
+ .element(page.getByRole("button", { name: "Remove project", exact: true }))
+ .toBeEnabled();
+ });
+});
+
it("returns to the project overview after removal, clearing the selection but keeping the environment", async () => {
state.projects = [state.projects[1]!];
const router = await mountPanel({ machine: remoteId, checkout: "remote:/work/remote" });
diff --git a/apps/web/src/components/settings/ProjectSettingsPanel.tsx b/apps/web/src/components/settings/ProjectSettingsPanel.tsx
index ed0c8569f6a..b9a2919786e 100644
--- a/apps/web/src/components/settings/ProjectSettingsPanel.tsx
+++ b/apps/web/src/components/settings/ProjectSettingsPanel.tsx
@@ -15,7 +15,7 @@ import { type EnvironmentId, type ProjectIconOverride } from "@t3tools/contracts
import { useLocation, useNavigate } from "@tanstack/react-router";
import * as Cause from "effect/Cause";
import { InfoIcon, Trash2Icon } from "lucide-react";
-import { lazy, Suspense, useEffect, useRef, useState } from "react";
+import { lazy, Suspense, useEffect, useRef, useState, type ReactNode } from "react";
import { useComposerDraftStore } from "../../composerDraftStore";
import { releaseProjectDraftUploads } from "../../lib/composerDraftUploads";
@@ -65,10 +65,12 @@ export function ProjectSettingsPanel({
projectKey,
environmentId = null,
checkoutKey = null,
+ unavailable,
}: {
projectKey: string;
environmentId?: EnvironmentId | null;
checkoutKey?: string | null;
+ unavailable?: ReactNode;
}) {
const groups = useSettingsProjectGroups();
const navigate = useNavigate({ from: "/settings" });
@@ -126,6 +128,8 @@ export function ProjectSettingsPanel({
}
}, [groups, navigate, pathname, projectKey, members.length, environmentId, checkoutKey]);
+ if ((!selected || members.length === 0) && unavailable != null) return unavailable;
+
if (!selected) {
return (
<div className="flex flex-1 items-center justify-center p-8 text-sm text-muted-foreground">
diff --git a/apps/web/src/components/settings/ProjectsSettings.tsx b/apps/web/src/components/settings/ProjectsSettings.tsx
index f29a07f2831..826c92bca26 100644
--- a/apps/web/src/components/settings/ProjectsSettings.tsx
+++ b/apps/web/src/components/settings/ProjectsSettings.tsx
@@ -1,12 +1,34 @@
import { EnvironmentId } from "@t3tools/contracts";
+import { LayersIcon } from "lucide-react";
+import { Button } from "../ui/button";
import { ProjectSettingsPanel } from "./ProjectSettingsPanel";
import { useSettingsScope } from "./SettingsScopeContext";
import { SettingsScopeNotice } from "./SettingsScopeNotice";
import { SettingsPageContainer } from "./settingsLayout";
+import { ALL_ENVIRONMENTS_VALUE, selectEnvironmentAxis } from "./settingsScopeAxis";
export function ProjectsSettings() {
- const { search: value, scope } = useSettingsScope();
+ const { search: value, scope, selectScope } = useSettingsScope();
+ const unavailable =
+ scope.kind === "unavailable" ? (
+ <SettingsPageContainer>
+ <div className="flex flex-col items-start gap-3">
+ <p className="text-sm text-muted-foreground">{scope.message}</p>
+ {value.machine || value.checkout ? (
+ <Button
+ variant="outline"
+ size="xs"
+ onClick={() => selectScope(selectEnvironmentAxis(value, ALL_ENVIRONMENTS_VALUE))}
+ >
+ <LayersIcon />
+ Show all environments
+ </Button>
+ ) : null}
+ </div>
+ </SettingsPageContainer>
+ ) : null;
+ // Keep the panel mounted so it can follow members when regrouping changes the project key.
const projectScope =
scope.kind === "project" ||
scope.kind === "checkout" ||
@@ -19,11 +41,10 @@ export function ProjectsSettings() {
projectKey={value.project}
environmentId={value.machine ? EnvironmentId.make(value.machine) : null}
checkoutKey={value.checkout ?? null}
+ unavailable={unavailable}
/>
- ) : scope.kind === "unavailable" ? (
- <SettingsPageContainer>
- <p className="text-sm text-muted-foreground">{scope.message}</p>
- </SettingsPageContainer>
+ ) : unavailable ? (
+ unavailable
) : (
<SettingsScopeNotice target="project">
Choose a project to manage its name, icon, checkouts and actions.
diff --git a/apps/web/src/components/settings/SettingsScopeSentence.tsx b/apps/web/src/components/settings/SettingsScopeSentence.tsx
index 1b9e542bbc4..200f09a8ec5 100644
--- a/apps/web/src/components/settings/SettingsScopeSentence.tsx
+++ b/apps/web/src/components/settings/SettingsScopeSentence.tsx
@@ -230,6 +230,13 @@ function EnvironmentScopeMenu({
const selected = environments.find(
(environment) => environment.environmentId === environmentValue,
);
+ const project = groups.find((group) => group.projectKey === value.project);
+ const memberEnvironmentIds = project
+ ? new Set(project.memberProjects.map((member) => member.environmentId))
+ : null;
+ const options = memberEnvironmentIds
+ ? environments.filter((environment) => memberEnvironmentIds.has(environment.environmentId))
+ : environments;
return (
<ScopeMenu
ariaLabel="Environment scope"
@@ -263,7 +270,7 @@ function EnvironmentScopeMenu({
icon: <LayersIcon aria-hidden className="size-3.5 shrink-0" />,
}
}
- options={environments.map((environment) => {
+ options={options.map((environment) => {
const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
return {
value: environment.environmentId,