@heymantle/react
Version:
Mantle's React component library
3 lines (2 loc) • 5.21 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const D=require("react/jsx-runtime"),R=require("@heymantle/client"),l=require("react"),U={Public:"public",CustomerTag:"customerTag",ShopifyPlan:"shopifyPlan",Customer:"customer",Hidden:"hidden"},c={AmountPerInterval:"{{ amount }} per {{ interval }}",Back:"Back",Cancel:"Cancel",CancelConfirmation:"Are you sure you want to cancel your subscription?",CancelPlan:"Cancel plan",ChangePlan:"Change plan",CurrentPlan:"Current plan",CustomPlans:"Custom plans",CustomPlansDescription:"Plans tailored to your specific needs",DiscountAmount:"{{ amount }} discount",DiscountAmountExpired:"{{ amount }} discount expired",FreeTrialLength:"{{ trialDays }}-day free trial",Features:"Features",Month:"month",MonthShort:"mo",Monthly:"Monthly",NextBillingDate:"Next billing date",NotSubscribed:"You're not subscribed to a plan yet.",Year:"year",YearShort:"yr",Yearly:"Yearly",MostPopular:"Most popular",Per:"/",Plans:"Plans",Price:"Price",SelectPlan:"Select plan",SubscribeSuccessTitle:"Subscription successful",SubscribeSuccessBody:"Thanks for subscribing to our app!",Subscription:"Subscription",SubscriptionCancelled:"Subscription cancelled",UsageCharges:"Usage charges",loading:"Loading...",error:"An error occurred",retry:"Retry",cancel:"Cancel",continue:"Continue",subscribe:"Subscribe",subscribeNow:"Subscribe Now",upgradeNow:"Upgrade Now",manageSubscription:"Manage Subscription",cancelSubscription:"Cancel Subscription",addPaymentMethod:"Add Payment Method",updatePaymentMethod:"Update Payment Method",featureNotAvailable:"This feature is not available on your current plan",upgradeRequired:"Upgrade Required",limitReached:"Limit Reached"},y=l.createContext(void 0),_=({feature:e,count:n=0})=>(e==null?void 0:e.type)==="boolean"?e.value:(e==null?void 0:e.type)==="limit"?n<e.value||e.value===-1:!1,F=({appId:e,customerApiToken:n,apiUrl:a="https://appapi.heymantle.com/v1",children:s,i18n:P=c,waitForCustomer:f=!1,loadingComponent:v=null})=>{const o=new R.MantleClient({appId:e,customerApiToken:n,apiUrl:a}),[r,C]=l.useState(null),[d,m]=l.useState(!0),b=async()=>{try{m(!0);const t=await o.getCustomer();C(t)}catch(t){console.error("[MantleProvider] Error fetching customer: ",t)}finally{m(!1)}},w=async t=>{await o.sendUsageEvent(t)},E=async({usageId:t,period:i})=>await o.getUsageMetricReport({id:t,period:i}),M=async t=>await o.subscribe(t),A=async({cancelReason:t}={})=>await o.cancelSubscription({...t&&{cancelReason:t}}),L=async({returnUrl:t})=>{if(!t)throw new Error("returnUrl is required");return await o.addPaymentMethod({returnUrl:t})},x=async({type:t,config:i})=>{if(!t)throw new Error("type is required");const p=new URL(document.location.toString()).searchParams.get("locale");return await o.createHostedSession({type:t,config:{...p?{locale:p}:{},...i||{}}})};l.useEffect(()=>{n&&b()},[n]);const N=(r==null?void 0:r.plans)||[],Y=(r==null?void 0:r.subscription)||null;return f&&d?v||null:D.jsx(y.Provider,{value:{client:o,customer:r,subscription:Y,plans:N,loading:d,i18n:{...c,...P},sendUsageEvent:w,getUsageReport:E,subscribe:M,cancelSubscription:A,addPaymentMethod:L,createHostedSession:x,isFeatureEnabled:({featureKey:t,count:i=0})=>r!=null&&r.features[t]?_({feature:r.features[t],count:i}):!1,limitForFeature:({featureKey:t})=>r!=null&&r.features[t]&&r.features[t].type==="limit"?r.features[t].value:-1,refetch:async()=>{await b()}},children:s})},q=()=>{const e=l.useContext(y);if(e===void 0)throw new Error("useMantle must be used within a MantleProvider");return e},u=e=>e.type==="boolean"&&e.value===!0||e.type==="limit"&&e.value!==0,B=(e,n)=>Number(u(n))-Number(u(e))||e.name.localeCompare(n.name),V=(e="USD")=>new Intl.NumberFormat("en-US",{style:"currency",currency:e,notation:"standard"}),T=(e,n="USD",a=!0)=>{let s=V(n).format(e);return a&&(s=s.replace(/\.00$/,"")),s};var h=(e=>(e.Annual="ANNUAL",e.Every30Days="EVERY_30_DAYS",e))(h||{});const g=(e="EVERY_30_DAYS")=>{switch(e){case"ANNUAL":return"year";case"EVERY_30_DAYS":default:return"month"}},S=(e="EVERY_30_DAYS")=>{switch(e){case"ANNUAL":return"yr";case"EVERY_30_DAYS":default:return"mo"}},j=({interval:e="EVERY_30_DAYS",useShortFormPlanIntervals:n=!0})=>n?S(e):g(e),H=({plan:e,customFieldKey:n="recommended"})=>{var a;return!!((a=e.customFields)!=null&&a[n])},I=({plan:e,customFieldKey:n="buttonLabel"})=>{var a;return((a=e.customFields)==null?void 0:a[n])||c.subscribe},k=({plan:e})=>{var n;return((n=e.discounts)==null?void 0:n.length)>0?e.discounts.reduce((a,s)=>a.discountedAmount<s.discountedAmount?a:s):void 0},O=(e=4)=>e%4===0?{xs:6,sm:6,md:2,lg:3,xl:3}:e%3===0?{xs:6,sm:6,md:2,lg:4,xl:4}:e%2===0?{xs:6,sm:6,md:3,lg:6,xl:6}:e===1?{xs:6,sm:6,md:6,lg:12,xl:12}:{xs:6,sm:6,md:2,lg:4,xl:4},$=(e=4)=>e%4===0?4:e%3===0?3:e%2===0?2:e===1?1:4;exports.Labels=c;exports.MantleProvider=F;exports.PlanAvailability=U;exports.PlanInterval=h;exports.columnCount=$;exports.columnSpan=O;exports.customButtonLabel=I;exports.featureEnabled=u;exports.featureSort=B;exports.highestDiscount=k;exports.intervalLabel=j;exports.intervalLabelLong=g;exports.intervalLabelShort=S;exports.isRecommendedPlan=H;exports.money=T;exports.useMantle=q;
//# sourceMappingURL=index.js.map