fix(web): keep the sidebar project filter across navigation (#9416)
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
4d3907f63d
commit
1963ca0abe
7 files changed
+252
-12
No files matched your search
@@ -111,7 +111,11 @@ import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
|
||||
import { useLocalStorage } from "../hooks/useLocalStorage";
|
||||
import { useNowMinute } from "../hooks/useNowMinute";
|
||||
import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments";
|
||||
import { useProjects, useThreadShells } from "../state/entities";
|
||||
import {
|
||||
useAllEnvironmentProjectSnapshotsReady,
|
||||
useProjects,
|
||||
useThreadShells,
|
||||
} from "../state/entities";
|
||||
import { environmentServerConfigsAtom, primaryServerKeybindingsAtom } from "../state/server";
|
||||
import { vcsEnvironment } from "../state/vcs";
|
||||
import { threadEnvironment } from "../state/threads";
|
||||
@@ -2098,7 +2102,11 @@ export default function Sidebar() {
|
||||
|
||||
// Project scope: one menu above the list. Scoping filters the list without
|
||||
// making the header width depend on the number or length of project names.
|
||||
const [projectScopeKey, setProjectScopeKey] = useState<string | null>(null);
|
||||
// The selection lives in the persisted UI store next to the other sidebar
|
||||
// project preferences, so routes that unmount the sidebar (Settings) and
|
||||
// app restarts keep it.
|
||||
const projectScopeKey = useUiStateStore((store) => store.sidebarProjectScopeKey);
|
||||
const setProjectScopeKey = useUiStateStore((store) => store.setSidebarProjectScopeKey);
|
||||
// {value, label} items let Base UI drive the combobox selection contract
|
||||
// while the popup search filters the same collection.
|
||||
const projectScopeItems = useMemo(
|
||||
@@ -2162,11 +2170,15 @@ export default function Sidebar() {
|
||||
),
|
||||
[scopedProjectGroup],
|
||||
);
|
||||
// A persisted scope whose project is gone falls back to all projects, but
|
||||
// only after every catalog environment has a live project snapshot. Cached
|
||||
// or disconnected environments cannot establish that the project is gone.
|
||||
const allProjectSnapshotsReady = useAllEnvironmentProjectSnapshotsReady();
|
||||
useEffect(() => {
|
||||
if (projectScopeKey !== null && scopedProjectGroup === null) {
|
||||
if (projectScopeKey !== null && allProjectSnapshotsReady && scopedProjectGroup === null) {
|
||||
setProjectScopeKey(null);
|
||||
}
|
||||
}, [projectScopeKey, scopedProjectGroup]);
|
||||
}, [allProjectSnapshotsReady, projectScopeKey, scopedProjectGroup, setProjectScopeKey]);
|
||||
// Count-only subscription: the parent needs "are there draft rows" for the
|
||||
// empty state, while SidebarDraftBlock owns the per-keystroke content
|
||||
// subscription. Selecting a number keeps typing in a draft composer from
|
||||
|
||||
@@ -40,8 +40,9 @@ export function isHostedStaticApp(url?: URL): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
// No window (tests, static render) means no origin to be hosted at.
|
||||
if (url === undefined && typeof window === "undefined") {
|
||||
// No window, or a window without a location (tests, static render), means
|
||||
// no origin to be hosted at.
|
||||
if (url === undefined && (typeof window === "undefined" || window.location === undefined)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,10 @@ import { useMemo } from "react";
|
||||
import { appAtomRegistry } from "../rpc/atomRegistry";
|
||||
import { environmentProjects } from "./projects";
|
||||
import { environmentServerConfigsAtom } from "./server";
|
||||
import { allEnvironmentShellsBootstrappedAtom } from "./shell";
|
||||
import {
|
||||
allEnvironmentProjectSnapshotsReadyAtom,
|
||||
allEnvironmentShellsBootstrappedAtom,
|
||||
} from "./shell";
|
||||
import { environmentThreadDetails, environmentThreadShells } from "./threads";
|
||||
|
||||
const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
|
||||
@@ -79,6 +82,10 @@ export function useAllEnvironmentShellsBootstrapped(): boolean {
|
||||
return useAtomValue(allEnvironmentShellsBootstrappedAtom);
|
||||
}
|
||||
|
||||
export function useAllEnvironmentProjectSnapshotsReady(): boolean {
|
||||
return useAtomValue(allEnvironmentProjectSnapshotsReadyAtom);
|
||||
}
|
||||
|
||||
export function useThreadShellsForProjectRefs(
|
||||
refs: ReadonlyArray<ScopedProjectRef>,
|
||||
): ReadonlyArray<EnvironmentThreadShell> {
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import {
|
||||
BearerConnectionTarget,
|
||||
PrimaryConnectionTarget,
|
||||
} from "@t3tools/client-runtime/connection";
|
||||
import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
|
||||
import type { EnvironmentShellState } from "@t3tools/client-runtime/state/shell";
|
||||
import { EnvironmentId } from "@t3tools/contracts";
|
||||
import * as Option from "effect/Option";
|
||||
import { Atom, AtomRegistry } from "effect/unstable/reactivity";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { createAllEnvironmentProjectSnapshotsReadyAtom } from "./shell";
|
||||
|
||||
const LOCAL = EnvironmentId.make("local");
|
||||
const REMOTE = EnvironmentId.make("remote");
|
||||
|
||||
function shellState(status: EnvironmentShellState["status"]): EnvironmentShellState {
|
||||
return {
|
||||
status,
|
||||
snapshot:
|
||||
status === "empty"
|
||||
? Option.none()
|
||||
: Option.some({
|
||||
snapshotSequence: 1,
|
||||
updatedAt: "2026-09-04T00:00:00.000Z",
|
||||
projects: [],
|
||||
threads: [],
|
||||
}),
|
||||
error: Option.none(),
|
||||
};
|
||||
}
|
||||
|
||||
function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCatalogState {
|
||||
return {
|
||||
isReady: true,
|
||||
entries: new Map(
|
||||
environmentIds.map((environmentId) => [
|
||||
environmentId,
|
||||
{
|
||||
target:
|
||||
environmentId === LOCAL
|
||||
? new PrimaryConnectionTarget({
|
||||
environmentId,
|
||||
label: environmentId,
|
||||
httpBaseUrl: `https://${environmentId}.example.test`,
|
||||
wsBaseUrl: `wss://${environmentId}.example.test`,
|
||||
})
|
||||
: new BearerConnectionTarget({
|
||||
environmentId,
|
||||
connectionId: environmentId,
|
||||
label: environmentId,
|
||||
}),
|
||||
profile: Option.none(),
|
||||
},
|
||||
]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function makeHarness(requiresPrimaryEnvironment = true) {
|
||||
const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() });
|
||||
const shells = Atom.family((_environmentId: EnvironmentId) =>
|
||||
Atom.make<EnvironmentShellState>(shellState("empty")),
|
||||
);
|
||||
const ready = createAllEnvironmentProjectSnapshotsReadyAtom({
|
||||
catalogValueAtom: catalog,
|
||||
shellStateValueAtom: shells,
|
||||
requiresPrimaryEnvironment,
|
||||
});
|
||||
const registry = AtomRegistry.make();
|
||||
return { catalog, shells, ready, registry };
|
||||
}
|
||||
|
||||
describe("project snapshot readiness", () => {
|
||||
it("does not clear a saved scope while the ready catalog is still empty", () => {
|
||||
const { catalog, ready, registry } = makeHarness();
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(catalog, catalogState([]));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("waits for primary discovery even if a persisted remote is already live", () => {
|
||||
const { catalog, shells, ready, registry } = makeHarness();
|
||||
registry.set(catalog, catalogState([REMOTE]));
|
||||
registry.set(shells(REMOTE), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
|
||||
registry.set(catalog, catalogState([LOCAL, REMOTE]));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(shells(LOCAL), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(true);
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("allows a hosted client to load projects without a primary environment", () => {
|
||||
const { catalog, shells, ready, registry } = makeHarness(false);
|
||||
registry.set(catalog, catalogState([REMOTE]));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(shells(REMOTE), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(true);
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("waits for live snapshots through offline startup and reconnect", () => {
|
||||
const { catalog, shells, ready, registry } = makeHarness();
|
||||
registry.set(catalog, catalogState([LOCAL, REMOTE]));
|
||||
registry.set(shells(LOCAL), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
|
||||
// An old cache and a reconnect in progress can both omit a real project.
|
||||
registry.set(shells(REMOTE), shellState("cached"));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(shells(REMOTE), shellState("synchronizing"));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(shells(REMOTE), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(true);
|
||||
|
||||
registry.set(shells(REMOTE), shellState("cached"));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(shells(REMOTE), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(true);
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("stops waiting for an environment only when it leaves the catalog", () => {
|
||||
const { catalog, shells, ready, registry } = makeHarness();
|
||||
registry.set(catalog, catalogState([LOCAL, REMOTE]));
|
||||
registry.set(shells(LOCAL), shellState("live"));
|
||||
expect(registry.get(ready)).toBe(false);
|
||||
registry.set(catalog, catalogState([LOCAL]));
|
||||
expect(registry.get(ready)).toBe(true);
|
||||
registry.dispose();
|
||||
});
|
||||
});
|
||||
@@ -7,12 +7,16 @@ import {
|
||||
createEnvironmentShellSummaryAtom,
|
||||
createEnvironmentSnapshotAtom,
|
||||
createShellEnvironmentAtoms,
|
||||
type EnvironmentShellState,
|
||||
} from "@t3tools/client-runtime/state/shell";
|
||||
import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import * as Option from "effect/Option";
|
||||
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import { environmentCatalog } from "../connection/catalog";
|
||||
import { connectionAtomRuntime } from "../connection/runtime";
|
||||
import { isHostedStaticApp } from "../hostedPairing";
|
||||
|
||||
export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
|
||||
export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
|
||||
@@ -46,3 +50,37 @@ export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
|
||||
}
|
||||
return true;
|
||||
}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
|
||||
|
||||
/** Cached or missing snapshots cannot establish that a saved project no longer exists. */
|
||||
export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
|
||||
readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
|
||||
readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
|
||||
readonly requiresPrimaryEnvironment: boolean;
|
||||
}) {
|
||||
return Atom.make((get) => {
|
||||
const catalog = get(input.catalogValueAtom);
|
||||
// The persisted catalog can emit before platform discovery registers the
|
||||
// primary environment. Neither that gap nor an empty catalog proves absence.
|
||||
if (!catalog.isReady || catalog.entries.size === 0) return false;
|
||||
if (
|
||||
input.requiresPrimaryEnvironment &&
|
||||
!Array.from(catalog.entries.values()).some(
|
||||
(entry) => entry.target._tag === "PrimaryConnectionTarget",
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
for (const environmentId of catalog.entries.keys()) {
|
||||
const shell = get(input.shellStateValueAtom(environmentId));
|
||||
if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
|
||||
}
|
||||
return true;
|
||||
}).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
|
||||
}
|
||||
|
||||
export const allEnvironmentProjectSnapshotsReadyAtom =
|
||||
createAllEnvironmentProjectSnapshotsReadyAtom({
|
||||
catalogValueAtom: environmentCatalog.catalogValueAtom,
|
||||
shellStateValueAtom: environmentShell.stateValueAtom,
|
||||
requiresPrimaryEnvironment: !isHostedStaticApp(),
|
||||
});
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
resolveProjectExpanded,
|
||||
setDefaultAdvertisedEndpointKey,
|
||||
setProjectExpanded,
|
||||
setSidebarProjectScopeKey,
|
||||
setThreadChangedFilesExpanded,
|
||||
type UiState,
|
||||
} from "./uiStateStore";
|
||||
@@ -21,6 +22,7 @@ function makeUiState(overrides: Partial<UiState> = {}): UiState {
|
||||
return {
|
||||
projectExpandedById: {},
|
||||
projectOrder: [],
|
||||
sidebarProjectScopeKey: null,
|
||||
threadLastVisitedAtById: {},
|
||||
threadChangedFilesExpandedById: {},
|
||||
defaultAdvertisedEndpointKey: null,
|
||||
@@ -144,6 +146,15 @@ describe("uiStateStore pure functions", () => {
|
||||
defaultAdvertisedEndpointKey: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("stores the sidebar project scope and resets it to all projects", () => {
|
||||
const scoped = setSidebarProjectScopeKey(makeUiState(), "github.com/pingdotgg/t3code");
|
||||
|
||||
expect(scoped.sidebarProjectScopeKey).toBe("github.com/pingdotgg/t3code");
|
||||
expect(setSidebarProjectScopeKey(scoped, "github.com/pingdotgg/t3code")).toBe(scoped);
|
||||
expect(setSidebarProjectScopeKey(scoped, null).sidebarProjectScopeKey).toBeNull();
|
||||
expect(setSidebarProjectScopeKey(scoped, "").sidebarProjectScopeKey).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("parsePersistedState", () => {
|
||||
@@ -177,6 +188,7 @@ describe("parsePersistedState", () => {
|
||||
"environment:thread-1": "2026-02-25T12:35:00.000Z",
|
||||
},
|
||||
defaultAdvertisedEndpointKey: "desktop-core:lan:http",
|
||||
sidebarProjectScopeKey: null,
|
||||
threadChangedFilesExpandedById: {
|
||||
"environment:thread-1": {
|
||||
"turn-1": false,
|
||||
@@ -297,6 +309,7 @@ describe("uiStateStore persistence", () => {
|
||||
"environment:thread-1": "2026-02-25T12:35:00.000Z",
|
||||
},
|
||||
defaultAdvertisedEndpointKey: "desktop-core:lan:http",
|
||||
sidebarProjectScopeKey: null,
|
||||
threadChangedFilesExpansionVersion: 2,
|
||||
threadChangedFilesExpandedById: {
|
||||
"environment:thread-1": {
|
||||
@@ -310,6 +323,18 @@ describe("uiStateStore persistence", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("restores the sidebar project scope across reloads", () => {
|
||||
persistState(makeUiState({ sidebarProjectScopeKey: "github.com/pingdotgg/t3code" }));
|
||||
|
||||
const persisted = JSON.parse(
|
||||
localStorageStub.getItem(PERSISTED_STATE_KEY) ?? "{}",
|
||||
) as PersistedUiState;
|
||||
|
||||
expect(parsePersistedState(persisted).sidebarProjectScopeKey).toBe(
|
||||
"github.com/pingdotgg/t3code",
|
||||
);
|
||||
});
|
||||
|
||||
it("drops the temporary expanded-only migration fallback when rewriting state", () => {
|
||||
const migrated = parsePersistedState({
|
||||
expandedProjectCwds: ["/repo/a"],
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface PersistedUiState {
|
||||
expandedProjectCwds?: string[];
|
||||
projectOrderCwds?: string[];
|
||||
defaultAdvertisedEndpointKey?: string | null;
|
||||
sidebarProjectScopeKey?: string | null;
|
||||
threadChangedFilesExpansionVersion?: number;
|
||||
threadChangedFilesExpandedById?: Record<string, Record<string, boolean>>;
|
||||
}
|
||||
@@ -33,6 +34,10 @@ export interface PersistedUiState {
|
||||
export interface UiProjectState {
|
||||
projectExpandedById: Record<string, boolean>;
|
||||
projectOrder: string[];
|
||||
// Logical project key the sidebar list is scoped to, or null for "all
|
||||
// projects". Lives here so routes that unmount the sidebar (Settings)
|
||||
// cannot reset the filter.
|
||||
sidebarProjectScopeKey: string | null;
|
||||
}
|
||||
|
||||
export interface UiThreadState {
|
||||
@@ -49,6 +54,7 @@ export interface UiState extends UiProjectState, UiThreadState, UiEndpointState
|
||||
const initialState: UiState = {
|
||||
projectExpandedById: {},
|
||||
projectOrder: [],
|
||||
sidebarProjectScopeKey: null,
|
||||
threadLastVisitedAtById: {},
|
||||
threadChangedFilesExpandedById: {},
|
||||
defaultAdvertisedEndpointKey: null,
|
||||
@@ -84,6 +90,10 @@ function sanitizeBooleanRecord(value: unknown): Record<string, boolean> {
|
||||
);
|
||||
}
|
||||
|
||||
function sanitizeOptionalKey(value: unknown): string | null {
|
||||
return typeof value === "string" && value.length > 0 ? value : null;
|
||||
}
|
||||
|
||||
function sanitizeTimestampRecord(value: unknown): Record<string, string> {
|
||||
if (!value || typeof value !== "object") {
|
||||
return {};
|
||||
@@ -131,11 +141,8 @@ export function parsePersistedState(parsed: PersistedUiState): UiState {
|
||||
parsed.threadChangedFilesExpansionVersion === THREAD_CHANGED_FILES_EXPANSION_VERSION
|
||||
? sanitizePersistedThreadChangedFilesExpanded(parsed.threadChangedFilesExpandedById)
|
||||
: {},
|
||||
defaultAdvertisedEndpointKey:
|
||||
typeof parsed.defaultAdvertisedEndpointKey === "string" &&
|
||||
parsed.defaultAdvertisedEndpointKey.length > 0
|
||||
? parsed.defaultAdvertisedEndpointKey
|
||||
: null,
|
||||
defaultAdvertisedEndpointKey: sanitizeOptionalKey(parsed.defaultAdvertisedEndpointKey),
|
||||
sidebarProjectScopeKey: sanitizeOptionalKey(parsed.sidebarProjectScopeKey),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -206,6 +213,7 @@ export function persistState(state: UiState): void {
|
||||
projectOrder: state.projectOrder,
|
||||
threadLastVisitedAtById: state.threadLastVisitedAtById,
|
||||
defaultAdvertisedEndpointKey: state.defaultAdvertisedEndpointKey,
|
||||
sidebarProjectScopeKey: state.sidebarProjectScopeKey,
|
||||
threadChangedFilesExpansionVersion: THREAD_CHANGED_FILES_EXPANSION_VERSION,
|
||||
threadChangedFilesExpandedById: state.threadChangedFilesExpandedById,
|
||||
} satisfies PersistedUiState),
|
||||
@@ -305,6 +313,17 @@ export function setDefaultAdvertisedEndpointKey(state: UiState, key: string | nu
|
||||
};
|
||||
}
|
||||
|
||||
export function setSidebarProjectScopeKey(state: UiState, projectKey: string | null): UiState {
|
||||
const nextKey = sanitizeOptionalKey(projectKey);
|
||||
if (state.sidebarProjectScopeKey === nextKey) {
|
||||
return state;
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
sidebarProjectScopeKey: nextKey,
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveProjectExpanded(
|
||||
projectExpandedById: Readonly<Record<string, boolean>>,
|
||||
preferenceKeys: readonly string[],
|
||||
@@ -387,6 +406,7 @@ interface UiStateStore extends UiState {
|
||||
markThreadUnread: (threadId: string, latestTurnCompletedAt: string | null | undefined) => void;
|
||||
setThreadChangedFilesExpanded: (threadId: string, turnId: string, expanded: boolean) => void;
|
||||
setDefaultAdvertisedEndpointKey: (key: string | null) => void;
|
||||
setSidebarProjectScopeKey: (projectKey: string | null) => void;
|
||||
setProjectExpanded: (projectIds: string | readonly string[], expanded: boolean) => void;
|
||||
reorderProjects: (
|
||||
currentProjectOrder: readonly string[],
|
||||
@@ -405,6 +425,8 @@ export const useUiStateStore = create<UiStateStore>((set) => ({
|
||||
set((state) => setThreadChangedFilesExpanded(state, threadId, turnId, expanded)),
|
||||
setDefaultAdvertisedEndpointKey: (key) =>
|
||||
set((state) => setDefaultAdvertisedEndpointKey(state, key)),
|
||||
setSidebarProjectScopeKey: (projectKey) =>
|
||||
set((state) => setSidebarProjectScopeKey(state, projectKey)),
|
||||
setProjectExpanded: (projectIds, expanded) =>
|
||||
set((state) => setProjectExpanded(state, projectIds, expanded)),
|
||||
reorderProjects: (currentProjectOrder, draggedProjectIds, targetProjectIds) =>
|
||||
|
||||
Reference in new issue
Block a user