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