Skip to content

Commit 2c1b4e5

Browse files
fix(web): custom shortcuts work when Local environment is off (pingdotgg#15825)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 02c81ec commit 2c1b4e5

3 files changed

Lines changed: 220 additions & 7 deletions

File tree

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

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

52+
export function isConnectedWithConfig(
53+
environment: Pick<ScopedSettingsEnvironment, "connection" | "serverConfig">,
54+
) {
55+
return environment.connection.phase === "connected" && environment.serverConfig !== null;
56+
}
57+
5258
/** The representative supplies display values, never the set of write targets. */
5359
export function selectScopedSettingsEnvironments<T extends ScopedSettingsEnvironment>(
5460
scope: ResolvedSettingsScope,
@@ -59,10 +65,7 @@ export function selectScopedSettingsEnvironments<T extends ScopedSettingsEnviron
5965
const environments = available.filter((environment) =>
6066
selectedIds.has(environment.environmentId),
6167
);
62-
const connectedEnvironments = environments.filter(
63-
(environment) =>
64-
environment.connection.phase === "connected" && environment.serverConfig !== null,
65-
);
68+
const connectedEnvironments = environments.filter(isConnectedWithConfig);
6669
const environment =
6770
connectedEnvironments.find((candidate) => candidate.environmentId === primaryEnvironmentId) ??
6871
connectedEnvironments[0] ??

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

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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/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+
cmdN: () =>
57+
resolveShortcutCommand(
58+
{ key: "n", metaKey: true, ctrlKey: false, shiftKey: false, altKey: false },
59+
registry.get(keybindingsAtom),
60+
{ platform: "MacIntel" },
61+
),
62+
};
63+
}
64+
65+
const registries: AtomRegistry.AtomRegistry[] = [];
66+
afterEach(() => {
67+
for (const registry of registries.splice(0)) registry.dispose();
68+
});
69+
70+
describe("createShortcutKeybindingsAtom", () => {
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" }]);
76+
expect(harness.cmdN()).toBe("chat.new");
77+
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+
}
106+
});
107+
108+
it("uses the primary config or defaults until the primary is removed", () => {
109+
const harness = makeHarness({
110+
environments: [[B, environment("connected", config(SWAPPED))]],
111+
});
112+
expect(harness.cmdN()).toBe("chat.newLocal");
113+
114+
harness.registry.set(harness.primaryEnvironmentIdAtom, PRIMARY);
115+
expect(harness.cmdN()).toBe("chat.new");
116+
117+
harness.registry.set(harness.primaryConfigAtom, config(STALE));
118+
expect(harness.cmdN()).toBe("chat.newWithoutProject");
119+
120+
harness.registry.set(harness.primaryConfigAtom, null);
121+
expect(harness.cmdN()).toBe("chat.new");
122+
123+
harness.registry.set(harness.primaryEnvironmentIdAtom, null);
124+
expect(harness.cmdN()).toBe("chat.newLocal");
125+
});
126+
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();
141+
142+
harness.registry.set(
143+
harness.environmentsAtom,
144+
new Map([
145+
[A, environment("connected", { ...selected, providers: [] })],
146+
[B, environment("connected", config([]))],
147+
]),
148+
);
149+
expect(harness.keybindings()).toBe(before);
150+
151+
if (hasPrimary) {
152+
harness.registry.set(harness.primaryConfigAtom, { ...selected, providers: [] });
153+
expect(harness.keybindings()).toBe(before);
154+
}
155+
},
156+
);
157+
});

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import {
1010
type ServerProvider,
1111
type ServerSettings,
1212
} from "@t3tools/contracts";
13+
import {
14+
AVAILABLE_CONNECTION_STATE,
15+
presentEnvironmentConnection,
16+
type EnvironmentPresentation,
17+
} from "@t3tools/client-runtime/connection";
1318
import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
1419
import { createOutdatedServerUpdateCommand } from "@t3tools/client-runtime/state/outdatedServerUpdate";
1520
import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
1621
import { mergeWithDefaultKeybindings } from "@t3tools/shared/keybindings";
1722
import * as Option from "effect/Option";
1823
import { AsyncResult, Atom } from "effect/reactivity";
1924

25+
import { isConnectedWithConfig } from "../components/settings/scopedSettings";
2026
import { environmentCatalog } from "../connection/catalog";
2127
import { connectionAtomRuntime } from "../connection/runtime";
2228
import { environmentAtomList } from "./environmentAtomList";
@@ -109,9 +115,56 @@ export const primaryServerProvidersAtom = Atom.make(
109115
get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
110116
).pipe(Atom.withLabel("web-primary-server-providers"));
111117

112-
export const primaryServerKeybindingsAtom = Atom.make((get): ServerConfig["keybindings"] =>
113-
mergeWithDefaultKeybindings(get(primaryServerConfigAtom)?.keybindings ?? []),
114-
).pipe(Atom.withLabel("web-primary-server-keybindings"));
118+
/** Without a primary, global shortcuts follow unscoped Settings. */
119+
export function createShortcutKeybindingsAtom(input: {
120+
readonly primaryEnvironmentIdAtom: Atom.Atom<EnvironmentId | null>;
121+
readonly primaryConfigAtom: Atom.Atom<ServerConfig | null>;
122+
readonly environmentsAtom: Atom.Atom<
123+
ReadonlyMap<EnvironmentId, Pick<EnvironmentPresentation, "connection" | "serverConfig">>
124+
>;
125+
}) {
126+
// Selected before merging, so unrelated config updates keep the merged array.
127+
const customAtom = Atom.make((get) => {
128+
if (get(input.primaryEnvironmentIdAtom) !== null) {
129+
// A primary stays authoritative while its config loads.
130+
return get(input.primaryConfigAtom)?.keybindings ?? null;
131+
}
132+
const environments = [...get(input.environmentsAtom).values()];
133+
return environments.find(isConnectedWithConfig)?.serverConfig?.keybindings ?? null;
134+
});
135+
return Atom.make((get): ServerConfig["keybindings"] =>
136+
mergeWithDefaultKeybindings(get(customAtom) ?? []),
137+
);
138+
}
139+
140+
// Settings' presentations import this module; mirror their catalog order and
141+
// connection presentation here to avoid a cycle.
142+
const environmentConnectionsAtom = Atom.make((get) => {
143+
const environments = new Map<
144+
EnvironmentId,
145+
Pick<EnvironmentPresentation, "connection" | "serverConfig">
146+
>();
147+
for (const [environmentId, entry] of get(environmentCatalog.catalogValueAtom).entries) {
148+
const state = Option.getOrElse(
149+
AsyncResult.value(get(environmentCatalog.stateAtom(environmentId))),
150+
() => AVAILABLE_CONNECTION_STATE,
151+
);
152+
environments.set(environmentId, {
153+
connection:
154+
entry.unsupportedReason === undefined
155+
? presentEnvironmentConnection(state)
156+
: { phase: "unsupported", error: entry.unsupportedReason, traceId: null },
157+
serverConfig: get(serverEnvironment.configValueAtom(environmentId)),
158+
});
159+
}
160+
return environments;
161+
});
162+
163+
export const primaryServerKeybindingsAtom = createShortcutKeybindingsAtom({
164+
primaryEnvironmentIdAtom,
165+
primaryConfigAtom: primaryServerConfigAtom,
166+
environmentsAtom: environmentConnectionsAtom,
167+
}).pipe(Atom.withLabel("web-primary-server-keybindings"));
115168

116169
export const primaryServerAvailableEditorsAtom = Atom.make(
117170
(get): ReadonlyArray<EditorId> =>

0 commit comments

Comments
 (0)