
This commit adds HTMLTagNameElementMap entries to every web component in the front end. Activating and associating the HTMLTagNamElementMap with its class has enabled [LitAnalyzer](https://github.com/runem/lit-analyzer/tree/master/packages/lit-analyzer) to reveal a *lot* of basic problems within the UI, the most popular of which is "missing import." We usually get away with it because the object being imported was already registered with the browser elsewhere, but it still surprises me that we haven't gotten any complaints over things like: ``` ./src/flow/stages/base.ts Missing import for <ak-form-static> 96: <ak-form-static no-missing-import ``` Given how early and fundamental that seems to be in our code, I'd have expected to hear _something_ about it. I have not enabled most of the possible checks because, well, there are just a ton of warnings when I do. I'd like to get in and fix those. Aside from this, I have also _removed_ `customElement` declarations from anything declared as an `abstract class`. It makes no sense to try and instantiate something that cannot, by definition, be instantiated. If the class is capable of running on its own, it's not abstract, it just needs to be overridden in child classes. Before removing the declaration I did check to make sure no other piece of code was even *trying* to instantiate it, and so far I have detected no failures. Those elements were: - elements/forms/Form.ts - element-/wizard/WizardFormPage.ts The one that blows my mind, though, is this: ``` src/elements/forms/ProxyForm.ts 6-@customElement("ak-proxy-form") 7:export abstract class ProxyForm extends Form<unknown> { ``` Which, despite being `abstract`, is somehow instantiable? ``` src/admin/outposts/ServiceConnectionListPage.ts: <ak-proxy-form src/admin/providers/ProviderListPage.ts: <ak-proxy-form src/admin/sources/SourceWizard.ts: <ak-proxy-form src/admin/sources/SourceListPage.ts: <ak-proxy-form src/admin/providers/ProviderWizard.ts: <ak-proxy-form type=${type.component}></ak-proxy-form> src/admin/stages/StageListPage.ts: <ak-proxy-form ``` I've made a note to investigate. I've started a new folder where all of my one-off tools for *how* a certain PR was run. It has a README describing what it's for, and the first tool, `add-htmlelementtagnamemaps-to-everything`, is its first entry. That tool is also documented internally. ``` Gilbert & Sullivan I've got a little list, I've got a little list, Of all the code that would never be missed, The duplicate code of cute-and-paste, The weak abstractions that lead to waste, The embedded templates-- you get the gist, There ain't none of 'em that will ever be missed, And that's why I've got them on my list! ```
118 lines
4.1 KiB
TypeScript
118 lines
4.1 KiB
TypeScript
import { AkWizard } from "@goauthentik/components/ak-wizard-main/AkWizard";
|
|
import { CustomListenerElement } from "@goauthentik/elements/utils/eventEmitter";
|
|
|
|
import { ContextProvider } from "@lit/context";
|
|
import { msg } from "@lit/localize";
|
|
import { customElement, state } from "lit/decorators.js";
|
|
|
|
import { applicationWizardContext } from "./ContextIdentity";
|
|
import { newSteps } from "./steps";
|
|
import {
|
|
ApplicationStep,
|
|
ApplicationWizardState,
|
|
ApplicationWizardStateUpdate,
|
|
OneOfProvider,
|
|
} from "./types";
|
|
|
|
const freshWizardState = (): ApplicationWizardState => ({
|
|
providerModel: "",
|
|
app: {},
|
|
provider: {},
|
|
errors: {},
|
|
});
|
|
|
|
@customElement("ak-application-wizard")
|
|
export class ApplicationWizard extends CustomListenerElement(
|
|
AkWizard<ApplicationWizardStateUpdate, ApplicationStep>,
|
|
) {
|
|
constructor() {
|
|
super(msg("Create With Wizard"), msg("New application"), msg("Create a new application"));
|
|
this.steps = newSteps();
|
|
}
|
|
|
|
/**
|
|
* We're going to be managing the content of the forms by percolating all of the data up to this
|
|
* class, which will ultimately transmit all of it to the server as a transaction. The
|
|
* WizardFramework doesn't know anything about the nature of the data itself; it just forwards
|
|
* valid updates to us. So here we maintain a state object *and* update it so all child
|
|
* components can access the wizard state.
|
|
*
|
|
*/
|
|
@state()
|
|
wizardState: ApplicationWizardState = freshWizardState();
|
|
|
|
wizardStateProvider = new ContextProvider(this, {
|
|
context: applicationWizardContext,
|
|
initialValue: this.wizardState,
|
|
});
|
|
|
|
/**
|
|
* One of our steps has multiple display variants, one for each type of service provider. We
|
|
* want to *preserve* a customer's decisions about different providers; never make someone "go
|
|
* back and type it all back in," even if it's probably rare that someone will chose one
|
|
* provider, realize it's the wrong one, and go back to chose a different one, *and then go
|
|
* back*. Nonetheless, strive to *never* lose customer input.
|
|
*
|
|
*/
|
|
providerCache: Map<string, OneOfProvider> = new Map();
|
|
|
|
// And this is where all the special cases go...
|
|
handleUpdate(detail: ApplicationWizardStateUpdate) {
|
|
if (detail.status === "submitted") {
|
|
this.step.valid = true;
|
|
this.requestUpdate();
|
|
return;
|
|
}
|
|
|
|
this.step.valid = this.step.valid || detail.status === "valid";
|
|
const update = detail.update;
|
|
|
|
if (!update) {
|
|
return;
|
|
}
|
|
|
|
// When the providerModel enum changes, retrieve the customer's prior work for *this* wizard
|
|
// session (and only this wizard session) or provide an empty model with a default provider
|
|
// name.
|
|
if (update.providerModel && update.providerModel !== this.wizardState.providerModel) {
|
|
const requestedProvider = this.providerCache.get(update.providerModel) ?? {
|
|
name: `Provider for ${this.wizardState.app.name}`,
|
|
};
|
|
if (this.wizardState.providerModel) {
|
|
this.providerCache.set(this.wizardState.providerModel, this.wizardState.provider);
|
|
}
|
|
update.provider = requestedProvider;
|
|
}
|
|
|
|
this.wizardState = update as ApplicationWizardState;
|
|
this.wizardStateProvider.setValue(this.wizardState);
|
|
this.requestUpdate();
|
|
}
|
|
|
|
close() {
|
|
this.steps = newSteps();
|
|
this.currentStep = 0;
|
|
this.wizardState = freshWizardState();
|
|
this.providerCache = new Map();
|
|
this.wizardStateProvider.setValue(this.wizardState);
|
|
this.frame.value!.open = false;
|
|
}
|
|
|
|
handleNav(stepId: number | undefined) {
|
|
if (stepId === undefined || this.steps[stepId] === undefined) {
|
|
throw new Error(`Attempt to navigate to undefined step: ${stepId}`);
|
|
}
|
|
if (stepId > this.currentStep && !this.step.valid) {
|
|
return;
|
|
}
|
|
this.currentStep = stepId;
|
|
this.requestUpdate();
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
"ak-application-wizard": ApplicationWizard;
|
|
}
|
|
}
|