Skip to content

Commit ceafdfd

Browse files
committed
fix(web): use the last project for new-thread shortcuts
1 parent 11c50ca commit ceafdfd

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎apps/web/src/hooks/useHandleNewThread.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { environmentServerConfigsAtom } from "../state/server";
4242
import { resolveThreadRouteTarget } from "../threadRoutes";
4343
import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
4444
import { getClientSettings } from "./useSettings";
45-
import { useLocalStorage } from "./useLocalStorage";
45+
import { getLocalStorageItem, useLocalStorage } from "./useLocalStorage";
4646
import * as Schema from "effect/Schema";
4747
import { useThreadRouteTarget } from "./useThreadRouteTarget";
4848
import { readUnloadedThreadCount } from "../state/olderSettledShelf";
@@ -52,6 +52,14 @@ import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";
5252
export const LAST_USED_PROJECT_STORAGE_KEY = "t3code:last-used-project:v1";
5353
export const lastUsedProjectKeySchema = Schema.NullOr(Schema.String);
5454

55+
export function readLastUsedProjectKey(): string | null {
56+
try {
57+
return getLocalStorageItem(LAST_USED_PROJECT_STORAGE_KEY, lastUsedProjectKeySchema);
58+
} catch {
59+
return null;
60+
}
61+
}
62+
5563
interface NewThreadWorkspaceOptions {
5664
branch?: string | null;
5765
worktreePath?: string | null;
@@ -431,7 +439,7 @@ export function createNewThreadHandler(router: AnyRouter) {
431439
export function resolveDefaultProjectRef(
432440
projects: ReadonlyArray<EnvironmentProject>,
433441
projectOrder: readonly string[],
434-
lastUsedProjectKey: string | null = null,
442+
lastUsedProjectKey: string | null,
435443
): ScopedProjectRef | null {
436444
const lastUsed = projects.find(
437445
(project) =>

‎apps/web/src/shortcuts/chatRouteCommands.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
44

55
import * as commandPalette from "../commandPaletteBus";
66
import * as settings from "../hooks/useSettings";
7+
import * as storage from "../hooks/useLocalStorage";
78
import * as threadActions from "../lib/chatThreadActions";
89
import * as entities from "../state/entities";
910
import type { Project } from "../types";
@@ -38,6 +39,7 @@ describe("new thread commands", () => {
3839
using _projects = vi
3940
.spyOn(entities, "readProjects")
4041
.mockReturnValue([project("active"), project("idle")].slice(0, scenario.count));
42+
using storedProject = vi.spyOn(storage, "getLocalStorageItem").mockReturnValue("local:active");
4143
using openPicker = vi.spyOn(commandPalette, "openCommandPalette").mockImplementation(() => {});
4244
using startLocal = vi.spyOn(threadActions, "startNewThreadFromContext").mockResolvedValue(true);
4345
using register = vi.spyOn(dispatcher, "registerCommands").mockReturnValue(() => {});
@@ -50,6 +52,7 @@ describe("new thread commands", () => {
5052
stopPropagation: vi.fn(),
5153
} as unknown as KeyboardEvent;
5254

55+
storedProject.mockReturnValue("local:idle");
5356
commands[scenario.command]!(event);
5457

5558
if (scenario.picker) {
@@ -58,6 +61,10 @@ describe("new thread commands", () => {
5861
} else {
5962
expect(openPicker).not.toHaveBeenCalled();
6063
expect(startLocal).toHaveBeenCalledOnce();
64+
expect(startLocal.mock.calls[0]![0].defaultProjectRef).toEqual({
65+
environmentId: "local",
66+
projectId: scenario.count === 1 ? "active" : "idle",
67+
});
6168
}
6269
});
6370
});

‎apps/web/src/shortcuts/chatRouteCommands.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { useComposerDraftStore } from "../composerDraftStore";
88
import { dispatchPreviewAction, type PreviewAction } from "../components/preview/previewActionBus";
99
import { isAppModalPage } from "../components/sidebar/mainAppLocation";
1010
import { shouldCreateNewThreadInCurrentProject } from "../components/Sidebar.logic";
11-
import { createNewThreadHandler, resolveDefaultProjectRef } from "../hooks/useHandleNewThread";
11+
import {
12+
createNewThreadHandler,
13+
readLastUsedProjectKey,
14+
resolveDefaultProjectRef,
15+
} from "../hooks/useHandleNewThread";
1216
import { createScratchProjectActions } from "../hooks/useScratchProject";
1317
import { getClientSettings } from "../hooks/useSettings";
1418
import { startNewThreadFromContext } from "../lib/chatThreadActions";
@@ -73,6 +77,7 @@ export function installChatRouteCommands(router: AnyRouter): () => void {
7377
defaultProjectRef: resolveDefaultProjectRef(
7478
readProjects(),
7579
useUiStateStore.getState().projectOrder,
80+
readLastUsedProjectKey(),
7681
),
7782
handleNewThread,
7883
};

0 commit comments

Comments
 (0)