-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathSubscriptionDetailsButton.tsx
More file actions
90 lines (81 loc) · 3.11 KB
/
SubscriptionDetailsButton.tsx
File metadata and controls
90 lines (81 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { __experimental_SubscriptionDetailsButtonProps } from '@clerk/types';
import React from 'react';
import { useAuth } from '../hooks';
import type { WithClerkProp } from '../types';
import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils';
import { withClerk } from './withClerk';
/**
* @experimental A button component that opens the Clerk Subscription Details drawer when clicked. This component must be rendered
* inside a `<SignedIn />` component to ensure the user is authenticated.
*
* @example
* ```tsx
* import { SignedIn } from '@clerk/clerk-react';
* import { SubscriptionDetailsButton } from '@clerk/clerk-react/experimental';
*
* // Basic usage with default "Subscription details" text
* function BasicSubscriptionDetails() {
* return (
* <SubscriptionDetailsButton />
* );
* }
*
* // Custom button with organization subscription
* function OrganizationSubscriptionDetails() {
* return (
* <SubscriptionDetailsButton
* for="organization"
* onSubscriptionCancel={() => console.log('Subscription canceled')}
* >
* <button>View Organization Subscription</button>
* </SubscriptionDetailsButton>
* );
* }
* ```
*
* @throws {Error} When rendered outside of a `<SignedIn />` component
* @throws {Error} When `for="organization"` is used without an active organization context
*
* @see https://clerk.com/docs/billing/overview
*/
export const SubscriptionDetailsButton = withClerk(
({
clerk,
children,
...props
}: WithClerkProp<React.PropsWithChildren<__experimental_SubscriptionDetailsButtonProps>>) => {
const { for: _for, subscriptionDetailsProps, onSubscriptionCancel, ...rest } = props;
children = normalizeWithDefaultValue(children, 'Subscription details');
const child = assertSingleChild(children)('SubscriptionDetailsButton');
const { userId, orgId } = useAuth();
if (userId === null) {
throw new Error(
'Clerk: Ensure that `<SubscriptionDetailsButton />` is rendered inside a `<SignedIn />` component.',
);
}
if (orgId === null && _for === 'organization') {
throw new Error(
'Clerk: Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization. Retrieve `orgId` from `useAuth()` and confirm it is defined. For SSR, see: https://clerk.com/docs/references/backend/types/auth-object#how-to-access-the-auth-object',
);
}
const clickHandler = () => {
if (!clerk) {
return;
}
return clerk.__internal_openSubscriptionDetails({
for: _for,
onSubscriptionCancel,
...subscriptionDetailsProps,
});
};
const wrappedChildClickHandler: React.MouseEventHandler = async e => {
if (child && typeof child === 'object' && 'props' in child) {
await safeExecute(child.props.onClick)(e);
}
return clickHandler();
};
const childProps = { ...rest, onClick: wrappedChildClickHandler };
return React.cloneElement(child as React.ReactElement<unknown>, childProps);
},
{ component: 'SubscriptionDetailsButton', renderWhileLoading: true },
);