@vetc-miniapp/ui-react 0.0.23 → 0.0.25

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.
Files changed (146) hide show
  1. package/README.md +375 -56
  2. package/dist/bridge.d.ts +11 -0
  3. package/dist/bridge.js +20 -0
  4. package/dist/components/app.d.ts +6 -0
  5. package/dist/components/app.js +34 -0
  6. package/dist/components/avatar/Avatar.d.ts +21 -0
  7. package/dist/components/avatar/Avatar.js +33 -0
  8. package/dist/components/avatar/index.d.ts +2 -0
  9. package/dist/components/avatar/index.js +1 -0
  10. package/dist/components/bottom-sheet/BottomSheet.d.ts +19 -0
  11. package/dist/components/bottom-sheet/BottomSheet.js +70 -0
  12. package/dist/components/bottom-sheet/index.d.ts +2 -0
  13. package/dist/components/bottom-sheet/index.js +1 -0
  14. package/dist/components/button/Button.d.ts +32 -0
  15. package/dist/components/button/Button.js +165 -0
  16. package/dist/components/button/index.d.ts +2 -0
  17. package/dist/components/button/index.js +1 -0
  18. package/dist/components/button-group/ButtonGroup.d.ts +28 -0
  19. package/dist/components/button-group/ButtonGroup.js +21 -0
  20. package/dist/components/button-group/index.d.ts +2 -0
  21. package/dist/components/button-group/index.js +1 -0
  22. package/dist/components/card/Card.d.ts +18 -0
  23. package/dist/components/card/Card.js +35 -0
  24. package/dist/components/card/index.d.ts +2 -0
  25. package/dist/components/card/index.js +1 -0
  26. package/dist/components/checkbox/Checkbox.d.ts +41 -0
  27. package/dist/components/checkbox/Checkbox.js +94 -0
  28. package/dist/components/checkbox/index.d.ts +2 -0
  29. package/dist/components/checkbox/index.js +1 -0
  30. package/dist/components/chip/Chip.d.ts +24 -0
  31. package/dist/components/chip/Chip.js +83 -0
  32. package/dist/components/chip/index.d.ts +2 -0
  33. package/dist/components/chip/index.js +1 -0
  34. package/dist/components/dialog/Dialog.d.ts +19 -0
  35. package/dist/components/dialog/Dialog.js +51 -0
  36. package/dist/components/dialog/index.d.ts +2 -0
  37. package/dist/components/dialog/index.js +1 -0
  38. package/dist/components/divider/Divider.d.ts +16 -0
  39. package/dist/components/divider/Divider.js +18 -0
  40. package/dist/components/divider/index.d.ts +2 -0
  41. package/dist/components/divider/index.js +1 -0
  42. package/dist/components/input/Input.d.ts +40 -0
  43. package/dist/components/input/Input.js +51 -0
  44. package/dist/components/input/index.d.ts +2 -0
  45. package/dist/components/input/index.js +1 -0
  46. package/dist/components/list/List.d.ts +31 -0
  47. package/dist/components/list/List.js +72 -0
  48. package/dist/components/list/index.d.ts +2 -0
  49. package/dist/components/list/index.js +1 -0
  50. package/dist/components/loading/Loading.d.ts +28 -0
  51. package/dist/components/loading/Loading.js +33 -0
  52. package/dist/components/loading/index.d.ts +2 -0
  53. package/dist/components/loading/index.js +1 -0
  54. package/dist/components/modal/Modal.d.ts +38 -0
  55. package/dist/components/modal/Modal.js +50 -0
  56. package/dist/components/modal/index.d.ts +2 -0
  57. package/dist/components/modal/index.js +1 -0
  58. package/dist/components/navigation-bar/NavigationBar.d.ts +44 -0
  59. package/dist/components/navigation-bar/NavigationBar.js +70 -0
  60. package/dist/components/navigation-bar/index.d.ts +2 -0
  61. package/dist/components/navigation-bar/index.js +1 -0
  62. package/dist/components/radio/Radio.d.ts +40 -0
  63. package/dist/components/radio/Radio.js +88 -0
  64. package/dist/components/radio/index.d.ts +2 -0
  65. package/dist/components/radio/index.js +1 -0
  66. package/dist/components/select/Select.d.ts +29 -0
  67. package/dist/components/select/Select.js +30 -0
  68. package/dist/components/select/index.d.ts +2 -0
  69. package/dist/components/select/index.js +1 -0
  70. package/dist/components/switch/Switch.d.ts +23 -0
  71. package/dist/components/switch/Switch.js +81 -0
  72. package/dist/components/switch/index.d.ts +2 -0
  73. package/dist/components/switch/index.js +1 -0
  74. package/dist/components/tab-bar/TabBar.d.ts +28 -0
  75. package/dist/components/tab-bar/TabBar.js +60 -0
  76. package/dist/components/tab-bar/index.d.ts +2 -0
  77. package/dist/components/tab-bar/index.js +1 -0
  78. package/dist/components/textarea/Textarea.d.ts +31 -0
  79. package/dist/components/textarea/Textarea.js +33 -0
  80. package/dist/components/textarea/index.d.ts +2 -0
  81. package/dist/components/textarea/index.js +1 -0
  82. package/dist/components/toast/Toast.d.ts +41 -0
  83. package/dist/components/toast/Toast.js +61 -0
  84. package/dist/components/toast/index.d.ts +2 -0
  85. package/dist/components/toast/index.js +1 -0
  86. package/dist/components/typography/Typography.d.ts +45 -0
  87. package/dist/components/typography/Typography.js +143 -0
  88. package/dist/components/typography/index.d.ts +2 -0
  89. package/dist/components/typography/index.js +1 -0
  90. package/dist/hooks/use-app-pause.d.ts +6 -0
  91. package/dist/hooks/use-app-pause.js +29 -0
  92. package/dist/hooks/use-app-resume.d.ts +6 -0
  93. package/dist/hooks/use-app-resume.js +28 -0
  94. package/{src/ui-react/hooks/use-app-state.ts → dist/hooks/use-app-state.d.ts} +0 -1
  95. package/dist/hooks/use-app-state.js +1 -0
  96. package/dist/hooks/use-did-hide.d.ts +6 -0
  97. package/dist/hooks/use-did-hide.js +21 -0
  98. package/dist/hooks/use-did-show.d.ts +6 -0
  99. package/dist/hooks/use-did-show.js +21 -0
  100. package/dist/hooks/use-listener-scan-qr.d.ts +21 -0
  101. package/dist/hooks/use-listener-scan-qr.js +29 -0
  102. package/dist/hooks/use-navigate.d.ts +8 -0
  103. package/dist/hooks/use-navigate.js +23 -0
  104. package/dist/hooks/use-tap-app-bar.d.ts +6 -0
  105. package/dist/hooks/use-tap-app-bar.js +21 -0
  106. package/dist/index.d.ts +56 -0
  107. package/dist/index.js +41 -0
  108. package/dist/styles/VETCProvider.d.ts +114 -0
  109. package/dist/styles/VETCProvider.js +124 -0
  110. package/dist/styles/tokens.css +448 -0
  111. package/dist/tokens/colors.d.ts +127 -0
  112. package/dist/tokens/colors.js +75 -0
  113. package/dist/tokens/index.d.ts +3 -0
  114. package/dist/tokens/index.js +3 -0
  115. package/dist/tokens/spacing.d.ts +56 -0
  116. package/dist/tokens/spacing.js +56 -0
  117. package/dist/tokens/typography.d.ts +121 -0
  118. package/dist/tokens/typography.js +57 -0
  119. package/dist/types/app.d.ts +21 -0
  120. package/dist/types/app.js +1 -0
  121. package/package.json +26 -8
  122. package/src/dist/ui-react/index.js +0 -2
  123. package/src/dist/ui-react/index.js.LICENSE.txt +0 -11
  124. package/src/ui-react/bridge.js +0 -36
  125. package/src/ui-react/bridge.ts +0 -48
  126. package/src/ui-react/components/app.d.ts +0 -7
  127. package/src/ui-react/components/app.jsx +0 -80
  128. package/src/ui-react/components/app.tsx +0 -42
  129. package/src/ui-react/components/app1.js +0 -101
  130. package/src/ui-react/hooks/use-app-pause.js +0 -35
  131. package/src/ui-react/hooks/use-app-pause.ts +0 -33
  132. package/src/ui-react/hooks/use-app-resume.js +0 -37
  133. package/src/ui-react/hooks/use-app-resume.ts +0 -32
  134. package/src/ui-react/hooks/use-app-state.js +0 -35
  135. package/src/ui-react/hooks/use-did-hide.js +0 -25
  136. package/src/ui-react/hooks/use-did-hide.ts +0 -34
  137. package/src/ui-react/hooks/use-did-show.js +0 -26
  138. package/src/ui-react/hooks/use-did-show.ts +0 -34
  139. package/src/ui-react/hooks/use-listener-scan-qr.js +0 -33
  140. package/src/ui-react/hooks/use-listener-scan-qr.ts +0 -52
  141. package/src/ui-react/hooks/use-navigate.js +0 -15
  142. package/src/ui-react/hooks/use-navigate.ts +0 -41
  143. package/src/ui-react/index.js +0 -8
  144. package/src/ui-react/index.ts +0 -9
  145. package/src/ui-react/types/app.js +0 -30
  146. package/src/ui-react/types/app.ts +0 -32
@@ -1,48 +0,0 @@
1
- /* ================= Types ================= */
2
-
3
- export interface FlutterInAppWebViewBridge {
4
- callHandler(
5
- handlerName: string,
6
- args?: unknown
7
- ): Promise<unknown>;
8
- }
9
-
10
- declare global {
11
- interface Window {
12
- flutter_inappwebview?: FlutterInAppWebViewBridge;
13
- }
14
- }
15
-
16
- /* ================= Runtime helpers ================= */
17
-
18
- export const isBrowser: boolean = typeof window !== "undefined";
19
-
20
- export const getBridge = (): FlutterInAppWebViewBridge | null => {
21
- if (!isBrowser || !window.flutter_inappwebview || !window.flutter_inappwebview.callHandler || typeof window.flutter_inappwebview.callHandler != "function") {
22
- return null;
23
- }
24
- return window.flutter_inappwebview;
25
- };
26
-
27
- /* ================= Core bridge call ================= */
28
-
29
- export const callHost = <T = unknown>(
30
- action: string,
31
- payload: Record<string, unknown> = {}
32
- ): Promise<T> => {
33
- const bridge = getBridge();
34
-
35
- if (!bridge) {
36
-
37
- return {
38
- ok: false,
39
- error: "BRIDGE_CALL_FAILED"
40
- };
41
- // return Promise.reject(new Error("MiniApp bridge not available"));
42
- }
43
-
44
- return bridge.callHandler("MiniAppBridge", {
45
- action,
46
- payload,
47
- }) as Promise<T>;
48
- };
@@ -1,7 +0,0 @@
1
- import { ComponentPropsWithRef, ReactNode } from 'react';
2
- import { IAppConfig } from '../types/app';
3
-
4
- export type IAppProps = ComponentPropsWithRef<'div'> & {
5
- config: IAppConfig;
6
- };
7
- export declare const App: ({ className, config, localesConfig, ...props }: IAppProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,80 +0,0 @@
1
- "use client";
2
-
3
- import React, { useEffect, useMemo, useRef, useState } from "react";
4
- import { callHost } from "../bridge";
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
- }
14
-
15
- function getCurrentPage() {
16
- return routeMap.get(currentPath);
17
- }
18
-
19
- function setCurrentPath(path) {
20
- currentPath = path;
21
- }
22
-
23
- /* ================= APP ================= */
24
-
25
- export function App({ config, children }) {
26
- const [page, setPage] = useState(null);
27
- const initializedRef = useRef(false);
28
-
29
- /* ================= INIT ONCE ================= */
30
-
31
- useEffect(() => {
32
- if (!config?.pages?.length) return;
33
- if (initializedRef.current) return;
34
-
35
- initializedRef.current = true;
36
-
37
- // 1. Init router
38
- initRouter(config);
39
-
40
- // 2. Lấy path từ URL
41
- const rawPath = window.location.pathname || "/";
42
- const path = rawPath.replace("/miniapp", "") || "/";
43
-
44
- setCurrentPath(path);
45
-
46
- // 3. Resolve page
47
- let current = getCurrentPage();
48
-
49
- if (!current) {
50
- setCurrentPath("/");
51
- current = getCurrentPage();
52
- }
53
-
54
- setPage(current || null);
55
-
56
- // 4. Send config to native (chỉ 1 lần)
57
- callHost("registerAppConfig", { config });
58
- }, [config]);
59
-
60
- /* ================= MEMO COMPONENT ================= */
61
-
62
- const CurrentComponent = useMemo(() => {
63
- if (!page || !page.Component) return null;
64
- return page.Component;
65
- }, [page]);
66
-
67
- /* ================= RENDER ================= */
68
-
69
- if (!CurrentComponent) {
70
- console.warn("MiniApp: Page not found");
71
- return <div />;
72
- }
73
-
74
- return (
75
- <>
76
- <CurrentComponent />
77
- {children}
78
- </>
79
- );
80
- }
@@ -1,42 +0,0 @@
1
- import { useEffect, useState } from "react";
2
- let routeMap = new Map()
3
- let currentPath = "/"
4
-
5
- export function initRouter(config) {
6
- routeMap = new Map(config.pages.map(p => [p.pathname, p]))
7
- }
8
-
9
- export function getCurrentPage() {
10
- return routeMap.get(currentPath)
11
- }
12
-
13
- export function setCurrentPath(path) {
14
- currentPath = path
15
- }
16
-
17
- export function App({ config }) {
18
- const [page, setPage] = useState(null)
19
-
20
- useEffect(() => {
21
- initRouter(config)
22
-
23
- const rawPath = window.location.pathname || "/"
24
- const path = rawPath.replace("/miniapp", "") || "/"
25
-
26
- setCurrentPath(path)
27
-
28
- let current = getCurrentPage()
29
- if (!current) {
30
- setCurrentPath("/")
31
- current = getCurrentPage()
32
- }
33
-
34
- setPage(current)
35
-
36
- callHost("registerAppConfig", { config })
37
- }, [config])
38
-
39
- if (!page) return <div />
40
- const Component = page.Component
41
- return <Component />
42
- }
@@ -1,101 +0,0 @@
1
- //'use client';
2
-
3
- /**
4
- * @typedef {import('../types/app').IAppConfig} IAppConfig
5
- */
6
-
7
- /**
8
- * @typedef {Object} IAppProps
9
- * @property {IAppConfig} config
10
- * @property {string=} className
11
- */
12
-
13
- /**
14
- * Root MiniApp wrapper
15
- * @param {IAppProps & React.HTMLAttributes<HTMLDivElement>} props
16
- */
17
- //import React from "react";
18
- 'use client';
19
-
20
- import { useEffect } from "react";
21
- import {callHost} from '../bridge.js';
22
- ////import MiniApp from "miniapp-sdk";
23
-
24
- /**
25
- * Root MiniApp wrapper
26
- */
27
- export function App({ className, config, localesConfig, ...props }) {
28
-
29
- useEffect(() => {
30
- // 🚀 Gửi config sang Host khi MiniApp mount
31
- if (config) {
32
- console.log(config);
33
- callHost("registerAppConfig", { config }).catch(console.error);
34
- }
35
- //
36
- // // 🎯 Ví dụ lắng nghe event từ Host
37
- // const handleStateUpdate = (data) => {
38
- // console.log("App state updated from host:", data);
39
- // };
40
- //
41
- // MiniApp.on?.("APP_STATE_UPDATE", handleStateUpdate);
42
- //
43
- // return () => {
44
- // MiniApp.off?.("APP_STATE_UPDATE", handleStateUpdate);
45
- // };
46
- }, [config]);
47
-
48
- return props.children;
49
- }
50
-
51
- //'use client'
52
- //
53
- //import React, { createContext, useContext, useMemo, useState, useEffect } from "react"
54
- //import {callHost} from '../bridge.js';
55
- //
56
- //const RouterCtx = createContext(null)
57
- //
58
- //export function MiniRouterProvider({ config, initialPath, children }) {
59
- // const [path, setPath] = useState(initialPath || "/")
60
- //
61
- // const routeMap = useMemo(
62
- // () => new Map(config.pages.map(p => [p.pathname, p])),
63
- // [config.pages]
64
- // )
65
- //
66
- // const value = useMemo(() => ({ path, setPath, routeMap }), [path, routeMap])
67
- //
68
- // return <RouterCtx.Provider value={value}>{children}</RouterCtx.Provider>
69
- //}
70
- //
71
- //export function useMiniRouter() {
72
- // const ctx = useContext(RouterCtx)
73
- // if (!ctx) throw new Error("useMiniRouter must be used inside MiniRouterProvider")
74
- // return ctx
75
- //}
76
- //
77
- //function Renderer({ config }) {
78
- // const { path, routeMap } = useMiniRouter()
79
- // const page = routeMap.get(path)
80
- //
81
- // useEffect(() => {
82
- // if (!page) return
83
- // callHost("registerAppConfig", {
84
- // config
85
- // })
86
- // }, [path, page])
87
- //
88
- // if (!page) return <div>Page not found</div>
89
- // const Component = page.Component
90
- // return <Component />
91
- //}
92
- //
93
- //export function App({ config, initialPath, ...props }) {
94
- // return (
95
- // <MiniRouterProvider config={config} initialPath={initialPath}>
96
- // <Renderer config={config}>{props.children}</Renderer>
97
- // </MiniRouterProvider>
98
- // )
99
- //}
100
-
101
-
@@ -1,35 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- /**
4
- * useAppPause
5
- *
6
- * Trigger khi Native App đi vào background.
7
- */
8
- export function useAppPause(callback) {
9
- const savedCallback = useRef(callback);
10
-
11
- // Luôn giữ callback mới nhất
12
- useEffect(() => {
13
- savedCallback.current = callback;
14
- }, [callback]);
15
-
16
- useEffect(() => {
17
- if (typeof window === "undefined" || !window.MiniApp) return;
18
-
19
- const handler = () => {
20
- try {
21
- if (savedCallback.current) {
22
- savedCallback.current();
23
- }
24
- } catch (err) {
25
- console.error("[useAppPause error]", err);
26
- }
27
- };
28
-
29
- window.MiniApp.on("appPause", handler);
30
-
31
- return () => {
32
- window.MiniApp.off("appPause", handler);
33
- };
34
- }, []);
35
- }
@@ -1,33 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- /**
4
- * useAppPause
5
- *
6
- * Trigger khi Native App đi vào background.
7
- */
8
- export function useAppPause(callback: () => void) {
9
- const savedCallback = useRef(callback);
10
-
11
- // giữ callback luôn mới nhất
12
- useEffect(() => {
13
- savedCallback.current = callback;
14
- }, [callback]);
15
-
16
- useEffect(() => {
17
- if (typeof window === "undefined" || !window.MiniApp) return;
18
-
19
- const handler = () => {
20
- try {
21
- savedCallback.current?.();
22
- } catch (err) {
23
- console.error("[useAppPause error]", err);
24
- }
25
- };
26
-
27
- window.MiniApp.on("appPause", handler);
28
-
29
- return () => {
30
- window.MiniApp.off("appPause", handler);
31
- };
32
- }, []);
33
- }
@@ -1,37 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- /**
4
- * useAppResume
5
- *
6
- * Trigger khi Native App quay lại foreground.
7
- */
8
- export function useAppResume(callback) {
9
- const savedCallback = useRef(callback);
10
-
11
- // Cập nhật callback mới nhất
12
- useEffect(() => {
13
- savedCallback.current = callback;
14
- }, [callback]);
15
-
16
- useEffect(() => {
17
- console.warn("MiniApp: useAppResume");
18
-
19
- if (typeof window === "undefined" || !window.MiniApp) return;
20
-
21
- const handler = () => {
22
- try {
23
- if (savedCallback.current) {
24
- savedCallback.current();
25
- }
26
- } catch (err) {
27
- console.error("[useAppResume error]", err);
28
- }
29
- };
30
-
31
- window.MiniApp.on("appResume", handler);
32
-
33
- return () => {
34
- window.MiniApp.off("appResume", handler);
35
- };
36
- }, []);
37
- }
@@ -1,32 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- /**
4
- * useAppResume
5
- *
6
- * Trigger khi Native App quay lại foreground.
7
- */
8
- export function useAppResume(callback: () => void) {
9
- const savedCallback = useRef(callback);
10
-
11
- useEffect(() => {
12
- savedCallback.current = callback;
13
- }, [callback]);
14
-
15
- useEffect(() => {
16
- if (typeof window === "undefined" || !window.MiniApp) return;
17
-
18
- const handler = () => {
19
- try {
20
- savedCallback.current?.();
21
- } catch (err) {
22
- console.error("[useAppResume error]", err);
23
- }
24
- };
25
-
26
- window.MiniApp.on("appResume", handler);
27
-
28
- return () => {
29
- window.MiniApp.off("appResume", handler);
30
- };
31
- }, []);
32
- }
@@ -1,35 +0,0 @@
1
- /**
2
- * @typedef {"light" | "dark"} ThemeMode
3
- */
4
-
5
- /**
6
- * @typedef {Object} IAppState
7
- * @property {string=} locale
8
- * @property {ThemeMode=} theme
9
- */
10
-
11
- /**
12
- * @typedef {Object} IPageConfig
13
- * @property {string} key - Key định danh page dùng cho navigation
14
- * @property {string=} title - Tên hiển thị (header native)
15
- * @property {string} url - URL nội bộ miniapp (nếu cần render state)
16
- * @property {boolean=} canGoBack - Có cho phép back không
17
- * @property {boolean=} isRoot - Có phải root page không
18
- * @property {Object<string, any>=} defaultParams - Params mặc định khi mở page
19
- */
20
-
21
- /**
22
- * @typedef {Object} IAppConfig
23
- * @property {IPageConfig[]} pages
24
- * @property {string=} locale
25
- * @property {ThemeMode=} theme
26
- */
27
-
28
- /** @type {IAppConfig} */
29
- export const appConfig = {
30
- locale: "vi",
31
- theme: "light",
32
-
33
- pages: [
34
- ]
35
- };
@@ -1,25 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- export function useDidHide(route, callback) {
4
- const saved = useRef(callback);
5
-
6
- useEffect(() => {
7
- saved.current = callback;
8
- }, [callback]);
9
-
10
- useEffect(() => {
11
- if (!window.MiniApp) return;
12
-
13
- const handler = (data) => {
14
- if (data?.route === route) {
15
- saved.current?.(data);
16
- }
17
- };
18
-
19
- window.MiniApp.on("didHide", handler);
20
-
21
- return () => {
22
- window.MiniApp.off("didHide", handler);
23
- };
24
- }, [route]);
25
- }
@@ -1,34 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- type DidHidePayload = {
4
- route?: string;
5
- [key: string]: any;
6
- };
7
-
8
- export function useDidHide(
9
- route: string,
10
- callback: (data?: DidHidePayload) => void
11
- ): void {
12
- const saved = useRef(callback);
13
-
14
- // luôn giữ callback mới nhất
15
- useEffect(() => {
16
- saved.current = callback;
17
- }, [callback]);
18
-
19
- useEffect(() => {
20
- if (typeof window === "undefined" || !window.MiniApp) return;
21
-
22
- const handler = (data?: DidHidePayload) => {
23
- if (data?.route === route) {
24
- saved.current?.(data);
25
- }
26
- };
27
-
28
- window.MiniApp.on("didHide", handler);
29
-
30
- return () => {
31
- window.MiniApp.off("didHide", handler);
32
- };
33
- }, [route]);
34
- }
@@ -1,26 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- export function useDidShow(route, callback) {
4
- const saved = useRef(callback);
5
-
6
- useEffect(() => {
7
- saved.current = callback;
8
- }, [callback]);
9
-
10
- useEffect(() => {
11
- if (!window.MiniApp) return;
12
-
13
- const handler = (data) => {
14
- if (data?.route === route) {
15
- console.log("didShow", data)
16
- saved.current?.(data);
17
- }
18
- };
19
-
20
- window.MiniApp.on("didShow", handler);
21
-
22
- return () => {
23
- window.MiniApp.off("didShow", handler);
24
- };
25
- }, [route]);
26
- }
@@ -1,34 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- type DidShowPayload = {
4
- route?: string;
5
- [key: string]: any;
6
- };
7
-
8
- export function useDidShow(
9
- route: string,
10
- callback: (data?: DidShowPayload) => void
11
- ): void {
12
- const saved = useRef(callback);
13
-
14
- // luôn giữ callback mới nhất
15
- useEffect(() => {
16
- saved.current = callback;
17
- }, [callback]);
18
-
19
- useEffect(() => {
20
- if (typeof window === "undefined" || !window.MiniApp) return;
21
-
22
- const handler = (data?: DidShowPayload) => {
23
- if (data?.route === route) {
24
- saved.current?.(data);
25
- }
26
- };
27
-
28
- window.MiniApp.on("didShow", handler);
29
-
30
- return () => {
31
- window.MiniApp.off("didShow", handler);
32
- };
33
- }, [route]);
34
- }
@@ -1,33 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- /**
4
- * useListenerScanQr
5
- *
6
- * Lắng nghe kết quả scan QR từ Native
7
- */
8
- export function useListenerScanQr(callback) {
9
- const savedCallback = useRef(callback);
10
-
11
- // luôn giữ callback mới nhất
12
- useEffect(() => {
13
- savedCallback.current = callback;
14
- }, [callback]);
15
-
16
- useEffect(() => {
17
- if (typeof window === "undefined" || !window.MiniApp) return;
18
-
19
- const handler = (data) => {
20
- try {
21
- savedCallback.current?.(data);
22
- } catch (err) {
23
- console.error("[useListenerScanQr error]", err);
24
- }
25
- };
26
-
27
- window.MiniApp.on("onScanQRResult", handler);
28
-
29
- return () => {
30
- window.MiniApp.off("onScanQRResult", handler);
31
- };
32
- }, []);
33
- }
@@ -1,52 +0,0 @@
1
- import { useEffect, useRef } from "react";
2
-
3
- type ScanQrResult = {
4
- raw: string;
5
- type?: string;
6
- [key: string]: any;
7
- };
8
-
9
- type MiniAppBridge = {
10
- on: (event: string, callback: (data: any) => void) => void;
11
- off: (event: string, callback: (data: any) => void) => void;
12
- };
13
-
14
- declare global {
15
- interface Window {
16
- MiniApp?: MiniAppBridge;
17
- }
18
- }
19
-
20
- /**
21
- * useListenerScanQr
22
- *
23
- * Lắng nghe kết quả scan QR từ Native
24
- */
25
- export function useListenerScanQr(
26
- callback: (data: ScanQrResult) => void
27
- ): void {
28
- const savedCallback = useRef(callback);
29
-
30
- // luôn giữ callback mới nhất
31
- useEffect(() => {
32
- savedCallback.current = callback;
33
- }, [callback]);
34
-
35
- useEffect(() => {
36
- if (typeof window === "undefined" || !window.MiniApp) return;
37
-
38
- const handler = (data: ScanQrResult) => {
39
- try {
40
- savedCallback.current?.(data);
41
- } catch (err) {
42
- console.error("[useListenerScanQr error]", err);
43
- }
44
- };
45
-
46
- window.MiniApp.on("scanQrResult", handler);
47
-
48
- return () => {
49
- window.MiniApp.off("scanQrResult", handler);
50
- };
51
- }, []);
52
- }
@@ -1,15 +0,0 @@
1
- import { callHost } from '../bridge.js';
2
-
3
- export function useNavigate() {
4
- return (route, params = {}, options = {}) => {
5
- // POP navigation
6
- if (typeof route === "number") {
7
- return callHost("navigate", {
8
- type: "native",
9
- action: "pop",
10
- delta: Math.abs(route)
11
- });
12
- }
13
- callHost("navigate", { type: "native", action: "push", route, params, options });
14
- };
15
- }