Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as Schema from "effect/Schema";
8
9import * as ElectronApp from "../electron/ElectronApp.ts";
10import * as DesktopAssets from "./DesktopAssets.ts";
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12
13const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,40}$/i;
14const COMMIT_HASH_DISPLAY_LENGTH = 12;
15
16const AppPackageMetadata = Schema.Struct({
17 t3codeCommitHash: Schema.optional(Schema.String),
18});
19
20export interface DesktopAppIdentityShape {
21 readonly resolveUserDataPath: Effect.Effect<string>;
22 readonly configure: Effect.Effect<void>;
23}
24
25export class DesktopAppIdentity extends Context.Service<
26 DesktopAppIdentity,
27 DesktopAppIdentityShape
28>()("t3/desktop/AppIdentity") {}
29
30const normalizeCommitHash = (value: string): Option.Option<string> => {
31 const trimmed = value.trim();
32 return COMMIT_HASH_PATTERN.test(trimmed)
33 ? Option.some(trimmed.slice(0, COMMIT_HASH_DISPLAY_LENGTH).toLowerCase())
34 : Option.none();
35};
36
37const make = Effect.gen(function* () {
38 const assets = yield* DesktopAssets.DesktopAssets;
39 const electronApp = yield* ElectronApp.ElectronApp;
40 const environment = yield* DesktopEnvironment.DesktopEnvironment;
41 const fileSystem = yield* FileSystem.FileSystem;
42 const commitHashCache = yield* Ref.make<Option.Option<Option.Option<string>>>(Option.none());
43
44 const resolveEmbeddedCommitHash = Effect.gen(function* () {
45 const packageJsonPath = environment.path.join(environment.appRoot, "package.json");
46 const raw = yield* fileSystem.readFileString(packageJsonPath).pipe(Effect.option);
47 return yield* Option.match(raw, {
48 onNone: () => Effect.succeed(Option.none<string>()),
49 onSome: (value) =>
50 Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata))(value).pipe(
51 Effect.map((parsed) =>
52 Option.fromNullishOr(parsed.t3codeCommitHash).pipe(Option.flatMap(normalizeCommitHash)),
53 ),
54 Effect.catch(() => Effect.succeed(Option.none<string>())),
55 ),
56 });
57 });
58
59 const resolveAboutCommitHash = Effect.gen(function* () {
60 const cached = yield* Ref.get(commitHashCache);
61 if (Option.isSome(cached)) {
62 return cached.value;
63 }
64
65 const override = Option.flatMap(environment.commitHashOverride, normalizeCommitHash);
66 if (Option.isSome(override)) {
67 yield* Ref.set(commitHashCache, Option.some(override));
68 return override;
69 }
70
71 if (!environment.isPackaged) {
72 const empty = Option.none<string>();
73 yield* Ref.set(commitHashCache, Option.some(empty));
74 return empty;
75 }
76
77 const commitHash = yield* resolveEmbeddedCommitHash;
78 yield* Ref.set(commitHashCache, Option.some(commitHash));
79 return commitHash;
80 });
81
82 const resolveUserDataPath = Effect.gen(function* () {
83 const legacyPath = environment.path.join(
84 environment.appDataDirectory,
85 environment.legacyUserDataDirName,
86 );
87 const legacyPathExists = yield* fileSystem
88 .exists(legacyPath)
89 .pipe(Effect.orElseSucceed(() => false));
90 return legacyPathExists
91 ? legacyPath
92 : environment.path.join(environment.appDataDirectory, environment.userDataDirName);
93 }).pipe(Effect.withSpan("desktop.appIdentity.resolveUserDataPath"));
94
95 const configure = Effect.gen(function* () {
96 const commitHash = yield* resolveAboutCommitHash;
97 yield* electronApp.setName(environment.displayName);
98 yield* electronApp.setAboutPanelOptions({
99 applicationName: environment.displayName,
100 applicationVersion: environment.appVersion,
101 version: Option.getOrElse(commitHash, () => "unknown"),
102 });
103
104 if (environment.platform === "win32") {
105 yield* electronApp.setAppUserModelId(environment.appUserModelId);
106 }
107
108 if (environment.platform === "linux") {
109 yield* electronApp.setDesktopName(environment.linuxDesktopEntryName);
110 }
111
112 if (environment.platform === "darwin") {
113 const iconPaths = yield* assets.iconPaths;
114 yield* Option.match(iconPaths.png, {
115 onNone: () => Effect.void,
116 onSome: electronApp.setDockIcon,
117 });
118 }
119 }).pipe(Effect.withSpan("desktop.appIdentity.configure"));
120
121 return DesktopAppIdentity.of({
122 resolveUserDataPath,
123 configure,
124 });
125});
126
127export const layer = Layer.effect(DesktopAppIdentity, make);