browsertrix/frontend/src/utils/LiteElement.ts
sua yoo 1a6892572d
chore: Refactor frontend shared state (#1997)
Refactors custom components to enable shared state accessors
(like `authState`) and helpers (like `api.fetch`.) Schemas are now
defined with [zod](https://zod.dev/?id=basic-usage) which enables
runtime schema validation.

See subtasks for full description of change:

- https://github.com/webrecorder/browsertrix/pull/1979
- https://github.com/webrecorder/browsertrix/pull/1981
- https://github.com/webrecorder/browsertrix/pull/1985
- https://github.com/webrecorder/browsertrix/pull/1986

---------

Co-authored-by: Emma Segal-Grossman <hi@emma.cafe>
2024-08-12 17:57:31 -07:00

78 lines
1.8 KiB
TypeScript

import { html, LitElement } from "lit";
import appState, { use } from "./state";
import { APIController } from "@/controllers/api";
import { NavigateController } from "@/controllers/navigate";
import { NotifyController } from "@/controllers/notify";
export { html };
/**
* @deprecated Use `BtrixElement` instead
*/
export default class LiteElement extends LitElement {
@use()
appState = appState;
private readonly apiController = new APIController(this);
private readonly notifyController = new NotifyController(this);
private readonly navigateController = new NavigateController(this);
protected get authState() {
return this.appState.auth;
}
protected get userInfo() {
return this.appState.userInfo;
}
protected get userOrg() {
return this.appState.userOrg;
}
protected get orgId() {
return this.appState.orgId;
}
protected get orgSlug() {
return this.appState.orgSlug;
}
protected get org() {
return this.appState.org;
}
protected get orgBasePath() {
return this.navigateController.orgBasePath;
}
createRenderRoot() {
return this;
}
/**
* @deprecated New components should use NavigateController directly
*/
navTo = (...args: Parameters<NavigateController["to"]>) =>
this.navigateController.to(...args);
/**
* @deprecated New components should use NavigateController directly
*/
navLink = (...args: Parameters<NavigateController["link"]>) =>
this.navigateController.link(...args);
/**
* @deprecated New components should use NotifyController directly
*/
notify = (...args: Parameters<NotifyController["toast"]>) =>
this.notifyController.toast(...args);
/**
* @deprecated New components should use APIController directly
*/
apiFetch = async <T = unknown>(...args: Parameters<APIController["fetch"]>) =>
this.apiController.fetch<T>(...args);
}