Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4da94ef
fix: remove some of the jank around our core App component
Parkreiner May 2, 2024
9986024
refactor: scope navigation logic more aggressively
Parkreiner May 2, 2024
7004c9c
refactor: add explicit return type to useAuthenticated
Parkreiner May 2, 2024
ebfaec5
refactor: clean up ProxyContext code
Parkreiner May 2, 2024
1192eb3
wip: add code for consolidating the HTML metadata
Parkreiner May 2, 2024
bbe2ae0
refactor: clean up hook logic
Parkreiner May 2, 2024
1c41937
refactor: rename useHtmlMetadata to useEmbeddedMetadata
Parkreiner May 2, 2024
79e9c45
fix: correct names that weren't updated
Parkreiner May 2, 2024
81f2cd9
fix: update type-safety of useEmbeddedMetadata further
Parkreiner May 2, 2024
390418f
wip: switch codebase to use metadata hook
Parkreiner May 3, 2024
486f292
Merge branch 'main' into mes/login-fix
Parkreiner May 3, 2024
b77af73
Merge branch 'mes/login-fix' of https://github.com/coder/coder into m…
Parkreiner May 3, 2024
e072f7a
refactor: simplify design of metadata hook
Parkreiner May 3, 2024
2a58322
fix: update stray type mismatches
Parkreiner May 3, 2024
b55abb7
fix: more type fixing
Parkreiner May 3, 2024
c45e1b7
fix: resolve illegal invocation error
Parkreiner May 3, 2024
2a63c1d
fix: get metadata issue resolved
Parkreiner May 3, 2024
4d3b155
fix: update comments
Parkreiner May 3, 2024
8067e77
chore: add unit tests for MetadataManager
Parkreiner May 3, 2024
5e6e974
fix: beef up tests
Parkreiner May 3, 2024
772b96f
fix: update typo in tests
Parkreiner May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: simplify design of metadata hook
  • Loading branch information
Parkreiner committed May 3, 2024
commit e072f7ad40a3814e6496df75b24b5096659046c6
9 changes: 5 additions & 4 deletions site/src/api/queries/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ export function cachedQuery<
>,
): FormattedQueryOptionsResult<TQueryFnData, TError, TData, TQueryKey> {
const { metadata, ...delegatedOptions } = options;
const metadataIsAvailable = metadata.status === "loaded";

const newOptions = {
...delegatedOptions,
...(metadataIsAvailable ? disabledFetchOptions : {}),
initialData: metadataIsAvailable ? metadata.value : undefined,
initialData: metadata.available ? metadata.value : undefined,

// Make sure the disabled options are always serialized last, so that no
// one using this function can accidentally override the values
...(metadata.available ? disabledFetchOptions : {}),
};

return newOptions as FormattedQueryOptionsResult<
Expand Down
33 changes: 16 additions & 17 deletions site/src/hooks/useEmbeddedMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ export type MetadataState<T extends MetadataValue> = Readonly<{
// undefined chosen to signify missing value because unlike null, it isn't a
// valid JSON-serializable value. It's impossible to be returned by the API
value: T | undefined;
status: "missing" | "loaded" | "deleted";
available: boolean;
}>;

const unavailableState = {
value: undefined,
available: false,
} as const satisfies MetadataState<MetadataValue>;

export type RuntimeHtmlMetadata = Readonly<{
[Key in MetadataKey]: MetadataState<AvailableMetadata[Key]>;
}>;
Expand Down Expand Up @@ -78,7 +83,7 @@ export class MetadataManager implements MetadataManagerApi {
entitlements: this.registerValue<Entitlements>("entitlements"),
experiments: this.registerValue<Experiments>("experiments"),
"build-info": this.registerValue<BuildInfoResponse>("build-info"),
regions: this.registerRegionValue("region"),
regions: this.registerRegionValue(),
};
}

Expand All @@ -99,7 +104,7 @@ export class MetadataManager implements MetadataManagerApi {
* can be tightened up even further (e.g., adding a type constraint to
* parseJson)
*/
private registerRegionValue(key: "region"): MetadataState<readonly Region[]> {
private registerRegionValue(): MetadataState<readonly Region[]> {
type RegionResponse =
| readonly Region[]
| Readonly<{
Expand All @@ -110,13 +115,14 @@ export class MetadataManager implements MetadataManagerApi {

let newEntry: MetadataState<readonly Region[]>;
if (!node || value === undefined) {
newEntry = { value: undefined, status: "missing" };
newEntry = unavailableState;
} else if ("regions" in value) {
newEntry = { value: value.regions, status: "loaded" };
newEntry = { value: value.regions, available: true };
} else {
newEntry = { value, status: "loaded" };
newEntry = { value, available: true };
}

const key = "regions" satisfies MetadataKey;
this.trackedMetadataNodes.set(key, node);
return newEntry;
}
Expand All @@ -128,9 +134,9 @@ export class MetadataManager implements MetadataManagerApi {

let newEntry: MetadataState<T>;
if (!node || value === undefined) {
newEntry = { value: undefined, status: "missing" };
newEntry = unavailableState;
} else {
newEntry = { value, status: "loaded" };
newEntry = { value, available: true };
}

this.trackedMetadataNodes.set(key, node);
Expand Down Expand Up @@ -177,7 +183,7 @@ export class MetadataManager implements MetadataManagerApi {

clearMetadataByKey = (key: MetadataKey): void => {
const metadataValue = this.metadata[key];
if (metadataValue.status === "missing") {
if (!metadataValue.available) {
return;
}

Expand All @@ -188,14 +194,7 @@ export class MetadataManager implements MetadataManagerApi {
// the value after it's supposed to have been made unavailable
metadataNode?.remove();

type NewState = MetadataState<NonNullable<typeof metadataValue.value>>;
const newState: NewState = {
...metadataValue,
value: undefined,
status: "deleted",
};

this.metadata = { ...this.metadata, [key]: newState };
this.metadata = { ...this.metadata, [key]: unavailableState };
this.notifySubscriptionsOfStateChange();
};
}
Expand Down