@heymantle/react
Version:
Mantle's React component library
3 lines (2 loc) • 7.32 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const G=require("react/jsx-runtime"),J=require("@heymantle/client"),m=require("react"),Q={Public:"public",CustomerTag:"customerTag",ShopifyPlan:"shopifyPlan",Customer:"customer",Hidden:"hidden"},p={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"},w=m.createContext(void 0),W=({feature:e,count:s=0})=>(e==null?void 0:e.type)==="boolean"?e.value:(e==null?void 0:e.type)==="limit"?s<e.value||e.value===-1:!1,X=({appId:e,customerApiToken:s,apiUrl:c="https://appapi.heymantle.com/v1",children:l,i18n:v=p,waitForCustomer:E=!1,loadingComponent:A=null,throwOnError:a=!1})=>{const o=new J.MantleClient({appId:e,customerApiToken:s,apiUrl:c}),[i,M]=m.useState(null),[y,f]=m.useState(!0),g=async()=>{try{f(!0);const r=await o.getCustomer();if(r&&"error"in r)throw new Error(r.error);M(r)}catch(r){if(a)throw r;console.error("[MantleProvider] Error fetching customer: ",r)}finally{f(!1)}},N=async r=>{const t=await o.sendUsageEvent(r);if("error"in t){if(a)throw new Error(t.error);return{success:!1}}return t},L=async({usageId:r,period:t})=>{const n=await o.getUsageMetricReport({id:r,period:t});if("error"in n&&a)throw new Error(n.error);return n},x=async r=>{const t=await o.subscribe(r);if("error"in t&&a)throw new Error(t.error);return t},Y=async({cancelReason:r}={})=>{const t=await o.cancelSubscription({...r&&{cancelReason:r}});if("error"in t&&a)throw new Error(t.error);return t},D=async({amount:r,name:t,currencyCode:n,returnUrl:u,test:d})=>{const b=await o.createOneTimeCharge({amount:r,name:t,currencyCode:n,returnUrl:u,test:d});if("error"in b&&a)throw new Error(b.error);return b},R=async({returnUrl:r,updateExistingPaymentMethods:t})=>{if(!r)throw new Error("returnUrl is required");const n=await o.addPaymentMethod({returnUrl:r,updateExistingPaymentMethods:t});if("error"in n&&a)throw new Error(n.error);return n},U=async({type:r,config:t})=>{if(!r)throw new Error("type is required");const u=new URL(document.location.toString()).searchParams.get("locale"),d=await o.createHostedSession({type:r,config:{...u?{locale:u}:{},...t||{}}});if("error"in d&&a)throw new Error(d.error);return d},k=async r=>{const t=await o.listNotifications(r);if("error"in t&&a)throw new Error(t.error);return t},_=async({id:r})=>{const t=await o.triggerNotificationCta({id:r});if("error"in t&&a)throw new Error(t.error);return t},F=async({id:r,readAt:t,dismissedAt:n})=>{const u=await o.updateNotification({id:r,readAt:t,dismissedAt:n});if("error"in u&&a)throw new Error(u.error);return u},q=async r=>{const t=await o.getChecklist(r);if(t&&"error"in t&&a)throw new Error(t.error);return t},T=async r=>{const t=await o.getChecklists(r);if(t&&"error"in t&&a)throw new Error(t.error);return t},B=async({idOrHandle:r})=>{const t=await o.showChecklist({idOrHandle:r});if(t&&"error"in t&&a)throw new Error(t.error);return t},V=async({idOrHandle:r,stepIdOrHandle:t})=>{const n=await o.skipChecklistStep({idOrHandle:r,stepIdOrHandle:t});if(n&&"error"in n&&a)throw new Error(n.error);return n},j=async({idOrHandle:r,stepIdOrHandle:t})=>{const n=await o.completeChecklistStep({idOrHandle:r,stepIdOrHandle:t});if("error"in n&&a)throw new Error(n.error);return n},H=async({customerId:r}={})=>{const t=await o.getAppInstallations({...r&&{customerId:r}});if("error"in t&&a)throw new Error(t.error);return t};m.useEffect(()=>{s&&g()},[s]);const I=(i==null?void 0:i.plans)||[],$=(i==null?void 0:i.subscription)||null;return E&&y?A||null:G.jsx(w.Provider,{value:{client:o,customer:i,subscription:$,plans:I,loading:y,i18n:{...p,...v},sendUsageEvent:N,getUsageReport:L,subscribe:x,cancelSubscription:Y,createOneTimeCharge:D,addPaymentMethod:R,createHostedSession:U,listNotifications:k,triggerNotificationCta:_,updateNotification:F,getChecklist:q,getChecklists:T,completeChecklistStep:j,showChecklist:B,skipChecklistStep:V,getAppInstallations:H,isFeatureEnabled:({featureKey:r,count:t=0})=>i!=null&&i.features[r]?W({feature:i.features[r],count:t}):!1,limitForFeature:({featureKey:r})=>i!=null&&i.features[r]&&i.features[r].type==="limit"?i.features[r].value:-1,refetch:async()=>{await g()}},children:l})},Z=()=>{const e=m.useContext(w);if(e===void 0)throw new Error("useMantle must be used within a MantleProvider");return e},h=e=>e.type==="boolean"&&e.value===!0||e.type==="limit"&&e.value!==0,z=(e,s)=>Number(h(s))-Number(h(e))||e.name.localeCompare(s.name),K=(e="USD")=>new Intl.NumberFormat("en-US",{style:"currency",currency:e,notation:"standard"}),O=(e,s="USD",c=!0)=>{let l=K(s).format(e);return c&&(l=l.replace(/\.00$/,"")),l};var S=(e=>(e.Annual="ANNUAL",e.Every30Days="EVERY_30_DAYS",e))(S||{});const C=(e="EVERY_30_DAYS")=>{switch(e){case"ANNUAL":return"year";case"EVERY_30_DAYS":default:return"month"}},P=(e="EVERY_30_DAYS")=>{switch(e){case"ANNUAL":return"yr";case"EVERY_30_DAYS":default:return"mo"}},ee=({interval:e="EVERY_30_DAYS",useShortFormPlanIntervals:s=!0})=>s?P(e):C(e),te=({plan:e,customFieldKey:s="recommended"})=>{var c;return!!((c=e.customFields)!=null&&c[s])},re=({plan:e,customFieldKey:s="buttonLabel"})=>{var c;return((c=e.customFields)==null?void 0:c[s])||p.subscribe},ne=({plan:e})=>{var s;return((s=e.discounts)==null?void 0:s.length)>0?e.discounts.reduce((c,l)=>c.discountedAmount<l.discountedAmount?c:l):void 0},se=(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},oe=(e=4)=>e%4===0?4:e%3===0?3:e%2===0?2:e===1?1:4;exports.Labels=p;exports.MantleProvider=X;exports.PlanAvailability=Q;exports.PlanInterval=S;exports.columnCount=oe;exports.columnSpan=se;exports.customButtonLabel=re;exports.featureEnabled=h;exports.featureSort=z;exports.highestDiscount=ne;exports.intervalLabel=ee;exports.intervalLabelLong=C;exports.intervalLabelShort=P;exports.isRecommendedPlan=te;exports.money=O;exports.useMantle=Z;
//# sourceMappingURL=index.js.map