Skip to content

Commit 6f17f91

Browse files
committed
Merge remote-tracking branch 'origin/main' into t3/fix-antigravity-signin-check
2 parents edf35c8 + 67fdc53 commit 6f17f91

15 files changed

Lines changed: 168 additions & 37 deletions

File tree

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { installNotificationBadge } from "./methods/notificationBadge.ts";
66
import { getClientSettings, setClientSettings } from "./methods/clientSettings.ts";
77
import { getConnectionCatalog, setConnectionCatalog } from "./methods/connectionCatalog.ts";
88
import {
9+
getHostEnvironmentId,
910
getLocalEnvironmentEnabled,
1011
setLocalEnvironmentEnabled,
1112
} from "./methods/localEnvironment.ts";
@@ -100,6 +101,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
100101
yield* ipc.handleSync(getLocalEnvironmentBootstraps);
101102
yield* ipc.handleSync(getLocalEnvironmentEnabled);
102103
yield* ipc.handle(setLocalEnvironmentEnabled);
104+
yield* ipc.handle(getHostEnvironmentId);
103105
yield* ipc.handle(getLocalEnvironmentBearerToken);
104106

105107
yield* ipc.handle(getClientSettings);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const GET_PREFERRED_SYSTEM_LANGUAGES_CHANNEL = "desktop:get-preferred-sys
3838
export const GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL = "desktop:get-local-environment-bootstraps";
3939
export const GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:get-local-environment-enabled";
4040
export const SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL = "desktop:set-local-environment-enabled";
41+
export const GET_HOST_ENVIRONMENT_ID_CHANNEL = "desktop:get-host-environment-id";
4142
export const GET_LOCAL_ENVIRONMENT_BEARER_TOKEN_CHANNEL =
4243
"desktop:get-local-environment-bearer-token";
4344
export const GET_CLIENT_SETTINGS_CHANNEL = "desktop:get-client-settings";

‎apps/desktop/src/ipc/methods/localEnvironment.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as FileSystem from "effect/FileSystem";
4+
import * as Path from "effect/Path";
25
import * as Effect from "effect/Effect";
36
import * as Layer from "effect/Layer";
47

@@ -10,7 +13,11 @@ import * as ElectronApp from "../../electron/ElectronApp.ts";
1013
import * as ElectronTheme from "../../electron/ElectronTheme.ts";
1114
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
1215
import * as DesktopWindow from "../../window/DesktopWindow.ts";
13-
import { getLocalEnvironmentEnabled, setLocalEnvironmentEnabled } from "./localEnvironment.ts";
16+
import {
17+
getHostEnvironmentId,
18+
getLocalEnvironmentEnabled,
19+
setLocalEnvironmentEnabled,
20+
} from "./localEnvironment.ts";
1421

1522
const layerUnusedLifecycleRuntime = Layer.mergeAll(
1623
DesktopShutdown.layer,
@@ -27,6 +34,26 @@ const layerUnusedLifecycleRuntime = Layer.mergeAll(
2734
);
2835

2936
describe("local environment IPC", () => {
37+
it.effect("reads the shared home identity without creating one", () =>
38+
Effect.gen(function* () {
39+
const fs = yield* FileSystem.FileSystem;
40+
const path = yield* Path.Path;
41+
const stateDir = yield* fs.makeTempDirectoryScoped();
42+
const environment = DesktopEnvironment.DesktopEnvironment.of({
43+
path,
44+
stateDir,
45+
} as DesktopEnvironment.DesktopEnvironment["Service"]);
46+
const read = getHostEnvironmentId
47+
.handler(undefined)
48+
.pipe(Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment));
49+
assert.strictEqual(yield* read, null);
50+
assert.isFalse(yield* fs.exists(path.join(stateDir, "environment-id")));
51+
yield* fs.writeFileString(path.join(stateDir, "environment-id"), " service-id\n");
52+
assert.strictEqual(yield* read, "service-id");
53+
yield* fs.writeFileString(path.join(stateDir, "environment-id"), "\n");
54+
assert.strictEqual(yield* read, null);
55+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
56+
);
3057
it.effect("relaunches only when the setting changes and keeps other settings", () => {
3158
const relaunchReasons: Array<string> = [];
3259
const layer = Layer.mergeAll(

‎apps/desktop/src/ipc/methods/localEnvironment.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as Effect from "effect/Effect";
2+
import * as FileSystem from "effect/FileSystem";
23
import * as Schema from "effect/Schema";
34

5+
import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts";
46
import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
57
import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
68
import * as IpcChannels from "../channels.ts";
@@ -28,3 +30,18 @@ export const setLocalEnvironmentEnabled = makeIpcMethod({
2830
}
2931
}),
3032
});
33+
34+
// A background service sharing this app's T3 home records its environment ID in the same state dir.
35+
export const getHostEnvironmentId = makeIpcMethod({
36+
channel: IpcChannels.GET_HOST_ENVIRONMENT_ID_CHANNEL,
37+
payload: Schema.Void,
38+
result: Schema.NullOr(Schema.String),
39+
handler: Effect.fn("desktop.ipc.localEnvironment.getHostEnvironmentId")(function* () {
40+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
41+
const fileSystem = yield* FileSystem.FileSystem;
42+
const contents = yield* fileSystem
43+
.readFileString(environment.path.join(environment.stateDir, "environment-id"))
44+
.pipe(Effect.orElseSucceed(() => ""));
45+
return contents.trim() || null;
46+
}),
47+
});

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
126126
ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL) !== false,
127127
setLocalEnvironmentEnabled: (enabled) =>
128128
ipcRenderer.invoke(IpcChannels.SET_LOCAL_ENVIRONMENT_ENABLED_CHANNEL, enabled),
129+
getHostEnvironmentId: () => ipcRenderer.invoke(IpcChannels.GET_HOST_ENVIRONMENT_ID_CHANNEL),
129130
getClientSettings: () => ipcRenderer.invoke(IpcChannels.GET_CLIENT_SETTINGS_CHANNEL),
130131
setClientSettings: (settings) =>
131132
ipcRenderer.invoke(IpcChannels.SET_CLIENT_SETTINGS_CHANNEL, settings),

‎apps/web/src/appInit.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { startDefaultThemeAdoption } from "./hooks/useDefaultTheme";
3333
import { startEnvironmentThemeRepaint } from "./hooks/useEnvironmentTheme";
3434
import { readLocalStorageValue, subscribeLocalStorageKey } from "./hooks/useLocalStorage";
35+
import { startHostEnvironmentLookup } from "./state/primaryEnvironment";
3536
import {
3637
getClientSettings,
3738
startModelPreferencesMigration,
@@ -180,6 +181,7 @@ export function startAppServices(): AppRouter {
180181

181182
startClientAppearance();
182183
startDocumentTitle();
184+
startHostEnvironmentLookup();
183185
startEnvironmentThemeRepaint();
184186
startBrowserChromeThemeSync(router);
185187
startDefaultThemeAdoption();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { appAtomRegistry } from "../rpc/atomRegistry";
2-
import { primaryEnvironmentIdAtom } from "../state/primaryEnvironment";
3-
import { primaryServerSettingsAtom } from "../state/server";
2+
import { themeEnvironmentIdAtom } from "../state/primaryEnvironment";
3+
import { themeServerSettingsAtom } from "../state/server";
44
import { getThemeDefinition, singleAppearanceOf, subscribeToCustomThemes } from "../themePalette";
55
import { setAppearanceMode, setTheme } from "./useTheme";
66

@@ -45,9 +45,9 @@ function writeAppliedGeneration(storageKey: string, generation: string): void {
4545

4646
function adoptDefaultTheme(): void {
4747
if (typeof window === "undefined") return;
48-
const environmentId = appAtomRegistry.get(primaryEnvironmentIdAtom);
48+
const environmentId = appAtomRegistry.get(themeEnvironmentIdAtom);
4949
if (environmentId === null) return;
50-
const { defaultTheme, defaultThemeSetAt } = appAtomRegistry.get(primaryServerSettingsAtom);
50+
const { defaultTheme, defaultThemeSetAt } = appAtomRegistry.get(themeServerSettingsAtom);
5151
const definition = getThemeDefinition(defaultTheme);
5252
const storageKey = `${APPLIED_DEFAULT_THEME_STORAGE_PREFIX}${environmentId}`;
5353
const generation = defaultThemeToApply({
@@ -68,15 +68,15 @@ function adoptDefaultTheme(): void {
6868
}
6969

7070
/**
71-
* Applies the primary environment's default theme once per generation. Start it after the
71+
* Applies the theme environment's default theme once per generation. Start it after the
7272
* environment theme sync so a first-run client adopting the environment's own theme finds it
7373
* already in the library; it re-checks when published or custom palettes change, so a theme
7474
* that resolves late still applies.
7575
*/
7676
export function startDefaultThemeAdoption(): () => void {
7777
const unsubscribes = [
78-
appAtomRegistry.subscribe(primaryEnvironmentIdAtom, adoptDefaultTheme),
79-
appAtomRegistry.subscribe(primaryServerSettingsAtom, adoptDefaultTheme),
78+
appAtomRegistry.subscribe(themeEnvironmentIdAtom, adoptDefaultTheme),
79+
appAtomRegistry.subscribe(themeServerSettingsAtom, adoptDefaultTheme),
8080
subscribeToCustomThemes(adoptDefaultTheme),
8181
];
8282
adoptDefaultTheme();

‎apps/web/src/hooks/useEnvironmentTheme.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const publishedThemes = vi.hoisted(() => ({
99
vi.mock(import("../state/server"), async () => {
1010
const { Atom } = await import("effect/reactivity");
1111
publishedThemes.atom = Atom.make<ReadonlyArray<EnvironmentTheme>>([]);
12-
return { primaryServerEnvironmentThemesAtom: publishedThemes.atom } as never;
12+
return { themeServerEnvironmentThemesAtom: publishedThemes.atom } as never;
1313
});
1414
const refreshTheme = vi.hoisted(() => vi.fn());
1515
vi.mock(import("./useTheme"), () => ({ refreshTheme }) as never);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { EnvironmentId } from "@t3tools/contracts";
2+
import { Atom, AtomRegistry } from "effect/reactivity";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import { createThemeEnvironmentIdAtom } from "./primaryEnvironment";
6+
7+
const PRIMARY = EnvironmentId.make("primary");
8+
const SERVICE = EnvironmentId.make("service");
9+
const REMOTE = EnvironmentId.make("remote");
10+
11+
function themeEnvironmentId(input: {
12+
primary: EnvironmentId | null;
13+
host: string | null;
14+
catalog: ReadonlyArray<EnvironmentId>;
15+
}) {
16+
const atom = createThemeEnvironmentIdAtom({
17+
primaryEnvironmentIdAtom: Atom.make(input.primary),
18+
hostEnvironmentIdAtom: Atom.make(input.host),
19+
catalogValueAtom: Atom.make({ entries: new Map(input.catalog.map((id) => [id, null])) }),
20+
});
21+
return AtomRegistry.make().get(atom);
22+
}
23+
24+
describe("themeEnvironmentIdAtom", () => {
25+
it("prefers the primary environment over this machine's background service", () => {
26+
expect(
27+
themeEnvironmentId({ primary: PRIMARY, host: "service", catalog: [PRIMARY, SERVICE] }),
28+
).toBe(PRIMARY);
29+
});
30+
31+
it("uses the connected background service sharing the app's T3 home when there is no primary", () => {
32+
expect(themeEnvironmentId({ primary: null, host: "service", catalog: [REMOTE, SERVICE] })).toBe(
33+
SERVICE,
34+
);
35+
});
36+
37+
it("uses no environment when that service is not among the connections", () => {
38+
expect(themeEnvironmentId({ primary: null, host: "service", catalog: [REMOTE] })).toBeNull();
39+
expect(themeEnvironmentId({ primary: null, host: null, catalog: [REMOTE] })).toBeNull();
40+
});
41+
});

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { EnvironmentId } from "@t3tools/contracts";
12
import { Atom } from "effect/reactivity";
23

34
import { environmentCatalog } from "../connection/catalog";
5+
import { appAtomRegistry } from "../rpc/atomRegistry";
46

57
export const primaryEnvironmentIdAtom = Atom.make((get) => {
68
for (const [environmentId, entry] of get(environmentCatalog.catalogValueAtom).entries) {
@@ -10,3 +12,38 @@ export const primaryEnvironmentIdAtom = Atom.make((get) => {
1012
}
1113
return null;
1214
}).pipe(Atom.withLabel("web-primary-environment-id"));
15+
16+
const hostEnvironmentIdAtom = Atom.make<string | null>(null).pipe(
17+
Atom.keepAlive,
18+
Atom.withLabel("web-host-environment-id"),
19+
);
20+
21+
// Without a primary, only the service sharing the desktop app's T3 home supplies themes.
22+
export function createThemeEnvironmentIdAtom(input: {
23+
readonly primaryEnvironmentIdAtom: Atom.Atom<EnvironmentId | null>;
24+
readonly hostEnvironmentIdAtom: Atom.Atom<string | null>;
25+
readonly catalogValueAtom: Atom.Atom<{ readonly entries: ReadonlyMap<EnvironmentId, unknown> }>;
26+
}) {
27+
return Atom.make((get) => {
28+
const primaryEnvironmentId = get(input.primaryEnvironmentIdAtom);
29+
if (primaryEnvironmentId !== null) return primaryEnvironmentId;
30+
const hostEnvironmentId = get(input.hostEnvironmentIdAtom);
31+
for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
32+
if (environmentId === hostEnvironmentId) return environmentId;
33+
}
34+
return null;
35+
}).pipe(Atom.withLabel("web-theme-environment-id"));
36+
}
37+
38+
export const themeEnvironmentIdAtom = createThemeEnvironmentIdAtom({
39+
primaryEnvironmentIdAtom,
40+
hostEnvironmentIdAtom,
41+
catalogValueAtom: environmentCatalog.catalogValueAtom,
42+
});
43+
44+
export function startHostEnvironmentLookup(): void {
45+
void window.desktopBridge
46+
?.getHostEnvironmentId?.()
47+
.then((environmentId) => appAtomRegistry.set(hostEnvironmentIdAtom, environmentId))
48+
.catch(() => {});
49+
}

0 commit comments

Comments
 (0)