@vetc-miniapp/ui-react 0.0.21 → 0.0.22
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.
package/package.json
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see index.js.LICENSE.txt */
|
|
2
|
-
!function(e
|
|
2
|
+
!function(n,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports["vetc-miniapp/ui-react"]=e(require("react")):n["vetc-miniapp/ui-react"]=e(n.react)}(this,n=>(()=>{"use strict";var e={155(e){e.exports=n},698(n,e){var r=Symbol.for("react.transitional.element"),t=Symbol.for("react.fragment");function o(n,e,t){var o=null;if(void 0!==t&&(o=""+t),void 0!==e.key&&(o=""+e.key),"key"in e)for(var i in t={},e)"key"!==i&&(t[i]=e[i]);else t=e;return e=t.ref,{$$typeof:r,type:n,key:o,ref:void 0!==e?e:null,props:t}}e.Fragment=t,e.jsx=o,e.jsxs=o},848(n,e,r){n.exports=r(698)}},r={};function t(n){var o=r[n];if(void 0!==o)return o.exports;var i=r[n]={exports:{}};return e[n](i,i.exports,t),i.exports}t.d=(n,e)=>{for(var r in e)t.o(e,r)&&!t.o(n,r)&&Object.defineProperty(n,r,{enumerable:!0,get:e[r]})},t.o=(n,e)=>Object.prototype.hasOwnProperty.call(n,e),t.r=n=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})};var o={};t.r(o),t.d(o,{App:()=>x,appConfig:()=>m,useAppPause:()=>u,useAppResume:()=>a,useDidHide:()=>f,useDidShow:()=>c,useListenerScanQr:()=>h,useNavigate:()=>y});var i=t(155);function u(n){var e=(0,i.useRef)(n);(0,i.useEffect)(function(){e.current=n},[n]),(0,i.useEffect)(function(){if("undefined"!=typeof window&&window.MiniApp){var n=function(){try{e.current&&e.current()}catch(n){console.error("[useAppPause error]",n)}};return window.MiniApp.on("appPause",n),function(){window.MiniApp.off("appPause",n)}}},[])}function a(n){var e=(0,i.useRef)(n);(0,i.useEffect)(function(){e.current=n},[n]),(0,i.useEffect)(function(){if(console.warn("MiniApp: useAppResume"),"undefined"!=typeof window&&window.MiniApp){var n=function(){try{e.current&&e.current()}catch(n){console.error("[useAppResume error]",n)}};return window.MiniApp.on("appResume",n),function(){window.MiniApp.off("appResume",n)}}},[])}function c(n,e){var r=(0,i.useRef)(e);(0,i.useEffect)(function(){r.current=e},[e]),(0,i.useEffect)(function(){if(window.MiniApp){var e=function(e){var t;(null==e?void 0:e.route)===n&&(null===(t=r.current)||void 0===t||t.call(r,e))};return window.MiniApp.on("didShow",e),function(){window.MiniApp.off("didShow",e)}}},[n])}function f(n,e){var r=(0,i.useRef)(e);(0,i.useEffect)(function(){r.current=e},[e]),(0,i.useEffect)(function(){if(window.MiniApp){var e=function(e){var t;(null==e?void 0:e.route)===n&&(null===(t=r.current)||void 0===t||t.call(r,e))};return window.MiniApp.on("didHide",e),function(){window.MiniApp.off("didHide",e)}}},[n])}function p(){var n,e,r="function"==typeof Symbol?Symbol:{},t=r.iterator||"@@iterator",o=r.toStringTag||"@@toStringTag";function i(r,t,o,i){var c=t&&t.prototype instanceof a?t:a,f=Object.create(c.prototype);return l(f,"_invoke",function(r,t,o){var i,a,c,f=0,p=o||[],l=!1,s={p:0,n:0,v:n,a:d,f:d.bind(n,4),d:function(e,r){return i=e,a=0,c=n,s.n=r,u}};function d(r,t){for(a=r,c=t,e=0;!l&&f&&!o&&e<p.length;e++){var o,i=p[e],d=s.p,v=i[2];r>3?(o=v===t)&&(c=i[(a=i[4])?5:(a=3,3)],i[4]=i[5]=n):i[0]<=d&&((o=r<2&&d<i[1])?(a=0,s.v=t,s.n=i[1]):d<v&&(o=r<3||i[0]>t||t>v)&&(i[4]=r,i[5]=t,s.n=v,a=0))}if(o||r>1)return u;throw l=!0,t}return function(o,p,v){if(f>1)throw TypeError("Generator is already running");for(l&&1===p&&d(p,v),a=p,c=v;(e=a<2?n:c)||!l;){i||(a?a<3?(a>1&&(s.n=-1),d(a,c)):s.n=c:s.v=c);try{if(f=2,i){if(a||(o="next"),e=i[o]){if(!(e=e.call(i,c)))throw TypeError("iterator result is not an object");if(!e.done)return e;c=e.value,a<2&&(a=0)}else 1===a&&(e=i.return)&&e.call(i),a<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),a=1);i=n}else if((e=(l=s.n<0)?c:r.call(t,s))!==u)break}catch(e){i=n,a=1,c=e}finally{f=1}}return{value:e,done:l}}}(r,o,i),!0),f}var u={};function a(){}function c(){}function f(){}e=Object.getPrototypeOf;var s=[][t]?e(e([][t]())):(l(e={},t,function(){return this}),e),d=f.prototype=a.prototype=Object.create(s);function v(n){return Object.setPrototypeOf?Object.setPrototypeOf(n,f):(n.__proto__=f,l(n,o,"GeneratorFunction")),n.prototype=Object.create(d),n}return c.prototype=f,l(d,"constructor",f),l(f,"constructor",c),c.displayName="GeneratorFunction",l(f,o,"GeneratorFunction"),l(d),l(d,o,"Generator"),l(d,t,function(){return this}),l(d,"toString",function(){return"[object Generator]"}),(p=function(){return{w:i,m:v}})()}function l(n,e,r,t){var o=Object.defineProperty;try{o({},"",{})}catch(n){o=0}l=function(n,e,r,t){function i(e,r){l(n,e,function(n){return this._invoke(e,r,n)})}e?o?o(n,e,{value:r,enumerable:!t,configurable:!t,writable:!t}):n[e]=r:(i("next",0),i("throw",1),i("return",2))},l(n,e,r,t)}function s(n,e,r,t,o,i,u){try{var a=n[i](u),c=a.value}catch(n){return void r(n)}a.done?e(c):Promise.resolve(c).then(t,o)}var d="undefined"!=typeof window,v=function(){return d&&window.flutter_inappwebview&&window.flutter_inappwebview.callHandler&&"function"==typeof window.flutter_inappwebview.callHandler?window.flutter_inappwebview:null},w=function(){var n,e=(n=p().m(function n(e){var r,t,o,i,u=arguments;return p().w(function(n){for(;;)switch(n.p=n.n){case 0:if(r=u.length>1&&void 0!==u[1]?u[1]:{},t=v()){n.n=1;break}return n.a(2,{ok:!1,error:"BRIDGE_CALL_FAILED"});case 1:return n.p=1,n.n=2,t.callHandler("MiniAppBridge",{action:e,payload:r});case 2:return o=n.v,n.a(2,o);case 3:return n.p=3,i=n.v,console.error("Bridge error:",i),n.a(2,{ok:!1,error:"BRIDGE_CALL_FAILED"})}},n,null,[[1,3]])}),function(){var e=this,r=arguments;return new Promise(function(t,o){var i=n.apply(e,r);function u(n){s(i,t,o,u,a,"next",n)}function a(n){s(i,t,o,u,a,"throw",n)}u(void 0)})});return function(n){return e.apply(this,arguments)}}();function y(){return function(n){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("number"==typeof n)return w("navigate",{type:"native",action:"pop",delta:Math.abs(n)});w("navigate",{type:"native",action:"push",route:n,params:e,options:r})}}var m={locale:"vi",theme:"light",pages:[]};function h(n){var e=(0,i.useRef)(n);(0,i.useEffect)(function(){e.current=n},[n]),(0,i.useEffect)(function(){if("undefined"!=typeof window&&window.MiniApp){var n=function(n){try{var r;null===(r=e.current)||void 0===r||r.call(e,n)}catch(n){console.error("[useListenerScanQr error]",n)}};return window.MiniApp.on("onScanQRResult",n),function(){window.MiniApp.off("onScanQRResult",n)}}},[])}var b=t(848);function g(n,e){(null==e||e>n.length)&&(e=n.length);for(var r=0,t=Array(e);r<e;r++)t[r]=n[r];return t}var A=new Map,j="/";function M(){return A.get(j)}function S(n){j=n}function x(n){var e,r,t=n.config,o=n.children,u=(e=(0,i.useState)(null),r=2,function(n){if(Array.isArray(n))return n}(e)||function(n,e){var r=null==n?null:"undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(null!=r){var t,o,i,u,a=[],c=!0,f=!1;try{if(i=(r=r.call(n)).next,0===e){if(Object(r)!==r)return;c=!1}else for(;!(c=(t=i.call(r)).done)&&(a.push(t.value),a.length!==e);c=!0);}catch(n){f=!0,o=n}finally{try{if(!c&&null!=r.return&&(u=r.return(),Object(u)!==u))return}finally{if(f)throw o}}return a}}(e,r)||function(n,e){if(n){if("string"==typeof n)return g(n,e);var r={}.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?g(n,e):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),a=u[0],c=u[1],f=(0,i.useRef)(!1);(0,i.useEffect)(function(){var n;if(null!=t&&null!==(n=t.pages)&&void 0!==n&&n.length&&!f.current){f.current=!0,function(n){A=new Map(n.pages.map(function(n){return[n.path,n]}))}(t),S((window.location.pathname||"/").replace("/miniapp","")||"/");var e=M();e||(S("/"),e=M()),c(e||null),w("registerAppConfig",{config:t})}},[t]);var p=(0,i.useMemo)(function(){return a&&a.Component?a.Component:null},[a]);return p?(0,b.jsxs)(b.Fragment,{children:[(0,b.jsx)(p,{}),o]}):(console.warn("MiniApp: Page not found"),(0,b.jsx)("div",{}))}return o})());
|
|
@@ -1,76 +1,80 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import React, { useEffect, useState } from "react";
|
|
3
|
+
import React, { useEffect, useMemo, useRef, useState } from "react";
|
|
4
4
|
import { callHost } from "../bridge";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
|
|
6
|
+
/* ================= ROUTER CORE ================= */
|
|
7
|
+
|
|
8
|
+
let routeMap = new Map();
|
|
9
|
+
let currentPath = "/";
|
|
10
|
+
|
|
11
|
+
function initRouter(config) {
|
|
12
|
+
routeMap = new Map(config.pages.map((p) => [p.path, p]));
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
return routeMap.get(currentPath)
|
|
15
|
+
function getCurrentPage() {
|
|
16
|
+
return routeMap.get(currentPath);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
currentPath = path
|
|
19
|
+
function setCurrentPath(path) {
|
|
20
|
+
currentPath = path;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*/
|
|
23
|
+
/* ================= APP ================= */
|
|
24
|
+
|
|
26
25
|
export function App({ config, children }) {
|
|
27
26
|
const [page, setPage] = useState(null);
|
|
27
|
+
const initializedRef = useRef(false);
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
/* ================= INIT ONCE ================= */
|
|
30
30
|
|
|
31
|
+
useEffect(() => {
|
|
31
32
|
if (!config?.pages?.length) return;
|
|
33
|
+
if (initializedRef.current) return;
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
initializedRef.current = true;
|
|
36
|
+
|
|
37
|
+
// 1. Init router
|
|
34
38
|
initRouter(config);
|
|
35
39
|
|
|
36
|
-
// 2. Lấy
|
|
40
|
+
// 2. Lấy path từ URL
|
|
37
41
|
const rawPath = window.location.pathname || "/";
|
|
38
42
|
const path = rawPath.replace("/miniapp", "") || "/";
|
|
39
|
-
console.log(rawPath);
|
|
40
43
|
|
|
41
44
|
setCurrentPath(path);
|
|
42
45
|
|
|
43
|
-
// 3.
|
|
46
|
+
// 3. Resolve page
|
|
44
47
|
let current = getCurrentPage();
|
|
45
|
-
console.warn(current);
|
|
46
48
|
|
|
47
|
-
// 4. Fallback về root nếu không match
|
|
48
49
|
if (!current) {
|
|
49
50
|
setCurrentPath("/");
|
|
50
51
|
current = getCurrentPage();
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
setPage(current);
|
|
54
|
-
|
|
54
|
+
setPage(current || null);
|
|
55
|
+
|
|
56
|
+
// 4. Send config to native (chỉ 1 lần)
|
|
55
57
|
callHost("registerAppConfig", { config });
|
|
56
58
|
}, [config]);
|
|
57
59
|
|
|
60
|
+
/* ================= MEMO COMPONENT ================= */
|
|
58
61
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
<div></div>
|
|
64
|
-
</>
|
|
65
|
-
);
|
|
66
|
-
}
|
|
62
|
+
const CurrentComponent = useMemo(() => {
|
|
63
|
+
if (!page || !page.Component) return null;
|
|
64
|
+
return page.Component;
|
|
65
|
+
}, [page]);
|
|
67
66
|
|
|
68
|
-
|
|
67
|
+
/* ================= RENDER ================= */
|
|
68
|
+
|
|
69
|
+
if (!CurrentComponent) {
|
|
70
|
+
console.warn("MiniApp: Page not found");
|
|
71
|
+
return <div />;
|
|
72
|
+
}
|
|
69
73
|
|
|
70
74
|
return (
|
|
71
75
|
<>
|
|
72
|
-
<
|
|
76
|
+
<CurrentComponent />
|
|
73
77
|
{children}
|
|
74
78
|
</>
|
|
75
79
|
);
|
|
76
|
-
}
|
|
80
|
+
}
|