import { state, property } from "lit/decorators.js"; import { msg, localized, str } from "@lit/localize"; import { ifDefined } from "lit/directives/if-defined.js"; import type { AuthState } from "../../utils/AuthService"; import LiteElement, { html } from "../../utils/LiteElement"; /** * Usage: * ```ts * * ``` */ @localized() export class BrowserProfilesNew extends LiteElement { @property({ type: Object }) authState!: AuthState; @property({ type: String }) orgId!: string; @property({ type: String }) browserId!: string; @state() private isSubmitting = false; @state() private isDialogVisible = false; // URL params can be used to pass name and description // base ID determines whether this is an edit/extension @state() private params: Partial<{ name: string; description: string; navigateUrl: string; profileId: string | null; }> = {}; firstUpdated() { const params = new URLSearchParams(window.location.search); const profileId = params.get("profileId"); this.params = { name: params.get("name") || "", description: params.get("description") || "", navigateUrl: params.get("navigateUrl") || "", profileId: profileId || null, }; } render() { return html`
${this.params.profileId ? msg("Back to Profile") : msg("Back to Browser Profiles")}
${this.params.profileId ? html`
${msg( html`Extending ${this.params.name}` )}
` : ""}

${msg( "Interact with the browsing tool to record your browser profile. You will complete and save your profile in the next step." )}

(this.isDialogVisible = true)} > ${msg("Next")}
(this.isDialogVisible = false)} > ${this.renderForm()} `; } private renderForm() { return html`
(this.isDialogVisible = false)} > ${msg("Back")} ${msg("Create Profile")}
`; } private async onSubmit(event: SubmitEvent) { event.preventDefault(); this.isSubmitting = true; const formData = new FormData(event.target as HTMLFormElement); const params = { browserid: this.browserId, name: formData.get("name"), description: formData.get("description"), }; try { const data = await this.apiFetch( `/orgs/${this.orgId}/profiles`, this.authState!, { method: "POST", body: JSON.stringify(params), } ); this.notify({ message: msg("Successfully created browser profile."), variant: "success", icon: "check2-circle", }); this.navTo(`/orgs/${this.orgId}/browser-profiles/profile/${data.id}`); } catch (e: any) { this.isSubmitting = false; let message = msg("Sorry, couldn't create browser profile at this time."); if (e.isApiError && e.statusCode === 403) { if (e.details === "storage_quota_reached") { message = msg( "Your org does not have enough storage to save this browser profile." ); } else { message = msg( "You do not have permission to create browser profiles." ); } } this.notify({ message: message, variant: "danger", icon: "exclamation-octagon", }); } } } customElements.define("btrix-browser-profiles-new", BrowserProfilesNew);