@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
package/src/lib/index.ts
ADDED
|
@@ -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,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useInitData } from './useInitData.js';
|
|
4
|
+
import type { InitData } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes InitData SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withInitData<P extends { initData?: InitData }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'initData'>> {
|
|
13
|
+
return function WithInitData(props: Omit<P, 'initData'>) {
|
|
14
|
+
const p = { ...props, initData: useInitData() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { LaunchParams } from '@tma.js/sdk';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { retrieveLaunchParams } from '@tma.js/sdk';
|
|
3
|
+
|
|
4
|
+
import type { LaunchParams } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The hook which returns launch parameters initially passed to Web App.
|
|
8
|
+
*/
|
|
9
|
+
export function useLaunchParams(): LaunchParams {
|
|
10
|
+
return useMemo(retrieveLaunchParams, []);
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { LaunchParams } from './types.js';
|
|
4
|
+
import { useLaunchParams } from './useLaunchParams.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes launch parameters to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withLaunchParams<P extends { launchParams?: LaunchParams }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'launchParams'>> {
|
|
13
|
+
return function WithLaunchParams(props: Omit<P, 'launchParams'>) {
|
|
14
|
+
const p = { ...props, launchParams: useLaunchParams() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { MainButton } from '@tma.js/sdk';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { MainButton } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns MainButton component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useMainButton(): MainButton {
|
|
9
|
+
const mainButton = useUnit('mainButton');
|
|
10
|
+
useEventsTracking(mainButton, [
|
|
11
|
+
'isEnabledChanged', 'backgroundColorChanged', 'isProgressVisibleChanged',
|
|
12
|
+
'textChanged', 'textColorChanged', 'isVisibleChanged',
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
return mainButton;
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useMainButton } from './useMainButton.js';
|
|
4
|
+
import type { MainButton } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes MainButton SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withMainButton<P extends { mainButton?: MainButton }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'mainButton'>> {
|
|
13
|
+
return function WithMainButton(props: Omit<P, 'mainButton'>) {
|
|
14
|
+
const p = { ...props, mainButton: useMainButton() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { Popup } from '@tma.js/sdk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { Popup } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns Popup component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function usePopup(): Popup {
|
|
9
|
+
const popup = useUnit('popup');
|
|
10
|
+
useEventsTracking(popup, ['isOpenedChanged']);
|
|
11
|
+
|
|
12
|
+
return popup;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { usePopup } from './usePopup.js';
|
|
4
|
+
import type { Popup } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes Popup SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withPopup<P extends { popup?: Popup }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'popup'>> {
|
|
13
|
+
return function WithPopup(props: Omit<P, 'popup'>) {
|
|
14
|
+
const p = { ...props, popup: usePopup() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { QRScanner } from '@tma.js/sdk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { QRScanner } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns QRScanner component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useQRScanner(): QRScanner {
|
|
9
|
+
const qrScanner = useUnit('qrScanner');
|
|
10
|
+
useEventsTracking(qrScanner, ['isOpenedChanged']);
|
|
11
|
+
|
|
12
|
+
return qrScanner;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useQRScanner } from './useQRScanner.js';
|
|
4
|
+
import type { QRScanner } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes QRScanner SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withQRScanner<P extends { qrScanner?: QRScanner }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'qrScanner'>> {
|
|
13
|
+
return function WithQRScanner(props: Omit<P, 'qrScanner'>) {
|
|
14
|
+
const p = { ...props, qrScanner: useQRScanner() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { ThemeParams } from '@tma.js/sdk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { ThemeParams } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns ThemeParams component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useThemeParams(): ThemeParams {
|
|
9
|
+
const theme = useUnit('themeParams');
|
|
10
|
+
useEventsTracking(theme, ['changed']);
|
|
11
|
+
|
|
12
|
+
return theme;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useThemeParams } from './useThemeParams.js';
|
|
4
|
+
import type { ThemeParams } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes ThemeParams SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withThemeParams<P extends { themeParams?: ThemeParams }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'themeParams'>> {
|
|
13
|
+
return function WithThemeParams(props: Omit<P, 'themeParams'>) {
|
|
14
|
+
const p = { ...props, themeParams: useThemeParams() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { Viewport } from '@tma.js/sdk';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import { useEventsTracking } from '../hooks.js';
|
|
3
|
+
import type { Viewport } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns Viewport component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useViewport(): Viewport {
|
|
9
|
+
const viewport = useUnit('viewport');
|
|
10
|
+
useEventsTracking(viewport, [
|
|
11
|
+
'stableHeightChanged', 'isExpandedChanged', 'heightChanged', 'widthChanged',
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
return viewport;
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useViewport } from './useViewport.js';
|
|
4
|
+
import type { Viewport } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes Viewport SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withViewport<P extends { viewport?: Viewport }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'viewport'>> {
|
|
13
|
+
return function WithViewport(props: Omit<P, 'viewport'>) {
|
|
14
|
+
const p = { ...props, viewport: useViewport() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { WebApp } from '@tma.js/sdk';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useUnit } from '../../provider/index.js';
|
|
2
|
+
import type { WebApp } from './types.js';
|
|
3
|
+
import { useEventsTracking } from '../hooks.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns WebApp component instance.
|
|
7
|
+
*/
|
|
8
|
+
export function useWebApp(): WebApp {
|
|
9
|
+
const webApp = useUnit('webApp');
|
|
10
|
+
useEventsTracking(webApp, ['headerColorChanged', 'backgroundColorChanged']);
|
|
11
|
+
|
|
12
|
+
return webApp;
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useWebApp } from './useWebApp.js';
|
|
4
|
+
import type { WebApp } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes WebApp SDK component to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withWebApp<P extends { webApp?: WebApp }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'webApp'>> {
|
|
13
|
+
return function WithWebApp(props: Omit<P, 'webApp'>) {
|
|
14
|
+
const p = { ...props, webApp: useWebApp() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
memo,
|
|
3
|
+
useEffect,
|
|
4
|
+
useMemo,
|
|
5
|
+
useState,
|
|
6
|
+
type PropsWithChildren,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { init, type InitOptions } from '@tma.js/sdk';
|
|
9
|
+
|
|
10
|
+
import { sdkContext } from './context.js';
|
|
11
|
+
import type { SDKComponents, SDKContext } from './types.js';
|
|
12
|
+
|
|
13
|
+
export type SDKInitOptions = InitOptions;
|
|
14
|
+
|
|
15
|
+
export type SDKProviderProps = PropsWithChildren<{ initOptions?: SDKInitOptions }>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Component which provides access to SDK components.
|
|
19
|
+
*/
|
|
20
|
+
export const SDKProvider = memo<SDKProviderProps>((props) => {
|
|
21
|
+
const { children, initOptions } = props;
|
|
22
|
+
const [didInit, setDidInit] = useState(false);
|
|
23
|
+
const [error, setError] = useState<null | unknown>(null);
|
|
24
|
+
const [components, setComponents] = useState<SDKComponents | null>(null);
|
|
25
|
+
|
|
26
|
+
// Initialize SDK on DOM attach.
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
// Update init status.
|
|
29
|
+
setDidInit(true);
|
|
30
|
+
|
|
31
|
+
// Init SDK.
|
|
32
|
+
init(initOptions)
|
|
33
|
+
.then(setComponents)
|
|
34
|
+
.catch(setError);
|
|
35
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
const context = useMemo<SDKContext>(() => ({
|
|
39
|
+
components,
|
|
40
|
+
didInit,
|
|
41
|
+
error,
|
|
42
|
+
}), [didInit, components, error]);
|
|
43
|
+
|
|
44
|
+
return <sdkContext.Provider value={context}>{children}</sdkContext.Provider>;
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
SDKProvider.displayName = 'SDKProvider';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React, { type ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
import type { SDKContext } from './types.js';
|
|
4
|
+
import { useSDK } from './hooks.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HOC which passes SDK context to wrapped React component.
|
|
8
|
+
* @param Component - component to wrap.
|
|
9
|
+
*/
|
|
10
|
+
export function withSDK<P extends { sdk?: SDKContext }>(
|
|
11
|
+
Component: ComponentType<P>,
|
|
12
|
+
): ComponentType<Omit<P, 'sdk'>> {
|
|
13
|
+
return function WithLaunchParams(props: Omit<P, 'sdk'>) {
|
|
14
|
+
const p = { ...props, sdk: useSDK() } as P;
|
|
15
|
+
|
|
16
|
+
return <Component {...p} />;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
import { sdkContext } from './context.js';
|
|
4
|
+
import type { SDKComponent, SDKComponentName } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns current SDK information.
|
|
8
|
+
*/
|
|
9
|
+
export const useSDK = () => useContext(sdkContext);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns value by its field name from SDK init result.
|
|
13
|
+
* @param name - component name.
|
|
14
|
+
* @throws {Error} SDK is not initialized.
|
|
15
|
+
*/
|
|
16
|
+
export function useUnit<N extends SDKComponentName>(name: N): SDKComponent<N> {
|
|
17
|
+
const { components } = useSDK();
|
|
18
|
+
|
|
19
|
+
if (components === null) {
|
|
20
|
+
throw new Error(`Unable to get unit "${name}" as long as SDK is not initialized.`);
|
|
21
|
+
}
|
|
22
|
+
return components[name];
|
|
23
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { InitResult } from '@tma.js/sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* List of provided SDK components.
|
|
5
|
+
*/
|
|
6
|
+
export type SDKComponents = InitResult;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Known SDK component name.
|
|
10
|
+
*/
|
|
11
|
+
export type SDKComponentName = keyof SDKComponents;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns SDK component type by its name.
|
|
15
|
+
*/
|
|
16
|
+
export type SDKComponent<N extends SDKComponentName> = SDKComponents[N];
|
|
17
|
+
|
|
18
|
+
export interface SDKContext {
|
|
19
|
+
/**
|
|
20
|
+
* Components ready to use. This value will be null until initialization
|
|
21
|
+
* is done.
|
|
22
|
+
*/
|
|
23
|
+
components: SDKComponents | null;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* States, that SDK called `init()` method.
|
|
27
|
+
*/
|
|
28
|
+
didInit: boolean;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Error occurred during initialization.
|
|
32
|
+
*/
|
|
33
|
+
error: unknown | null;
|
|
34
|
+
}
|