@tma.js/sdk-react 1.1.7 → 1.1.9
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/README.md +2 -2
- package/dist/index.cjs +0 -1
- package/dist/index.mjs +0 -1
- package/package.json +3 -4
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/DisplayGate.tsx +0 -43
- package/src/createHoc.tsx +0 -32
- package/src/createHook.ts +0 -31
- package/src/index.ts +0 -54
- package/src/provider/SDKContext.ts +0 -9
- package/src/provider/SDKProvider.tsx +0 -65
- package/src/provider/index.ts +0 -7
- package/src/provider/types.ts +0 -38
- package/src/provider/useInitResultDynamicValue.ts +0 -34
- package/src/provider/useInitResultValue.ts +0 -16
- package/src/provider/useSDKContext.ts +0 -11
- package/src/provider/withSDKContext.tsx +0 -21
- package/src/tools/back-button.ts +0 -12
- package/src/tools/closing-behavior.ts +0 -12
- package/src/tools/cloud-storage.ts +0 -12
- package/src/tools/haptic-feedback.ts +0 -12
- package/src/tools/index.ts +0 -17
- package/src/tools/init-data-raw.ts +0 -12
- package/src/tools/init-data.ts +0 -12
- package/src/tools/invoice.ts +0 -12
- package/src/tools/launch-params.tsx +0 -21
- package/src/tools/main-button.ts +0 -12
- package/src/tools/mini-app.ts +0 -12
- package/src/tools/popup.ts +0 -12
- package/src/tools/post-event.ts +0 -12
- package/src/tools/qr-scanner.ts +0 -12
- package/src/tools/settings-button.ts +0 -12
- package/src/tools/theme-params.ts +0 -12
- package/src/tools/utils.ts +0 -12
- package/src/tools/viewport.ts +0 -12
- package/src/types.ts +0 -30
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
[sdk-react-code-link]: https://github.com/Telegram-Mini-Apps/tma.js/tree/master/packages/sdk-react
|
|
8
8
|
|
|
9
|
-
[sdk-react-docs-link]: https://docs.telegram-mini-apps.com/packages/
|
|
9
|
+
[sdk-react-docs-link]: https://docs.telegram-mini-apps.com/packages/tma-js-sdk-react
|
|
10
10
|
|
|
11
11
|
[sdk-react-npm-link]: https://npmjs.com/package/@tma.js/sdk-react
|
|
12
12
|
|
|
@@ -20,6 +20,6 @@
|
|
|
20
20
|
[![code-badge]][sdk-react-code-link]
|
|
21
21
|
|
|
22
22
|
React JS bindings
|
|
23
|
-
for [client SDK](https://docs.telegram-mini-apps.com/packages/
|
|
23
|
+
for [client SDK](https://docs.telegram-mini-apps.com/packages/tma-js-sdk). Includes
|
|
24
24
|
hooks, components and utilities for comfortable usage of React JS on the Telegram Mini Apps
|
|
25
25
|
platform.
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),p=require("@tma.js/sdk");function s(t,e){return i=>{function o(a){const l={...a,[t]:e()};return n.createElement(i,{...l})}return Object.defineProperty(o,"name",{value:`With${t[0].toUpperCase()}${t.slice(1)}`}),o}}const u=n.createContext({loading:!1});u.displayName="SDKContext";function H({options:t,children:e}){const[i,o]=n.useState(!1),[a,l]=n.useState(),[h,O]=n.useState();n.useEffect(()=>{o(!0),p.init({...t,complete:!0}).then(O).catch(l).finally(()=>o(!1))},[]);const F=n.useMemo(()=>{const w={loading:i};return a&&(w.error=a),h&&(w.initResult=h),w},[i,h,a]);return n.createElement(u.Provider,{value:F},e)}function L({options:t={},children:e}){const i=n.useMemo(()=>{const o={loading:!1};try{o.initResult=p.init({...t,complete:!1})}catch(a){o.error=a}return o},[]);return n.createElement(u.Provider,{value:i},e)}function U(t){const{options:e={},async:i}=t;return e.async||e.complete||i?n.createElement(H,{...t}):n.createElement(L,{...t})}function c(){return n.useContext(u)}function d(t){const{initResult:e}=c();if(!e)throw new Error(`Unable to get init result key "${t}" as long as SDK is not initialized`);return e[t]}function m(t){return Object.create(Object.getPrototypeOf(t),Object.getOwnPropertyDescriptors(t))}function V(t){const e=d(t),[i,o]=n.useState(m(e));return n.useEffect(()=>e.on("change",()=>{o(m(e))}),[e]),i}function j(t){return function(i){const o={...i,sdk:c()};return n.createElement(t,{...o})}}function r(t,e){return e?()=>V(t):()=>d(t)}const S=r("backButton",!0),T=s("backButton",S),g=r("closingBehavior",!0),q=s("closingBehavior",g),v=r("cloudStorage"),Q=s("cloudStorage",v),P=r("hapticFeedback"),W=s("hapticFeedback",P),B=r("initData"),$=s("initData",B),D=r("initDataRaw"),G=s("initDataRaw",D),C=r("invoice",!0),z=s("invoice",C);function E(){return n.useMemo(()=>p.retrieveLaunchData().launchParams,[])}function N(t){return function(i){return n.createElement(t,{...i,launchParams:E()})}}const y=r("mainButton",!0),J=s("mainButton",y),I=r("miniApp",!0),X=s("miniApp",I),R=r("popup",!0),Y=s("popup",R),b=r("postEvent"),Z=s("postEvent",b),k=r("settingsButton",!0),_=s("settingsButton",k),x=r("qrScanner",!0),tt=s("qrScanner",x),M=r("themeParams",!0),et=s("themeParams",M),K=r("utils"),nt=s("utils",K),A=r("viewport",!0),it=s("viewport",A);function f(t,e={}){return typeof t=="function"?n.createElement(t,{...e}):t}function ot(t){const{loading:e,initResult:i,error:o}=c();return i?t.children:o?f(t.error,{error:o}):f(e?t.loading:t.initial)}exports.DisplayGate=ot;exports.SDKContext=u;exports.SDKProvider=U;exports.useBackButton=S;exports.useClosingBehavior=g;exports.useCloudStorage=v;exports.useHapticFeedback=P;exports.useInitData=B;exports.useInitDataRaw=D;exports.useInvoice=C;exports.useLaunchParams=E;exports.useMainButton=y;exports.useMiniApp=I;exports.usePopup=R;exports.usePostEvent=b;exports.useQRScanner=x;exports.useSDKContext=c;exports.useSettingsButton=k;exports.useThemeParams=M;exports.useUtils=K;exports.useViewport=A;exports.withBackButton=T;exports.withClosingBehavior=q;exports.withCloudStorage=Q;exports.withHapticFeedback=W;exports.withInitData=$;exports.withInitDataRaw=G;exports.withInvoice=z;exports.withLaunchParams=N;exports.withMainButton=J;exports.withMiniApp=X;exports.withPopup=Y;exports.withPostEvent=Z;exports.withQRScanner=tt;exports.withSDKContext=j;exports.withSettingsButton=_;exports.withThemeParams=et;exports.withUtils=nt;exports.withViewport=it;
|
|
2
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tma.js/sdk-react",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"description": "React bindings for Mini Apps client SDK. Contains hooks, components and other useful tools which allow usage of React along with Mini Apps client SDK.",
|
|
5
5
|
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
|
@@ -22,8 +22,7 @@
|
|
|
22
22
|
"type": "module",
|
|
23
23
|
"sideEffects": false,
|
|
24
24
|
"files": [
|
|
25
|
-
"dist"
|
|
26
|
-
"src"
|
|
25
|
+
"dist"
|
|
27
26
|
],
|
|
28
27
|
"main": "./dist/index.cjs",
|
|
29
28
|
"module": "./dist/index.mjs",
|
|
@@ -46,7 +45,7 @@
|
|
|
46
45
|
}
|
|
47
46
|
},
|
|
48
47
|
"dependencies": {
|
|
49
|
-
"@tma.js/sdk": "1.4.
|
|
48
|
+
"@tma.js/sdk": "1.4.6"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
51
|
"build-utils": "0.0.1",
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/createHoc.tsx","../src/provider/SDKContext.ts","../src/provider/SDKProvider.tsx","../src/provider/useSDKContext.ts","../src/provider/useInitResultValue.ts","../src/provider/useInitResultDynamicValue.ts","../src/provider/withSDKContext.tsx","../src/createHook.ts","../src/tools/back-button.ts","../src/tools/closing-behavior.ts","../src/tools/cloud-storage.ts","../src/tools/haptic-feedback.ts","../src/tools/init-data.ts","../src/tools/init-data-raw.ts","../src/tools/invoice.ts","../src/tools/launch-params.tsx","../src/tools/main-button.ts","../src/tools/mini-app.ts","../src/tools/popup.ts","../src/tools/post-event.ts","../src/tools/settings-button.ts","../src/tools/qr-scanner.ts","../src/tools/theme-params.ts","../src/tools/utils.ts","../src/tools/viewport.ts","../src/DisplayGate.tsx"],"sourcesContent":["import React, { type ComponentType } from 'react';\n\nimport type { Hook } from './createHook.js';\nimport type { InitResultKey, InitResultValue } from './types.js';\n\nexport type Hoc<K extends InitResultKey> = <P extends { [Key in K]?: InitResultValue<K> }>(\n Component: ComponentType<P>,\n) => ComponentType<P>;\n\n/**\n * Creates HOC for static init result value.\n * @param initResultKey - init result key.\n * @param hook - hook which returns init result value.\n */\nexport function createHoc<K extends InitResultKey>(initResultKey: K, hook: Hook<K>): Hoc<K> {\n return (Component: ComponentType<any>) => {\n function Wrapper(props: Record<string, unknown>) {\n const wrappedProps = {\n ...props,\n [initResultKey]: hook(),\n };\n\n return <Component {...wrappedProps} />;\n }\n\n Object.defineProperty(Wrapper, 'name', {\n value: `With${initResultKey[0].toUpperCase()}${initResultKey.slice(1)}`,\n });\n\n return Wrapper;\n };\n}\n","import { createContext } from 'react';\n\nimport type { SDKContextType } from './types.js';\n\nexport const SDKContext = createContext<SDKContextType>({\n loading: false,\n});\n\nSDKContext.displayName = 'SDKContext';\n","import { init } from '@tma.js/sdk';\nimport React, { useEffect, useMemo, useState } from 'react';\n\nimport { SDKContext } from './SDKContext.js';\nimport type { SDKContextType, SDKProviderProps } from './types.js';\nimport type { InitResult } from '../types.js';\n\nfunction AsyncProvider({ options, children }: SDKProviderProps) {\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<unknown | undefined>();\n const [initResult, setInitResult] = useState<InitResult | undefined>();\n\n useEffect(() => {\n setLoading(true);\n\n init({ ...options, complete: true })\n .then(setInitResult)\n .catch(setError)\n .finally(() => setLoading(false));\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n const context = useMemo<SDKContextType>(() => {\n const result: SDKContextType = { loading };\n if (error) {\n result.error = error;\n }\n\n if (initResult) {\n result.initResult = initResult;\n }\n\n return result;\n }, [loading, initResult, error]);\n\n return <SDKContext.Provider value={context}>{children}</SDKContext.Provider>;\n}\n\nfunction SyncProvider({ options = {}, children }: SDKProviderProps) {\n const context = useMemo<SDKContextType>(() => {\n const result: SDKContextType = { loading: false };\n\n try {\n result.initResult = init({ ...options, complete: false });\n } catch (e) {\n result.error = e;\n }\n\n return result;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return <SDKContext.Provider value={context}>{children}</SDKContext.Provider>;\n}\n\n/**\n * Component which provides access to SDK initialization state.\n */\nexport function SDKProvider(props: SDKProviderProps) {\n const { options = {}, async } = props;\n\n return options.async || options.complete || async\n ? <AsyncProvider {...props} />\n : <SyncProvider {...props} />;\n}\n","import { useContext } from 'react';\n\nimport { SDKContext } from './SDKContext.js';\nimport type { SDKContextType } from './types.js';\n\n/**\n * Returns current SDK information.\n */\nexport function useSDKContext(): SDKContextType {\n return useContext(SDKContext);\n}\n","import { useSDKContext } from './useSDKContext.js';\nimport type { InitResultKey, InitResultValue } from '../types.js';\n\n/**\n * Returns value by its field name from SDK init result.\n * @param key - init result key.\n * @throws {Error} SDK is not initialized.\n */\nexport function useInitResultValue<K extends InitResultKey>(key: K): InitResultValue<K> {\n const { initResult } = useSDKContext();\n\n if (!initResult) {\n throw new Error(`Unable to get init result key \"${key}\" as long as SDK is not initialized`);\n }\n return initResult[key];\n}\n","import { useEffect, useState } from 'react';\n\nimport { useInitResultValue } from './useInitResultValue.js';\nimport type {\n DynamicInitResultKey,\n InitResultValue,\n Trackable,\n} from '../types.js';\n\nfunction shallowCopy<T>(value: T): T {\n return Object.create(\n Object.getPrototypeOf(value),\n Object.getOwnPropertyDescriptors(value),\n );\n}\n\nexport function useInitResultDynamicValue<K extends DynamicInitResultKey>(\n initResultKey: K,\n): InitResultValue<K> {\n // Extract init result value.\n const initial = useInitResultValue(initResultKey);\n\n // Create value which will be returned to the external environment.\n const [copy, setCopy] = useState(shallowCopy(initial));\n\n // Effect which listens to change event and calls update.\n useEffect(() => {\n return (initial as Trackable).on('change', () => {\n setCopy(shallowCopy(initial));\n });\n }, [initial]);\n\n return copy;\n}\n","import React, { type ComponentType } from 'react';\n\nimport type { SDKContextType } from './types.js';\nimport { useSDKContext } from './useSDKContext.js';\n\n/**\n * HOC which passes SDK context to wrapped React component.\n * @param Component - component to wrap.\n */\nexport function withSDKContext<P extends { sdk?: SDKContextType }>(\n Component: ComponentType<P>,\n): ComponentType<Omit<P, 'sdk'>> {\n return function WithSDKContext(props: Omit<P, 'sdk'>) {\n const p = {\n ...props,\n sdk: useSDKContext(),\n } as P;\n\n return <Component {...p} />;\n };\n}\n","import { useInitResultDynamicValue, useInitResultValue } from './provider/index.js';\nimport type {\n DynamicInitResultKey,\n InitResultKey,\n InitResultValue,\n StaticInitResultKey,\n} from './types.js';\n\nexport type Hook<K extends InitResultKey> = () => InitResultValue<K>;\n\n/**\n * Creates hook to retrieve static init result value.\n * @param initResultKey - init result key.\n */\nexport function createHook<K extends StaticInitResultKey>(initResultKey: K): Hook<K>;\n\n/**\n * Creates hook to retrieve dynamic init result value.\n * @param initResultKey - init result key.\n * @param dynamic - flag to let function know this init result value is dynamic.\n */\nexport function createHook<K extends DynamicInitResultKey>(\n initResultKey: K,\n dynamic: true,\n): Hook<K>;\n\nexport function createHook(initResultKey: InitResultKey, dynamic?: true): Hook<any> {\n return dynamic\n ? () => useInitResultDynamicValue(initResultKey as DynamicInitResultKey)\n : () => useInitResultValue(initResultKey);\n}\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve BackButton component.\n */\nexport const useBackButton = createHook('backButton', true);\n\n/**\n * HOC to wrap specified component to pass BackButton instance.\n */\nexport const withBackButton = createHoc('backButton', useBackButton);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve ClosingBehavior component.\n */\nexport const useClosingBehavior = createHook('closingBehavior', true);\n\n/**\n * HOC to wrap specified component to pass ClosingBehavior instance.\n */\nexport const withClosingBehavior = createHoc('closingBehavior', useClosingBehavior);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve CloudStorage component.\n */\nexport const useCloudStorage = createHook('cloudStorage');\n\n/**\n * HOC to wrap specified component to pass CloudStorage instance.\n */\nexport const withCloudStorage = createHoc('cloudStorage', useCloudStorage);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve HapticFeedback component.\n */\nexport const useHapticFeedback = createHook('hapticFeedback');\n\n/**\n * HOC to wrap specified component to pass HapticFeedback instance.\n */\nexport const withHapticFeedback = createHoc('hapticFeedback', useHapticFeedback);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve InitData component.\n */\nexport const useInitData = createHook('initData');\n\n/**\n * HOC to wrap specified component to pass InitData instance.\n */\nexport const withInitData = createHoc('initData', useInitData);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve init data raw representation.\n */\nexport const useInitDataRaw = createHook('initDataRaw');\n\n/**\n * HOC to wrap specified component to pass init data raw representation.\n */\nexport const withInitDataRaw = createHoc('initDataRaw', useInitDataRaw);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Invoice component.\n */\nexport const useInvoice = createHook('invoice', true);\n\n/**\n * HOC to wrap specified component to pass Invoice instance.\n */\nexport const withInvoice = createHoc('invoice', useInvoice);\n","import { type LaunchParams, retrieveLaunchData } from '@tma.js/sdk';\nimport React, { type ComponentType, useMemo } from 'react';\n\n/**\n * Hooks to retrieve launch parameters.\n */\nexport function useLaunchParams(): LaunchParams {\n return useMemo(() => retrieveLaunchData().launchParams, []);\n}\n\n/**\n * HOC to wrap specified component to pass launch parameters.\n * @param Component - wrapped component.\n */\nexport function withLaunchParams<P extends { launchParams?: LaunchParams }>(\n Component: ComponentType<P>,\n) {\n return function WithInvoice(props: P) {\n return <Component {...props} launchParams={useLaunchParams()} />;\n };\n}\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve MainButton component.\n */\nexport const useMainButton = createHook('mainButton', true);\n\n/**\n * HOC to wrap specified component to pass MainButton instance.\n */\nexport const withMainButton = createHoc('mainButton', useMainButton);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve MiniApp component.\n */\nexport const useMiniApp = createHook('miniApp', true);\n\n/**\n * HOC to wrap specified component to pass MiniApp instance.\n */\nexport const withMiniApp = createHoc('miniApp', useMiniApp);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Popup component.\n */\nexport const usePopup = createHook('popup', true);\n\n/**\n * HOC to wrap specified component to pass Popup instance.\n */\nexport const withPopup = createHoc('popup', usePopup);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve postEvent function.\n */\nexport const usePostEvent = createHook('postEvent');\n\n/**\n * HOC to wrap specified component to pass postEvent function.\n */\nexport const withPostEvent = createHoc('postEvent', usePostEvent);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve SettingsButton component.\n */\nexport const useSettingsButton = createHook('settingsButton', true);\n\n/**\n * HOC to wrap specified component to pass SettingsButton instance.\n */\nexport const withSettingsButton = createHoc('settingsButton', useSettingsButton);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve QRScanner component.\n */\nexport const useQRScanner = createHook('qrScanner', true);\n\n/**\n * HOC to wrap specified component to pass QRScanner instance.\n */\nexport const withQRScanner = createHoc('qrScanner', useQRScanner);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve ThemeParams component.\n */\nexport const useThemeParams = createHook('themeParams', true);\n\n/**\n * HOC to wrap specified component to pass ThemeParams instance.\n */\nexport const withThemeParams = createHoc('themeParams', useThemeParams);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Utils component.\n */\nexport const useUtils = createHook('utils');\n\n/**\n * HOC to wrap specified component to pass Utils instance.\n */\nexport const withUtils = createHoc('utils', useUtils);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Viewport component.\n */\nexport const useViewport = createHook('viewport', true);\n\n/**\n * HOC to wrap specified component to pass Viewport instance.\n */\nexport const withViewport = createHoc('viewport', useViewport);\n","import React, { type ComponentType, type PropsWithChildren, type ReactNode } from 'react';\n\nimport { useSDKContext } from './provider/index.js';\n\nexport interface DisplayGateProps extends PropsWithChildren {\n /**\n * Element or component, which will be displayed in case, SDK is not loading, didn't receive\n * any error during initialization as well as the initialization result.\n */\n initial?: ReactNode | ComponentType;\n\n /**\n * Element or component which will be displayed in case, SDK initialization failed.\n */\n error?: ReactNode | ComponentType<{ error: unknown }>;\n\n /**\n * Element or component which will be displayed in case, SDK is currently loading.\n */\n loading?: ReactNode | ComponentType;\n}\n\nfunction render(Component: ReactNode | ComponentType): ReactNode;\nfunction render<T extends object>(Component: ReactNode | ComponentType<T>, props: T): ReactNode;\nfunction render(Component: ReactNode | ComponentType, props = {}): ReactNode {\n return typeof Component === 'function' ? <Component {...props} /> : Component;\n}\n\n/**\n * This component is the layer controlling the application display. It displays application in\n * case, the SDK is initialized, displays an error if something went wrong, and a loader\n * if the SDK is warming up.\n */\nexport function DisplayGate(props: DisplayGateProps): ReactNode {\n const { loading, initResult, error } = useSDKContext();\n\n if (initResult) {\n return props.children;\n }\n return error\n ? render(props.error, { error })\n : render(loading ? props.loading : props.initial);\n}\n"],"names":["createHoc","initResultKey","hook","Component","Wrapper","props","wrappedProps","React","SDKContext","createContext","AsyncProvider","options","children","loading","setLoading","useState","error","setError","initResult","setInitResult","useEffect","init","context","useMemo","result","SyncProvider","e","SDKProvider","async","useSDKContext","useContext","useInitResultValue","key","shallowCopy","value","useInitResultDynamicValue","initial","copy","setCopy","withSDKContext","p","createHook","dynamic","useBackButton","withBackButton","useClosingBehavior","withClosingBehavior","useCloudStorage","withCloudStorage","useHapticFeedback","withHapticFeedback","useInitData","withInitData","useInitDataRaw","withInitDataRaw","useInvoice","withInvoice","useLaunchParams","retrieveLaunchData","withLaunchParams","useMainButton","withMainButton","useMiniApp","withMiniApp","usePopup","withPopup","usePostEvent","withPostEvent","useSettingsButton","withSettingsButton","useQRScanner","withQRScanner","useThemeParams","withThemeParams","useUtils","withUtils","useViewport","withViewport","render","DisplayGate"],"mappings":"kIAcgB,SAAAA,EAAmCC,EAAkBC,EAAuB,CAC1F,OAAQC,GAAkC,CACxC,SAASC,EAAQC,EAAgC,CAC/C,MAAMC,EAAe,CACnB,GAAGD,EACH,CAACJ,CAAa,EAAGC,EAAK,CAAA,EAGjB,OAAAK,EAAA,cAACJ,EAAW,CAAA,GAAGG,CAAc,CAAA,CACtC,CAEO,cAAA,eAAeF,EAAS,OAAQ,CACrC,MAAO,OAAOH,EAAc,CAAC,EAAE,YAAa,CAAA,GAAGA,EAAc,MAAM,CAAC,CAAC,EAAA,CACtE,EAEMG,CAAA,CAEX,CC3BO,MAAMI,EAAaC,EAAAA,cAA8B,CACtD,QAAS,EACX,CAAC,EAEDD,EAAW,YAAc,aCDzB,SAASE,EAAc,CAAE,QAAAC,EAAS,SAAAC,GAA8B,CAC9D,KAAM,CAACC,EAASC,CAAU,EAAIC,WAAS,EAAK,EACtC,CAACC,EAAOC,CAAQ,EAAIF,EAA8B,SAAA,EAClD,CAACG,EAAYC,CAAa,EAAIJ,EAAiC,SAAA,EAErEK,EAAAA,UAAU,IAAM,CACdN,EAAW,EAAI,EAEfO,EAAA,KAAK,CAAE,GAAGV,EAAS,SAAU,E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s","sources":["../src/createHoc.tsx","../src/provider/SDKContext.ts","../src/provider/SDKProvider.tsx","../src/provider/useSDKContext.ts","../src/provider/useInitResultValue.ts","../src/provider/useInitResultDynamicValue.ts","../src/provider/withSDKContext.tsx","../src/createHook.ts","../src/tools/back-button.ts","../src/tools/closing-behavior.ts","../src/tools/cloud-storage.ts","../src/tools/haptic-feedback.ts","../src/tools/init-data.ts","../src/tools/init-data-raw.ts","../src/tools/invoice.ts","../src/tools/launch-params.tsx","../src/tools/main-button.ts","../src/tools/mini-app.ts","../src/tools/popup.ts","../src/tools/post-event.ts","../src/tools/settings-button.ts","../src/tools/qr-scanner.ts","../src/tools/theme-params.ts","../src/tools/utils.ts","../src/tools/viewport.ts","../src/DisplayGate.tsx"],"sourcesContent":["import React, { type ComponentType } from 'react';\n\nimport type { Hook } from './createHook.js';\nimport type { InitResultKey, InitResultValue } from './types.js';\n\nexport type Hoc<K extends InitResultKey> = <P extends { [Key in K]?: InitResultValue<K> }>(\n Component: ComponentType<P>,\n) => ComponentType<P>;\n\n/**\n * Creates HOC for static init result value.\n * @param initResultKey - init result key.\n * @param hook - hook which returns init result value.\n */\nexport function createHoc<K extends InitResultKey>(initResultKey: K, hook: Hook<K>): Hoc<K> {\n return (Component: ComponentType<any>) => {\n function Wrapper(props: Record<string, unknown>) {\n const wrappedProps = {\n ...props,\n [initResultKey]: hook(),\n };\n\n return <Component {...wrappedProps} />;\n }\n\n Object.defineProperty(Wrapper, 'name', {\n value: `With${initResultKey[0].toUpperCase()}${initResultKey.slice(1)}`,\n });\n\n return Wrapper;\n };\n}\n","import { createContext } from 'react';\n\nimport type { SDKContextType } from './types.js';\n\nexport const SDKContext = createContext<SDKContextType>({\n loading: false,\n});\n\nSDKContext.displayName = 'SDKContext';\n","import { init } from '@tma.js/sdk';\nimport React, { useEffect, useMemo, useState } from 'react';\n\nimport { SDKContext } from './SDKContext.js';\nimport type { SDKContextType, SDKProviderProps } from './types.js';\nimport type { InitResult } from '../types.js';\n\nfunction AsyncProvider({ options, children }: SDKProviderProps) {\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<unknown | undefined>();\n const [initResult, setInitResult] = useState<InitResult | undefined>();\n\n useEffect(() => {\n setLoading(true);\n\n init({ ...options, complete: true })\n .then(setInitResult)\n .catch(setError)\n .finally(() => setLoading(false));\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n const context = useMemo<SDKContextType>(() => {\n const result: SDKContextType = { loading };\n if (error) {\n result.error = error;\n }\n\n if (initResult) {\n result.initResult = initResult;\n }\n\n return result;\n }, [loading, initResult, error]);\n\n return <SDKContext.Provider value={context}>{children}</SDKContext.Provider>;\n}\n\nfunction SyncProvider({ options = {}, children }: SDKProviderProps) {\n const context = useMemo<SDKContextType>(() => {\n const result: SDKContextType = { loading: false };\n\n try {\n result.initResult = init({ ...options, complete: false });\n } catch (e) {\n result.error = e;\n }\n\n return result;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return <SDKContext.Provider value={context}>{children}</SDKContext.Provider>;\n}\n\n/**\n * Component which provides access to SDK initialization state.\n */\nexport function SDKProvider(props: SDKProviderProps) {\n const { options = {}, async } = props;\n\n return options.async || options.complete || async\n ? <AsyncProvider {...props} />\n : <SyncProvider {...props} />;\n}\n","import { useContext } from 'react';\n\nimport { SDKContext } from './SDKContext.js';\nimport type { SDKContextType } from './types.js';\n\n/**\n * Returns current SDK information.\n */\nexport function useSDKContext(): SDKContextType {\n return useContext(SDKContext);\n}\n","import { useSDKContext } from './useSDKContext.js';\nimport type { InitResultKey, InitResultValue } from '../types.js';\n\n/**\n * Returns value by its field name from SDK init result.\n * @param key - init result key.\n * @throws {Error} SDK is not initialized.\n */\nexport function useInitResultValue<K extends InitResultKey>(key: K): InitResultValue<K> {\n const { initResult } = useSDKContext();\n\n if (!initResult) {\n throw new Error(`Unable to get init result key \"${key}\" as long as SDK is not initialized`);\n }\n return initResult[key];\n}\n","import { useEffect, useState } from 'react';\n\nimport { useInitResultValue } from './useInitResultValue.js';\nimport type {\n DynamicInitResultKey,\n InitResultValue,\n Trackable,\n} from '../types.js';\n\nfunction shallowCopy<T>(value: T): T {\n return Object.create(\n Object.getPrototypeOf(value),\n Object.getOwnPropertyDescriptors(value),\n );\n}\n\nexport function useInitResultDynamicValue<K extends DynamicInitResultKey>(\n initResultKey: K,\n): InitResultValue<K> {\n // Extract init result value.\n const initial = useInitResultValue(initResultKey);\n\n // Create value which will be returned to the external environment.\n const [copy, setCopy] = useState(shallowCopy(initial));\n\n // Effect which listens to change event and calls update.\n useEffect(() => {\n return (initial as Trackable).on('change', () => {\n setCopy(shallowCopy(initial));\n });\n }, [initial]);\n\n return copy;\n}\n","import React, { type ComponentType } from 'react';\n\nimport type { SDKContextType } from './types.js';\nimport { useSDKContext } from './useSDKContext.js';\n\n/**\n * HOC which passes SDK context to wrapped React component.\n * @param Component - component to wrap.\n */\nexport function withSDKContext<P extends { sdk?: SDKContextType }>(\n Component: ComponentType<P>,\n): ComponentType<Omit<P, 'sdk'>> {\n return function WithSDKContext(props: Omit<P, 'sdk'>) {\n const p = {\n ...props,\n sdk: useSDKContext(),\n } as P;\n\n return <Component {...p} />;\n };\n}\n","import { useInitResultDynamicValue, useInitResultValue } from './provider/index.js';\nimport type {\n DynamicInitResultKey,\n InitResultKey,\n InitResultValue,\n StaticInitResultKey,\n} from './types.js';\n\nexport type Hook<K extends InitResultKey> = () => InitResultValue<K>;\n\n/**\n * Creates hook to retrieve static init result value.\n * @param initResultKey - init result key.\n */\nexport function createHook<K extends StaticInitResultKey>(initResultKey: K): Hook<K>;\n\n/**\n * Creates hook to retrieve dynamic init result value.\n * @param initResultKey - init result key.\n * @param dynamic - flag to let function know this init result value is dynamic.\n */\nexport function createHook<K extends DynamicInitResultKey>(\n initResultKey: K,\n dynamic: true,\n): Hook<K>;\n\nexport function createHook(initResultKey: InitResultKey, dynamic?: true): Hook<any> {\n return dynamic\n ? () => useInitResultDynamicValue(initResultKey as DynamicInitResultKey)\n : () => useInitResultValue(initResultKey);\n}\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve BackButton component.\n */\nexport const useBackButton = createHook('backButton', true);\n\n/**\n * HOC to wrap specified component to pass BackButton instance.\n */\nexport const withBackButton = createHoc('backButton', useBackButton);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve ClosingBehavior component.\n */\nexport const useClosingBehavior = createHook('closingBehavior', true);\n\n/**\n * HOC to wrap specified component to pass ClosingBehavior instance.\n */\nexport const withClosingBehavior = createHoc('closingBehavior', useClosingBehavior);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve CloudStorage component.\n */\nexport const useCloudStorage = createHook('cloudStorage');\n\n/**\n * HOC to wrap specified component to pass CloudStorage instance.\n */\nexport const withCloudStorage = createHoc('cloudStorage', useCloudStorage);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve HapticFeedback component.\n */\nexport const useHapticFeedback = createHook('hapticFeedback');\n\n/**\n * HOC to wrap specified component to pass HapticFeedback instance.\n */\nexport const withHapticFeedback = createHoc('hapticFeedback', useHapticFeedback);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve InitData component.\n */\nexport const useInitData = createHook('initData');\n\n/**\n * HOC to wrap specified component to pass InitData instance.\n */\nexport const withInitData = createHoc('initData', useInitData);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve init data raw representation.\n */\nexport const useInitDataRaw = createHook('initDataRaw');\n\n/**\n * HOC to wrap specified component to pass init data raw representation.\n */\nexport const withInitDataRaw = createHoc('initDataRaw', useInitDataRaw);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Invoice component.\n */\nexport const useInvoice = createHook('invoice', true);\n\n/**\n * HOC to wrap specified component to pass Invoice instance.\n */\nexport const withInvoice = createHoc('invoice', useInvoice);\n","import { type LaunchParams, retrieveLaunchData } from '@tma.js/sdk';\nimport React, { type ComponentType, useMemo } from 'react';\n\n/**\n * Hooks to retrieve launch parameters.\n */\nexport function useLaunchParams(): LaunchParams {\n return useMemo(() => retrieveLaunchData().launchParams, []);\n}\n\n/**\n * HOC to wrap specified component to pass launch parameters.\n * @param Component - wrapped component.\n */\nexport function withLaunchParams<P extends { launchParams?: LaunchParams }>(\n Component: ComponentType<P>,\n) {\n return function WithInvoice(props: P) {\n return <Component {...props} launchParams={useLaunchParams()} />;\n };\n}\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve MainButton component.\n */\nexport const useMainButton = createHook('mainButton', true);\n\n/**\n * HOC to wrap specified component to pass MainButton instance.\n */\nexport const withMainButton = createHoc('mainButton', useMainButton);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve MiniApp component.\n */\nexport const useMiniApp = createHook('miniApp', true);\n\n/**\n * HOC to wrap specified component to pass MiniApp instance.\n */\nexport const withMiniApp = createHoc('miniApp', useMiniApp);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Popup component.\n */\nexport const usePopup = createHook('popup', true);\n\n/**\n * HOC to wrap specified component to pass Popup instance.\n */\nexport const withPopup = createHoc('popup', usePopup);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve postEvent function.\n */\nexport const usePostEvent = createHook('postEvent');\n\n/**\n * HOC to wrap specified component to pass postEvent function.\n */\nexport const withPostEvent = createHoc('postEvent', usePostEvent);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve SettingsButton component.\n */\nexport const useSettingsButton = createHook('settingsButton', true);\n\n/**\n * HOC to wrap specified component to pass SettingsButton instance.\n */\nexport const withSettingsButton = createHoc('settingsButton', useSettingsButton);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve QRScanner component.\n */\nexport const useQRScanner = createHook('qrScanner', true);\n\n/**\n * HOC to wrap specified component to pass QRScanner instance.\n */\nexport const withQRScanner = createHoc('qrScanner', useQRScanner);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve ThemeParams component.\n */\nexport const useThemeParams = createHook('themeParams', true);\n\n/**\n * HOC to wrap specified component to pass ThemeParams instance.\n */\nexport const withThemeParams = createHoc('themeParams', useThemeParams);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Utils component.\n */\nexport const useUtils = createHook('utils');\n\n/**\n * HOC to wrap specified component to pass Utils instance.\n */\nexport const withUtils = createHoc('utils', useUtils);\n","import { createHoc } from '../createHoc.js';\nimport { createHook } from '../createHook.js';\n\n/**\n * Hook to retrieve Viewport component.\n */\nexport const useViewport = createHook('viewport', true);\n\n/**\n * HOC to wrap specified component to pass Viewport instance.\n */\nexport const withViewport = createHoc('viewport', useViewport);\n","import React, { type ComponentType, type PropsWithChildren, type ReactNode } from 'react';\n\nimport { useSDKContext } from './provider/index.js';\n\nexport interface DisplayGateProps extends PropsWithChildren {\n /**\n * Element or component, which will be displayed in case, SDK is not loading, didn't receive\n * any error during initialization as well as the initialization result.\n */\n initial?: ReactNode | ComponentType;\n\n /**\n * Element or component which will be displayed in case, SDK initialization failed.\n */\n error?: ReactNode | ComponentType<{ error: unknown }>;\n\n /**\n * Element or component which will be displayed in case, SDK is currently loading.\n */\n loading?: ReactNode | ComponentType;\n}\n\nfunction render(Component: ReactNode | ComponentType): ReactNode;\nfunction render<T extends object>(Component: ReactNode | ComponentType<T>, props: T): ReactNode;\nfunction render(Component: ReactNode | ComponentType, props = {}): ReactNode {\n return typeof Component === 'function' ? <Component {...props} /> : Component;\n}\n\n/**\n * This component is the layer controlling the application display. It displays application in\n * case, the SDK is initialized, displays an error if something went wrong, and a loader\n * if the SDK is warming up.\n */\nexport function DisplayGate(props: DisplayGateProps): ReactNode {\n const { loading, initResult, error } = useSDKContext();\n\n if (initResult) {\n return props.children;\n }\n return error\n ? render(props.error, { error })\n : render(loading ? props.loading : props.initial);\n}\n"],"names":["createHoc","initResultKey","hook","Component","Wrapper","props","wrappedProps","React","SDKContext","createContext","AsyncProvider","options","children","loading","setLoading","useState","error","setError","initResult","setInitResult","useEffect","init","context","useMemo","result","SyncProvider","e","SDKProvider","async","useSDKContext","useContext","useInitResultValue","key","shallowCopy","value","useInitResultDynamicValue","initial","copy","setCopy","withSDKContext","p","createHook","dynamic","useBackButton","withBackButton","useClosingBehavior","withClosingBehavior","useCloudStorage","withCloudStorage","useHapticFeedback","withHapticFeedback","useInitData","withInitData","useInitDataRaw","withInitDataRaw","useInvoice","withInvoice","useLaunchParams","retrieveLaunchData","withLaunchParams","useMainButton","withMainButton","useMiniApp","withMiniApp","usePopup","withPopup","usePostEvent","withPostEvent","useSettingsButton","withSettingsButton","useQRScanner","withQRScanner","useThemeParams","withThemeParams","useUtils","withUtils","useViewport","withViewport","render","DisplayGate"],"mappings":";;AAcgB,SAAAA,EAAmCC,GAAkBC,GAAuB;AAC1F,SAAO,CAACC,MAAkC;AACxC,aAASC,EAAQC,GAAgC;AAC/C,YAAMC,IAAe;AAAA,QACnB,GAAGD;AAAA,QACH,CAACJ,CAAa,GAAGC,EAAK;AAAA,MAAA;AAGjB,aAAA,gBAAAK,EAAA,cAACJ,GAAW,EAAA,GAAGG,EAAc,CAAA;AAAA,IACtC;AAEO,kBAAA,eAAeF,GAAS,QAAQ;AAAA,MACrC,OAAO,OAAOH,EAAc,CAAC,EAAE,YAAa,CAAA,GAAGA,EAAc,MAAM,CAAC,CAAC;AAAA,IAAA,CACtE,GAEMG;AAAA,EAAA;AAEX;AC3BO,MAAMI,IAAaC,EAA8B;AA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Q,IAAe;AAC3E,SAAO,OAAOF,KAAc,6CAAcA,GAAW,EAAA,GAAGE,GAAO,IAAKF;AACtE;AAOO,SAAS4E,GAAY1E,GAAoC;AAC9D,QAAM,EAAE,SAAAQ,GAAS,YAAAK,GAAY,OAAAF,MAAUa,EAAc;AAErD,SAAIX,IACKb,EAAM,WAERW,IACH8D,EAAOzE,EAAM,OAAO,EAAE,OAAAW,EAAO,CAAA,IAC7B8D,EAAOjE,IAAUR,EAAM,UAAUA,EAAM,OAAO;AACpD;"}
|
package/src/DisplayGate.tsx
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import React, { type ComponentType, type PropsWithChildren, type ReactNode } from 'react';
|
|
2
|
-
|
|
3
|
-
import { useSDKContext } from './provider/index.js';
|
|
4
|
-
|
|
5
|
-
export interface DisplayGateProps extends PropsWithChildren {
|
|
6
|
-
/**
|
|
7
|
-
* Element or component, which will be displayed in case, SDK is not loading, didn't receive
|
|
8
|
-
* any error during initialization as well as the initialization result.
|
|
9
|
-
*/
|
|
10
|
-
initial?: ReactNode | ComponentType;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Element or component which will be displayed in case, SDK initialization failed.
|
|
14
|
-
*/
|
|
15
|
-
error?: ReactNode | ComponentType<{ error: unknown }>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Element or component which will be displayed in case, SDK is currently loading.
|
|
19
|
-
*/
|
|
20
|
-
loading?: ReactNode | ComponentType;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function render(Component: ReactNode | ComponentType): ReactNode;
|
|
24
|
-
function render<T extends object>(Component: ReactNode | ComponentType<T>, props: T): ReactNode;
|
|
25
|
-
function render(Component: ReactNode | ComponentType, props = {}): ReactNode {
|
|
26
|
-
return typeof Component === 'function' ? <Component {...props} /> : Component;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* This component is the layer controlling the application display. It displays application in
|
|
31
|
-
* case, the SDK is initialized, displays an error if something went wrong, and a loader
|
|
32
|
-
* if the SDK is warming up.
|
|
33
|
-
*/
|
|
34
|
-
export function DisplayGate(props: DisplayGateProps): ReactNode {
|
|
35
|
-
const { loading, initResult, error } = useSDKContext();
|
|
36
|
-
|
|
37
|
-
if (initResult) {
|
|
38
|
-
return props.children;
|
|
39
|
-
}
|
|
40
|
-
return error
|
|
41
|
-
? render(props.error, { error })
|
|
42
|
-
: render(loading ? props.loading : props.initial);
|
|
43
|
-
}
|
package/src/createHoc.tsx
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import React, { type ComponentType } from 'react';
|
|
2
|
-
|
|
3
|
-
import type { Hook } from './createHook.js';
|
|
4
|
-
import type { InitResultKey, InitResultValue } from './types.js';
|
|
5
|
-
|
|
6
|
-
export type Hoc<K extends InitResultKey> = <P extends { [Key in K]?: InitResultValue<K> }>(
|
|
7
|
-
Component: ComponentType<P>,
|
|
8
|
-
) => ComponentType<P>;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Creates HOC for static init result value.
|
|
12
|
-
* @param initResultKey - init result key.
|
|
13
|
-
* @param hook - hook which returns init result value.
|
|
14
|
-
*/
|
|
15
|
-
export function createHoc<K extends InitResultKey>(initResultKey: K, hook: Hook<K>): Hoc<K> {
|
|
16
|
-
return (Component: ComponentType<any>) => {
|
|
17
|
-
function Wrapper(props: Record<string, unknown>) {
|
|
18
|
-
const wrappedProps = {
|
|
19
|
-
...props,
|
|
20
|
-
[initResultKey]: hook(),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
return <Component {...wrappedProps} />;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
Object.defineProperty(Wrapper, 'name', {
|
|
27
|
-
value: `With${initResultKey[0].toUpperCase()}${initResultKey.slice(1)}`,
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return Wrapper;
|
|
31
|
-
};
|
|
32
|
-
}
|
package/src/createHook.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { useInitResultDynamicValue, useInitResultValue } from './provider/index.js';
|
|
2
|
-
import type {
|
|
3
|
-
DynamicInitResultKey,
|
|
4
|
-
InitResultKey,
|
|
5
|
-
InitResultValue,
|
|
6
|
-
StaticInitResultKey,
|
|
7
|
-
} from './types.js';
|
|
8
|
-
|
|
9
|
-
export type Hook<K extends InitResultKey> = () => InitResultValue<K>;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Creates hook to retrieve static init result value.
|
|
13
|
-
* @param initResultKey - init result key.
|
|
14
|
-
*/
|
|
15
|
-
export function createHook<K extends StaticInitResultKey>(initResultKey: K): Hook<K>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Creates hook to retrieve dynamic init result value.
|
|
19
|
-
* @param initResultKey - init result key.
|
|
20
|
-
* @param dynamic - flag to let function know this init result value is dynamic.
|
|
21
|
-
*/
|
|
22
|
-
export function createHook<K extends DynamicInitResultKey>(
|
|
23
|
-
initResultKey: K,
|
|
24
|
-
dynamic: true,
|
|
25
|
-
): Hook<K>;
|
|
26
|
-
|
|
27
|
-
export function createHook(initResultKey: InitResultKey, dynamic?: true): Hook<any> {
|
|
28
|
-
return dynamic
|
|
29
|
-
? () => useInitResultDynamicValue(initResultKey as DynamicInitResultKey)
|
|
30
|
-
: () => useInitResultValue(initResultKey);
|
|
31
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
useThemeParams,
|
|
3
|
-
useInitData,
|
|
4
|
-
useBackButton,
|
|
5
|
-
useClosingBehavior,
|
|
6
|
-
useInitDataRaw,
|
|
7
|
-
useCloudStorage,
|
|
8
|
-
useLaunchParams,
|
|
9
|
-
useMainButton,
|
|
10
|
-
useHapticFeedback,
|
|
11
|
-
useInvoice,
|
|
12
|
-
useMiniApp,
|
|
13
|
-
usePopup,
|
|
14
|
-
usePostEvent,
|
|
15
|
-
useQRScanner,
|
|
16
|
-
useSettingsButton,
|
|
17
|
-
useUtils,
|
|
18
|
-
useViewport,
|
|
19
|
-
withBackButton,
|
|
20
|
-
withClosingBehavior,
|
|
21
|
-
withCloudStorage,
|
|
22
|
-
withInitData,
|
|
23
|
-
withHapticFeedback,
|
|
24
|
-
withInitDataRaw,
|
|
25
|
-
withInvoice,
|
|
26
|
-
withLaunchParams,
|
|
27
|
-
withMainButton,
|
|
28
|
-
withMiniApp,
|
|
29
|
-
withPostEvent,
|
|
30
|
-
withPopup,
|
|
31
|
-
withThemeParams,
|
|
32
|
-
withQRScanner,
|
|
33
|
-
withSettingsButton,
|
|
34
|
-
withUtils,
|
|
35
|
-
withViewport,
|
|
36
|
-
} from './tools/index.js';
|
|
37
|
-
export {
|
|
38
|
-
useSDKContext,
|
|
39
|
-
withSDKContext,
|
|
40
|
-
SDKProvider,
|
|
41
|
-
SDKContext,
|
|
42
|
-
type SDKProviderProps,
|
|
43
|
-
type SDKContextType,
|
|
44
|
-
type SDKInitOptions,
|
|
45
|
-
type SDKInitResult,
|
|
46
|
-
} from './provider/index.js';
|
|
47
|
-
export {
|
|
48
|
-
DisplayGate,
|
|
49
|
-
type DisplayGateProps,
|
|
50
|
-
} from './DisplayGate.js';
|
|
51
|
-
export type {
|
|
52
|
-
InitOptions,
|
|
53
|
-
InitResult,
|
|
54
|
-
} from './types.js';
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { init } from '@tma.js/sdk';
|
|
2
|
-
import React, { useEffect, useMemo, useState } from 'react';
|
|
3
|
-
|
|
4
|
-
import { SDKContext } from './SDKContext.js';
|
|
5
|
-
import type { SDKContextType, SDKProviderProps } from './types.js';
|
|
6
|
-
import type { InitResult } from '../types.js';
|
|
7
|
-
|
|
8
|
-
function AsyncProvider({ options, children }: SDKProviderProps) {
|
|
9
|
-
const [loading, setLoading] = useState(false);
|
|
10
|
-
const [error, setError] = useState<unknown | undefined>();
|
|
11
|
-
const [initResult, setInitResult] = useState<InitResult | undefined>();
|
|
12
|
-
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
setLoading(true);
|
|
15
|
-
|
|
16
|
-
init({ ...options, complete: true })
|
|
17
|
-
.then(setInitResult)
|
|
18
|
-
.catch(setError)
|
|
19
|
-
.finally(() => setLoading(false));
|
|
20
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
21
|
-
}, []);
|
|
22
|
-
|
|
23
|
-
const context = useMemo<SDKContextType>(() => {
|
|
24
|
-
const result: SDKContextType = { loading };
|
|
25
|
-
if (error) {
|
|
26
|
-
result.error = error;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (initResult) {
|
|
30
|
-
result.initResult = initResult;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return result;
|
|
34
|
-
}, [loading, initResult, error]);
|
|
35
|
-
|
|
36
|
-
return <SDKContext.Provider value={context}>{children}</SDKContext.Provider>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function SyncProvider({ options = {}, children }: SDKProviderProps) {
|
|
40
|
-
const context = useMemo<SDKContextType>(() => {
|
|
41
|
-
const result: SDKContextType = { loading: false };
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
result.initResult = init({ ...options, complete: false });
|
|
45
|
-
} catch (e) {
|
|
46
|
-
result.error = e;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return result;
|
|
50
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
51
|
-
}, []);
|
|
52
|
-
|
|
53
|
-
return <SDKContext.Provider value={context}>{children}</SDKContext.Provider>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Component which provides access to SDK initialization state.
|
|
58
|
-
*/
|
|
59
|
-
export function SDKProvider(props: SDKProviderProps) {
|
|
60
|
-
const { options = {}, async } = props;
|
|
61
|
-
|
|
62
|
-
return options.async || options.complete || async
|
|
63
|
-
? <AsyncProvider {...props} />
|
|
64
|
-
: <SyncProvider {...props} />;
|
|
65
|
-
}
|
package/src/provider/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export * from './SDKContext.js';
|
|
2
|
-
export * from './SDKProvider.js';
|
|
3
|
-
export * from './types.js';
|
|
4
|
-
export * from './useInitResultDynamicValue.js';
|
|
5
|
-
export * from './useInitResultValue.js';
|
|
6
|
-
export * from './useSDKContext.js';
|
|
7
|
-
export * from './withSDKContext.js';
|
package/src/provider/types.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { PropsWithChildren } from 'react';
|
|
2
|
-
|
|
3
|
-
import type { InitOptions, InitResult } from '../types.js';
|
|
4
|
-
|
|
5
|
-
export type SDKInitOptions = InitOptions;
|
|
6
|
-
|
|
7
|
-
export type SDKInitResult = InitResult;
|
|
8
|
-
|
|
9
|
-
export type SDKProviderProps = PropsWithChildren<{
|
|
10
|
-
/**
|
|
11
|
-
* Should initialization be asynchronous. This mode is useful in case, you are using SSR
|
|
12
|
-
* and want to perform initialization on the client side only.
|
|
13
|
-
* @default false
|
|
14
|
-
*/
|
|
15
|
-
async?: boolean;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Init function options.
|
|
19
|
-
*/
|
|
20
|
-
options?: SDKInitOptions;
|
|
21
|
-
}>;
|
|
22
|
-
|
|
23
|
-
export interface SDKContextType {
|
|
24
|
-
/**
|
|
25
|
-
* Error occurred during initialization.
|
|
26
|
-
*/
|
|
27
|
-
error?: unknown;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Initialization result.
|
|
31
|
-
*/
|
|
32
|
-
initResult?: SDKInitResult;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* True if SDK is loading.
|
|
36
|
-
*/
|
|
37
|
-
loading: boolean;
|
|
38
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
2
|
-
|
|
3
|
-
import { useInitResultValue } from './useInitResultValue.js';
|
|
4
|
-
import type {
|
|
5
|
-
DynamicInitResultKey,
|
|
6
|
-
InitResultValue,
|
|
7
|
-
Trackable,
|
|
8
|
-
} from '../types.js';
|
|
9
|
-
|
|
10
|
-
function shallowCopy<T>(value: T): T {
|
|
11
|
-
return Object.create(
|
|
12
|
-
Object.getPrototypeOf(value),
|
|
13
|
-
Object.getOwnPropertyDescriptors(value),
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function useInitResultDynamicValue<K extends DynamicInitResultKey>(
|
|
18
|
-
initResultKey: K,
|
|
19
|
-
): InitResultValue<K> {
|
|
20
|
-
// Extract init result value.
|
|
21
|
-
const initial = useInitResultValue(initResultKey);
|
|
22
|
-
|
|
23
|
-
// Create value which will be returned to the external environment.
|
|
24
|
-
const [copy, setCopy] = useState(shallowCopy(initial));
|
|
25
|
-
|
|
26
|
-
// Effect which listens to change event and calls update.
|
|
27
|
-
useEffect(() => {
|
|
28
|
-
return (initial as Trackable).on('change', () => {
|
|
29
|
-
setCopy(shallowCopy(initial));
|
|
30
|
-
});
|
|
31
|
-
}, [initial]);
|
|
32
|
-
|
|
33
|
-
return copy;
|
|
34
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { useSDKContext } from './useSDKContext.js';
|
|
2
|
-
import type { InitResultKey, InitResultValue } from '../types.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Returns value by its field name from SDK init result.
|
|
6
|
-
* @param key - init result key.
|
|
7
|
-
* @throws {Error} SDK is not initialized.
|
|
8
|
-
*/
|
|
9
|
-
export function useInitResultValue<K extends InitResultKey>(key: K): InitResultValue<K> {
|
|
10
|
-
const { initResult } = useSDKContext();
|
|
11
|
-
|
|
12
|
-
if (!initResult) {
|
|
13
|
-
throw new Error(`Unable to get init result key "${key}" as long as SDK is not initialized`);
|
|
14
|
-
}
|
|
15
|
-
return initResult[key];
|
|
16
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { useContext } from 'react';
|
|
2
|
-
|
|
3
|
-
import { SDKContext } from './SDKContext.js';
|
|
4
|
-
import type { SDKContextType } from './types.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Returns current SDK information.
|
|
8
|
-
*/
|
|
9
|
-
export function useSDKContext(): SDKContextType {
|
|
10
|
-
return useContext(SDKContext);
|
|
11
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import React, { type ComponentType } from 'react';
|
|
2
|
-
|
|
3
|
-
import type { SDKContextType } from './types.js';
|
|
4
|
-
import { useSDKContext } from './useSDKContext.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* HOC which passes SDK context to wrapped React component.
|
|
8
|
-
* @param Component - component to wrap.
|
|
9
|
-
*/
|
|
10
|
-
export function withSDKContext<P extends { sdk?: SDKContextType }>(
|
|
11
|
-
Component: ComponentType<P>,
|
|
12
|
-
): ComponentType<Omit<P, 'sdk'>> {
|
|
13
|
-
return function WithSDKContext(props: Omit<P, 'sdk'>) {
|
|
14
|
-
const p = {
|
|
15
|
-
...props,
|
|
16
|
-
sdk: useSDKContext(),
|
|
17
|
-
} as P;
|
|
18
|
-
|
|
19
|
-
return <Component {...p} />;
|
|
20
|
-
};
|
|
21
|
-
}
|
package/src/tools/back-button.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve BackButton component.
|
|
6
|
-
*/
|
|
7
|
-
export const useBackButton = createHook('backButton', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass BackButton instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withBackButton = createHoc('backButton', useBackButton);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve ClosingBehavior component.
|
|
6
|
-
*/
|
|
7
|
-
export const useClosingBehavior = createHook('closingBehavior', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass ClosingBehavior instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withClosingBehavior = createHoc('closingBehavior', useClosingBehavior);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve CloudStorage component.
|
|
6
|
-
*/
|
|
7
|
-
export const useCloudStorage = createHook('cloudStorage');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass CloudStorage instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withCloudStorage = createHoc('cloudStorage', useCloudStorage);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve HapticFeedback component.
|
|
6
|
-
*/
|
|
7
|
-
export const useHapticFeedback = createHook('hapticFeedback');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass HapticFeedback instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withHapticFeedback = createHoc('hapticFeedback', useHapticFeedback);
|
package/src/tools/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export * from './back-button.js';
|
|
2
|
-
export * from './closing-behavior.js';
|
|
3
|
-
export * from './cloud-storage.js';
|
|
4
|
-
export * from './haptic-feedback.js';
|
|
5
|
-
export * from './init-data.js';
|
|
6
|
-
export * from './init-data-raw.js';
|
|
7
|
-
export * from './invoice.js';
|
|
8
|
-
export * from './launch-params.js';
|
|
9
|
-
export * from './main-button.js';
|
|
10
|
-
export * from './mini-app.js';
|
|
11
|
-
export * from './popup.js';
|
|
12
|
-
export * from './post-event.js';
|
|
13
|
-
export * from './settings-button.js';
|
|
14
|
-
export * from './qr-scanner.js';
|
|
15
|
-
export * from './theme-params.js';
|
|
16
|
-
export * from './utils.js';
|
|
17
|
-
export * from './viewport.js';
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve init data raw representation.
|
|
6
|
-
*/
|
|
7
|
-
export const useInitDataRaw = createHook('initDataRaw');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass init data raw representation.
|
|
11
|
-
*/
|
|
12
|
-
export const withInitDataRaw = createHoc('initDataRaw', useInitDataRaw);
|
package/src/tools/init-data.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve InitData component.
|
|
6
|
-
*/
|
|
7
|
-
export const useInitData = createHook('initData');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass InitData instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withInitData = createHoc('initData', useInitData);
|
package/src/tools/invoice.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve Invoice component.
|
|
6
|
-
*/
|
|
7
|
-
export const useInvoice = createHook('invoice', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass Invoice instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withInvoice = createHoc('invoice', useInvoice);
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type LaunchParams, retrieveLaunchData } from '@tma.js/sdk';
|
|
2
|
-
import React, { type ComponentType, useMemo } from 'react';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hooks to retrieve launch parameters.
|
|
6
|
-
*/
|
|
7
|
-
export function useLaunchParams(): LaunchParams {
|
|
8
|
-
return useMemo(() => retrieveLaunchData().launchParams, []);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* HOC to wrap specified component to pass launch parameters.
|
|
13
|
-
* @param Component - wrapped component.
|
|
14
|
-
*/
|
|
15
|
-
export function withLaunchParams<P extends { launchParams?: LaunchParams }>(
|
|
16
|
-
Component: ComponentType<P>,
|
|
17
|
-
) {
|
|
18
|
-
return function WithInvoice(props: P) {
|
|
19
|
-
return <Component {...props} launchParams={useLaunchParams()} />;
|
|
20
|
-
};
|
|
21
|
-
}
|
package/src/tools/main-button.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve MainButton component.
|
|
6
|
-
*/
|
|
7
|
-
export const useMainButton = createHook('mainButton', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass MainButton instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withMainButton = createHoc('mainButton', useMainButton);
|
package/src/tools/mini-app.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve MiniApp component.
|
|
6
|
-
*/
|
|
7
|
-
export const useMiniApp = createHook('miniApp', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass MiniApp instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withMiniApp = createHoc('miniApp', useMiniApp);
|
package/src/tools/popup.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve Popup component.
|
|
6
|
-
*/
|
|
7
|
-
export const usePopup = createHook('popup', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass Popup instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withPopup = createHoc('popup', usePopup);
|
package/src/tools/post-event.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve postEvent function.
|
|
6
|
-
*/
|
|
7
|
-
export const usePostEvent = createHook('postEvent');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass postEvent function.
|
|
11
|
-
*/
|
|
12
|
-
export const withPostEvent = createHoc('postEvent', usePostEvent);
|
package/src/tools/qr-scanner.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve QRScanner component.
|
|
6
|
-
*/
|
|
7
|
-
export const useQRScanner = createHook('qrScanner', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass QRScanner instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withQRScanner = createHoc('qrScanner', useQRScanner);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve SettingsButton component.
|
|
6
|
-
*/
|
|
7
|
-
export const useSettingsButton = createHook('settingsButton', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass SettingsButton instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withSettingsButton = createHoc('settingsButton', useSettingsButton);
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve ThemeParams component.
|
|
6
|
-
*/
|
|
7
|
-
export const useThemeParams = createHook('themeParams', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass ThemeParams instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withThemeParams = createHoc('themeParams', useThemeParams);
|
package/src/tools/utils.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve Utils component.
|
|
6
|
-
*/
|
|
7
|
-
export const useUtils = createHook('utils');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass Utils instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withUtils = createHoc('utils', useUtils);
|
package/src/tools/viewport.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createHoc } from '../createHoc.js';
|
|
2
|
-
import { createHook } from '../createHook.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Hook to retrieve Viewport component.
|
|
6
|
-
*/
|
|
7
|
-
export const useViewport = createHook('viewport', true);
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* HOC to wrap specified component to pass Viewport instance.
|
|
11
|
-
*/
|
|
12
|
-
export const withViewport = createHoc('viewport', useViewport);
|
package/src/types.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { InitOptions, InitResult } from '@tma.js/sdk';
|
|
2
|
-
|
|
3
|
-
// TODO: Remove in the future.
|
|
4
|
-
export { InitOptions, InitResult };
|
|
5
|
-
|
|
6
|
-
export interface Trackable {
|
|
7
|
-
on(event: 'change', listener: () => void): () => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* SDK init result keys, which describe values with trackable changes.
|
|
12
|
-
*/
|
|
13
|
-
export type DynamicInitResultKey = {
|
|
14
|
-
[K in InitResultKey]-?: InitResultValue<K> extends Trackable ? K : never;
|
|
15
|
-
}[InitResultKey];
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* SDK init result keys, which describe static values.
|
|
19
|
-
*/
|
|
20
|
-
export type StaticInitResultKey = Exclude<InitResultKey, DynamicInitResultKey>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* SDK initialization result key.
|
|
24
|
-
*/
|
|
25
|
-
export type InitResultKey = keyof InitResult;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Returns SDK init result value type by its key.
|
|
29
|
-
*/
|
|
30
|
-
export type InitResultValue<K extends InitResultKey> = InitResult[K];
|