Line data Source code
1 : import { handsPopupsToSystemBrowser } from '@/platform/distribution/desktopBrowser'
2 :
3 : export interface OpeningPage {
4 : readonly lang: string
5 : readonly tabTitle: string
6 : readonly title: string
7 : readonly detail: string
8 : }
9 :
10 2356 : const OPENING_PAGE_STYLE = `
11 : html, body { height: 100%; margin: 0; }
12 : body { display: flex; align-items: center; justify-content: center; background: #171717; color: #fff; font-family: Inter, system-ui, sans-serif; }
13 : main { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; }
14 : h1 { margin: 8px 0 0; font-size: 16px; font-weight: 600; }
15 : p { margin: 0; font-size: 12px; color: #8a8a8a; }
16 : [role="progressbar"] { width: 20px; height: 20px; border: 2px solid #fff; border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; }
17 : @keyframes spin { to { transform: rotate(360deg); } }
18 : @media (prefers-reduced-motion: reduce) { [role="progressbar"] { animation: none; } }
19 : `
20 :
21 5 : function paintOpeningPage(page: Document, opening: OpeningPage): void {
22 5 : page.documentElement.lang = opening.lang
23 5 : page.title = opening.tabTitle
24 5 : const style = page.createElement('style')
25 5 : style.textContent = OPENING_PAGE_STYLE
26 5 : const spinner = page.createElement('div')
27 5 : spinner.setAttribute('role', 'progressbar')
28 5 : spinner.setAttribute('aria-label', opening.title)
29 5 : const heading = page.createElement('h1')
30 5 : heading.textContent = opening.title
31 5 : const detail = page.createElement('p')
32 5 : detail.textContent = opening.detail
33 5 : const main = page.createElement('main')
34 5 : main.append(spinner, heading, detail)
35 5 : page.head.append(style)
36 5 : page.body.replaceChildren(main)
37 : }
38 :
39 : export interface BillingTab {
40 : navigate(url: string): void
41 : close(): void
42 : }
43 :
44 : /** Opens a billing page in a new tab; `false` only when the browser blocked it. */
45 0 : export function openBillingUrl(url: string): boolean {
46 : const tab = window.open(url, '_blank')
47 0 : return tab !== null || handsPopupsToSystemBrowser()
48 : }
49 :
50 : /**
51 : * Reserves a billing tab during the click, before its URL is known; `null`
52 : * when the browser blocked it. On Desktop the open waits for the URL.
53 : * `opening` is shown in the reserved tab until it navigates.
54 : */
55 5 : export function reserveBillingTab(opening?: OpeningPage): BillingTab | null {
56 5 : if (handsPopupsToSystemBrowser()) {
57 0 : return {
58 : navigate: (url) => {
59 0 : window.open(url, '_blank')
60 : },
61 : close: () => {}
62 : }
63 : }
64 5 : const tab = window.open('', '_blank')
65 5 : if (!tab) return null
66 5 : tab.opener = null
67 5 : if (opening) paintOpeningPage(tab.document, opening)
68 5 : return {
69 : navigate: (url) => {
70 3 : tab.location.href = url
71 : },
72 : close: () => tab.close()
73 : }
74 : }
|