|
| 1 | +import { localized, msg, str } from "@lit/localize"; |
| 2 | +import { Task } from "@lit/task"; |
| 3 | +import { html } from "lit"; |
| 4 | +import { customElement, property, query } from "lit/decorators.js"; |
| 5 | +import { when } from "lit/directives/when.js"; |
| 6 | + |
| 7 | +import { BtrixElement } from "@/classes/BtrixElement"; |
| 8 | +import type { Dialog } from "@/components/ui/dialog"; |
| 9 | +import type { ArchivedItem } from "@/types/crawler"; |
| 10 | +import { isCrawl, renderName } from "@/utils/crawler"; |
| 11 | +import { pluralOf } from "@/utils/pluralize"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Confirm deletion of an archived item, crawl associated |
| 15 | + * with an archived item, or crawl run. |
| 16 | + * |
| 17 | + * @slot name |
| 18 | + */ |
| 19 | +@customElement("btrix-delete-item-dialog") |
| 20 | +@localized() |
| 21 | +export class DeleteItemDialog extends BtrixElement { |
| 22 | + @property({ type: Object }) |
| 23 | + item?: ArchivedItem; |
| 24 | + |
| 25 | + @property({ type: Boolean }) |
| 26 | + open = false; |
| 27 | + |
| 28 | + @query("btrix-dialog") |
| 29 | + readonly dialog?: Dialog | null; |
| 30 | + |
| 31 | + private readonly collectionsTask = new Task(this, { |
| 32 | + task: async ([open, crawl], { signal }) => { |
| 33 | + if (!crawl?.collectionIds) return; |
| 34 | + |
| 35 | + if (!open) { |
| 36 | + return crawl.collectionIds.map((id) => ({ id })); |
| 37 | + } |
| 38 | + |
| 39 | + return (await this.getCrawl(crawl.id, signal)).collections; |
| 40 | + }, |
| 41 | + args: () => [this.open, this.item] as const, |
| 42 | + }); |
| 43 | + |
| 44 | + render() { |
| 45 | + return html`<btrix-dialog |
| 46 | + .label=${this.item |
| 47 | + ? isCrawl(this.item) |
| 48 | + ? msg("Delete Crawl?") |
| 49 | + : msg("Delete Archived Item?") |
| 50 | + : msg("Delete")} |
| 51 | + .open=${this.open} |
| 52 | + > |
| 53 | + ${this.renderContent()} |
| 54 | + </btrix-dialog>`; |
| 55 | + } |
| 56 | + |
| 57 | + private renderContent() { |
| 58 | + const item = this.item; |
| 59 | + |
| 60 | + if (!item) return; |
| 61 | + |
| 62 | + const crawl = isCrawl(item); |
| 63 | + const item_name = html`<slot name="name" |
| 64 | + ><strong class="font-semibold">${renderName(item)}</strong></slot |
| 65 | + >`; |
| 66 | + |
| 67 | + return html` |
| 68 | + <p> |
| 69 | + ${msg(html`Are you sure you want to delete ${item_name}?`)} |
| 70 | + ${msg("All associated files and logs will be deleted.")} |
| 71 | + </p> |
| 72 | +
|
| 73 | + ${this.renderCollections()} |
| 74 | + <div slot="footer" class="flex justify-between"> |
| 75 | + <sl-button |
| 76 | + size="small" |
| 77 | + .autofocus=${true} |
| 78 | + @click=${() => { |
| 79 | + void this.dialog?.hide(); |
| 80 | + this.dispatchEvent(new CustomEvent("btrix-cancel")); |
| 81 | + }} |
| 82 | + >${msg("Cancel")}</sl-button |
| 83 | + > |
| 84 | + <sl-button |
| 85 | + size="small" |
| 86 | + variant="danger" |
| 87 | + @click=${() => { |
| 88 | + this.dispatchEvent(new CustomEvent("btrix-confirm")); |
| 89 | + }} |
| 90 | + >${crawl |
| 91 | + ? msg("Delete Crawl") |
| 92 | + : msg("Delete Archived Item")}</sl-button |
| 93 | + > |
| 94 | + </div> |
| 95 | + `; |
| 96 | + } |
| 97 | + |
| 98 | + private renderCollections() { |
| 99 | + if (!this.item?.collectionIds.length) return; |
| 100 | + |
| 101 | + const { collectionIds } = this.item; |
| 102 | + const count = collectionIds.length; |
| 103 | + |
| 104 | + const number_of_collections = this.localize.number(count); |
| 105 | + const plural_of_collections = pluralOf("collections", count); |
| 106 | + |
| 107 | + return html` |
| 108 | + <p class="my-2"> |
| 109 | + ${msg( |
| 110 | + str`The archived item will be removed from ${number_of_collections} ${plural_of_collections}:`, |
| 111 | + )} |
| 112 | + </p> |
| 113 | + ${this.collectionsTask.render({ |
| 114 | + pending: () => |
| 115 | + html`<btrix-linked-collections-list |
| 116 | + .collections=${collectionIds.map((id) => ({ id }))} |
| 117 | + baseUrl="${this.navigate.orgBasePath}/collections/view" |
| 118 | + > |
| 119 | + </btrix-linked-collections-list>`, |
| 120 | + complete: (res) => |
| 121 | + when( |
| 122 | + res, |
| 123 | + (collections) => |
| 124 | + html`<btrix-linked-collections-list |
| 125 | + .collections=${collections} |
| 126 | + baseUrl="${this.navigate.orgBasePath}/collections/view" |
| 127 | + > |
| 128 | + </btrix-linked-collections-list>`, |
| 129 | + ), |
| 130 | + })} |
| 131 | + `; |
| 132 | + } |
| 133 | + |
| 134 | + private async getCrawl(id: string, signal: AbortSignal) { |
| 135 | + const data: ArchivedItem = await this.api.fetch<ArchivedItem>( |
| 136 | + `/orgs/${this.orgId}/crawls/${id}/replay.json`, |
| 137 | + { |
| 138 | + signal, |
| 139 | + }, |
| 140 | + ); |
| 141 | + |
| 142 | + return data; |
| 143 | + } |
| 144 | +} |
0 commit comments