@munchi_oy/react 1.2.5

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.
@@ -0,0 +1,4 @@
1
+ export * from "./src/SdkContainer";
2
+ export * from "./src/useMunchi";
3
+ export * from "./src/usePaymentCountdown";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var a=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var P=(t,e)=>{for(var n in e)a(t,n,{get:e[n],enumerable:!0})},l=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of C(e))!h.call(t,o)&&o!==n&&a(t,o,{get:()=>e[o],enumerable:!(r=k(e,o))||r.enumerable});return t};var M=t=>l(a({},"__esModule",{value:!0}),t);var D={};P(D,{SdkContainer:()=>b,SdkContext:()=>u,useMunchi:()=>S,usePaymentCountdown:()=>w,usePaymentState:()=>T,usePayments:()=>I,useSdk:()=>i});module.exports=M(D);var m=require("react"),f=require("react/jsx-runtime"),u=(0,m.createContext)(void 0),b=({children:t,sdk:e})=>{let n=(0,m.useMemo)(()=>({sdk:e}),[e]);return(0,f.jsx)(u.Provider,{value:n,children:t})};var s=require("react");var S=()=>{let t=(0,s.useContext)(u);if(!t)throw new Error("useMunchi must be used within a SdkContainer");return t},i=()=>{let{sdk:t}=S();return t},I=i,T=()=>{let t=i(),[e,n]=(0,s.useState)(t.currentState);return(0,s.useEffect)(()=>t.subscribe(r=>{n(r)}),[t]),e};var c=require("react");var w=()=>{let t=i(),[e,n]=(0,c.useState)(null);return(0,c.useEffect)(()=>{let r,o=()=>{let p=t.nextAutoResetAt;if(!p){n(null);return}let y=Date.now(),d=Math.max(0,p-y);n(d),d>0&&(r=requestAnimationFrame(o))};o();let x=t.subscribe(()=>{o()});return()=>{x(),r&&cancelAnimationFrame(r)}},[t]),e};0&&(module.exports={SdkContainer,SdkContext,useMunchi,usePaymentCountdown,usePaymentState,usePayments,useSdk});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{createContext as p,useMemo as d}from"react";import{jsx as f}from"react/jsx-runtime";var i=p(void 0),l=({children:t,sdk:e})=>{let n=d(()=>({sdk:e}),[e]);return f(i.Provider,{value:n,children:t})};import{useContext as S,useEffect as x,useState as y}from"react";var k=()=>{let t=S(i);if(!t)throw new Error("useMunchi must be used within a SdkContainer");return t},r=()=>{let{sdk:t}=k();return t},w=r,D=()=>{let t=r(),[e,n]=y(t.currentState);return x(()=>t.subscribe(o=>{n(o)}),[t]),e};import{useEffect as C,useState as h}from"react";var A=()=>{let t=r(),[e,n]=h(null);return C(()=>{let o,s=()=>{let m=t.nextAutoResetAt;if(!m){n(null);return}let a=Date.now(),u=Math.max(0,m-a);n(u),u>0&&(o=requestAnimationFrame(s))};s();let c=t.subscribe(()=>{s()});return()=>{c(),o&&cancelAnimationFrame(o)}},[t]),e};export{l as SdkContainer,i as SdkContext,k as useMunchi,A as usePaymentCountdown,D as usePaymentState,w as usePayments,r as useSdk};
@@ -0,0 +1,16 @@
1
+ import type { FC, ReactNode } from "react";
2
+ import type { IMunchiPaymentSDK } from "@munchi_oy/payments";
3
+ export interface SdkContextType {
4
+ sdk: IMunchiPaymentSDK;
5
+ }
6
+ export declare const SdkContext: import("react").Context<SdkContextType | undefined>;
7
+ export interface SdkContainerProps {
8
+ children: ReactNode;
9
+ sdk: IMunchiPaymentSDK;
10
+ }
11
+ /**
12
+ * SdkContainer provides the Munchi SDK instances to the application via context.
13
+ * Works in React and React Native.
14
+ */
15
+ export declare const SdkContainer: FC<SdkContainerProps>;
16
+ //# sourceMappingURL=SdkContainer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SdkContainer.d.ts","sourceRoot":"","sources":["../../src/SdkContainer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAE3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE7D,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,iBAAiB,CAAC;CACxB;AAED,eAAO,MAAM,UAAU,qDAAuD,CAAC;AAE/E,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,CAAC;IACpB,GAAG,EAAE,iBAAiB,CAAC;CACxB;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,EAAE,CAAC,iBAAiB,CAI9C,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { type SdkContextType } from "./SdkContainer";
2
+ import type { IMunchiPaymentSDK, PaymentInteractionState } from "@munchi_oy/payments";
3
+ /**
4
+ * Hook to access all Munchi SDK instances.
5
+ */
6
+ export declare const useMunchi: () => SdkContextType;
7
+ /**
8
+ * Hook to access the Munchi Payment SDK instance.
9
+ */
10
+ export declare const useSdk: () => IMunchiPaymentSDK;
11
+ /**
12
+ * @deprecated Use useSdk instead
13
+ */
14
+ export declare const usePayments: () => IMunchiPaymentSDK;
15
+ /**
16
+ * Hook to access the current state of the Payment SDK reactively.
17
+ */
18
+ export declare const usePaymentState: () => PaymentInteractionState;
19
+ //# sourceMappingURL=useMunchi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useMunchi.d.ts","sourceRoot":"","sources":["../../src/useMunchi.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,KAAK,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAEtF;;GAEG;AACH,eAAO,MAAM,SAAS,QAAO,cAM5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,MAAM,QAAO,iBAGzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,QARE,iBAQO,CAAC;AAElC;;GAEG;AACH,eAAO,MAAM,eAAe,QAAO,uBAWlC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Hook that returns the milliseconds remaining until the SDK auto-resets.
3
+ * Returns null if no auto-reset is scheduled.
4
+ *
5
+ * Useful for showing progress bars or countdown timers on Success/Failure screens.
6
+ */
7
+ export declare const usePaymentCountdown: () => number | null;
8
+ //# sourceMappingURL=usePaymentCountdown.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePaymentCountdown.d.ts","sourceRoot":"","sources":["../../src/usePaymentCountdown.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,qBA0C/B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const VERSION = "1.2.5";
2
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,UAAU,CAAC"}
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./src/SdkContainer";
2
+ export * from "./src/useMunchi";
3
+ export * from "./src/usePaymentCountdown";
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@munchi_oy/react",
3
+ "version": "1.2.5",
4
+ "description": "Munchi React SDK - React Context and Hooks for Munchi SDKs",
5
+ "license": "UNLICENSED",
6
+ "publishConfig": {
7
+ "access": "restricted"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
+ "react-native": "index.ts",
13
+ "files": [
14
+ "dist",
15
+ "src",
16
+ "index.ts",
17
+ "README.md"
18
+ ],
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.mjs",
23
+ "require": "./dist/index.js",
24
+ "react-native": "./index.ts"
25
+ }
26
+ },
27
+ "scripts": {
28
+ "version:sync": "node scripts/sync-version.mjs",
29
+ "prebuild": "pnpm version:sync",
30
+ "build:runtime": "tsup",
31
+ "build:types": "tsc -p tsconfig.build.json",
32
+ "build": "pnpm run build:runtime && pnpm run build:types",
33
+ "yalc:publish": "pnpm build && yalc publish --push",
34
+ "dev": "tsup --watch",
35
+ "prepare": "npm run build",
36
+ "prepublishOnly": "pnpm build"
37
+ },
38
+ "dependencies": {},
39
+ "peerDependencies": {
40
+ "@munchi_oy/core": ">=1.0.0",
41
+ "@munchi_oy/payments": ">=1.0.0",
42
+ "react": ">=18.2.0",
43
+ "react-native": ">=0.76.0"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "react-native": {
47
+ "optional": true
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "@munchi_oy/core": "workspace:*",
52
+ "@munchi_oy/payments": "workspace:*",
53
+ "@types/react": "^18.3.0",
54
+ "react": "^18.3.0",
55
+ "react-native": "0.76.9",
56
+ "tsup": "^8.0.0",
57
+ "typescript": "^5.0.0"
58
+ }
59
+ }
@@ -0,0 +1,24 @@
1
+ import type { FC, ReactNode } from "react";
2
+ import { createContext, useMemo } from "react";
3
+ import type { IMunchiPaymentSDK } from "@munchi_oy/payments";
4
+
5
+ export interface SdkContextType {
6
+ sdk: IMunchiPaymentSDK;
7
+ }
8
+
9
+ export const SdkContext = createContext<SdkContextType | undefined>(undefined);
10
+
11
+ export interface SdkContainerProps {
12
+ children: ReactNode;
13
+ sdk: IMunchiPaymentSDK;
14
+ }
15
+
16
+ /**
17
+ * SdkContainer provides the Munchi SDK instances to the application via context.
18
+ * Works in React and React Native.
19
+ */
20
+ export const SdkContainer: FC<SdkContainerProps> = ({ children, sdk }) => {
21
+ const value = useMemo(() => ({ sdk }), [sdk]);
22
+
23
+ return <SdkContext.Provider value={value}>{children}</SdkContext.Provider>;
24
+ };
@@ -0,0 +1,43 @@
1
+ import { useContext, useEffect, useState } from "react";
2
+ import { SdkContext, type SdkContextType } from "./SdkContainer";
3
+ import type { IMunchiPaymentSDK, PaymentInteractionState } from "@munchi_oy/payments";
4
+
5
+ /**
6
+ * Hook to access all Munchi SDK instances.
7
+ */
8
+ export const useMunchi = (): SdkContextType => {
9
+ const context = useContext(SdkContext);
10
+ if (!context) {
11
+ throw new Error("useMunchi must be used within a SdkContainer");
12
+ }
13
+ return context;
14
+ };
15
+
16
+ /**
17
+ * Hook to access the Munchi Payment SDK instance.
18
+ */
19
+ export const useSdk = (): IMunchiPaymentSDK => {
20
+ const { sdk } = useMunchi();
21
+ return sdk;
22
+ };
23
+
24
+ /**
25
+ * @deprecated Use useSdk instead
26
+ */
27
+ export const usePayments = useSdk;
28
+
29
+ /**
30
+ * Hook to access the current state of the Payment SDK reactively.
31
+ */
32
+ export const usePaymentState = (): PaymentInteractionState => {
33
+ const sdk = useSdk();
34
+ const [state, setState] = useState(sdk.currentState);
35
+
36
+ useEffect(() => {
37
+ return sdk.subscribe((newState) => {
38
+ setState(newState);
39
+ });
40
+ }, [sdk]);
41
+
42
+ return state;
43
+ };
@@ -0,0 +1,52 @@
1
+ import { useEffect, useState } from "react";
2
+ import { useSdk } from "./useMunchi";
3
+
4
+ /**
5
+ * Hook that returns the milliseconds remaining until the SDK auto-resets.
6
+ * Returns null if no auto-reset is scheduled.
7
+ *
8
+ * Useful for showing progress bars or countdown timers on Success/Failure screens.
9
+ */
10
+ export const usePaymentCountdown = () => {
11
+ const sdk = useSdk();
12
+ const [timeLeftMs, setTimeLeftMs] = useState<number | null>(null);
13
+
14
+ useEffect(() => {
15
+ let animationFrameId: number;
16
+
17
+ const checkTimer = () => {
18
+ const resetTime = sdk.nextAutoResetAt;
19
+
20
+ if (!resetTime) {
21
+ setTimeLeftMs(null);
22
+ return;
23
+ }
24
+
25
+ const now = Date.now();
26
+ const remaining = Math.max(0, resetTime - now);
27
+
28
+ setTimeLeftMs(remaining);
29
+
30
+ if (remaining > 0) {
31
+ animationFrameId = requestAnimationFrame(checkTimer);
32
+ }
33
+ };
34
+
35
+ // Initial check
36
+ checkTimer();
37
+
38
+ // Subscribe to state changes to trigger re-checks (e.g. when entering SUCCESS state)
39
+ const unsubscribe = sdk.subscribe(() => {
40
+ checkTimer();
41
+ });
42
+
43
+ return () => {
44
+ unsubscribe();
45
+ if (animationFrameId) {
46
+ cancelAnimationFrame(animationFrameId);
47
+ }
48
+ };
49
+ }, [sdk]);
50
+
51
+ return timeLeftMs;
52
+ };
package/src/version.ts ADDED
@@ -0,0 +1,3 @@
1
+ // This file is auto-generated. Do not edit manually.
2
+ // Run 'pnpm version:sync' to update this file.
3
+ export const VERSION = "1.2.5";