@nebulr-group/bridge-svelte 1.0.0-alpha.12 → 1.0.0-alpha.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/bootstrap.js +17 -2
- package/dist/components/billing/BillingNotice.svelte +24 -3
- package/dist/components/billing/BillingNotice.svelte.d.ts +2 -0
- package/dist/components/billing/Entitled.svelte +44 -0
- package/dist/components/billing/Entitled.svelte.d.ts +14 -0
- package/dist/components/billing/QuotaBanner.svelte +26 -41
- package/dist/components/billing/QuotaBanner.svelte.d.ts +2 -2
- package/dist/components/billing/QuotaGate.svelte +71 -0
- package/dist/components/billing/QuotaGate.svelte.d.ts +15 -0
- package/dist/components/billing/quota-copy.d.ts +13 -0
- package/dist/components/billing/quota-copy.js +80 -0
- package/dist/core/billing-gate.d.ts +11 -0
- package/dist/core/billing-gate.js +14 -0
- package/dist/core/entitlements.d.ts +21 -0
- package/dist/core/entitlements.js +21 -0
- package/dist/core/fetch.js +6 -2
- package/dist/core/guard.d.ts +6 -0
- package/dist/core/guard.js +5 -1
- package/dist/core/quota.d.ts +67 -0
- package/dist/core/quota.js +156 -0
- package/dist/core/upgrade.d.ts +26 -0
- package/dist/core/upgrade.js +29 -0
- package/dist/index.d.ts +13 -5
- package/dist/index.js +9 -2
- package/dist/styles.css +58 -2
- package/package.json +2 -2
- package/dist/components/billing/quota.d.ts +0 -27
- package/dist/components/billing/quota.js +0 -18
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ In your app's settings in Bridge, add your app's address as a browser address an
|
|
|
71
71
|
|---|---|
|
|
72
72
|
| Sign-in | `BridgeAuthRoutes` (sign-in, sign-up, verify-email, forgot-password, set-password, invite, workspaces, handoff), `SignInForm`, `SignUpForm`, `VerifyEmail`, `ForgotPassword`, `SetPassword`, `InviteLanding`, `WorkspaceChooser`, `SignOutButton`, `ProfileName`, `RecheckDialog` with `withRecheck` and `confirmItIsYou` ("confirm it is you" instead of a sign-out) |
|
|
73
73
|
| Gates | route `rules`, `FeatureFlag`, `useFlag`, `flag`, `requireFlag`, the paywall: all on flag results, never a role or the plan |
|
|
74
|
-
| Billing | `BridgeBillingRoutes`, `PlanPicker`, `BillingPortalButton`, `SubscriptionStatus`, `BillingNotice`, `QuotaBanner`, `CheckoutNotice`, `UpgradeDialog`, `bridgeFetch` |
|
|
74
|
+
| Billing | `BridgeBillingRoutes`, `PlanPicker`, `BillingPortalButton`, `SubscriptionStatus`, `BillingNotice`, `QuotaBanner`, `QuotaGate`, `Entitled`, `CheckoutNotice`, `UpgradeDialog`, `bridgeFetch` |
|
|
75
75
|
| Team | `TeamPanel`, `MemberList`, `InviteDialog`, `InviteList`, `EditMemberDialog`, `WorkspaceForm`, `ProfileForm` |
|
|
76
76
|
| State | `session`, `user`, `workspace`, `workspaces`, `me`, `flags`, `billing`, `plans`, `usage`, `liveStatus`, `getBridge()` |
|
|
77
77
|
| Server | `@nebulr-group/bridge-svelte/server`: `bridgeHandle`, `requireActor`, `meter`, `flagsFor` |
|
package/dist/bootstrap.js
CHANGED
|
@@ -48,18 +48,33 @@ function followLink(config, url) {
|
|
|
48
48
|
return;
|
|
49
49
|
redirect(307, `${page}${hash}`);
|
|
50
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Whether the workspace is locked for payment, asked when the app's active flag is off. The flag is off both for a
|
|
53
|
+
* workspace with no plan and for one that is locked; only the first goes to the paywall. The flag can turn off before
|
|
54
|
+
* the lock held for the page is read again, so the lock is read now rather than taken from what is held.
|
|
55
|
+
*/
|
|
56
|
+
async function lockedNow(client) {
|
|
57
|
+
if (client.isLocked())
|
|
58
|
+
return true;
|
|
59
|
+
const facts = await within(client.billing.gate(), READY_WAIT_MS);
|
|
60
|
+
return facts?.access === 'blocked';
|
|
61
|
+
}
|
|
51
62
|
async function guard(client, config, url) {
|
|
52
63
|
await within(client.ready, READY_WAIT_MS);
|
|
53
64
|
const flagsNeeded = authPageAt(normalizePath(url.pathname), config.authBase) === null;
|
|
54
65
|
if (flagsNeeded && get(client.flags.status) !== 'ready')
|
|
55
66
|
await within(client.flags.load(), READY_WAIT_MS);
|
|
56
|
-
|
|
67
|
+
const input = {
|
|
57
68
|
path: url.pathname,
|
|
58
69
|
target: `${normalizePath(url.pathname)}${url.search}`,
|
|
59
70
|
session: client.session.get(),
|
|
60
71
|
flags: client.flags,
|
|
61
72
|
returnTo: readReturnTo(url, config.returnToParam),
|
|
62
|
-
}
|
|
73
|
+
};
|
|
74
|
+
const decision = decide(config, input);
|
|
75
|
+
if (decision.type !== 'redirect' || decision.reason !== 'paywall')
|
|
76
|
+
return decision;
|
|
77
|
+
return (await lockedNow(client)) ? decide(config, { ...input, locked: true }) : decision;
|
|
63
78
|
}
|
|
64
79
|
/**
|
|
65
80
|
* Makes the root layout's load function. Set `export const ssr = false`
|
|
@@ -4,11 +4,17 @@
|
|
|
4
4
|
change waiting. Renders nothing when all is well. A person with the
|
|
5
5
|
`billingManage` flag gets a link to the billing pages; anyone else is told
|
|
6
6
|
to ask the workspace owner. Not dismissible: it goes when the facts change.
|
|
7
|
+
|
|
8
|
+
A workspace locked for payment shows to every person, from the lock Bridge
|
|
9
|
+
keeps (`billingGate`), and at once when your API refuses a write with the lock's
|
|
10
|
+
402 through `bridgeFetch`. With `mode="hard"` the lock covers the page as a
|
|
11
|
+
blocking screen; every other notice stays a banner.
|
|
7
12
|
-->
|
|
8
13
|
<script lang="ts">
|
|
9
14
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
10
15
|
import { billingNotice, type BillingNotice as Notice } from '@nebulr-group/bridge-auth-core';
|
|
11
16
|
import { peekConfig } from '../../core/client.js';
|
|
17
|
+
import { billingGate } from '../../core/billing-gate.js';
|
|
12
18
|
import { canManageBilling } from '../../core/flags.js';
|
|
13
19
|
import { billing, plans } from '../../core/stores.js';
|
|
14
20
|
import { formatDate } from './plan-format.js';
|
|
@@ -19,13 +25,15 @@
|
|
|
19
25
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
20
26
|
/** The notice's shape: a full-width bar, a rail with a left edge, or a card. @default 'rail' */
|
|
21
27
|
chassis?: Chassis;
|
|
28
|
+
/** `hard` covers the page with a blocking screen while the workspace is locked for payment; other notices stay banners. @default 'soft' */
|
|
29
|
+
mode?: 'soft' | 'hard';
|
|
22
30
|
/** Where the action link goes. @default the plan page for no plan, else the subscription page */
|
|
23
31
|
actionHref?: string;
|
|
24
32
|
/** Replaces the action link's navigation. */
|
|
25
33
|
onActionClick?: (notice: Notice) => void;
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
let { chassis = 'rail', actionHref, onActionClick, class: className, style, ...rest }: Props = $props();
|
|
36
|
+
let { chassis = 'rail', mode = 'soft', actionHref, onActionClick, class: className, style, ...rest }: Props = $props();
|
|
29
37
|
|
|
30
38
|
const config = peekConfig();
|
|
31
39
|
const base = config?.billingBase ?? '/subscription';
|
|
@@ -42,7 +50,10 @@
|
|
|
42
50
|
|
|
43
51
|
const OWNER = 'Please contact your workspace owner.';
|
|
44
52
|
|
|
45
|
-
const
|
|
53
|
+
const BLOCKED: Notice = { kind: 'blocked' };
|
|
54
|
+
// The lock is shown from the gate as well as the billing facts: the gate is read by every person, and a refusal shows it at once.
|
|
55
|
+
const notice = $derived<Notice | null>($billingGate.locked ? BLOCKED : $billing.value ? billingNotice($billing.value) : null);
|
|
56
|
+
const lockscreen = $derived(mode === 'hard' && notice?.kind === 'blocked');
|
|
46
57
|
const severity = $derived(notice ? SEVERITY[notice.kind] : 'info');
|
|
47
58
|
const urgent = $derived(severity === 'critical' || severity === 'locked');
|
|
48
59
|
const href = $derived(actionHref ?? (notice?.kind === 'needs-plan' ? `${base}/plan` : base || '/'));
|
|
@@ -102,7 +113,17 @@
|
|
|
102
113
|
}
|
|
103
114
|
</script>
|
|
104
115
|
|
|
105
|
-
{#if notice && copy}
|
|
116
|
+
{#if notice && copy && lockscreen}
|
|
117
|
+
<div class={['bridge-billing-lockscreen', className]} {style} data-bridge-billing-lockscreen role="alertdialog" aria-modal="true" aria-live="assertive" aria-labelledby="bridge-lock-title">
|
|
118
|
+
<div class="bbl-panel">
|
|
119
|
+
<strong id="bridge-lock-title" class="bbl-title">{copy.title}</strong>
|
|
120
|
+
<span class="bbl-body">{copy.body}</span>
|
|
121
|
+
{#if copy.cta && $canManage}
|
|
122
|
+
<a class="bbl-cta" {href} data-bridge-billing-notice-cta onclick={handleAction}>{copy.cta}</a>
|
|
123
|
+
{/if}
|
|
124
|
+
</div>
|
|
125
|
+
</div>
|
|
126
|
+
{:else if notice && copy}
|
|
106
127
|
<div
|
|
107
128
|
class={['bridge-billing-notice', className]}
|
|
108
129
|
{style}
|
|
@@ -4,6 +4,8 @@ type Chassis = 'bar' | 'rail' | 'card';
|
|
|
4
4
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
5
5
|
/** The notice's shape: a full-width bar, a rail with a left edge, or a card. @default 'rail' */
|
|
6
6
|
chassis?: Chassis;
|
|
7
|
+
/** `hard` covers the page with a blocking screen while the workspace is locked for payment; other notices stay banners. @default 'soft' */
|
|
8
|
+
mode?: 'soft' | 'hard';
|
|
7
9
|
/** Where the action link goes. @default the plan page for no plan, else the subscription page */
|
|
8
10
|
actionHref?: string;
|
|
9
11
|
/** Replaces the action link's navigation. */
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
<Entitled to="analytics">: the children render when the plan has the feature,
|
|
3
|
+
the `fallback` snippet when it does not.
|
|
4
|
+
|
|
5
|
+
<Entitled to="analytics">
|
|
6
|
+
<AnalyticsPanel />
|
|
7
|
+
{#snippet fallback()}
|
|
8
|
+
<a href="/subscription">Upgrade for analytics</a>
|
|
9
|
+
{/snippet}
|
|
10
|
+
</Entitled>
|
|
11
|
+
|
|
12
|
+
Until Bridge has answered (`$entitlements.ready`) it renders neither, only the
|
|
13
|
+
optional `loading` snippet, so a cold start never flashes the upgrade prompt
|
|
14
|
+
at a paying workspace, nor the paid feature at a free one.
|
|
15
|
+
|
|
16
|
+
It reads the flag `bridge:billing.entitlement.<to>`, as
|
|
17
|
+
`$entitlements.can('analytics')` does. To show something for a flag in
|
|
18
|
+
general, with its reason, use `<FeatureFlag key>`.
|
|
19
|
+
-->
|
|
20
|
+
<script lang="ts">
|
|
21
|
+
import type { Snippet } from 'svelte';
|
|
22
|
+
import { entitlements } from '../../core/entitlements.js';
|
|
23
|
+
|
|
24
|
+
interface Props {
|
|
25
|
+
/** The plan feature's key, such as `analytics`. */
|
|
26
|
+
to: string;
|
|
27
|
+
/** Rendered when the plan has `to`. */
|
|
28
|
+
children: Snippet;
|
|
29
|
+
/** Rendered when Bridge has answered and the plan does not have `to`. */
|
|
30
|
+
fallback?: Snippet;
|
|
31
|
+
/** Rendered until Bridge has answered. Nothing by default. */
|
|
32
|
+
loading?: Snippet;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let { to, children, fallback, loading }: Props = $props();
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
{#if !$entitlements.ready}
|
|
39
|
+
{@render loading?.()}
|
|
40
|
+
{:else if $entitlements.can(to)}
|
|
41
|
+
{@render children()}
|
|
42
|
+
{:else}
|
|
43
|
+
{@render fallback?.()}
|
|
44
|
+
{/if}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
interface Props {
|
|
3
|
+
/** The plan feature's key, such as `analytics`. */
|
|
4
|
+
to: string;
|
|
5
|
+
/** Rendered when the plan has `to`. */
|
|
6
|
+
children: Snippet;
|
|
7
|
+
/** Rendered when Bridge has answered and the plan does not have `to`. */
|
|
8
|
+
fallback?: Snippet;
|
|
9
|
+
/** Rendered until Bridge has answered. Nothing by default. */
|
|
10
|
+
loading?: Snippet;
|
|
11
|
+
}
|
|
12
|
+
declare const Entitled: import("svelte").Component<Props, {}, "">;
|
|
13
|
+
type Entitled = ReturnType<typeof Entitled>;
|
|
14
|
+
export default Entitled;
|
|
@@ -3,17 +3,19 @@
|
|
|
3
3
|
|
|
4
4
|
<QuotaBanner metric="projects" label="Projects" />
|
|
5
5
|
|
|
6
|
-
Renders nothing
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
else is told to contact
|
|
6
|
+
Renders nothing while the metric has no limit or is below 80% of it. It moves
|
|
7
|
+
by itself: Bridge's quota notices read the numbers again. It shows numbers
|
|
8
|
+
only and blocks nothing: whether something is allowed is a flag. A person with
|
|
9
|
+
the `billingManage` flag gets an Upgrade link; anyone else is told to contact
|
|
10
|
+
the workspace owner. A metered limit shows the overage instead of a warning.
|
|
10
11
|
-->
|
|
11
12
|
<script lang="ts">
|
|
13
|
+
import type { QuotaSnapshot } from '@nebulr-group/bridge-auth-core';
|
|
12
14
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
13
15
|
import { peekConfig } from '../../core/client.js';
|
|
14
16
|
import { canManageBilling } from '../../core/flags.js';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
+
import { useQuota } from '../../core/quota.js';
|
|
18
|
+
import { quotaBannerCopy, quotaBannerState } from './quota-copy.js';
|
|
17
19
|
|
|
18
20
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
19
21
|
/** The metric to watch, such as `projects`. */
|
|
@@ -23,71 +25,54 @@
|
|
|
23
25
|
/** Where Upgrade goes. @default the subscription page */
|
|
24
26
|
actionHref?: string;
|
|
25
27
|
/** Replaces Upgrade's navigation. */
|
|
26
|
-
onActionClick?: (
|
|
28
|
+
onActionClick?: (snapshot: QuotaSnapshot) => void;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
let { metric, label, actionHref, onActionClick, class: className, style, ...rest }: Props = $props();
|
|
30
32
|
|
|
31
33
|
const base = peekConfig()?.billingBase ?? '/subscription';
|
|
32
34
|
const canManage = canManageBilling();
|
|
33
|
-
const
|
|
35
|
+
const quota = useQuota(() => metric);
|
|
34
36
|
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const
|
|
37
|
+
const snapshot = $derived(quota.snapshot);
|
|
38
|
+
const state = $derived(quotaBannerState(snapshot));
|
|
39
|
+
const critical = $derived(state === 'critical' || state === 'reached' || state === 'over');
|
|
40
|
+
const name = $derived(label ?? snapshot?.label ?? metric);
|
|
41
|
+
const percent = $derived(snapshot && snapshot.limit > 0 ? Math.min(100, Math.round((snapshot.used / snapshot.limit) * 100)) : 0);
|
|
39
42
|
const href = $derived(actionHref ?? (base || '/'));
|
|
40
|
-
const copy = $derived(
|
|
41
|
-
|
|
42
|
-
function copyFor(q: Quota, admin: boolean, what: string): { title: string; body: string } {
|
|
43
|
-
const used = q.used.toLocaleString();
|
|
44
|
-
const limit = q.limit.toLocaleString();
|
|
45
|
-
if (q.used > q.limit) {
|
|
46
|
-
return admin
|
|
47
|
-
? { title: `${what} over cap`, body: `You've used ${used} of ${limit}. Upgrade your plan to add headroom.` }
|
|
48
|
-
: { title: `${what} over cap`, body: `Your workspace is over its ${what} cap. ${OWNER}` };
|
|
49
|
-
}
|
|
50
|
-
if (q.state === 'over') {
|
|
51
|
-
return admin
|
|
52
|
-
? { title: `${what} limit reached`, body: `You've used all ${limit} on your plan. Upgrade your plan to add more.` }
|
|
53
|
-
: { title: `${what} limit reached`, body: `Your workspace has reached its ${what} limit. ${OWNER}` };
|
|
54
|
-
}
|
|
55
|
-
const left = Math.max(0, q.limit - q.used).toLocaleString();
|
|
56
|
-
return admin
|
|
57
|
-
? { title: `${what} approaching cap`, body: `You've used ${used} of ${limit} (${left} left).` }
|
|
58
|
-
: { title: `${what} approaching cap`, body: `Your workspace is approaching its ${what} cap.` };
|
|
59
|
-
}
|
|
43
|
+
const copy = $derived(quotaBannerCopy(snapshot, $canManage, name));
|
|
60
44
|
|
|
61
45
|
function handleAction(event: MouseEvent) {
|
|
62
|
-
if (!onActionClick || !
|
|
46
|
+
if (!onActionClick || !snapshot) return;
|
|
63
47
|
event.preventDefault();
|
|
64
|
-
onActionClick(
|
|
48
|
+
onActionClick(snapshot);
|
|
65
49
|
}
|
|
66
50
|
</script>
|
|
67
51
|
|
|
68
|
-
{#if
|
|
52
|
+
{#if state !== 'hidden' && snapshot}
|
|
69
53
|
<div
|
|
70
54
|
class={['bridge-quota-banner', className]}
|
|
71
55
|
{style}
|
|
72
56
|
data-bridge-quota-banner
|
|
73
57
|
data-metric={metric}
|
|
74
|
-
data-state={
|
|
75
|
-
|
|
76
|
-
|
|
58
|
+
data-state={state}
|
|
59
|
+
data-severity={critical ? 'critical' : 'warn'}
|
|
60
|
+
role={critical ? 'alert' : 'status'}
|
|
61
|
+
aria-live={critical ? 'assertive' : 'polite'}
|
|
77
62
|
aria-label={`${name} quota ${percent}% used`}
|
|
78
63
|
{...rest}
|
|
79
64
|
>
|
|
80
65
|
<div class="bqb-content">
|
|
81
66
|
<strong class="bqb-title">{copy.title}</strong>
|
|
82
67
|
<span class="bqb-body">{copy.body}</span>
|
|
83
|
-
{#if
|
|
68
|
+
{#if snapshot.limit > 0}
|
|
84
69
|
<div class="bqb-meter" aria-hidden="true">
|
|
85
70
|
<div class="bqb-meter-fill" style:width="{percent}%"></div>
|
|
86
71
|
</div>
|
|
87
72
|
{/if}
|
|
88
73
|
</div>
|
|
89
|
-
{#if $canManage}
|
|
90
|
-
<a class="bqb-cta" {href} data-bridge-quota-banner-cta onclick={handleAction}>
|
|
74
|
+
{#if copy.cta && $canManage}
|
|
75
|
+
<a class="bqb-cta" {href} data-bridge-quota-banner-cta onclick={handleAction}>{copy.cta}</a>
|
|
91
76
|
{/if}
|
|
92
77
|
</div>
|
|
93
78
|
{/if}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type { QuotaSnapshot } from '@nebulr-group/bridge-auth-core';
|
|
1
2
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
2
|
-
import { type Quota } from './quota.js';
|
|
3
3
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
4
4
|
/** The metric to watch, such as `projects`. */
|
|
5
5
|
metric: string;
|
|
@@ -8,7 +8,7 @@ interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
|
8
8
|
/** Where Upgrade goes. @default the subscription page */
|
|
9
9
|
actionHref?: string;
|
|
10
10
|
/** Replaces Upgrade's navigation. */
|
|
11
|
-
onActionClick?: (
|
|
11
|
+
onActionClick?: (snapshot: QuotaSnapshot) => void;
|
|
12
12
|
}
|
|
13
13
|
declare const QuotaBanner: import("svelte").Component<Props, {}, "">;
|
|
14
14
|
type QuotaBanner = ReturnType<typeof QuotaBanner>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
<QuotaGate metric="tickets">: the action inside is disabled once the workspace
|
|
3
|
+
is at its plan's hard limit, and an upgrade prompt shows beside it.
|
|
4
|
+
|
|
5
|
+
<QuotaGate metric="tickets">
|
|
6
|
+
<button onclick={createTicket}>New ticket</button>
|
|
7
|
+
{#snippet atLimit(quota)}
|
|
8
|
+
{quota.used} of {quota.limit} tickets used. <a href="/subscription">Upgrade</a>
|
|
9
|
+
{/snippet}
|
|
10
|
+
</QuotaGate>
|
|
11
|
+
|
|
12
|
+
Disabling is a `<fieldset disabled>` around the children, so every button,
|
|
13
|
+
input, select and textarea inside is disabled natively and announced as such,
|
|
14
|
+
with no props to thread through your markup. Links are not form controls: put
|
|
15
|
+
a link's action behind a button.
|
|
16
|
+
|
|
17
|
+
Never disables on "don't know yet": while the quota is loading, the plan sets
|
|
18
|
+
no limit on the metric, or the limit is metered (it bills the overage instead
|
|
19
|
+
of blocking), the children are enabled. Only a known hard limit with nothing
|
|
20
|
+
left disables them.
|
|
21
|
+
|
|
22
|
+
Decoration only. Your server refuses the write; this saves the person a click
|
|
23
|
+
that would be refused.
|
|
24
|
+
-->
|
|
25
|
+
<script lang="ts">
|
|
26
|
+
import type { Snippet } from 'svelte';
|
|
27
|
+
import { peekConfig } from '../../core/client.js';
|
|
28
|
+
import { useQuota, type QuotaState } from '../../core/quota.js';
|
|
29
|
+
|
|
30
|
+
interface Props {
|
|
31
|
+
/** The metric's key, such as `tickets`. */
|
|
32
|
+
metric: string;
|
|
33
|
+
/** The action or actions to disable at the limit. */
|
|
34
|
+
children: Snippet;
|
|
35
|
+
/** What to show at the limit instead of the default line. Receives the live quota. */
|
|
36
|
+
atLimit?: Snippet<[QuotaState]>;
|
|
37
|
+
/** Class on the wrapper. */
|
|
38
|
+
class?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let { metric, children, atLimit, class: className = '' }: Props = $props();
|
|
42
|
+
|
|
43
|
+
const quota = useQuota(() => metric);
|
|
44
|
+
const base = peekConfig()?.billingBase ?? '/subscription';
|
|
45
|
+
|
|
46
|
+
// 'loading' | 'unlimited' | 'metered' | 'available' | 'at-limit'
|
|
47
|
+
const gateState = $derived.by(() => {
|
|
48
|
+
if (quota.loading) return 'loading';
|
|
49
|
+
if (quota.unlimited) return 'unlimited';
|
|
50
|
+
if (quota.snapshot?.policy === 'metered') return 'metered';
|
|
51
|
+
const atCap = (quota.remaining !== null && quota.remaining <= 0) || (quota.used !== null && quota.limit !== null && quota.used >= quota.limit);
|
|
52
|
+
return atCap ? 'at-limit' : 'available';
|
|
53
|
+
});
|
|
54
|
+
const blocked = $derived(gateState === 'at-limit');
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<div class={['bridge-quota-gate', className]} data-bridge-quota-gate data-metric={metric} data-state={gateState}>
|
|
58
|
+
<fieldset disabled={blocked} class="bridge-quota-gate-controls" style="display: contents">
|
|
59
|
+
{@render children()}
|
|
60
|
+
</fieldset>
|
|
61
|
+
{#if blocked}
|
|
62
|
+
<div class="bridge-quota-gate-limit" data-bridge-quota-gate-limit role="status">
|
|
63
|
+
{#if atLimit}
|
|
64
|
+
{@render atLimit(quota)}
|
|
65
|
+
{:else}
|
|
66
|
+
You've used all {quota.limit?.toLocaleString()} {metric} on your plan.
|
|
67
|
+
<a href={base || '/'}>Upgrade</a>
|
|
68
|
+
{/if}
|
|
69
|
+
</div>
|
|
70
|
+
{/if}
|
|
71
|
+
</div>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import { type QuotaState } from '../../core/quota.js';
|
|
3
|
+
interface Props {
|
|
4
|
+
/** The metric's key, such as `tickets`. */
|
|
5
|
+
metric: string;
|
|
6
|
+
/** The action or actions to disable at the limit. */
|
|
7
|
+
children: Snippet;
|
|
8
|
+
/** What to show at the limit instead of the default line. Receives the live quota. */
|
|
9
|
+
atLimit?: Snippet<[QuotaState]>;
|
|
10
|
+
/** Class on the wrapper. */
|
|
11
|
+
class?: string;
|
|
12
|
+
}
|
|
13
|
+
declare const QuotaGate: import("svelte").Component<Props, {}, "">;
|
|
14
|
+
type QuotaGate = ReturnType<typeof QuotaGate>;
|
|
15
|
+
export default QuotaGate;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** What `QuotaBanner` says, as a pure decision: where a quota sits, and the words for it. */
|
|
2
|
+
import type { QuotaSnapshot } from '@nebulr-group/bridge-auth-core';
|
|
3
|
+
export type QuotaBannerState = 'hidden' | 'approaching' | 'critical' | 'reached' | 'over' | 'metered';
|
|
4
|
+
export interface QuotaBannerCopy {
|
|
5
|
+
readonly title: string;
|
|
6
|
+
readonly body: string;
|
|
7
|
+
readonly cta?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Where a snapshot sits. A metered quota bills instead of blocking and keeps its own words; exactly at a hard limit is `reached`, not "approaching". */
|
|
10
|
+
export declare function quotaBannerState(snapshot: QuotaSnapshot | undefined | null): QuotaBannerState;
|
|
11
|
+
/** An amount of money as `$1.00` or `1.00 SEK`. */
|
|
12
|
+
export declare function formatCost(amount: number | undefined, currency: string | undefined): string;
|
|
13
|
+
export declare function quotaBannerCopy(snapshot: QuotaSnapshot | undefined | null, admin: boolean, label: string): QuotaBannerCopy;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { warningOf } from '../../core/quota.js';
|
|
2
|
+
const OWNER = 'Contact your workspace owner.';
|
|
3
|
+
/** Where a snapshot sits. A metered quota bills instead of blocking and keeps its own words; exactly at a hard limit is `reached`, not "approaching". */
|
|
4
|
+
export function quotaBannerState(snapshot) {
|
|
5
|
+
if (snapshot === undefined || snapshot === null)
|
|
6
|
+
return 'hidden';
|
|
7
|
+
const level = warningOf(snapshot);
|
|
8
|
+
if (snapshot.policy === 'metered')
|
|
9
|
+
return level !== null || (snapshot.overcap ?? snapshot.used > snapshot.limit) ? 'metered' : 'hidden';
|
|
10
|
+
if (snapshot.limit > 0 && snapshot.used > snapshot.limit)
|
|
11
|
+
return 'over';
|
|
12
|
+
if (snapshot.limit > 0 && snapshot.used >= snapshot.limit)
|
|
13
|
+
return 'reached';
|
|
14
|
+
return level === 'critical' ? 'critical' : level === 'approaching' ? 'approaching' : 'hidden';
|
|
15
|
+
}
|
|
16
|
+
/** An amount of money as `$1.00` or `1.00 SEK`. */
|
|
17
|
+
export function formatCost(amount, currency) {
|
|
18
|
+
if (amount === undefined)
|
|
19
|
+
return '';
|
|
20
|
+
const code = (currency ?? '').toUpperCase();
|
|
21
|
+
try {
|
|
22
|
+
return new Intl.NumberFormat(undefined, { style: 'currency', currency: code || 'USD' }).format(amount);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return `${amount.toFixed(2)}${code ? ` ${code}` : ''}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function memberBody(label, state) {
|
|
29
|
+
switch (state) {
|
|
30
|
+
case 'over':
|
|
31
|
+
return `Your workspace is over its ${label} cap. ${OWNER}`;
|
|
32
|
+
case 'reached':
|
|
33
|
+
return `Your workspace has reached its ${label} limit. ${OWNER}`;
|
|
34
|
+
case 'critical':
|
|
35
|
+
return `Your workspace is approaching its ${label} cap. ${OWNER}`;
|
|
36
|
+
case 'approaching':
|
|
37
|
+
return `Your workspace is approaching its ${label} cap.`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function quotaBannerCopy(snapshot, admin, label) {
|
|
41
|
+
const state = quotaBannerState(snapshot);
|
|
42
|
+
if (snapshot === undefined || snapshot === null || state === 'hidden')
|
|
43
|
+
return { title: '', body: '' };
|
|
44
|
+
if (state === 'metered') {
|
|
45
|
+
const over = snapshot.limit > 0 ? Math.max(0, snapshot.used - snapshot.limit) : snapshot.used;
|
|
46
|
+
const cost = formatCost(snapshot.overageEstimate, snapshot.currency);
|
|
47
|
+
const estimate = cost ? ` · ~${cost} estimated this period` : '';
|
|
48
|
+
if (snapshot.limit > 0) {
|
|
49
|
+
if (over > 0)
|
|
50
|
+
return { title: `${label} overage`, body: `${over.toLocaleString()} over your ${snapshot.limit.toLocaleString()} included${estimate}.` };
|
|
51
|
+
const unit = formatCost(snapshot.unitAmount, snapshot.currency);
|
|
52
|
+
return {
|
|
53
|
+
title: `${label} approaching included limit`,
|
|
54
|
+
body: `You've used ${snapshot.used.toLocaleString()} of ${snapshot.limit.toLocaleString()} included${unit ? ` — extra usage is billed at ${unit}/unit` : ''}.`,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return { title: `${label} usage`, body: `${snapshot.used.toLocaleString()} ${label}${estimate}.` };
|
|
58
|
+
}
|
|
59
|
+
const used = snapshot.used.toLocaleString();
|
|
60
|
+
const limit = snapshot.limit.toLocaleString();
|
|
61
|
+
const left = Math.max(0, snapshot.remaining).toLocaleString();
|
|
62
|
+
switch (state) {
|
|
63
|
+
case 'over':
|
|
64
|
+
return admin
|
|
65
|
+
? { title: `${label} over cap`, body: `You've used ${used} of ${limit}. Upgrade your plan to add headroom.`, cta: 'Upgrade' }
|
|
66
|
+
: { title: `${label} over cap`, body: memberBody(label, 'over') };
|
|
67
|
+
case 'reached':
|
|
68
|
+
return admin
|
|
69
|
+
? { title: `${label} limit reached`, body: `You've used all ${limit} on your plan. Upgrade your plan to add more.`, cta: 'Upgrade' }
|
|
70
|
+
: { title: `${label} limit reached`, body: memberBody(label, 'reached') };
|
|
71
|
+
case 'critical':
|
|
72
|
+
return admin
|
|
73
|
+
? { title: `${label} near cap`, body: `You've used ${used} of ${limit} (${left} left). Upgrade to avoid hitting the cap.`, cta: 'Upgrade' }
|
|
74
|
+
: { title: `${label} near cap`, body: memberBody(label, 'critical') };
|
|
75
|
+
default:
|
|
76
|
+
return admin
|
|
77
|
+
? { title: `${label} approaching cap`, body: `You've used ${used} of ${limit} (${left} left).`, cta: 'Upgrade' }
|
|
78
|
+
: { title: `${label} approaching cap`, body: memberBody(label, 'approaching') };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the workspace is locked for payment, as a store. The lock is Bridge's: a locked workspace can be read but not
|
|
3
|
+
* changed. It is read when the page starts and again when Bridge says access changed, and a 402 for the lock that your
|
|
4
|
+
* own API answers through `bridgeFetch` shows it at once.
|
|
5
|
+
*/
|
|
6
|
+
import type { BillingGateState } from '@nebulr-group/bridge-auth-core';
|
|
7
|
+
import { type Readable } from 'svelte/store';
|
|
8
|
+
/** The lock: `{ locked, severity, noticeState, recoveryUrl }`. Not locked until Bridge says so. */
|
|
9
|
+
export declare const billingGate: Readable<BillingGateState>;
|
|
10
|
+
/** Whether the workspace is locked for payment, as a store. */
|
|
11
|
+
export declare const billingLocked: Readable<boolean>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { derived } from 'svelte/store';
|
|
2
|
+
import { bridgeClient } from './client.js';
|
|
3
|
+
const OPEN = Object.freeze({ locked: false, severity: 'info', noticeState: 'active' });
|
|
4
|
+
/** The lock: `{ locked, severity, noticeState, recoveryUrl }`. Not locked until Bridge says so. */
|
|
5
|
+
export const billingGate = derived(bridgeClient, (client, set) => {
|
|
6
|
+
if (client === null) {
|
|
7
|
+
set(OPEN);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
set(client.gateState());
|
|
11
|
+
return client.on('gate-changed', set);
|
|
12
|
+
}, OPEN);
|
|
13
|
+
/** Whether the workspace is locked for payment, as a store. */
|
|
14
|
+
export const billingLocked = derived(billingGate, (gate) => gate.locked);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `entitlements`: which plan features the workspace has, as one store.
|
|
3
|
+
*
|
|
4
|
+
* ```svelte
|
|
5
|
+
* {#if !$entitlements.ready}…{:else if $entitlements.can('analytics')}<Analytics />{:else}Upgrade for analytics{/if}
|
|
6
|
+
* ```
|
|
7
|
+
*
|
|
8
|
+
* A plan feature is the flag `bridge:billing.entitlement.<key>`: `can('analytics')` reads that flag, never the plan, so
|
|
9
|
+
* a rule on the flag (a rollout, a privilege) is honoured. It is fail-closed: `false` until Bridge has answered and for a
|
|
10
|
+
* feature the plan does not have, and `ready` tells those two apart. It moves when the flags do.
|
|
11
|
+
*/
|
|
12
|
+
import { type Readable } from 'svelte/store';
|
|
13
|
+
export interface EntitlementsState {
|
|
14
|
+
/** True once Bridge has answered. Before that every `can()` is `false`. */
|
|
15
|
+
readonly ready: boolean;
|
|
16
|
+
/** `true` only when the plan has the feature `key` on. */
|
|
17
|
+
can(key: string): boolean;
|
|
18
|
+
/** Every plan feature Bridge gave a result for, `{ key: on }`. */
|
|
19
|
+
readonly all: Readonly<Record<string, boolean>>;
|
|
20
|
+
}
|
|
21
|
+
export declare const entitlements: Readable<EntitlementsState>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `entitlements`: which plan features the workspace has, as one store.
|
|
3
|
+
*
|
|
4
|
+
* ```svelte
|
|
5
|
+
* {#if !$entitlements.ready}…{:else if $entitlements.can('analytics')}<Analytics />{:else}Upgrade for analytics{/if}
|
|
6
|
+
* ```
|
|
7
|
+
*
|
|
8
|
+
* A plan feature is the flag `bridge:billing.entitlement.<key>`: `can('analytics')` reads that flag, never the plan, so
|
|
9
|
+
* a rule on the flag (a rollout, a privilege) is honoured. It is fail-closed: `false` until Bridge has answered and for a
|
|
10
|
+
* feature the plan does not have, and `ready` tells those two apart. It moves when the flags do.
|
|
11
|
+
*/
|
|
12
|
+
import { derived } from 'svelte/store';
|
|
13
|
+
import { bridgeClient } from './client.js';
|
|
14
|
+
import { flags, flagsStatus } from './stores.js';
|
|
15
|
+
const NOT_READY = Object.freeze({ ready: false, all: Object.freeze({}), can: () => false });
|
|
16
|
+
export const entitlements = derived([bridgeClient, flags, flagsStatus], ([client, , status]) => {
|
|
17
|
+
if (client === null || status !== 'ready')
|
|
18
|
+
return NOT_READY;
|
|
19
|
+
const all = Object.freeze(client.entitlements.all());
|
|
20
|
+
return Object.freeze({ ready: true, all, can: (key) => client.entitlements.can(key) });
|
|
21
|
+
});
|
package/dist/core/fetch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getBridge, peekConfig } from './client.js';
|
|
2
|
-
import { openUpgrade, readPaymentRequired } from './upgrade.js';
|
|
2
|
+
import { openUpgrade, parseQuotaRefusal, readPaymentRequired, reportQuotaRefusal } from './upgrade.js';
|
|
3
3
|
function originOf(url) {
|
|
4
4
|
try {
|
|
5
5
|
return new URL(url, typeof location === 'undefined' ? undefined : location.href).origin;
|
|
@@ -39,9 +39,13 @@ export async function bridgeFetch(input, init) {
|
|
|
39
39
|
const requested = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
|
|
40
40
|
if (response.status === 402 && mayExplain(bridge, requested, response)) {
|
|
41
41
|
try {
|
|
42
|
-
const
|
|
42
|
+
const body = await response.clone().json();
|
|
43
|
+
const request = readPaymentRequired(body);
|
|
43
44
|
if (request !== null)
|
|
44
45
|
openUpgrade(request);
|
|
46
|
+
const refusal = parseQuotaRefusal(body, response.url !== '' ? response.url : requested);
|
|
47
|
+
if (refusal !== null)
|
|
48
|
+
reportQuotaRefusal(refusal);
|
|
45
49
|
}
|
|
46
50
|
catch {
|
|
47
51
|
// Not JSON: not a refusal the dialog can explain.
|
package/dist/core/guard.d.ts
CHANGED
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
*
|
|
7
7
|
* It fails closed: while Bridge cannot be reached, a protected page sends
|
|
8
8
|
* the visitor to sign-in, and a flag that has no result is off.
|
|
9
|
+
*
|
|
10
|
+
* The paywall is for a workspace that has no plan yet. A workspace locked for
|
|
11
|
+
* payment keeps its page: it can still be read, and the lock screen covers it
|
|
12
|
+
* where the person is (`<BillingNotice mode="hard">`), as in 0.9.
|
|
9
13
|
*/
|
|
10
14
|
import { type SessionState } from '@nebulr-group/bridge-auth-core';
|
|
11
15
|
import type { FlagRequirement, ResolvedConfig, RouteRule } from './config.js';
|
|
@@ -46,6 +50,8 @@ export interface GuardInput {
|
|
|
46
50
|
readonly flags: FlagReader;
|
|
47
51
|
/** The return address already on the URL, on an auth page. */
|
|
48
52
|
readonly returnTo: string | null;
|
|
53
|
+
/** Whether the workspace is locked for payment: then the page stays and shows the lock, rather than the paywall. */
|
|
54
|
+
readonly locked?: boolean;
|
|
49
55
|
}
|
|
50
56
|
/** Decides one navigation. */
|
|
51
57
|
export declare function decide(config: ResolvedConfig, given: GuardInput): GuardDecision;
|
package/dist/core/guard.js
CHANGED
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
*
|
|
7
7
|
* It fails closed: while Bridge cannot be reached, a protected page sends
|
|
8
8
|
* the visitor to sign-in, and a flag that has no result is off.
|
|
9
|
+
*
|
|
10
|
+
* The paywall is for a workspace that has no plan yet. A workspace locked for
|
|
11
|
+
* payment keeps its page: it can still be read, and the lock screen covers it
|
|
12
|
+
* where the person is (`<BillingNotice mode="hard">`), as in 0.9.
|
|
9
13
|
*/
|
|
10
14
|
import { sanitizeReturnTo, withReturnTo } from '@nebulr-group/bridge-auth-core';
|
|
11
15
|
import { authPageAt, isUnder } from './routes.js';
|
|
@@ -78,7 +82,7 @@ export function decide(config, given) {
|
|
|
78
82
|
return { type: 'redirect', to, reason: 'flag' };
|
|
79
83
|
}
|
|
80
84
|
const billingPage = isUnder(input.path, config.billingBase);
|
|
81
|
-
if (signedIn && !isPublic && !billingPage && config.paywall !== null && !input.flags.enabled(config.flagKeys.appActive)) {
|
|
85
|
+
if (signedIn && !isPublic && !billingPage && config.paywall !== null && input.locked !== true && !input.flags.enabled(config.flagKeys.appActive)) {
|
|
82
86
|
if (config.paywall === input.path)
|
|
83
87
|
return { type: 'allow' };
|
|
84
88
|
return { type: 'redirect', to: withReturnTo(config.paywall, input.target, config.returnToParam), reason: 'paywall' };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One metric's quota against the plan, for your own UI. The numbers are
|
|
3
|
+
* auth-core's quota store: one read when first asked for, then again when
|
|
4
|
+
* Bridge says a quota or the plan changed.
|
|
5
|
+
*
|
|
6
|
+
* ```svelte
|
|
7
|
+
* <script lang="ts">
|
|
8
|
+
* import { useQuota } from '@nebulr-group/bridge-svelte';
|
|
9
|
+
* const projects = useQuota('projects');
|
|
10
|
+
* </script>
|
|
11
|
+
*
|
|
12
|
+
* {#if projects.loading}…{:else if projects.unlimited}Unlimited{:else}{projects.used} of {projects.limit}{/if}
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* `useQuota` is also a store, `$projects`, with the numbers only: `{ used, limit, ratio, state }`, or null while loading
|
|
16
|
+
* and for a metric without a limit.
|
|
17
|
+
*
|
|
18
|
+
* Numbers only, never a gate: whether something is allowed is a flag, and your server refuses the write.
|
|
19
|
+
*/
|
|
20
|
+
import type { QuotaSnapshot } from '@nebulr-group/bridge-auth-core';
|
|
21
|
+
import { type Readable } from 'svelte/store';
|
|
22
|
+
/** How near a metric is to its limit: `near` from 80%, `over` once the limit is reached. */
|
|
23
|
+
export type QuotaLevel = 'ok' | 'near' | 'over';
|
|
24
|
+
/** The numbers of a quota, as the store `$useQuota(metric)` holds them. */
|
|
25
|
+
export interface Quota {
|
|
26
|
+
readonly used: number;
|
|
27
|
+
readonly limit: number;
|
|
28
|
+
/** `used / limit`; 1 for a limit of 0. */
|
|
29
|
+
readonly ratio: number;
|
|
30
|
+
readonly state: QuotaLevel;
|
|
31
|
+
}
|
|
32
|
+
/** What `useQuota(metric)` reads. */
|
|
33
|
+
export interface QuotaState {
|
|
34
|
+
/** True until Bridge has answered for this metric. The numbers are `null` meanwhile, never 0. */
|
|
35
|
+
readonly loading: boolean;
|
|
36
|
+
/** True once Bridge has answered that the plan puts no limit on the metric. */
|
|
37
|
+
readonly unlimited: boolean;
|
|
38
|
+
/** `counter`: this period's total. `gauge`: how many exist now. `null` while loading or unlimited. */
|
|
39
|
+
readonly used: number | null;
|
|
40
|
+
/** The plan's limit. `null` while loading or unlimited. */
|
|
41
|
+
readonly limit: number | null;
|
|
42
|
+
/** What is left before the limit. `null` while loading or unlimited. */
|
|
43
|
+
readonly remaining: number | null;
|
|
44
|
+
/** `approaching` from 80%, `critical` from 95%; `null` below that, while loading or unlimited. */
|
|
45
|
+
readonly warningLevel: 'approaching' | 'critical' | null;
|
|
46
|
+
/** `counter` counts up from your server's usage reports and resets each period; `gauge` is the level your server sets. */
|
|
47
|
+
readonly kind: 'counter' | 'gauge' | null;
|
|
48
|
+
/** The whole snapshot (policy, overage), or `null` while loading or unlimited. */
|
|
49
|
+
readonly snapshot: QuotaSnapshot | null;
|
|
50
|
+
}
|
|
51
|
+
/** `useQuota(metric)`: the state as properties, and the numbers as a store. */
|
|
52
|
+
export type QuotaHandle = QuotaState & Readable<Quota | null>;
|
|
53
|
+
/** From this share of the limit, a metric is `near` it. */
|
|
54
|
+
export declare const NEAR_LIMIT = 0.8;
|
|
55
|
+
/** A snapshot's numbers for the store. */
|
|
56
|
+
export declare function quotaOf(snapshot: QuotaSnapshot): Quota;
|
|
57
|
+
/**
|
|
58
|
+
* How near a hard limit a snapshot is: the server's level, or from the numbers when it sent none. The same 80% and 95%
|
|
59
|
+
* the server uses.
|
|
60
|
+
*/
|
|
61
|
+
export declare function warningOf(snapshot: QuotaSnapshot): 'approaching' | 'critical' | null;
|
|
62
|
+
/**
|
|
63
|
+
* Live numbers for one metric, as properties for a component or a `$derived`, and as the store `$projects`.
|
|
64
|
+
*
|
|
65
|
+
* @param metric The metric's key, such as `projects`. Pass `() => metric` when it changes, such as a prop.
|
|
66
|
+
*/
|
|
67
|
+
export declare function useQuota(metric: string | (() => string)): QuotaHandle;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { createSubscriber } from 'svelte/reactivity';
|
|
2
|
+
import { get, readable } from 'svelte/store';
|
|
3
|
+
import { bridgeClient } from './client.js';
|
|
4
|
+
/** From this share of the limit, a metric is `near` it. */
|
|
5
|
+
export const NEAR_LIMIT = 0.8;
|
|
6
|
+
const CRITICAL = 0.95;
|
|
7
|
+
/** A snapshot's numbers for the store. */
|
|
8
|
+
export function quotaOf(snapshot) {
|
|
9
|
+
const { used, limit } = snapshot;
|
|
10
|
+
const ratio = limit > 0 ? used / limit : 1;
|
|
11
|
+
return { used, limit, ratio, state: used >= limit ? 'over' : ratio >= NEAR_LIMIT ? 'near' : 'ok' };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* How near a hard limit a snapshot is: the server's level, or from the numbers when it sent none. The same 80% and 95%
|
|
15
|
+
* the server uses.
|
|
16
|
+
*/
|
|
17
|
+
export function warningOf(snapshot) {
|
|
18
|
+
if (snapshot.warningLevel !== null)
|
|
19
|
+
return snapshot.warningLevel;
|
|
20
|
+
if (snapshot.limit <= 0)
|
|
21
|
+
return null;
|
|
22
|
+
const ratio = snapshot.used / snapshot.limit;
|
|
23
|
+
return ratio >= CRITICAL ? 'critical' : ratio >= NEAR_LIMIT ? 'approaching' : null;
|
|
24
|
+
}
|
|
25
|
+
const tracked = new WeakMap();
|
|
26
|
+
function trackingOf(client) {
|
|
27
|
+
let t = tracked.get(client);
|
|
28
|
+
if (t === undefined) {
|
|
29
|
+
const made = { unlimited: new Set(), workspace: undefined };
|
|
30
|
+
t = made;
|
|
31
|
+
tracked.set(client, made);
|
|
32
|
+
client.quotas.subscribe((metric, snapshot) => {
|
|
33
|
+
if (snapshot === undefined)
|
|
34
|
+
made.unlimited.add(metric);
|
|
35
|
+
else
|
|
36
|
+
made.unlimited.delete(metric);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return t;
|
|
40
|
+
}
|
|
41
|
+
function noteSession(t, key) {
|
|
42
|
+
// Another person or workspace has another plan, and signing out ends the answers: what was "unlimited" no longer holds.
|
|
43
|
+
if (t.workspace !== undefined && key !== t.workspace)
|
|
44
|
+
t.unlimited.clear();
|
|
45
|
+
t.workspace = key;
|
|
46
|
+
}
|
|
47
|
+
const LOADING = Object.freeze({ loading: true, unlimited: false, used: null, limit: null, remaining: null, warningLevel: null, kind: null, snapshot: null });
|
|
48
|
+
const UNLIMITED = Object.freeze({ ...LOADING, loading: false, unlimited: true });
|
|
49
|
+
function readState(metric) {
|
|
50
|
+
const client = get(bridgeClient);
|
|
51
|
+
if (client === null)
|
|
52
|
+
return LOADING;
|
|
53
|
+
const t = trackingOf(client);
|
|
54
|
+
// The first read starts the request; the store dedupes one in flight.
|
|
55
|
+
const snapshot = t.unlimited.has(metric) ? client.quotas.get(metric) : client.quota(metric);
|
|
56
|
+
if (snapshot !== undefined) {
|
|
57
|
+
return { loading: false, unlimited: false, used: snapshot.used, limit: snapshot.limit, remaining: snapshot.remaining, warningLevel: warningOf(snapshot), kind: snapshot.kind === 'gauge' ? 'gauge' : 'counter', snapshot };
|
|
58
|
+
}
|
|
59
|
+
return t.unlimited.has(metric) ? UNLIMITED : LOADING;
|
|
60
|
+
}
|
|
61
|
+
function quotaStore(key) {
|
|
62
|
+
return readable(null, (set) => {
|
|
63
|
+
let stopQuota;
|
|
64
|
+
const stopClient = bridgeClient.subscribe((client) => {
|
|
65
|
+
stopQuota?.();
|
|
66
|
+
stopQuota = undefined;
|
|
67
|
+
if (client === null) {
|
|
68
|
+
set(null);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const push = () => {
|
|
72
|
+
const snapshot = client.quotas.get(key());
|
|
73
|
+
set(snapshot === undefined ? null : quotaOf(snapshot));
|
|
74
|
+
};
|
|
75
|
+
stopQuota = client.quotas.subscribe((metric) => {
|
|
76
|
+
if (metric === key())
|
|
77
|
+
push();
|
|
78
|
+
});
|
|
79
|
+
// Not for a metric already answered as unlimited: the store would read it again for every reader.
|
|
80
|
+
if (!trackingOf(client).unlimited.has(key()))
|
|
81
|
+
client.quota(key());
|
|
82
|
+
push();
|
|
83
|
+
});
|
|
84
|
+
return () => {
|
|
85
|
+
stopClient();
|
|
86
|
+
stopQuota?.();
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Live numbers for one metric, as properties for a component or a `$derived`, and as the store `$projects`.
|
|
92
|
+
*
|
|
93
|
+
* @param metric The metric's key, such as `projects`. Pass `() => metric` when it changes, such as a prop.
|
|
94
|
+
*/
|
|
95
|
+
export function useQuota(metric) {
|
|
96
|
+
const key = typeof metric === 'function' ? metric : () => metric;
|
|
97
|
+
const subscribe = createSubscriber((update) => {
|
|
98
|
+
let stopQuota;
|
|
99
|
+
let stopSession;
|
|
100
|
+
const stopClient = bridgeClient.subscribe((client) => {
|
|
101
|
+
stopQuota?.();
|
|
102
|
+
stopSession?.();
|
|
103
|
+
stopQuota = stopSession = undefined;
|
|
104
|
+
if (client !== null) {
|
|
105
|
+
const t = trackingOf(client);
|
|
106
|
+
stopQuota = client.quotas.subscribe((m) => {
|
|
107
|
+
if (m === key())
|
|
108
|
+
update();
|
|
109
|
+
});
|
|
110
|
+
stopSession = client.session.subscribe((s) => {
|
|
111
|
+
noteSession(t, s.status === 'signed-in' ? `${s.user.id}|${s.workspace?.id ?? ''}` : null);
|
|
112
|
+
// A person signing in is what lets a read made before it start.
|
|
113
|
+
queueMicrotask(update);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
update();
|
|
117
|
+
});
|
|
118
|
+
return () => {
|
|
119
|
+
stopClient();
|
|
120
|
+
stopQuota?.();
|
|
121
|
+
stopSession?.();
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
const current = () => {
|
|
125
|
+
subscribe();
|
|
126
|
+
return readState(key());
|
|
127
|
+
};
|
|
128
|
+
const store = quotaStore(key);
|
|
129
|
+
return {
|
|
130
|
+
subscribe: store.subscribe,
|
|
131
|
+
get loading() {
|
|
132
|
+
return current().loading;
|
|
133
|
+
},
|
|
134
|
+
get unlimited() {
|
|
135
|
+
return current().unlimited;
|
|
136
|
+
},
|
|
137
|
+
get used() {
|
|
138
|
+
return current().used;
|
|
139
|
+
},
|
|
140
|
+
get limit() {
|
|
141
|
+
return current().limit;
|
|
142
|
+
},
|
|
143
|
+
get remaining() {
|
|
144
|
+
return current().remaining;
|
|
145
|
+
},
|
|
146
|
+
get warningLevel() {
|
|
147
|
+
return current().warningLevel;
|
|
148
|
+
},
|
|
149
|
+
get kind() {
|
|
150
|
+
return current().kind;
|
|
151
|
+
},
|
|
152
|
+
get snapshot() {
|
|
153
|
+
return current().snapshot;
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
package/dist/core/upgrade.d.ts
CHANGED
|
@@ -40,3 +40,29 @@ export declare function closeUpgrade(): void;
|
|
|
40
40
|
* feature, fix }` for a flag that is off because the plan lacks a feature.
|
|
41
41
|
*/
|
|
42
42
|
export declare function readPaymentRequired(body: unknown): UpgradeRequest | null;
|
|
43
|
+
/** A plan limit your API refused: the 402 `QUOTA_EXCEEDED` answer, for code that wants to show something other than the dialog. */
|
|
44
|
+
export interface BridgeQuotaRefusal {
|
|
45
|
+
/** The metric the plan ran out of, such as `tickets`. */
|
|
46
|
+
readonly metric: string;
|
|
47
|
+
/** How much was used when the request was refused, if the API said. */
|
|
48
|
+
readonly used: number | null;
|
|
49
|
+
/** The plan's limit, if the API said. */
|
|
50
|
+
readonly limit: number | null;
|
|
51
|
+
/** Where the refusal said to upgrade, when it named a page of this app; else null. */
|
|
52
|
+
readonly fix: string | null;
|
|
53
|
+
/** The API's own words, if it sent some. */
|
|
54
|
+
readonly message: string | null;
|
|
55
|
+
/** The address of the request that was refused. */
|
|
56
|
+
readonly url: string;
|
|
57
|
+
}
|
|
58
|
+
/** The refusal in a 402 body, or null when the body is not a plan limit (a declined card, a missing plan feature). */
|
|
59
|
+
export declare function parseQuotaRefusal(body: unknown, url?: string): BridgeQuotaRefusal | null;
|
|
60
|
+
type QuotaListener = (refusal: BridgeQuotaRefusal) => void;
|
|
61
|
+
/**
|
|
62
|
+
* Runs `handler` each time your API refuses a request at a plan limit, through `bridgeFetch`: for an app that turned the
|
|
63
|
+
* dialog off (`billing.upgradeDialog: false`) and shows its own. Returns how to stop.
|
|
64
|
+
*/
|
|
65
|
+
export declare function onBridgeQuotaExceeded(handler: QuotaListener): () => void;
|
|
66
|
+
/** Tells the listeners of one refusal. A broken listener stops neither the others nor the dialog. */
|
|
67
|
+
export declare function reportQuotaRefusal(refusal: BridgeQuotaRefusal): void;
|
|
68
|
+
export {};
|
package/dist/core/upgrade.js
CHANGED
|
@@ -50,3 +50,32 @@ export function readPaymentRequired(body) {
|
|
|
50
50
|
}
|
|
51
51
|
return null;
|
|
52
52
|
}
|
|
53
|
+
/** The refusal in a 402 body, or null when the body is not a plan limit (a declined card, a missing plan feature). */
|
|
54
|
+
export function parseQuotaRefusal(body, url = '') {
|
|
55
|
+
const request = readPaymentRequired(body);
|
|
56
|
+
if (request === null || request.kind !== 'limit' || request.metric === null || request.metric === undefined)
|
|
57
|
+
return null;
|
|
58
|
+
return { metric: request.metric, used: request.used ?? null, limit: request.limit ?? null, fix: request.upgradeHref ?? null, message: request.message ?? null, url };
|
|
59
|
+
}
|
|
60
|
+
const listeners = new Set();
|
|
61
|
+
/**
|
|
62
|
+
* Runs `handler` each time your API refuses a request at a plan limit, through `bridgeFetch`: for an app that turned the
|
|
63
|
+
* dialog off (`billing.upgradeDialog: false`) and shows its own. Returns how to stop.
|
|
64
|
+
*/
|
|
65
|
+
export function onBridgeQuotaExceeded(handler) {
|
|
66
|
+
listeners.add(handler);
|
|
67
|
+
return () => {
|
|
68
|
+
listeners.delete(handler);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** Tells the listeners of one refusal. A broken listener stops neither the others nor the dialog. */
|
|
72
|
+
export function reportQuotaRefusal(refusal) {
|
|
73
|
+
for (const listener of [...listeners]) {
|
|
74
|
+
try {
|
|
75
|
+
listener(refusal);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// One listener's mistake is its own.
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -16,10 +16,15 @@ export { decide, findRule, flagsAllow } from './core/guard.js';
|
|
|
16
16
|
export type { GuardDecision, GuardInput } from './core/guard.js';
|
|
17
17
|
export { billing, flags, flagsStatus, liveStatus, me, plans, resource, session, signedIn, usage, user, workspace, workspaces } from './core/stores.js';
|
|
18
18
|
export type { Resource, ResourceStore } from './core/stores.js';
|
|
19
|
+
export { entitlements } from './core/entitlements.js';
|
|
20
|
+
export type { EntitlementsState } from './core/entitlements.js';
|
|
21
|
+
export { billingGate, billingLocked } from './core/billing-gate.js';
|
|
22
|
+
export { useQuota } from './core/quota.js';
|
|
23
|
+
export type { Quota, QuotaHandle, QuotaLevel, QuotaState } from './core/quota.js';
|
|
19
24
|
export { canManageBilling, canManageTeam, flag, flagReason, requireFlag, useFlag } from './core/flags.js';
|
|
20
25
|
export { bridgeFetch } from './core/fetch.js';
|
|
21
|
-
export { closeUpgrade, openUpgrade, readPaymentRequired, upgradeRequest } from './core/upgrade.js';
|
|
22
|
-
export type { UpgradeDialogProps, UpgradeRequest } from './core/upgrade.js';
|
|
26
|
+
export { closeUpgrade, onBridgeQuotaExceeded, openUpgrade, parseQuotaRefusal, readPaymentRequired, upgradeRequest } from './core/upgrade.js';
|
|
27
|
+
export type { BridgeQuotaRefusal, UpgradeDialogProps, UpgradeRequest } from './core/upgrade.js';
|
|
23
28
|
export { cancelRecheck, confirmItIsYou, recheckRequest, withRecheck } from './core/recheck.js';
|
|
24
29
|
export type { RecheckRequest } from './core/recheck.js';
|
|
25
30
|
export { handoffSignIn, readHandoff, returnToProgram } from './core/handoff.js';
|
|
@@ -28,7 +33,7 @@ export { errorMessage, translator } from './core/i18n.js';
|
|
|
28
33
|
export type { BridgeMessageKey, BridgeTranslator } from './core/i18n.js';
|
|
29
34
|
export type { PluginMessageKey } from './core/messages.js';
|
|
30
35
|
export { BridgeError, isBridgeError, needsRecheck, STANDARD_FLAGS } from '@nebulr-group/bridge-auth-core';
|
|
31
|
-
export type { Bridge, BridgeErrorKind, RecheckMethod, SessionState, SessionUser, SessionWorkspace } from '@nebulr-group/bridge-auth-core';
|
|
36
|
+
export type { BillingGateState, Bridge, BridgeErrorKind, QuotaSnapshot, RecheckMethod, SessionState, SessionUser, SessionWorkspace } from '@nebulr-group/bridge-auth-core';
|
|
32
37
|
export { default as BridgeBootstrap } from './components/BridgeBootstrap.svelte';
|
|
33
38
|
export { default as BridgeAuthRoutes } from './components/auth/BridgeAuthRoutes.svelte';
|
|
34
39
|
export { default as SignInForm } from './components/auth/SignInForm.svelte';
|
|
@@ -50,6 +55,7 @@ export { default as ProfileName } from './components/auth/ProfileName.svelte';
|
|
|
50
55
|
export { default as FeatureFlag } from './components/flags/FeatureFlag.svelte';
|
|
51
56
|
export type { FeatureFlagOffInfo } from './components/flags/FeatureFlag.svelte';
|
|
52
57
|
export { default as UpgradeDialog } from './components/billing/UpgradeDialog.svelte';
|
|
58
|
+
export { default as BridgeUpgradeDialog } from './components/billing/UpgradeDialog.svelte';
|
|
53
59
|
export { default as LiveStatusBadge } from './components/live/LiveStatusBadge.svelte';
|
|
54
60
|
export { default as TeamPanel } from './components/team/TeamPanel.svelte';
|
|
55
61
|
export { default as MemberList } from './components/team/MemberList.svelte';
|
|
@@ -64,7 +70,9 @@ export { default as PlanPicker } from './components/billing/PlanPicker.svelte';
|
|
|
64
70
|
export { default as BillingPortalButton } from './components/billing/BillingPortalButton.svelte';
|
|
65
71
|
export { default as SubscriptionStatus } from './components/billing/SubscriptionStatus.svelte';
|
|
66
72
|
export { default as BillingNotice } from './components/billing/BillingNotice.svelte';
|
|
73
|
+
export { default as BridgeBillingNotice } from './components/billing/BillingNotice.svelte';
|
|
67
74
|
export { default as QuotaBanner } from './components/billing/QuotaBanner.svelte';
|
|
75
|
+
export { default as BridgeQuotaBanner } from './components/billing/QuotaBanner.svelte';
|
|
76
|
+
export { default as QuotaGate } from './components/billing/QuotaGate.svelte';
|
|
77
|
+
export { default as Entitled } from './components/billing/Entitled.svelte';
|
|
68
78
|
export { default as CheckoutNotice } from './components/billing/CheckoutNotice.svelte';
|
|
69
|
-
export { useQuota } from './components/billing/quota.js';
|
|
70
|
-
export type { Quota, QuotaState } from './components/billing/quota.js';
|
package/dist/index.js
CHANGED
|
@@ -13,11 +13,14 @@ export { AUTH_PAGES, BILLING_PAGES } from './core/routes.js';
|
|
|
13
13
|
export { decide, findRule, flagsAllow } from './core/guard.js';
|
|
14
14
|
// State
|
|
15
15
|
export { billing, flags, flagsStatus, liveStatus, me, plans, resource, session, signedIn, usage, user, workspace, workspaces } from './core/stores.js';
|
|
16
|
+
export { entitlements } from './core/entitlements.js';
|
|
17
|
+
export { billingGate, billingLocked } from './core/billing-gate.js';
|
|
18
|
+
export { useQuota } from './core/quota.js';
|
|
16
19
|
// Flags
|
|
17
20
|
export { canManageBilling, canManageTeam, flag, flagReason, requireFlag, useFlag } from './core/flags.js';
|
|
18
21
|
// Calling your API
|
|
19
22
|
export { bridgeFetch } from './core/fetch.js';
|
|
20
|
-
export { closeUpgrade, openUpgrade, readPaymentRequired, upgradeRequest } from './core/upgrade.js';
|
|
23
|
+
export { closeUpgrade, onBridgeQuotaExceeded, openUpgrade, parseQuotaRefusal, readPaymentRequired, upgradeRequest } from './core/upgrade.js';
|
|
21
24
|
// Confirming it is you
|
|
22
25
|
export { cancelRecheck, confirmItIsYou, recheckRequest, withRecheck } from './core/recheck.js';
|
|
23
26
|
// Hand-off mode
|
|
@@ -47,6 +50,7 @@ export { default as SignOutButton } from './components/auth/SignOutButton.svelte
|
|
|
47
50
|
export { default as ProfileName } from './components/auth/ProfileName.svelte';
|
|
48
51
|
export { default as FeatureFlag } from './components/flags/FeatureFlag.svelte';
|
|
49
52
|
export { default as UpgradeDialog } from './components/billing/UpgradeDialog.svelte';
|
|
53
|
+
export { default as BridgeUpgradeDialog } from './components/billing/UpgradeDialog.svelte';
|
|
50
54
|
export { default as LiveStatusBadge } from './components/live/LiveStatusBadge.svelte';
|
|
51
55
|
export { default as TeamPanel } from './components/team/TeamPanel.svelte';
|
|
52
56
|
export { default as MemberList } from './components/team/MemberList.svelte';
|
|
@@ -61,6 +65,9 @@ export { default as PlanPicker } from './components/billing/PlanPicker.svelte';
|
|
|
61
65
|
export { default as BillingPortalButton } from './components/billing/BillingPortalButton.svelte';
|
|
62
66
|
export { default as SubscriptionStatus } from './components/billing/SubscriptionStatus.svelte';
|
|
63
67
|
export { default as BillingNotice } from './components/billing/BillingNotice.svelte';
|
|
68
|
+
export { default as BridgeBillingNotice } from './components/billing/BillingNotice.svelte';
|
|
64
69
|
export { default as QuotaBanner } from './components/billing/QuotaBanner.svelte';
|
|
70
|
+
export { default as BridgeQuotaBanner } from './components/billing/QuotaBanner.svelte';
|
|
71
|
+
export { default as QuotaGate } from './components/billing/QuotaGate.svelte';
|
|
72
|
+
export { default as Entitled } from './components/billing/Entitled.svelte';
|
|
65
73
|
export { default as CheckoutNotice } from './components/billing/CheckoutNotice.svelte';
|
|
66
|
-
export { useQuota } from './components/billing/quota.js';
|
package/dist/styles.css
CHANGED
|
@@ -1755,6 +1755,62 @@
|
|
|
1755
1755
|
color: var(--bridge-foreground, #111827);
|
|
1756
1756
|
}
|
|
1757
1757
|
|
|
1758
|
+
/* A locked workspace in hard mode: a blocking screen over the page. */
|
|
1759
|
+
.bridge-billing-lockscreen {
|
|
1760
|
+
position: fixed;
|
|
1761
|
+
inset: 0;
|
|
1762
|
+
z-index: 9999;
|
|
1763
|
+
display: flex;
|
|
1764
|
+
align-items: center;
|
|
1765
|
+
justify-content: center;
|
|
1766
|
+
padding: 1rem;
|
|
1767
|
+
background: var(--bridge-paywall-bg, rgba(15, 23, 42, 0.72));
|
|
1768
|
+
backdrop-filter: blur(2px);
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
.bbl-panel {
|
|
1772
|
+
display: flex;
|
|
1773
|
+
flex-direction: column;
|
|
1774
|
+
gap: 0.75rem;
|
|
1775
|
+
max-width: 28rem;
|
|
1776
|
+
width: 100%;
|
|
1777
|
+
padding: 1.5rem;
|
|
1778
|
+
border-radius: 0.75rem;
|
|
1779
|
+
background: var(--bridge-foreground, #111827);
|
|
1780
|
+
color: var(--bridge-bg, #ffffff);
|
|
1781
|
+
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
|
|
1782
|
+
text-align: center;
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
.bbl-title {
|
|
1786
|
+
font-size: calc(1.125 * var(--bridge-text-base, 1rem));
|
|
1787
|
+
font-weight: var(--bridge-font-weight-heading, 600);
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
.bbl-body {
|
|
1791
|
+
font-size: calc(0.9375 * var(--bridge-text-base, 1rem));
|
|
1792
|
+
opacity: 0.85;
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
.bbl-cta {
|
|
1796
|
+
align-self: center;
|
|
1797
|
+
margin-top: 0.5rem;
|
|
1798
|
+
padding: 0.625rem 1.25rem;
|
|
1799
|
+
border-radius: 0.5rem;
|
|
1800
|
+
border: 1px solid currentColor;
|
|
1801
|
+
background: transparent;
|
|
1802
|
+
color: inherit;
|
|
1803
|
+
font: inherit;
|
|
1804
|
+
font-weight: var(--bridge-font-weight-button, 500);
|
|
1805
|
+
text-decoration: none;
|
|
1806
|
+
cursor: pointer;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
.bbl-cta:hover {
|
|
1810
|
+
background: var(--bridge-bg, #ffffff);
|
|
1811
|
+
color: var(--bridge-foreground, #111827);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1758
1814
|
/* ─── QuotaBanner ─────────────────────────────────────────────────────── */
|
|
1759
1815
|
|
|
1760
1816
|
[data-bridge-quota-banner] {
|
|
@@ -1769,12 +1825,12 @@
|
|
|
1769
1825
|
transition: opacity 280ms ease, transform 320ms ease;
|
|
1770
1826
|
}
|
|
1771
1827
|
|
|
1772
|
-
[data-bridge-quota-banner][data-
|
|
1828
|
+
[data-bridge-quota-banner][data-severity="warn"] {
|
|
1773
1829
|
background: var(--bridge-alert-warning-bg, #fffbeb);
|
|
1774
1830
|
color: var(--bridge-alert-warning-fg, #92400e);
|
|
1775
1831
|
}
|
|
1776
1832
|
|
|
1777
|
-
[data-bridge-quota-banner][data-
|
|
1833
|
+
[data-bridge-quota-banner][data-severity="critical"] {
|
|
1778
1834
|
background: var(--bridge-alert-error-bg, #fef2f2);
|
|
1779
1835
|
color: var(--bridge-alert-error-fg, #991b1b);
|
|
1780
1836
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nebulr-group/bridge-svelte",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.14",
|
|
4
4
|
"description": "The Bridge for SvelteKit: sign-in in your own pages, workspaces, flag-based route guards, plans and checkout, the team panel and live updates, on one setting: the issuer.",
|
|
5
5
|
"author": "Iman Pouya",
|
|
6
6
|
"license": "MIT",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@sveltejs/kit": "^2.0.0"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@nebulr-group/bridge-auth-core": "1.0.0-alpha.
|
|
63
|
+
"@nebulr-group/bridge-auth-core": "1.0.0-alpha.12"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@sveltejs/kit": "^2.16.0",
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One metric's usage against its plan limit, for your own UI:
|
|
3
|
-
*
|
|
4
|
-
* ```svelte
|
|
5
|
-
* const projects = useQuota('projects');
|
|
6
|
-
* {#if $projects}{$projects.used} of {$projects.limit} projects{/if}
|
|
7
|
-
* ```
|
|
8
|
-
*
|
|
9
|
-
* Numbers only, never a gate: whether something is allowed is a flag.
|
|
10
|
-
*/
|
|
11
|
-
import type { UsageNumbers } from '@nebulr-group/bridge-auth-core';
|
|
12
|
-
import { type Readable } from 'svelte/store';
|
|
13
|
-
export type QuotaState = 'ok' | 'near' | 'over';
|
|
14
|
-
export interface Quota {
|
|
15
|
-
readonly used: number;
|
|
16
|
-
readonly limit: number;
|
|
17
|
-
/** `used / limit`; 1 for a limit of 0. */
|
|
18
|
-
readonly ratio: number;
|
|
19
|
-
/** `near` from 80% of the limit, `over` once the limit is reached. */
|
|
20
|
-
readonly state: QuotaState;
|
|
21
|
-
}
|
|
22
|
-
/** From this share of the limit, a metric is `near` it. */
|
|
23
|
-
export declare const NEAR_LIMIT = 0.8;
|
|
24
|
-
/** A metric's quota from the usage numbers, or null when it has no limit or is not loaded. */
|
|
25
|
-
export declare function quotaFor(numbers: UsageNumbers | null | undefined, metric: string): Quota | null;
|
|
26
|
-
/** A metric's quota as a store: null while loading and for a metric without a limit. */
|
|
27
|
-
export declare function useQuota(metric: string): Readable<Quota | null>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { derived } from 'svelte/store';
|
|
2
|
-
import { usage } from '../../core/stores.js';
|
|
3
|
-
/** From this share of the limit, a metric is `near` it. */
|
|
4
|
-
export const NEAR_LIMIT = 0.8;
|
|
5
|
-
/** A metric's quota from the usage numbers, or null when it has no limit or is not loaded. */
|
|
6
|
-
export function quotaFor(numbers, metric) {
|
|
7
|
-
const row = numbers?.metrics.find((m) => m.metric === metric);
|
|
8
|
-
if (!row || row.limit === null)
|
|
9
|
-
return null;
|
|
10
|
-
const { used, limit } = row;
|
|
11
|
-
const ratio = limit > 0 ? used / limit : 1;
|
|
12
|
-
const state = used >= limit ? 'over' : ratio >= NEAR_LIMIT ? 'near' : 'ok';
|
|
13
|
-
return { used, limit, ratio, state };
|
|
14
|
-
}
|
|
15
|
-
/** A metric's quota as a store: null while loading and for a metric without a limit. */
|
|
16
|
-
export function useQuota(metric) {
|
|
17
|
-
return derived(usage, (numbers) => quotaFor(numbers.value, metric));
|
|
18
|
-
}
|