@stigg/react-sdk 6.2.0 → 6.4.0

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "6.2.0",
2
+ "version": "6.4.0",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -114,7 +114,7 @@
114
114
  "@emotion/react": "^11.10.5",
115
115
  "@emotion/styled": "^11.10.5",
116
116
  "@mui/material": "^5.12.0",
117
- "@stigg/js-client-sdk": "3.48.0",
117
+ "@stigg/js-client-sdk": "3.49.0",
118
118
  "@stripe/react-stripe-js": "^2.1.1",
119
119
  "@stripe/stripe-js": "^1.54.1",
120
120
  "@types/styled-components": "^5.1.26",
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ import { GetEnumEntitlement } from '@stigg/js-client-sdk';
3
+ import { useEnumEntitlement } from '../hooks';
4
+
5
+ type EnumEntitlementGuardProps = GetEnumEntitlement & {
6
+ noAccessComponent?: React.ReactNode;
7
+ children: React.ReactElement;
8
+ };
9
+
10
+ export function EnumEntitlementGuard({ noAccessComponent, children, ...rest }: EnumEntitlementGuardProps): JSX.Element {
11
+ const { hasAccess } = useEnumEntitlement(rest);
12
+
13
+ if (!hasAccess) {
14
+ return <>{noAccessComponent ?? null}</>;
15
+ }
16
+
17
+ return children;
18
+ }
@@ -36,6 +36,7 @@ type EntitlementRowProps = {
36
36
  maxUnitQuantity?: number | null;
37
37
  resetPeriod?: EntitlementResetPeriod | null;
38
38
  displayNameOverride?: string | null;
39
+ enumValues?: string[] | null;
39
40
  feature?: {
40
41
  id: string;
41
42
  units?: string | null | undefined;
@@ -80,10 +81,16 @@ function getEntitlementDisplay({
80
81
  minUnitQuantity,
81
82
  maxUnitQuantity,
82
83
  isCustom,
84
+ enumValues,
83
85
  }: EntitlementRowProps) {
84
86
  const unitBasedEntitlement = minUnitQuantity || maxUnitQuantity;
85
87
  const resetPeriodSuffix = `${resetPeriod && usageLimit ? `${getResetPeriodText(resetPeriod)}` : ''}`;
86
88
 
89
+ if (enumValues) {
90
+ const featureUnits = enumValues.length === 1 ? feature?.units : feature?.unitsPlural;
91
+ return `${enumValues.length} ${featureUnits}`;
92
+ }
93
+
87
94
  if (hasUnlimitedUsage) {
88
95
  return `Unlimited ${feature?.unitsPlural}`;
89
96
  }
@@ -67,6 +67,7 @@ export function PlanEntitlements({
67
67
  resetPeriod={entitlement.resetPeriod}
68
68
  hasUnlimitedUsage={entitlement.hasUnlimitedUsage}
69
69
  usageLimit={entitlement.usageLimit}
70
+ enumValues={entitlement.enumValues}
70
71
  isCustom={entitlement.isCustom}
71
72
  />
72
73
  ))}
@@ -1,6 +1,7 @@
1
1
  export { useBooleanEntitlement } from './useBooleanEntitlement';
2
2
  export { useNumericEntitlement } from './useNumericEntitlement';
3
3
  export { useMeteredEntitlement } from './useMeteredEntitlement';
4
+ export { useEnumEntitlement } from './useEnumEntitlement';
4
5
  export { useActiveSubscriptions } from './useActiveSubscriptions';
5
6
  export { usePaywall } from './usePaywall';
6
7
  export { useCustomerPortal } from './useCustomerPortal';
@@ -0,0 +1,20 @@
1
+ import { GetEnumEntitlement, ENUM_DEFAULT_FALLBACK_ENTITLEMENT, EnumEntitlement } from '@stigg/js-client-sdk';
2
+ import { useStiggContext } from './useStiggContext';
3
+
4
+ type TypedGetEnumEntitlement<T> = Omit<GetEnumEntitlement, 'featureId'> & {
5
+ featureId: T;
6
+ };
7
+
8
+ export function useEnumEntitlement<T extends string>(params: TypedGetEnumEntitlement<T>): EnumEntitlement {
9
+ const stiggContext = useStiggContext({ optional: true });
10
+ const stigg = stiggContext?.stigg;
11
+
12
+ if (!stigg) {
13
+ return {
14
+ ...ENUM_DEFAULT_FALLBACK_ENTITLEMENT,
15
+ ...params.options?.fallback,
16
+ };
17
+ }
18
+
19
+ return stigg.getEnumEntitlement(params);
20
+ }
package/src/index.ts CHANGED
@@ -56,6 +56,7 @@ export { useWaitForCheckoutCompleted, ProvisionStatus } from './components/hooks
56
56
  export {
57
57
  useBooleanEntitlement,
58
58
  useMeteredEntitlement,
59
+ useEnumEntitlement,
59
60
  useNumericEntitlement,
60
61
  useActiveSubscriptions,
61
62
  useCustomerPortal,
@@ -67,3 +68,4 @@ export { CustomizedTheme as Theme } from './theme/Theme';
67
68
  export { BooleanEntitlementGuard } from './components/BooleanEntitlementGuard';
68
69
  export { NumericEntitlementGuard } from './components/NumericEntitlementGuard';
69
70
  export { MeteredEntitlementGuard } from './components/MeteredEntitlementGuard';
71
+ export { EnumEntitlementGuard } from './components/EnumEntitlementGuard';