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