@tma.js/sdk-react 0.4.3
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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/lib/browser.js +2 -0
- package/dist/lib/browser.js.map +1 -0
- package/dist/lib/index.cjs +2 -0
- package/dist/lib/index.cjs.map +1 -0
- package/dist/lib/index.mjs +2 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/lib/back-button/index.d.ts +3 -0
- package/dist/types/lib/back-button/types.d.ts +1 -0
- package/dist/types/lib/back-button/useBackButton.d.ts +5 -0
- package/dist/types/lib/back-button/withBackButton.d.ts +9 -0
- package/dist/types/lib/closing-behavior/index.d.ts +3 -0
- package/dist/types/lib/closing-behavior/types.d.ts +1 -0
- package/dist/types/lib/closing-behavior/useClosingBehaviour.d.ts +5 -0
- package/dist/types/lib/closing-behavior/withClosingBehaviour.d.ts +9 -0
- package/dist/types/lib/cloud-storage.d.ts +14 -0
- package/dist/types/lib/haptic-feedback/index.d.ts +3 -0
- package/dist/types/lib/haptic-feedback/types.d.ts +1 -0
- package/dist/types/lib/haptic-feedback/useHapticFeedback.d.ts +5 -0
- package/dist/types/lib/haptic-feedback/withHapticFeedback.d.ts +9 -0
- package/dist/types/lib/hooks.d.ts +26 -0
- package/dist/types/lib/index.d.ts +13 -0
- package/dist/types/lib/init-data/index.d.ts +3 -0
- package/dist/types/lib/init-data/types.d.ts +1 -0
- package/dist/types/lib/init-data/useInitData.d.ts +5 -0
- package/dist/types/lib/init-data/withInitData.d.ts +9 -0
- package/dist/types/lib/launch-params/index.d.ts +3 -0
- package/dist/types/lib/launch-params/types.d.ts +1 -0
- package/dist/types/lib/launch-params/useLaunchParams.d.ts +5 -0
- package/dist/types/lib/launch-params/withLaunchParams.d.ts +9 -0
- package/dist/types/lib/main-button/index.d.ts +3 -0
- package/dist/types/lib/main-button/types.d.ts +1 -0
- package/dist/types/lib/main-button/useMainButton.d.ts +5 -0
- package/dist/types/lib/main-button/withMainButton.d.ts +9 -0
- package/dist/types/lib/popup/index.d.ts +3 -0
- package/dist/types/lib/popup/types.d.ts +1 -0
- package/dist/types/lib/popup/usePopup.d.ts +5 -0
- package/dist/types/lib/popup/withPopup.d.ts +9 -0
- package/dist/types/lib/qr-scanner/index.d.ts +3 -0
- package/dist/types/lib/qr-scanner/types.d.ts +1 -0
- package/dist/types/lib/qr-scanner/useQRScanner.d.ts +5 -0
- package/dist/types/lib/qr-scanner/withQRScanner.d.ts +9 -0
- package/dist/types/lib/theme-params/index.d.ts +3 -0
- package/dist/types/lib/theme-params/types.d.ts +1 -0
- package/dist/types/lib/theme-params/useThemeParams.d.ts +5 -0
- package/dist/types/lib/theme-params/withThemeParams.d.ts +9 -0
- package/dist/types/lib/viewport/index.d.ts +3 -0
- package/dist/types/lib/viewport/types.d.ts +1 -0
- package/dist/types/lib/viewport/useViewport.d.ts +5 -0
- package/dist/types/lib/viewport/withViewport.d.ts +9 -0
- package/dist/types/lib/web-app/index.d.ts +3 -0
- package/dist/types/lib/web-app/types.d.ts +1 -0
- package/dist/types/lib/web-app/useWebApp.d.ts +5 -0
- package/dist/types/lib/web-app/withWebApp.d.ts +9 -0
- package/dist/types/provider/SDKProvider.d.ts +10 -0
- package/dist/types/provider/context.d.ts +3 -0
- package/dist/types/provider/hocs.d.ts +9 -0
- package/dist/types/provider/hooks.d.ts +11 -0
- package/dist/types/provider/index.d.ts +5 -0
- package/dist/types/provider/types.d.ts +28 -0
- package/package.json +70 -0
- package/src/index.ts +2 -0
- package/src/lib/back-button/index.ts +3 -0
- package/src/lib/back-button/types.ts +1 -0
- package/src/lib/back-button/useBackButton.ts +13 -0
- package/src/lib/back-button/withBackButton.tsx +18 -0
- package/src/lib/closing-behavior/index.ts +3 -0
- package/src/lib/closing-behavior/types.ts +1 -0
- package/src/lib/closing-behavior/useClosingBehaviour.ts +13 -0
- package/src/lib/closing-behavior/withClosingBehaviour.tsx +18 -0
- package/src/lib/cloud-storage.tsx +28 -0
- package/src/lib/haptic-feedback/index.ts +3 -0
- package/src/lib/haptic-feedback/types.ts +1 -0
- package/src/lib/haptic-feedback/useHapticFeedback.ts +9 -0
- package/src/lib/haptic-feedback/withHapticFeedback.tsx +18 -0
- package/src/lib/hooks.ts +51 -0
- package/src/lib/index.ts +13 -0
- package/src/lib/init-data/index.ts +3 -0
- package/src/lib/init-data/types.ts +1 -0
- package/src/lib/init-data/useInitData.ts +9 -0
- package/src/lib/init-data/withInitData.tsx +18 -0
- package/src/lib/launch-params/index.ts +3 -0
- package/src/lib/launch-params/types.ts +1 -0
- package/src/lib/launch-params/useLaunchParams.ts +11 -0
- package/src/lib/launch-params/withLaunchParams.tsx +18 -0
- package/src/lib/main-button/index.ts +3 -0
- package/src/lib/main-button/types.ts +1 -0
- package/src/lib/main-button/useMainButton.ts +16 -0
- package/src/lib/main-button/withMainButton.tsx +18 -0
- package/src/lib/popup/index.ts +3 -0
- package/src/lib/popup/types.ts +1 -0
- package/src/lib/popup/usePopup.ts +13 -0
- package/src/lib/popup/withPopup.tsx +18 -0
- package/src/lib/qr-scanner/index.ts +3 -0
- package/src/lib/qr-scanner/types.ts +1 -0
- package/src/lib/qr-scanner/useQRScanner.ts +13 -0
- package/src/lib/qr-scanner/withQRScanner.tsx +18 -0
- package/src/lib/theme-params/index.ts +3 -0
- package/src/lib/theme-params/types.ts +1 -0
- package/src/lib/theme-params/useThemeParams.ts +13 -0
- package/src/lib/theme-params/withThemeParams.tsx +18 -0
- package/src/lib/viewport/index.ts +3 -0
- package/src/lib/viewport/types.ts +1 -0
- package/src/lib/viewport/useViewport.ts +15 -0
- package/src/lib/viewport/withViewport.tsx +18 -0
- package/src/lib/web-app/index.ts +3 -0
- package/src/lib/web-app/types.ts +1 -0
- package/src/lib/web-app/useWebApp.ts +13 -0
- package/src/lib/web-app/withWebApp.tsx +18 -0
- package/src/provider/SDKProvider.tsx +47 -0
- package/src/provider/context.ts +11 -0
- package/src/provider/hocs.tsx +18 -0
- package/src/provider/hooks.ts +23 -0
- package/src/provider/index.ts +5 -0
- package/src/provider/types.ts +34 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { HapticFeedback } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { HapticFeedback } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes HapticFeedback SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withHapticFeedback<P extends {
|
|
8
|
+
hapticFeedback?: HapticFeedback;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'hapticFeedback'>>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PostEvent } from '@tma.js/sdk/types';
|
|
2
|
+
interface Trackable {
|
|
3
|
+
on: (event: any, ...args: any[]) => void;
|
|
4
|
+
off: (event: any, ...args: any[]) => void;
|
|
5
|
+
}
|
|
6
|
+
type EventName<T extends Trackable> = T extends {
|
|
7
|
+
on(event: infer E, ...args: any[]): any;
|
|
8
|
+
} ? E : never;
|
|
9
|
+
/**
|
|
10
|
+
* The hook which returns function used for force update.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useForceUpdate(): (() => void);
|
|
13
|
+
/**
|
|
14
|
+
* The hook which listens to the specified object events and calls force update
|
|
15
|
+
* in case, some of them was emitted.
|
|
16
|
+
* @param trackable - object which allows events listening.
|
|
17
|
+
* @param events - events to listen. This array will be memoized during first
|
|
18
|
+
* hook call.
|
|
19
|
+
*/
|
|
20
|
+
export declare function useEventsTracking<T extends Trackable>(trackable: T, events: EventName<T>[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* Returns event posting function which was created during SDK
|
|
23
|
+
* initialization.
|
|
24
|
+
*/
|
|
25
|
+
export declare function usePostEvent(): PostEvent;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './back-button/index.js';
|
|
2
|
+
export * from './closing-behavior/index.js';
|
|
3
|
+
export * from './haptic-feedback/index.js';
|
|
4
|
+
export * from './init-data/index.js';
|
|
5
|
+
export * from './launch-params/index.js';
|
|
6
|
+
export * from './main-button/index.js';
|
|
7
|
+
export * from './popup/index.js';
|
|
8
|
+
export * from './qr-scanner/index.js';
|
|
9
|
+
export * from './theme-params/index.js';
|
|
10
|
+
export * from './viewport/index.js';
|
|
11
|
+
export * from './web-app/index.js';
|
|
12
|
+
export * from './cloud-storage.js';
|
|
13
|
+
export { usePostEvent } from './hooks.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { InitData } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { InitData } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes InitData SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withInitData<P extends {
|
|
8
|
+
initData?: InitData;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'initData'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { LaunchParams } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { LaunchParams } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes launch parameters to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withLaunchParams<P extends {
|
|
8
|
+
launchParams?: LaunchParams;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'launchParams'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { MainButton } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { MainButton } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes MainButton SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withMainButton<P extends {
|
|
8
|
+
mainButton?: MainButton;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'mainButton'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { Popup } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { Popup } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes Popup SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withPopup<P extends {
|
|
8
|
+
popup?: Popup;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'popup'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { QRScanner } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { QRScanner } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes QRScanner SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withQRScanner<P extends {
|
|
8
|
+
qrScanner?: QRScanner;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'qrScanner'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { ThemeParams } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { ThemeParams } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes ThemeParams SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withThemeParams<P extends {
|
|
8
|
+
themeParams?: ThemeParams;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'themeParams'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { Viewport } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { Viewport } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes Viewport SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withViewport<P extends {
|
|
8
|
+
viewport?: Viewport;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'viewport'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { WebApp } from '@tma.js/sdk';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { WebApp } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes WebApp SDK component to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withWebApp<P extends {
|
|
8
|
+
webApp?: WebApp;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'webApp'>>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React, { type PropsWithChildren } from 'react';
|
|
2
|
+
import { type InitOptions } from '@tma.js/sdk';
|
|
3
|
+
export type SDKInitOptions = InitOptions;
|
|
4
|
+
export type SDKProviderProps = PropsWithChildren<{
|
|
5
|
+
initOptions?: SDKInitOptions;
|
|
6
|
+
}>;
|
|
7
|
+
/**
|
|
8
|
+
* Component which provides access to SDK components.
|
|
9
|
+
*/
|
|
10
|
+
export declare const SDKProvider: React.NamedExoticComponent<SDKProviderProps>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ComponentType } from 'react';
|
|
2
|
+
import type { SDKContext } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* HOC which passes SDK context to wrapped React component.
|
|
5
|
+
* @param Component - component to wrap.
|
|
6
|
+
*/
|
|
7
|
+
export declare function withSDK<P extends {
|
|
8
|
+
sdk?: SDKContext;
|
|
9
|
+
}>(Component: ComponentType<P>): ComponentType<Omit<P, 'sdk'>>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SDKComponent, SDKComponentName } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns current SDK information.
|
|
4
|
+
*/
|
|
5
|
+
export declare const useSDK: () => import("./types.js").SDKContext;
|
|
6
|
+
/**
|
|
7
|
+
* Returns value by its field name from SDK init result.
|
|
8
|
+
* @param name - component name.
|
|
9
|
+
* @throws {Error} SDK is not initialized.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useUnit<N extends SDKComponentName>(name: N): SDKComponent<N>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { InitResult } from '@tma.js/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* List of provided SDK components.
|
|
4
|
+
*/
|
|
5
|
+
export type SDKComponents = InitResult;
|
|
6
|
+
/**
|
|
7
|
+
* Known SDK component name.
|
|
8
|
+
*/
|
|
9
|
+
export type SDKComponentName = keyof SDKComponents;
|
|
10
|
+
/**
|
|
11
|
+
* Returns SDK component type by its name.
|
|
12
|
+
*/
|
|
13
|
+
export type SDKComponent<N extends SDKComponentName> = SDKComponents[N];
|
|
14
|
+
export interface SDKContext {
|
|
15
|
+
/**
|
|
16
|
+
* Components ready to use. This value will be null until initialization
|
|
17
|
+
* is done.
|
|
18
|
+
*/
|
|
19
|
+
components: SDKComponents | null;
|
|
20
|
+
/**
|
|
21
|
+
* States, that SDK called `init()` method.
|
|
22
|
+
*/
|
|
23
|
+
didInit: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Error occurred during initialization.
|
|
26
|
+
*/
|
|
27
|
+
error: unknown | null;
|
|
28
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tma.js/sdk-react",
|
|
3
|
+
"version": "0.4.3",
|
|
4
|
+
"description": "React bindings for Web Apps client SDK. Contains hooks, components and other useful tools which allow usage of React along with Web Apps client SDK.",
|
|
5
|
+
"author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
|
|
6
|
+
"homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git@github.com:Telegram-Mini-Apps/tma.js.git",
|
|
10
|
+
"directory": "packages/sdk-react"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Telegram-Mini-Apps/tma.js/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"telegram-mini-apps",
|
|
17
|
+
"typescript",
|
|
18
|
+
"sdk",
|
|
19
|
+
"react"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src"
|
|
27
|
+
],
|
|
28
|
+
"main": "dist/lib/index.cjs",
|
|
29
|
+
"browser": "dist/lib/browser.js",
|
|
30
|
+
"module": "dist/lib/index.mjs",
|
|
31
|
+
"types": "dist/types/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/types/index.d.ts",
|
|
35
|
+
"import": "./dist/lib/index.mjs",
|
|
36
|
+
"require": "./dist/lib/index.cjs",
|
|
37
|
+
"default": "./dist/lib/index.cjs"
|
|
38
|
+
},
|
|
39
|
+
"./*": {
|
|
40
|
+
"types": "./dist/types/*.d.ts",
|
|
41
|
+
"import": "./dist/lib/*.mjs",
|
|
42
|
+
"require": "./dist/lib/*.cjs",
|
|
43
|
+
"default": "./dist/lib/*.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@types/react": "^17.0.0 || ^18.0.0",
|
|
48
|
+
"react": "^17.0.0 || ^18.0.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"@types/react": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@tma.js/bridge": "1.3.2",
|
|
57
|
+
"@tma.js/sdk": "0.11.3"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"tsconfig": "0.0.2",
|
|
61
|
+
"eslint-config-custom": "0.1.0"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"lint": "eslint -c .eslintrc.cjs src/**/*",
|
|
68
|
+
"build": "rimraf dist && rollup --config rollup.config.js"
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { BackButton } from '@tma.js/sdk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { BackButton } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns BackButton component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useBackButton(): BackButton {
|
|
9
|
+
const backButton = useUnit('backButton');
|
|
10
|
+
useEventsTracking(backButton, ['isVisibleChanged']);
|
|
11
|
+
|
|
12
|
+
return backButton;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useBackButton } from './useBackButton.js';
|
|
4
|
+
import type { BackButton } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes BackButton SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withBackButton<P extends { backButton?: BackButton }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'backButton'>> {
|
|
13
|
+
return function WithBackButton(props: Omit<P, 'backButton'>) {
|
|
14
|
+
const p = { ...props, backButton: useBackButton() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { ClosingBehaviour } from '@tma.js/sdk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { ClosingBehaviour } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns ClosingBehaviour component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useClosingBehaviour(): ClosingBehaviour {
|
|
9
|
+
const component = useUnit('closingBehavior');
|
|
10
|
+
useEventsTracking(component, ['isConfirmationNeededChanged']);
|
|
11
|
+
|
|
12
|
+
return component;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useClosingBehaviour } from './useClosingBehaviour.js';
|
|
4
|
+
import type { ClosingBehaviour } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes ClosingBehaviour SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withClosingBehaviour<P extends { closingBehaviour?: ClosingBehaviour }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'closingBehaviour'>> {
|
|
13
|
+
return function WithClosingConfirmation(props: Omit<P, 'closingBehaviour'>) {
|
|
14
|
+
const p = { ...props, closingBehaviour: useClosingBehaviour() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { CloudStorage } from '@tma.js/sdk';
|
|
4
|
+
|
|
5
|
+
import { useUnit } from '../provider/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns CloudStorage component instance.
|
|
9
|
+
*/
|
|
10
|
+
export function useCloudStorage(): CloudStorage {
|
|
11
|
+
return useUnit('cloudStorage');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* HOC which passes CloudStorage SDK component to wrapped React component.
|
|
16
|
+
* @param Component - component to wrap.
|
|
17
|
+
*/
|
|
18
|
+
export function withCloudStorage<P extends { cloudStorage?: CloudStorage }>(
|
|
19
|
+
Component: ComponentType<P>,
|
|
20
|
+
): ComponentType<Omit<P, 'cloudStorage'>> {
|
|
21
|
+
return function WithCloudStorage(props: Omit<P, 'cloudStorage'>) {
|
|
22
|
+
const p = { ...props, cloudStorage: useCloudStorage() } as P;
|
|
23
|
+
|
|
24
|
+
return <Component {...p} />;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type { CloudStorage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { HapticFeedback } from '@tma.js/sdk';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useHapticFeedback } from './useHapticFeedback.js';
|
|
4
|
+
import type { HapticFeedback } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes HapticFeedback SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withHapticFeedback<P extends { hapticFeedback?: HapticFeedback }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'hapticFeedback'>> {
|
|
13
|
+
return function WithHapticFeedback(props: Omit<P, 'hapticFeedback'>) {
|
|
14
|
+
const p = { ...props, hapticFeedback: useHapticFeedback() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
package/src/lib/hooks.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useCallback, useState, useEffect } from 'react';
|
|
2
|
+
import type { PostEvent } from '@tma.js/sdk/types';
|
|
3
|
+
import { useUnit } from '../provider/index.js';
|
|
4
|
+
|
|
5
|
+
interface Trackable {
|
|
6
|
+
on: (event: any, ...args: any[]) => void;
|
|
7
|
+
off: (event: any, ...args: any[]) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type EventName<T extends Trackable> = T extends {
|
|
11
|
+
on(event: infer E, ...args: any[]): any
|
|
12
|
+
} ? E : never;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The hook which returns function used for force update.
|
|
16
|
+
*/
|
|
17
|
+
export function useForceUpdate(): (() => void) {
|
|
18
|
+
const [, setHash] = useState(0);
|
|
19
|
+
// TODO: Maybe just assign new symbol?
|
|
20
|
+
return useCallback(() => setHash((hash) => (hash + 1) % 1_000_000), []);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The hook which listens to the specified object events and calls force update
|
|
25
|
+
* in case, some of them was emitted.
|
|
26
|
+
* @param trackable - object which allows events listening.
|
|
27
|
+
* @param events - events to listen. This array will be memoized during first
|
|
28
|
+
* hook call.
|
|
29
|
+
*/
|
|
30
|
+
export function useEventsTracking<T extends Trackable>(
|
|
31
|
+
trackable: T,
|
|
32
|
+
events: EventName<T>[],
|
|
33
|
+
) {
|
|
34
|
+
const forceUpdate = useForceUpdate();
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
// Start listening to each event.
|
|
38
|
+
events.forEach((event) => trackable.on(event, forceUpdate));
|
|
39
|
+
|
|
40
|
+
// On cleanup remove event listeners.
|
|
41
|
+
return () => events.forEach((event) => trackable.off(event, forceUpdate));
|
|
42
|
+
}, [trackable, forceUpdate, events]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns event posting function which was created during SDK
|
|
47
|
+
* initialization.
|
|
48
|
+
*/
|
|
49
|
+
export function usePostEvent(): PostEvent {
|
|
50
|
+
return useUnit('postEvent');
|
|
51
|
+
}
|