|
| 1 | +import { localized, msg } from "@lit/localize"; |
| 2 | +import { Task, TaskStatus } from "@lit/task"; |
| 3 | +import { type SlSelectEvent } from "@shoelace-style/shoelace"; |
| 4 | +import { html } from "lit"; |
| 5 | +import { customElement, state } from "lit/decorators.js"; |
| 6 | + |
| 7 | +import { BtrixElement } from "@/classes/BtrixElement"; |
| 8 | +import { type BillingAddonCheckout } from "@/types/billing"; |
| 9 | +import appState from "@/utils/state"; |
| 10 | + |
| 11 | +const PRESET_MINUTES = [600, 1500, 3000]; |
| 12 | +const PRICE_PER_MINUTE: { value: number; currency: string } | undefined = |
| 13 | + undefined; |
| 14 | + |
| 15 | +@customElement("btrix-org-settings-billing-addon-link") |
| 16 | +@localized() |
| 17 | +export class OrgSettingsBillingAddonLink extends BtrixElement { |
| 18 | + @state() |
| 19 | + private lastClickedMinutesPreset: number | undefined = undefined; |
| 20 | + |
| 21 | + private readonly checkoutUrl = new Task(this, { |
| 22 | + task: async ([minutes]) => { |
| 23 | + if (!appState.settings?.billingEnabled || !appState.org?.subscription) |
| 24 | + return; |
| 25 | + |
| 26 | + try { |
| 27 | + const { checkoutUrl } = await this.getCheckoutUrl(minutes); |
| 28 | + |
| 29 | + if (checkoutUrl) { |
| 30 | + return checkoutUrl; |
| 31 | + } else { |
| 32 | + throw new Error("Missing checkoutUrl"); |
| 33 | + } |
| 34 | + } catch (e) { |
| 35 | + console.debug(e); |
| 36 | + |
| 37 | + throw new Error( |
| 38 | + msg("Sorry, couldn't retrieve current plan at this time."), |
| 39 | + ); |
| 40 | + } |
| 41 | + }, |
| 42 | + args: () => [undefined] as readonly [number | undefined], |
| 43 | + autoRun: false, |
| 44 | + }); |
| 45 | + private async getCheckoutUrl(minutes?: number | undefined) { |
| 46 | + const params = new URLSearchParams(); |
| 47 | + if (minutes) params.append("minutes", minutes.toString()); |
| 48 | + return this.api.fetch<BillingAddonCheckout>( |
| 49 | + `/orgs/${this.orgId}/checkout/execution-minutes?${params.toString()}`, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private readonly localizeMinutes = (minutes: number) => { |
| 54 | + return this.localize.number(minutes, { |
| 55 | + style: "unit", |
| 56 | + unit: "minute", |
| 57 | + unitDisplay: "long", |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + private async checkout(minutes?: number | undefined) { |
| 62 | + await this.checkoutUrl.run([minutes]); |
| 63 | + if (this.checkoutUrl.value) { |
| 64 | + window.location.href = this.checkoutUrl.value; |
| 65 | + } else { |
| 66 | + this.notify.toast({ |
| 67 | + message: msg("Sorry, checkout isn’t available at this time."), |
| 68 | + id: "checkout-unavailable", |
| 69 | + variant: "warning", |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + render() { |
| 75 | + const priceForMinutes = (minutes: number) => { |
| 76 | + if (!PRICE_PER_MINUTE) return; |
| 77 | + return this.localize.number(minutes * PRICE_PER_MINUTE.value, { |
| 78 | + style: "currency", |
| 79 | + currency: PRICE_PER_MINUTE.currency, |
| 80 | + }); |
| 81 | + }; |
| 82 | + const price = priceForMinutes(1); |
| 83 | + return html` |
| 84 | + <sl-button |
| 85 | + @click=${async () => { |
| 86 | + this.lastClickedMinutesPreset = undefined; |
| 87 | + await this.checkout(); |
| 88 | + }} |
| 89 | + size="small" |
| 90 | + variant="text" |
| 91 | + ?loading=${this.checkoutUrl.status === TaskStatus.PENDING && |
| 92 | + this.lastClickedMinutesPreset === undefined} |
| 93 | + ?disabled=${this.checkoutUrl.status === TaskStatus.PENDING && |
| 94 | + this.lastClickedMinutesPreset !== undefined} |
| 95 | + class="-ml-3" |
| 96 | + > |
| 97 | + ${msg("Add More Execution Minutes")} |
| 98 | + </sl-button> |
| 99 | + <hr class="h-6 border-l" aria-orientation="vertical" /> |
| 100 | + <sl-dropdown |
| 101 | + distance="4" |
| 102 | + placement="bottom-end" |
| 103 | + hoist |
| 104 | + stay-open-on-select |
| 105 | + @sl-select=${async (e: SlSelectEvent) => { |
| 106 | + this.lastClickedMinutesPreset = parseInt(e.detail.item.value); |
| 107 | + await this.checkout(this.lastClickedMinutesPreset); |
| 108 | + void e.detail.item.closest("sl-dropdown")!.hide(); |
| 109 | + }} |
| 110 | + > |
| 111 | + <sl-button caret slot="trigger" variant="text" size="small" class=""> |
| 112 | + <sl-visually-hidden> |
| 113 | + ${msg("Preset minute amounts")} |
| 114 | + </sl-visually-hidden> |
| 115 | + </sl-button> |
| 116 | + <sl-menu> |
| 117 | + <sl-menu-label>${msg("Preset minute amounts")}</sl-menu-label> |
| 118 | + ${PRESET_MINUTES.map((m) => { |
| 119 | + const minutes = this.localizeMinutes(m); |
| 120 | + return html` |
| 121 | + <sl-menu-item |
| 122 | + value=${m} |
| 123 | + ?loading=${this.checkoutUrl.status === TaskStatus.PENDING && |
| 124 | + this.lastClickedMinutesPreset === m} |
| 125 | + ?disabled=${this.checkoutUrl.status === TaskStatus.PENDING && |
| 126 | + this.lastClickedMinutesPreset !== m} |
| 127 | + > |
| 128 | + ${minutes} |
| 129 | + ${PRICE_PER_MINUTE && |
| 130 | + html`<span class="text-xs text-stone-500" slot="suffix"> |
| 131 | + ${priceForMinutes(m)} |
| 132 | + </span>`} |
| 133 | + </sl-menu-item> |
| 134 | + `; |
| 135 | + })} |
| 136 | + </sl-menu> |
| 137 | + </sl-dropdown> |
| 138 | + ${PRICE_PER_MINUTE && |
| 139 | + html`<div class="ml-auto text-xs text-stone-500"> |
| 140 | + ${msg(html`${price} per minute`)} |
| 141 | + </div>`} |
| 142 | + `; |
| 143 | + } |
| 144 | +} |
0 commit comments