Skip to content
TypeScript

fix(web): project settings can set a checkout's grouping rule again (#23641)

Julius Marmingef2513bd28870
diff --git a/apps/web/src/components/settings/ProjectSettingsPanel.tsx b/apps/web/src/components/settings/ProjectSettingsPanel.tsx
index a67e914182e..379be839e2e 100644
--- a/apps/web/src/components/settings/ProjectSettingsPanel.tsx
+++ b/apps/web/src/components/settings/ProjectSettingsPanel.tsx
@@ -5,7 +5,7 @@ import { useEnvironmentsWithScope } from "../../state/session";
import { mapAtomCommandResult, settlePromise } from "@t3tools/client-runtime/state/runtime";
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
import { planProjectRemoval } from "@t3tools/client-runtime/state/projects";
-import { type EnvironmentId } from "@t3tools/contracts";
+import { type EnvironmentId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
import {
checkProjectAccess,
reportProjectFailure,
@@ -18,8 +18,12 @@ import { lazy, Suspense, useRef, useState, type ReactNode } from "react";
import { useComposerDraftStore } from "../../composerDraftStore";
import { releaseProjectDraftUploads } from "../../lib/composerDraftUploads";
+import { persistClientSettingsUpdate, useClientSettings } from "../../hooks/useSettings";
import { readLocalApi } from "../../localApi";
+import { derivePhysicalProjectKey } from "@t3tools/client-runtime/state/project-grouping";
import {
+ PROJECT_GROUPING_MODE_LABELS,
+ withProjectGroupingOverride,
type SidebarProjectGroupMember,
type SidebarProjectSnapshot,
} from "../../sidebarProjectGrouping";
@@ -30,8 +34,9 @@ import { ProjectFavicon } from "../ProjectFavicon";
import { Alert, AlertDescription } from "../ui/alert";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
+import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
import { Switch } from "../ui/switch";
-import { toastManager } from "../ui/toast";
+import { stackedThreadToast, toastManager } from "../ui/toast";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import {
SettingResetButton,
@@ -61,6 +66,78 @@ const ProjectIconPickerDialog = lazy(() =>
})),
);
+function isGroupingSelection(value: unknown): value is SidebarProjectGroupingMode | "inherit" {
+ return (
+ value === "inherit" ||
+ value === "repository" ||
+ value === "repository_path" ||
+ value === "separate"
+ );
+}
+
+// Grouping overrides are client settings, so they stay editable without project write access.
+function CheckoutGroupingSelect({ member }: { member: SidebarProjectGroupMember }) {
+ const overrideKey = derivePhysicalProjectKey(member);
+ const globalMode = useClientSettings((settings) => settings.sidebarProjectGroupingMode);
+ const selection = useClientSettings(
+ (settings) => settings.sidebarProjectGroupingOverrides?.[overrideKey] ?? "inherit",
+ );
+ return (
+ <Select
+ value={selection}
+ onValueChange={(value) => {
+ if (!isGroupingSelection(value)) return;
+ persistClientSettingsUpdate((current) => ({
+ ...current,
+ sidebarProjectGroupingOverrides: withProjectGroupingOverride(
+ current.sidebarProjectGroupingOverrides,
+ overrideKey,
+ value,
+ ),
+ })).catch((error) => {
+ console.error("Failed to save checkout grouping rule", error);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title: "Could not save grouping rule",
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
+ });
+ }}
+ >
+ <SelectTrigger
+ size="sm"
+ aria-label={`Grouping rule for ${member.workspaceRoot} on ${
+ member.environmentLabel
+ ? `${member.environmentLabel} (${member.environmentId})`
+ : member.environmentId
+ }`}
+ >
+ <SelectValue>
+ {selection === "inherit"
+ ? `Default (${PROJECT_GROUPING_MODE_LABELS[globalMode]})`
+ : PROJECT_GROUPING_MODE_LABELS[selection]}
+ </SelectValue>
+ </SelectTrigger>
+ <SelectPopup align="end" alignItemWithTrigger={false}>
+ <SelectItem hideIndicator value="inherit">
+ Use global default
+ </SelectItem>
+ <SelectItem hideIndicator value="repository">
+ {PROJECT_GROUPING_MODE_LABELS.repository}
+ </SelectItem>
+ <SelectItem hideIndicator value="repository_path">
+ {PROJECT_GROUPING_MODE_LABELS.repository_path}
+ </SelectItem>
+ <SelectItem hideIndicator value="separate">
+ {PROJECT_GROUPING_MODE_LABELS.separate}
+ </SelectItem>
+ </SelectPopup>
+ </Select>
+ );
+}
+
export type ProjectSettingsCategory = "general" | "source-control" | "project";
interface ProjectSelection {
@@ -334,19 +411,32 @@ function ProjectDetail({
if (members) members.push(member);
else checkoutsByEnvironment.set(member.environmentId, [member]);
}
- const checkoutRemoveButton = (member: SidebarProjectGroupMember) => (
- <Button
- size="sm"
- variant="outline"
- disabled={!editableIds.has(member.environmentId)}
- onClick={() => void removeMembers([member])}
- aria-label={`Remove checkout ${member.workspaceRoot}`}
- >
- Remove
- </Button>
+ // A lone checkout is removed from the Danger section, but it still needs its grouping
+ // rule here so "Keep separate" can be undone.
+ const checkoutControls = (member: SidebarProjectGroupMember) => (
+ <div className="flex w-full min-w-0 items-center gap-2 @min-[32rem]/settings-row:w-auto">
+ <div className="min-w-0 flex-1 @min-[32rem]/settings-row:w-72 @min-[32rem]/settings-row:flex-none">
+ <CheckoutGroupingSelect member={member} />
+ </div>
+ {hasMultipleCheckouts ? (
+ <Button
+ size="sm"
+ variant="outline"
+ disabled={!editableIds.has(member.environmentId)}
+ onClick={() => void removeMembers([member])}
+ aria-label={`Remove checkout ${member.workspaceRoot}`}
+ >
+ Remove
+ </Button>
+ ) : null}
+ </div>
);
const checkoutChoices = (
<SettingsSection title="Checkouts">
+ <p className="px-3 py-2.5 text-xs text-muted-foreground sm:px-4">
+ Grouping rule decides how each checkout joins project groups in the sidebar. Changing it can
+ move a checkout to a different project.
+ </p>
{Array.from(checkoutsByEnvironment, ([environmentId, members]) => {
if (checkoutsByEnvironment.size === 1) {
return members.map((member) => (
@@ -354,7 +444,7 @@ function ProjectDetail({
key={member.physicalProjectKey}
title={member.environmentLabel ?? "Environment"}
description={member.workspaceRoot}
- control={checkoutRemoveButton(member)}
+ control={checkoutControls(member)}
/>
));
}
@@ -383,26 +473,26 @@ function ProjectDetail({
) : null}
</>
}
- control={hasMultiple ? undefined : checkoutRemoveButton(firstMember)}
+ control={hasMultiple ? undefined : checkoutControls(firstMember)}
>
{hasMultiple ? (
<div className="space-y-3 pb-2 pt-1">
{members.map((member) => (
<div
key={member.physicalProjectKey}
- className="flex min-w-0 items-center justify-between gap-3"
+ className="flex min-w-0 flex-col gap-1.5 @min-[36rem]/settings-row:flex-row @min-[36rem]/settings-row:items-center @min-[36rem]/settings-row:justify-between @min-[36rem]/settings-row:gap-3"
>
<Tooltip>
<TooltipTrigger
render={
- <span className="min-w-0 truncate text-xs text-muted-foreground/80" />
+ <span className="min-w-0 max-w-full truncate text-xs text-muted-foreground/80" />
}
>
{member.workspaceRoot}
</TooltipTrigger>
<TooltipPopup>{member.workspaceRoot}</TooltipPopup>
</Tooltip>
- <div className="shrink-0">{checkoutRemoveButton(member)}</div>
+ <div className="shrink-0">{checkoutControls(member)}</div>
</div>
))}
</div>
@@ -565,7 +655,7 @@ function ProjectDetail({
</SettingsSection>
<ProjectDefaultsSettings category="project" />
<ProjectActionsSettings />
- {hasMultipleCheckouts ? checkoutChoices : null}
+ {checkoutChoices}
<SettingsSection title="Danger">
<SettingsRow
title={
diff --git a/apps/web/src/sidebarProjectGrouping.ts b/apps/web/src/sidebarProjectGrouping.ts
index 4dbd65b0abc..5f9f7d981da 100644
--- a/apps/web/src/sidebarProjectGrouping.ts
+++ b/apps/web/src/sidebarProjectGrouping.ts
@@ -1,4 +1,8 @@
-import type { EnvironmentId, ScopedProjectRef } from "@t3tools/contracts";
+import type {
+ EnvironmentId,
+ ScopedProjectRef,
+ SidebarProjectGroupingMode,
+} from "@t3tools/contracts";
import {
buildProjectGroups,
derivePhysicalProjectKey,
@@ -7,6 +11,23 @@ import {
} from "@t3tools/client-runtime/state/project-grouping";
import type { Project } from "./types";
+export const PROJECT_GROUPING_MODE_LABELS: Record<SidebarProjectGroupingMode, string> = {
+ repository: "Group by repository",
+ repository_path: "Group by repository path",
+ separate: "Keep separate",
+};
+
+export function withProjectGroupingOverride(
+ overrides: Readonly<Record<string, SidebarProjectGroupingMode>> | undefined,
+ overrideKey: string,
+ selection: SidebarProjectGroupingMode | "inherit",
+): Record<string, SidebarProjectGroupingMode> {
+ const next = { ...overrides };
+ if (selection === "inherit") delete next[overrideKey];
+ else next[overrideKey] = selection;
+ return next;
+}
+
export type EnvironmentPresence = "local-only" | "remote-only" | "mixed";
export interface SidebarProjectGroupMember extends Project {