Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,42 @@ describe("orderItemsByPreferredIds", () => {
ProjectId.make("project-1"),
]);
});

it("honors projectOrder physical keys via getProjectOrderKey", async () => {
// Regression guard for #1904 / the regression introduced by #2055:
// `projectOrder` is populated with physical keys (envId + cwd-derived)
// by the store and by drag-end handlers. Readers must identify projects
// with the same key format, or manual sort silently snaps back.
const { getProjectOrderKey } = await import("../logicalProject");
const projects = [
{
environmentId: EnvironmentId.make("environment-local"),
id: ProjectId.make("id-alpha"),
cwd: "/work/alpha",
},
{
environmentId: EnvironmentId.make("environment-local"),
id: ProjectId.make("id-beta"),
cwd: "/work/beta",
},
{
environmentId: EnvironmentId.make("environment-local"),
id: ProjectId.make("id-gamma"),
cwd: "/work/gamma",
},
];
const ordered = orderItemsByPreferredIds({
items: projects,
preferredIds: [getProjectOrderKey(projects[2]!), getProjectOrderKey(projects[0]!)],
getId: getProjectOrderKey,
});

expect(ordered.map((project) => project.cwd)).toEqual([
"/work/gamma",
"/work/alpha",
"/work/beta",
]);
});
});

describe("resolveAdjacentThreadId", () => {
Expand Down
8 changes: 6 additions & 2 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ import { CommandDialogTrigger } from "./ui/command";
import { readEnvironmentApi } from "../environmentApi";
import { useSettings, useUpdateSettings } from "~/hooks/useSettings";
import { useServerKeybindings } from "../rpc/serverState";
import { derivePhysicalProjectKey, deriveProjectGroupingOverrideKey } from "../logicalProject";
import {
derivePhysicalProjectKey,
deriveProjectGroupingOverrideKey,
getProjectOrderKey,
} from "../logicalProject";
import {
useSavedEnvironmentRegistryStore,
useSavedEnvironmentRuntimeStore,
Expand Down Expand Up @@ -2708,7 +2712,7 @@ export default function Sidebar() {
return orderItemsByPreferredIds({
items: projects,
preferredIds: projectOrder,
getId: (project) => scopedProjectKey(scopeProjectRef(project.environmentId, project.id)),
getId: getProjectOrderKey,
});
}, [projectOrder, projects]);

Expand Down
10 changes: 9 additions & 1 deletion apps/web/src/environments/runtime/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ import { useTerminalStateStore } from "~/terminalStateStore";
import { useUiStateStore } from "~/uiStateStore";
import { WsTransport } from "../../rpc/wsTransport";
import { createWsRpcClient, type WsRpcClient } from "../../rpc/wsRpcClient";
import { derivePhysicalProjectKey } from "../../logicalProject";
import {
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
} from "../../logicalProject";
import { getClientSettings } from "~/hooks/useSettings";

type EnvironmentServiceState = {
readonly queryClient: QueryClient;
Expand Down Expand Up @@ -468,9 +472,11 @@ function coalesceOrchestrationUiEvents(

function syncProjectUiFromStore() {
const projects = selectProjectsAcrossEnvironments(useStore.getState());
const clientSettings = getClientSettings();
useUiStateStore.getState().syncProjects(
projects.map((project) => ({
key: derivePhysicalProjectKey(project),
logicalKey: deriveLogicalProjectKeyFromSettings(project, clientSettings),
cwd: project.cwd,
})),
);
Expand Down Expand Up @@ -541,9 +547,11 @@ function applyRecoveredEventBatch(
useStore.getState().applyOrchestrationEvents(uiEvents, environmentId);
if (needsProjectUiSync) {
const projects = selectProjectsAcrossEnvironments(useStore.getState());
const clientSettings = getClientSettings();
useUiStateStore.getState().syncProjects(
projects.map((project) => ({
key: derivePhysicalProjectKey(project),
logicalKey: deriveLogicalProjectKeyFromSettings(project, clientSettings),
cwd: project.cwd,
})),
);
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/hooks/useHandleNewThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../composerDraftStore";
import { newDraftId, newThreadId } from "../lib/utils";
import { orderItemsByPreferredIds } from "../components/Sidebar.logic";
import { deriveLogicalProjectKeyFromSettings } from "../logicalProject";
import { deriveLogicalProjectKeyFromSettings, getProjectOrderKey } from "../logicalProject";
import { selectProjectsAcrossEnvironments, useStore } from "../store";
import { createThreadSelectorByRef } from "../storeSelectors";
import { resolveThreadRouteTarget } from "../threadRoutes";
Expand Down Expand Up @@ -169,7 +169,7 @@ export function useHandleNewThread() {
return orderItemsByPreferredIds({
items: projects,
preferredIds: projectOrder,
getId: (project) => scopedProjectKey(scopeProjectRef(project.environmentId, project.id)),
getId: getProjectOrderKey,
});
}, [projectOrder, projects]);
const handleNewThread = useNewThreadState();
Expand Down
9 changes: 9 additions & 0 deletions apps/web/src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ function splitPatch(patch: Partial<UnifiedSettings>): {
* only re-render when the slice they care about changes.
*/

/**
* Non-hook accessor for the current merged client settings snapshot.
* Used by non-React code paths (e.g. runtime services) that need the latest
* settings without subscribing.
*/
export function getClientSettings(): ClientSettings {
return getClientSettingsSnapshot();
}

export function useSettings<T = UnifiedSettings>(selector?: (s: UnifiedSettings) => T): T {
const serverSettings = useServerSettings();
const clientSettings = useSyncExternalStore(
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/logicalProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export function deriveProjectGroupingOverrideKey(
return derivePhysicalProjectKey(project);
}

// Key under which a project's manual sort order (projectOrder) is stored.
// Must stay aligned with the writer side in `uiStateStore.syncProjects` and
// the drag handlers in `Sidebar` so readers and writers agree.
export function getProjectOrderKey(project: Pick<Project, "environmentId" | "cwd">): string {
return derivePhysicalProjectKey(project);
}

export function resolveProjectGroupingMode(
project: Pick<Project, "environmentId" | "cwd">,
settings: ProjectGroupingSettings,
Expand Down
Loading
Loading