Skip to content

Commit 81f2cd9

Browse files
committed
fix: update type-safety of useEmbeddedMetadata further
1 parent 79e9c45 commit 81f2cd9

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

site/src/hooks/useEmbeddedMetadata.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ import type {
1212
* application during production. These values are embedded via the Go server,
1313
* so they will never exist when using a JavaScript runtime for the backend
1414
*
15-
* If you need to add a new type of metadata value, add a new property to the
16-
* type alias here, and then rest of the file should light up with errors for
17-
* what else needs to be adjusted
15+
* If you want to add new metadata in a type-safe way, add it to this type.
16+
* Each key should the name of the "property" attribute that will be used on the
17+
* HTML elements themselves, and the values should be the data you get back from
18+
* parsing those element's text content
1819
*/
1920
type AvailableMetadata = Readonly<{
2021
user: User;
2122
experiments: Experiments;
22-
appearanceConfig: AppearanceConfig;
23-
buildInfo: BuildInfoResponse;
23+
appearance: AppearanceConfig;
2424
entitlements: Entitlements;
25+
"build-info": BuildInfoResponse;
2526
}>;
2627

2728
type MetadataKey = keyof AvailableMetadata;
@@ -71,10 +72,10 @@ export class MetadataManager implements MetadataManagerApi {
7172

7273
this.metadata = {
7374
user: this.registerValue<User>("user"),
74-
appearanceConfig: this.registerValue<AppearanceConfig>("appearance"),
75-
buildInfo: this.registerValue<BuildInfoResponse>("build-info"),
75+
appearance: this.registerValue<AppearanceConfig>("appearance"),
7676
entitlements: this.registerValue<Entitlements>("entitlements"),
7777
experiments: this.registerValue<Experiments>("experiments"),
78+
"build-info": this.registerValue<BuildInfoResponse>("build-info"),
7879
};
7980
}
8081

@@ -84,9 +85,9 @@ export class MetadataManager implements MetadataManagerApi {
8485
}
8586

8687
private registerValue<T extends MetadataValue>(
87-
propertyName: string,
88+
key: MetadataKey,
8889
): MetadataState<T> {
89-
const { value, node } = this.parseJson<T>(propertyName);
90+
const { value, node } = this.parseJson<T>(key);
9091

9192
let newEntry: MetadataState<T>;
9293
if (!node || value === undefined) {
@@ -101,7 +102,7 @@ export class MetadataManager implements MetadataManagerApi {
101102
};
102103
}
103104

104-
this.trackedMetadataNodes.set(propertyName, node);
105+
this.trackedMetadataNodes.set(key, node);
105106
return newEntry;
106107
}
107108

0 commit comments

Comments
 (0)