import { state, property } from "lit/decorators.js";
import { msg, localized, str } from "@lit/localize";
import type { AuthState } from "../../utils/AuthService";
import LiteElement, { html } from "../../utils/LiteElement";
import { Profile } from "./types";
/**
* Usage:
* ```ts
*
* ```
*/
@localized()
export class BrowserProfilesList extends LiteElement {
@property({ type: Object })
authState!: AuthState;
@property({ type: String })
archiveId?: string;
@state()
browserProfiles?: Profile[];
firstUpdated() {
this.fetchCrawls();
}
render() {
return html` ${this.renderTable()} `;
}
renderTable() {
return html`
${msg("Description")}
${msg("Created")}
${msg("Visited URLs")}
${this.browserProfiles
? this.browserProfiles.length
? html`
${this.browserProfiles.map(this.renderItem.bind(this))}
`
: html`
${msg("No browser profiles yet.")}
`
: ""}
`;
}
private renderItem(data: Profile) {
return html`
${data.name}
${data.description}
${new Date(data.created).toLocaleDateString()}
${data.origins.join(", ")}
`;
}
/**
* Fetch browser profiles and update internal state
*/
private async fetchCrawls(): Promise {
try {
const data = await this.getProfiles();
this.browserProfiles = data;
} catch (e) {
this.notify({
message: msg("Sorry, couldn't retrieve browser profiles at this time."),
type: "danger",
icon: "exclamation-octagon",
});
}
}
private async getProfiles(): Promise {
if (!this.archiveId) {
throw new Error(`Archive ID ${typeof this.archiveId}`);
}
const data = await this.apiFetch(
`/archives/${this.archiveId}/profiles`,
this.authState!
);
return data;
}
}
customElements.define("btrix-browser-profiles-list", BrowserProfilesList);