Skip to content

Commit 3d82104

Browse files
fix(web): keep custom shortcuts reactive without a primary
1 parent 32b32e2 commit 3d82104

3 files changed

Lines changed: 69 additions & 93 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
4949
return typeof value === "object" && value !== null && !Array.isArray(value);
5050
}
5151

52-
/** Settings reads and writes these; without a primary, shortcuts follow the first one. */
5352
export function isConnectedWithConfig(
5453
environment: Pick<ScopedSettingsEnvironment, "connection" | "serverConfig">,
5554
) {

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

Lines changed: 65 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
22
import { EnvironmentId, type KeybindingRule, type ServerConfig } from "@t3tools/contracts";
33
import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
4-
import { Atom, AtomRegistry } from "effect/unstable/reactivity";
4+
import { Atom, AtomRegistry } from "effect/reactivity";
55
import { afterEach, describe, expect, it } from "vite-plus/test";
66

77
import { resolveShortcutCommand } from "../keybindings";
@@ -53,7 +53,6 @@ function makeHarness(input: {
5353
primaryConfigAtom,
5454
environmentsAtom,
5555
keybindings: () => registry.get(keybindingsAtom),
56-
/** The command Cmd+N runs on macOS. */
5756
cmdN: () =>
5857
resolveShortcutCommand(
5958
{ key: "n", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false },
@@ -69,109 +68,90 @@ afterEach(() => {
6968
});
7069

7170
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-
});
71+
it("updates shortcuts from the first connected, configured environment without a primary", () => {
72+
const harness = makeHarness({});
73+
const aConfig = config(STALE);
74+
const bConfig = config(SWAPPED);
75+
const edited = config([{ key: "mod+n", command: "sidebar.toggle" }]);
8276
expect(harness.cmdN()).toBe("chat.new");
83-
});
8477

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");
78+
// One mounted consumer follows config loading, edits, disconnects and reconnects.
79+
for (const [a, b, command] of [
80+
[environment("offline", aConfig), environment("connected", bConfig), "chat.newLocal"],
81+
[environment("connected", null), environment("connected", bConfig), "chat.newLocal"],
82+
[
83+
environment("connected", aConfig),
84+
environment("connected", bConfig),
85+
"chat.newWithoutProject",
86+
],
87+
[environment("connected", edited), environment("connected", bConfig), "sidebar.toggle"],
88+
[environment("offline", edited), environment("connected", bConfig), "chat.newLocal"],
89+
[environment("offline", edited), environment("offline", bConfig), "chat.new"],
90+
[environment("reconnecting", edited), environment("offline", bConfig), "chat.new"],
91+
[
92+
environment("connected", aConfig),
93+
environment("offline", bConfig),
94+
"chat.newWithoutProject",
95+
],
96+
] as const) {
97+
harness.registry.set(
98+
harness.environmentsAtom,
99+
new Map([
100+
[A, a],
101+
[B, b],
102+
]),
103+
);
104+
expect(harness.cmdN()).toBe(command);
105+
}
88106
});
89107

90-
it("skips an offline environment's cached config, as Settings does", () => {
108+
it("uses the primary config or defaults until the primary is removed", () => {
91109
const harness = makeHarness({
92-
environments: [
93-
[A, environment("offline", config(STALE))],
94-
[B, environment("connected", config(SWAPPED))],
95-
],
110+
environments: [[B, environment("connected", config(SWAPPED))]],
96111
});
97112
expect(harness.cmdN()).toBe("chat.newLocal");
98-
});
99113

100-
it("keeps the defaults when no environment is connected", () => {
101-
const harness = makeHarness({
102-
environments: [[A, environment("offline", config(STALE))]],
103-
});
114+
harness.registry.set(harness.primaryEnvironmentIdAtom, PRIMARY);
104115
expect(harness.cmdN()).toBe("chat.new");
105-
});
106116

107-
it("follows edits to the connected environment's keybindings", () => {
108-
const harness = makeHarness({ environments: [[B, environment("connected", config([]))]] });
117+
harness.registry.set(harness.primaryConfigAtom, config(STALE));
118+
expect(harness.cmdN()).toBe("chat.newWithoutProject");
119+
120+
harness.registry.set(harness.primaryConfigAtom, null);
109121
expect(harness.cmdN()).toBe("chat.new");
110122

111-
harness.registry.set(
112-
harness.environmentsAtom,
113-
new Map([[B, environment("connected", config(SWAPPED))]]),
114-
);
123+
harness.registry.set(harness.primaryEnvironmentIdAtom, null);
115124
expect(harness.cmdN()).toBe("chat.newLocal");
116125
});
117126

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-
});
127+
it.each([false, true])(
128+
"preserves the bindings array through unrelated updates (primary: %s)",
129+
(hasPrimary) => {
130+
const selected = config(SWAPPED);
131+
const harness = makeHarness({
132+
primaryEnvironmentId: hasPrimary ? PRIMARY : null,
133+
primaryConfig: hasPrimary ? selected : null,
134+
environments: [
135+
[A, environment("connected", selected)],
136+
[B, environment("connected", config(STALE))],
137+
],
138+
});
139+
expect(harness.cmdN()).toBe("chat.newLocal");
140+
const before = harness.keybindings();
138141

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) =>
149142
harness.registry.set(
150143
harness.environmentsAtom,
151144
new Map([
152-
[A, environment(a, aConfig)],
153-
[B, environment(b, bConfig)],
145+
[A, environment("connected", { ...selected, providers: [] })],
146+
[B, environment("connected", config([]))],
154147
]),
155148
);
149+
expect(harness.keybindings()).toBe(before);
156150

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-
});
151+
if (hasPrimary) {
152+
harness.registry.set(harness.primaryConfigAtom, { ...selected, providers: [] });
153+
expect(harness.keybindings()).toBe(before);
154+
}
155+
},
156+
);
177157
});

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,7 @@ export const primaryServerProvidersAtom = Atom.make(
115115
get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
116116
).pipe(Atom.withLabel("web-primary-server-providers"));
117117

118-
/**
119-
* Shortcut bindings. Without a primary environment (Local environment off, the
120-
* hosted app) they follow the environment unscoped Settings shows and edits. A
121-
* primary whose config is still loading keeps the defaults.
122-
*/
118+
/** Without a primary, global shortcuts follow unscoped Settings. */
123119
export function createShortcutKeybindingsAtom(input: {
124120
readonly primaryEnvironmentIdAtom: Atom.Atom<EnvironmentId | null>;
125121
readonly primaryConfigAtom: Atom.Atom<ServerConfig | null>;
@@ -130,6 +126,7 @@ export function createShortcutKeybindingsAtom(input: {
130126
// Selected before merging, so unrelated config updates keep the merged array.
131127
const customAtom = Atom.make((get) => {
132128
if (get(input.primaryEnvironmentIdAtom) !== null) {
129+
// A primary stays authoritative while its config loads.
133130
return get(input.primaryConfigAtom)?.keybindings ?? null;
134131
}
135132
const environments = [...get(input.environmentsAtom).values()];
@@ -140,8 +137,8 @@ export function createShortcutKeybindingsAtom(input: {
140137
);
141138
}
142139

143-
// Each environment's connection and config in catalog order, presented as
144-
// ./presentation does for Settings; that module imports this one.
140+
// Settings' presentations import this module; mirror their catalog order and
141+
// connection presentation here to avoid a cycle.
145142
const environmentConnectionsAtom = Atom.make((get) => {
146143
const environments = new Map<
147144
EnvironmentId,

0 commit comments

Comments
 (0)