import { state, property, customElement } 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()
@customElement("btrix-browser-profiles-new")
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
? html`
${msg(
html`Extending ${this.params.name}`
)}
`
: ""}
${msg(
"Interact with the browsing tool to record your browser profile. It is highly recommended to create dedicated accounts to use when crawling. For details refer to the best practices on the "
)}
${msg("browser profiles documentation page.")}
(this.isDialogVisible = true)}
>
${msg("Finish Browsing")}
(this.isDialogVisible = false)}
>
${this.renderForm()}
`;
}
private renderForm() {
return html``;
}
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<{ id: string }>(
`/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(`${this.orgBasePath}/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",
});
}
}
}