Skip to content

Commit 7f6d079

Browse files
committed
fix(web): custom shortcuts work when Local environment is off
Shortcut handlers and hints read the primary environment's keybindings only. With Local environment off, or in the hosted app, there is no primary, so custom rules were ignored while Settings still listed them as Custom. Without a primary, shortcuts now use the first connected environment that has a config, the same rule unscoped Settings uses to choose what it shows and edits; both call one shared predicate. A primary whose config is still loading keeps the defaults. The custom rules are selected before they are merged with the defaults, so unrelated config updates keep the same bindings array.
1 parent fe417df commit 7f6d079

4 files changed

Lines changed: 229 additions & 17 deletions

File tree

‎apps/web/src/components/settings/scopedSettings.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
4848
return typeof value === "object" && value !== null && !Array.isArray(value);
4949
}
5050

51+
/** Settings reads and writes these; without a primary, shortcuts follow the first one. */
52+
export function isConnectedWithConfig(
53+
environment: Pick<ScopedSettingsEnvironment, "connection" | "serverConfig">,
54+
) {
55+
return environment.connection.phase === "connected" && environment.serverConfig !== null;
56+
}
57+
5158
/** The representative supplies display values, never the set of write targets. */
5259
export function selectScopedSettingsEnvironments<T extends ScopedSettingsEnvironment>(
5360
scope: ResolvedSettingsScope,
@@ -58,10 +65,7 @@ export function selectScopedSettingsEnvironments<T extends ScopedSettingsEnviron
5865
const environments = available.filter((environment) =>
5966
selectedIds.has(environment.environmentId),
6067
);
61-
const connectedEnvironments = environments.filter(
62-
(environment) =>
63-
environment.connection.phase === "connected" && environment.serverConfig !== null,
64-
);
68+
const connectedEnvironments = environments.filter(isConnectedWithConfig);
6569
const environment =
6670
connectedEnvironments.find((candidate) => candidate.environmentId === primaryEnvironmentId) ??
6771
connectedEnvironments[0] ??

‎apps/web/src/state/presentation.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import type { EnvironmentPresentation } from "@t3tools/client-runtime/connection";
3-
import {
4-
createEnvironmentPresentationAtoms,
5-
createEnvironmentSummaryAtoms,
6-
} from "@t3tools/client-runtime/state/presentation";
3+
import { createEnvironmentSummaryAtoms } from "@t3tools/client-runtime/state/presentation";
74
import type { EnvironmentId } from "@t3tools/contracts";
85
import { Atom } from "effect/unstable/reactivity";
96

107
import { environmentCatalog } from "../connection/catalog";
11-
import { serverEnvironment } from "./server";
8+
import { environmentPresentations } from "./server";
129

13-
export const environmentPresentations = createEnvironmentPresentationAtoms({
14-
catalogValueAtom: environmentCatalog.catalogValueAtom,
15-
stateAtom: environmentCatalog.stateAtom,
16-
serverConfigValueAtom: serverEnvironment.configValueAtom,
17-
});
10+
export { environmentPresentations };
1811

1912
export const environmentSummaries = createEnvironmentSummaryAtoms({
2013
catalogValueAtom: environmentCatalog.catalogValueAtom,

‎apps/web/src/state/server.test.ts‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2+
import { EnvironmentId, type KeybindingRule, type ServerConfig } from "@t3tools/contracts";
3+
import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
4+
import { Atom, AtomRegistry } from "effect/unstable/reactivity";
5+
import { afterEach, describe, expect, it } from "vite-plus/test";
6+
7+
import { resolveShortcutCommand } from "../keybindings";
8+
import { createShortcutKeybindingsAtom } from "./server";
9+
10+
const PRIMARY = EnvironmentId.make("primary");
11+
const A = EnvironmentId.make("a");
12+
const B = EnvironmentId.make("b");
13+
14+
// The reporter's swap: Cmd+N opens a local thread instead of a regular one.
15+
const SWAPPED: KeybindingRule[] = [{ key: "mod+n", command: "chat.newLocal" }];
16+
const STALE: KeybindingRule[] = [{ key: "mod+n", command: "chat.newWithoutProject" }];
17+
18+
function config(rules: readonly KeybindingRule[]) {
19+
return {
20+
keybindings: compileResolvedKeybindingsConfig(rules),
21+
providers: [],
22+
} as unknown as ServerConfig;
23+
}
24+
25+
function environment(phase: EnvironmentConnectionPhase, serverConfig: ServerConfig | null) {
26+
return { connection: { phase, error: null, traceId: null }, serverConfig };
27+
}
28+
29+
type Environment = ReturnType<typeof environment>;
30+
31+
function makeHarness(input: {
32+
primaryEnvironmentId?: EnvironmentId | null;
33+
primaryConfig?: ServerConfig | null;
34+
environments?: ReadonlyArray<readonly [EnvironmentId, Environment]>;
35+
}) {
36+
const primaryEnvironmentIdAtom = Atom.make(input.primaryEnvironmentId ?? null);
37+
const primaryConfigAtom = Atom.make(input.primaryConfig ?? null);
38+
const environmentsAtom = Atom.make<ReadonlyMap<EnvironmentId, Environment>>(
39+
new Map(input.environments ?? []),
40+
);
41+
const keybindingsAtom = createShortcutKeybindingsAtom({
42+
primaryEnvironmentIdAtom,
43+
primaryConfigAtom,
44+
environmentsAtom,
45+
});
46+
const registry = AtomRegistry.make();
47+
registries.push(registry);
48+
// Keep the atom mounted, like the shortcut handlers do.
49+
registry.subscribe(keybindingsAtom, () => {});
50+
return {
51+
registry,
52+
primaryEnvironmentIdAtom,
53+
primaryConfigAtom,
54+
environmentsAtom,
55+
keybindings: () => registry.get(keybindingsAtom),
56+
/** The command Cmd+N runs on macOS. */
57+
cmdN: () =>
58+
resolveShortcutCommand(
59+
{ key: "n", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false },
60+
registry.get(keybindingsAtom),
61+
{ platform: "MacIntel" },
62+
),
63+
};
64+
}
65+
66+
const registries: AtomRegistry.AtomRegistry[] = [];
67+
afterEach(() => {
68+
for (const registry of registries.splice(0)) registry.dispose();
69+
});
70+
71+
describe("createShortcutKeybindingsAtom", () => {
72+
it("follows the primary environment when it has a config", () => {
73+
const harness = makeHarness({ primaryEnvironmentId: PRIMARY, primaryConfig: config(SWAPPED) });
74+
expect(harness.cmdN()).toBe("chat.newLocal");
75+
});
76+
77+
it("keeps the defaults while the primary config is loading", () => {
78+
const harness = makeHarness({
79+
primaryEnvironmentId: PRIMARY,
80+
environments: [[B, environment("connected", config(SWAPPED))]],
81+
});
82+
expect(harness.cmdN()).toBe("chat.new");
83+
});
84+
85+
it("follows the connected environment without a primary", () => {
86+
const harness = makeHarness({ environments: [[B, environment("connected", config(SWAPPED))]] });
87+
expect(harness.cmdN()).toBe("chat.newLocal");
88+
});
89+
90+
it("skips an offline environment's cached config, as Settings does", () => {
91+
const harness = makeHarness({
92+
environments: [
93+
[A, environment("offline", config(STALE))],
94+
[B, environment("connected", config(SWAPPED))],
95+
],
96+
});
97+
expect(harness.cmdN()).toBe("chat.newLocal");
98+
});
99+
100+
it("keeps the defaults when no environment is connected", () => {
101+
const harness = makeHarness({
102+
environments: [[A, environment("offline", config(STALE))]],
103+
});
104+
expect(harness.cmdN()).toBe("chat.new");
105+
});
106+
107+
it("follows edits to the connected environment's keybindings", () => {
108+
const harness = makeHarness({ environments: [[B, environment("connected", config([]))]] });
109+
expect(harness.cmdN()).toBe("chat.new");
110+
111+
harness.registry.set(
112+
harness.environmentsAtom,
113+
new Map([[B, environment("connected", config(SWAPPED))]]),
114+
);
115+
expect(harness.cmdN()).toBe("chat.newLocal");
116+
});
117+
118+
it("keeps the same bindings when an unrelated config update arrives", () => {
119+
const remote = config(SWAPPED);
120+
const harness = makeHarness({
121+
environments: [
122+
[A, environment("offline", config([]))],
123+
[B, environment("connected", remote)],
124+
],
125+
});
126+
const before = harness.keybindings();
127+
128+
// A provider status update on B and a reconnect attempt on A.
129+
harness.registry.set(
130+
harness.environmentsAtom,
131+
new Map([
132+
[A, environment("reconnecting", config([]))],
133+
[B, environment("connected", { ...remote, providers: [] })],
134+
]),
135+
);
136+
expect(harness.keybindings()).toBe(before);
137+
});
138+
139+
it("follows connection and primary changes without a reload", () => {
140+
const aConfig = config(STALE);
141+
const bConfig = config(SWAPPED);
142+
const harness = makeHarness({
143+
environments: [
144+
[A, environment("connected", aConfig)],
145+
[B, environment("connected", bConfig)],
146+
],
147+
});
148+
const setEnvironments = (a: EnvironmentConnectionPhase, b: EnvironmentConnectionPhase) =>
149+
harness.registry.set(
150+
harness.environmentsAtom,
151+
new Map([
152+
[A, environment(a, aConfig)],
153+
[B, environment(b, bConfig)],
154+
]),
155+
);
156+
157+
// Both connected: the first in catalog order wins.
158+
expect(harness.cmdN()).toBe("chat.newWithoutProject");
159+
160+
setEnvironments("offline", "connected");
161+
expect(harness.cmdN()).toBe("chat.newLocal");
162+
163+
setEnvironments("offline", "offline");
164+
expect(harness.cmdN()).toBe("chat.new");
165+
166+
// A primary appears while its config is loading; B is back but must not leak in.
167+
setEnvironments("offline", "connected");
168+
harness.registry.set(harness.primaryEnvironmentIdAtom, PRIMARY);
169+
expect(harness.cmdN()).toBe("chat.new");
170+
171+
harness.registry.set(
172+
harness.primaryConfigAtom,
173+
config([{ key: "mod+n", command: "sidebar.toggle" }]),
174+
);
175+
expect(harness.cmdN()).toBe("sidebar.toggle");
176+
});
177+
});

‎apps/web/src/state/server.ts‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
DEFAULT_SERVER_SETTINGS,
33
type EditorId,
4+
type EnvironmentId,
45
type EnvironmentTheme,
56
type ServerConfig,
67
type ServerConfigStreamEvent,
@@ -9,13 +10,16 @@ import {
910
type ServerProvider,
1011
type ServerSettings,
1112
} from "@t3tools/contracts";
13+
import type { EnvironmentPresentation } from "@t3tools/client-runtime/connection";
14+
import { createEnvironmentPresentationAtoms } from "@t3tools/client-runtime/state/presentation";
1215
import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
1316
import { createOutdatedServerUpdateCommand } from "@t3tools/client-runtime/state/outdatedServerUpdate";
1417
import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
1518
import { mergeWithDefaultKeybindings } from "@t3tools/shared/keybindings";
1619
import * as Option from "effect/Option";
1720
import { AsyncResult, Atom } from "effect/unstable/reactivity";
1821

22+
import { isConnectedWithConfig } from "../components/settings/scopedSettings";
1923
import { environmentCatalog } from "../connection/catalog";
2024
import { connectionAtomRuntime } from "../connection/runtime";
2125
import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
@@ -39,6 +43,13 @@ export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
3943
catalogValueAtom: environmentCatalog.catalogValueAtom,
4044
serverConfigValueAtom: serverEnvironment.configValueAtom,
4145
});
46+
// Lives here, not in ./presentation, so shortcuts can read connection phases
47+
// without an import cycle; ./presentation re-exports it.
48+
export const environmentPresentations = createEnvironmentPresentationAtoms({
49+
catalogValueAtom: environmentCatalog.catalogValueAtom,
50+
stateAtom: environmentCatalog.stateAtom,
51+
serverConfigValueAtom: serverEnvironment.configValueAtom,
52+
});
4253

4354
interface PrimaryServerState {
4455
readonly config: ServerConfig | null;
@@ -106,9 +117,36 @@ export const primaryServerProvidersAtom = Atom.make(
106117
get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
107118
).pipe(Atom.withLabel("web-primary-server-providers"));
108119

109-
export const primaryServerKeybindingsAtom = Atom.make((get): ServerConfig["keybindings"] =>
110-
mergeWithDefaultKeybindings(get(primaryServerConfigAtom)?.keybindings ?? []),
111-
).pipe(Atom.withLabel("web-primary-server-keybindings"));
120+
/**
121+
* Shortcut bindings. Without a primary environment (Local environment off, the
122+
* hosted app) they follow the environment unscoped Settings shows and edits. A
123+
* primary whose config is still loading keeps the defaults.
124+
*/
125+
export function createShortcutKeybindingsAtom(input: {
126+
readonly primaryEnvironmentIdAtom: Atom.Atom<EnvironmentId | null>;
127+
readonly primaryConfigAtom: Atom.Atom<ServerConfig | null>;
128+
readonly environmentsAtom: Atom.Atom<
129+
ReadonlyMap<EnvironmentId, Pick<EnvironmentPresentation, "connection" | "serverConfig">>
130+
>;
131+
}) {
132+
// Selected before merging, so unrelated config updates keep the merged array.
133+
const customAtom = Atom.make((get) => {
134+
if (get(input.primaryEnvironmentIdAtom) !== null) {
135+
return get(input.primaryConfigAtom)?.keybindings ?? null;
136+
}
137+
const environments = [...get(input.environmentsAtom).values()];
138+
return environments.find(isConnectedWithConfig)?.serverConfig?.keybindings ?? null;
139+
});
140+
return Atom.make((get): ServerConfig["keybindings"] =>
141+
mergeWithDefaultKeybindings(get(customAtom) ?? []),
142+
);
143+
}
144+
145+
export const primaryServerKeybindingsAtom = createShortcutKeybindingsAtom({
146+
primaryEnvironmentIdAtom,
147+
primaryConfigAtom: primaryServerConfigAtom,
148+
environmentsAtom: environmentPresentations.presentationsAtom,
149+
}).pipe(Atom.withLabel("web-primary-server-keybindings"));
112150

113151
export const primaryServerAvailableEditorsAtom = Atom.make(
114152
(get): ReadonlyArray<EditorId> =>

0 commit comments

Comments
 (0)