@tma.js/sdk-react 0.5.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/dist/dts/createHoc.d.ts +12 -0
  2. package/dist/dts/createHook.d.ts +13 -0
  3. package/dist/dts/index.d.ts +3 -2
  4. package/dist/dts/provider/SDKContext.d.ts +3 -0
  5. package/dist/dts/provider/SDKProvider.d.ts +4 -8
  6. package/dist/dts/provider/index.d.ts +5 -3
  7. package/dist/dts/provider/types.d.ts +15 -21
  8. package/dist/dts/provider/useInitResultDynamicValue.d.ts +2 -0
  9. package/dist/dts/provider/useInitResultValue.d.ts +7 -0
  10. package/dist/dts/provider/useSDKContext.d.ts +5 -0
  11. package/dist/dts/provider/{hocs.d.ts → withSDKContext.d.ts} +3 -3
  12. package/dist/dts/tools/back-button.d.ts +8 -0
  13. package/dist/dts/tools/closing-behavior.d.ts +8 -0
  14. package/dist/dts/tools/cloud-storage.d.ts +8 -0
  15. package/dist/dts/tools/haptic-feedback.d.ts +8 -0
  16. package/dist/dts/tools/index.d.ts +16 -0
  17. package/dist/dts/tools/init-data-raw.d.ts +8 -0
  18. package/dist/dts/tools/init-data.d.ts +8 -0
  19. package/dist/dts/tools/invoice.d.ts +8 -0
  20. package/dist/dts/tools/launch-params.d.ts +13 -0
  21. package/dist/dts/tools/main-button.d.ts +8 -0
  22. package/dist/dts/tools/mini-app.d.ts +8 -0
  23. package/dist/dts/tools/popup.d.ts +8 -0
  24. package/dist/dts/tools/post-event.d.ts +8 -0
  25. package/dist/dts/tools/qr-scanner.d.ts +8 -0
  26. package/dist/dts/tools/theme-params.d.ts +8 -0
  27. package/dist/dts/tools/utils.d.ts +8 -0
  28. package/dist/dts/tools/viewport.d.ts +8 -0
  29. package/dist/dts/types.d.ts +23 -0
  30. package/dist/index.cjs +1 -1
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.mjs +116 -203
  33. package/dist/index.mjs.map +1 -1
  34. package/package.json +7 -9
  35. package/src/createHoc.tsx +32 -0
  36. package/src/createHook.ts +31 -0
  37. package/src/index.ts +45 -2
  38. package/src/provider/SDKContext.ts +9 -0
  39. package/src/provider/SDKProvider.tsx +54 -37
  40. package/src/provider/index.ts +5 -3
  41. package/src/provider/types.ts +15 -22
  42. package/src/provider/useInitResultDynamicValue.ts +34 -0
  43. package/src/provider/useInitResultValue.ts +16 -0
  44. package/src/provider/useSDKContext.ts +11 -0
  45. package/src/provider/withSDKContext.tsx +21 -0
  46. package/src/tools/back-button.ts +12 -0
  47. package/src/tools/closing-behavior.ts +12 -0
  48. package/src/tools/cloud-storage.ts +12 -0
  49. package/src/tools/haptic-feedback.ts +12 -0
  50. package/src/tools/index.ts +16 -0
  51. package/src/tools/init-data-raw.ts +12 -0
  52. package/src/tools/init-data.ts +12 -0
  53. package/src/tools/invoice.ts +12 -0
  54. package/src/tools/launch-params.tsx +21 -0
  55. package/src/tools/main-button.ts +12 -0
  56. package/src/tools/mini-app.ts +12 -0
  57. package/src/tools/popup.ts +12 -0
  58. package/src/tools/post-event.ts +12 -0
  59. package/src/tools/qr-scanner.ts +12 -0
  60. package/src/tools/theme-params.ts +12 -0
  61. package/src/tools/utils.ts +12 -0
  62. package/src/tools/viewport.ts +12 -0
  63. package/src/types.ts +29 -0
  64. package/dist/dts/lib/back-button/index.d.ts +0 -3
  65. package/dist/dts/lib/back-button/types.d.ts +0 -1
  66. package/dist/dts/lib/back-button/useBackButton.d.ts +0 -5
  67. package/dist/dts/lib/back-button/withBackButton.d.ts +0 -9
  68. package/dist/dts/lib/closing-behavior/index.d.ts +0 -3
  69. package/dist/dts/lib/closing-behavior/types.d.ts +0 -1
  70. package/dist/dts/lib/closing-behavior/useClosingBehaviour.d.ts +0 -5
  71. package/dist/dts/lib/closing-behavior/withClosingBehaviour.d.ts +0 -9
  72. package/dist/dts/lib/cloud-storage.d.ts +0 -14
  73. package/dist/dts/lib/haptic-feedback/index.d.ts +0 -3
  74. package/dist/dts/lib/haptic-feedback/types.d.ts +0 -1
  75. package/dist/dts/lib/haptic-feedback/useHapticFeedback.d.ts +0 -5
  76. package/dist/dts/lib/haptic-feedback/withHapticFeedback.d.ts +0 -9
  77. package/dist/dts/lib/hooks.d.ts +0 -26
  78. package/dist/dts/lib/index.d.ts +0 -13
  79. package/dist/dts/lib/init-data.d.ts +0 -20
  80. package/dist/dts/lib/launch-params/index.d.ts +0 -3
  81. package/dist/dts/lib/launch-params/types.d.ts +0 -1
  82. package/dist/dts/lib/launch-params/useLaunchParams.d.ts +0 -5
  83. package/dist/dts/lib/launch-params/withLaunchParams.d.ts +0 -9
  84. package/dist/dts/lib/main-button/index.d.ts +0 -3
  85. package/dist/dts/lib/main-button/types.d.ts +0 -1
  86. package/dist/dts/lib/main-button/useMainButton.d.ts +0 -5
  87. package/dist/dts/lib/main-button/withMainButton.d.ts +0 -9
  88. package/dist/dts/lib/popup/index.d.ts +0 -3
  89. package/dist/dts/lib/popup/types.d.ts +0 -1
  90. package/dist/dts/lib/popup/usePopup.d.ts +0 -5
  91. package/dist/dts/lib/popup/withPopup.d.ts +0 -9
  92. package/dist/dts/lib/qr-scanner/index.d.ts +0 -3
  93. package/dist/dts/lib/qr-scanner/types.d.ts +0 -1
  94. package/dist/dts/lib/qr-scanner/useQRScanner.d.ts +0 -5
  95. package/dist/dts/lib/qr-scanner/withQRScanner.d.ts +0 -9
  96. package/dist/dts/lib/theme-params.d.ts +0 -13
  97. package/dist/dts/lib/viewport/index.d.ts +0 -3
  98. package/dist/dts/lib/viewport/types.d.ts +0 -1
  99. package/dist/dts/lib/viewport/useViewport.d.ts +0 -5
  100. package/dist/dts/lib/viewport/withViewport.d.ts +0 -9
  101. package/dist/dts/lib/web-app/index.d.ts +0 -3
  102. package/dist/dts/lib/web-app/types.d.ts +0 -1
  103. package/dist/dts/lib/web-app/useWebApp.d.ts +0 -5
  104. package/dist/dts/lib/web-app/withWebApp.d.ts +0 -9
  105. package/dist/dts/provider/context.d.ts +0 -3
  106. package/dist/dts/provider/hooks.d.ts +0 -11
  107. package/src/lib/back-button/index.ts +0 -3
  108. package/src/lib/back-button/types.ts +0 -1
  109. package/src/lib/back-button/useBackButton.ts +0 -13
  110. package/src/lib/back-button/withBackButton.tsx +0 -18
  111. package/src/lib/closing-behavior/index.ts +0 -3
  112. package/src/lib/closing-behavior/types.ts +0 -1
  113. package/src/lib/closing-behavior/useClosingBehaviour.ts +0 -13
  114. package/src/lib/closing-behavior/withClosingBehaviour.tsx +0 -18
  115. package/src/lib/cloud-storage.tsx +0 -28
  116. package/src/lib/haptic-feedback/index.ts +0 -3
  117. package/src/lib/haptic-feedback/types.ts +0 -1
  118. package/src/lib/haptic-feedback/useHapticFeedback.ts +0 -9
  119. package/src/lib/haptic-feedback/withHapticFeedback.tsx +0 -18
  120. package/src/lib/hooks.ts +0 -52
  121. package/src/lib/index.ts +0 -13
  122. package/src/lib/init-data.tsx +0 -43
  123. package/src/lib/launch-params/index.ts +0 -3
  124. package/src/lib/launch-params/types.ts +0 -1
  125. package/src/lib/launch-params/useLaunchParams.ts +0 -11
  126. package/src/lib/launch-params/withLaunchParams.tsx +0 -18
  127. package/src/lib/main-button/index.ts +0 -3
  128. package/src/lib/main-button/types.ts +0 -1
  129. package/src/lib/main-button/useMainButton.ts +0 -16
  130. package/src/lib/main-button/withMainButton.tsx +0 -18
  131. package/src/lib/popup/index.ts +0 -3
  132. package/src/lib/popup/types.ts +0 -1
  133. package/src/lib/popup/usePopup.ts +0 -13
  134. package/src/lib/popup/withPopup.tsx +0 -18
  135. package/src/lib/qr-scanner/index.ts +0 -3
  136. package/src/lib/qr-scanner/types.ts +0 -1
  137. package/src/lib/qr-scanner/useQRScanner.ts +0 -13
  138. package/src/lib/qr-scanner/withQRScanner.tsx +0 -18
  139. package/src/lib/theme-params.tsx +0 -29
  140. package/src/lib/viewport/index.ts +0 -3
  141. package/src/lib/viewport/types.ts +0 -1
  142. package/src/lib/viewport/useViewport.ts +0 -15
  143. package/src/lib/viewport/withViewport.tsx +0 -18
  144. package/src/lib/web-app/index.ts +0 -3
  145. package/src/lib/web-app/types.ts +0 -1
  146. package/src/lib/web-app/useWebApp.ts +0 -13
  147. package/src/lib/web-app/withWebApp.tsx +0 -18
  148. package/src/provider/context.ts +0 -11
  149. package/src/provider/hocs.tsx +0 -18
  150. package/src/provider/hooks.ts +0 -23
@@ -1,47 +1,64 @@
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';
1
+ import { init } from '@tma.js/sdk';
2
+ import React, { useEffect, useMemo, useState } from 'react';
9
3
 
10
- import { sdkContext } from './context.js';
11
- import type { SDKComponents, SDKContext } from './types.js';
4
+ import { SDKContext } from './SDKContext.js';
5
+ import type { SDKContextType, SDKProviderProps } from './types.js';
6
+ import type { InitResult } from '../types.js';
12
7
 
13
- export type SDKInitOptions = InitOptions;
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>();
14
12
 
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
13
  useEffect(() => {
28
- // Update init status.
29
- setDidInit(true);
14
+ setLoading(true);
30
15
 
31
- // Init SDK.
32
- init(initOptions)
33
- .then(setComponents)
34
- .catch(setError);
16
+ init({ ...options, async: true })
17
+ .then(setInitResult)
18
+ .catch(setError)
19
+ .finally(() => setLoading(false));
35
20
  // eslint-disable-next-line react-hooks/exhaustive-deps
36
21
  }, []);
37
22
 
38
- const context = useMemo<SDKContext>(() => ({
39
- components,
40
- didInit,
41
- error,
42
- }), [didInit, components, error]);
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
+ }
43
38
 
44
- return <sdkContext.Provider value={context}>{children}</sdkContext.Provider>;
45
- });
39
+ function SyncProvider({ options = {}, children }: SDKProviderProps) {
40
+ const context = useMemo<SDKContextType>(() => {
41
+ const result: SDKContextType = { loading: false };
46
42
 
47
- SDKProvider.displayName = 'SDKProvider';
43
+ try {
44
+ result.initResult = init({ ...options, async: 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
+ // eslint-disable-next-line react/destructuring-assignment
61
+ return props.options?.async
62
+ ? <AsyncProvider {...props} />
63
+ : <SyncProvider {...props} />;
64
+ }
@@ -1,5 +1,7 @@
1
- export * from './context.js';
2
- export * from './hocs.js';
3
- export * from './hooks.js';
1
+ export * from './SDKContext.js';
4
2
  export * from './SDKProvider.js';
5
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';
@@ -1,34 +1,27 @@
1
- import type { InitResult } from '@tma.js/sdk';
1
+ import type { PropsWithChildren } from 'react';
2
2
 
3
- /**
4
- * List of provided SDK components.
5
- */
6
- export type SDKComponents = InitResult;
3
+ import type { InitOptions, InitResult } from '../types.js';
7
4
 
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];
5
+ export type SDKProviderProps = PropsWithChildren<{
6
+ /**
7
+ * Init function options.
8
+ */
9
+ options?: InitOptions;
10
+ }>;
17
11
 
18
- export interface SDKContext {
12
+ export interface SDKContextType {
19
13
  /**
20
- * Components ready to use. This value will be null until initialization
21
- * is done.
14
+ * Error occurred during initialization.
22
15
  */
23
- components: SDKComponents | null;
16
+ error?: unknown;
24
17
 
25
18
  /**
26
- * States, that SDK called `init()` method.
19
+ * Initialization result.
27
20
  */
28
- didInit: boolean;
21
+ initResult?: InitResult;
29
22
 
30
23
  /**
31
- * Error occurred during initialization.
24
+ * True if SDK is loading.
32
25
  */
33
- error: unknown | null;
26
+ loading: boolean;
34
27
  }
@@ -0,0 +1,34 @@
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
+ }
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,11 @@
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
+ }
@@ -0,0 +1,21 @@
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
+ }
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,16 @@
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 './qr-scanner.js';
14
+ export * from './theme-params.js';
15
+ export * from './utils.js';
16
+ export * from './viewport.js';
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,21 @@
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
+ }
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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);
@@ -0,0 +1,12 @@
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 ADDED
@@ -0,0 +1,29 @@
1
+ import type { InitOptions, InitResult } from '@tma.js/sdk';
2
+
3
+ export { InitOptions, InitResult };
4
+
5
+ export interface Trackable {
6
+ on(event: 'change', listener: () => void): () => void;
7
+ }
8
+
9
+ /**
10
+ * SDK init result keys, which describe values with trackable changes.
11
+ */
12
+ export type DynamicInitResultKey = {
13
+ [K in InitResultKey]-?: InitResultValue<K> extends Trackable ? K : never;
14
+ }[InitResultKey];
15
+
16
+ /**
17
+ * SDK init result keys, which describe static values.
18
+ */
19
+ export type StaticInitResultKey = Exclude<InitResultKey, DynamicInitResultKey>;
20
+
21
+ /**
22
+ * SDK initialization result key.
23
+ */
24
+ export type InitResultKey = keyof InitResult;
25
+
26
+ /**
27
+ * Returns SDK init result value type by its key.
28
+ */
29
+ export type InitResultValue<K extends InitResultKey> = InitResult[K];
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useBackButton.js';
3
- export * from './withBackButton.js';
@@ -1 +0,0 @@
1
- export type { BackButton } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { BackButton } from './types.js';
2
- /**
3
- * Returns BackButton component instance.
4
- */
5
- export declare function useBackButton(): BackButton;
@@ -1,9 +0,0 @@
1
- import { type ComponentType } from 'react';
2
- import type { BackButton } from './types.js';
3
- /**
4
- * HOC which passes BackButton SDK component to wrapped React component.
5
- * @param Component - component to wrap.
6
- */
7
- export declare function withBackButton<P extends {
8
- backButton?: BackButton;
9
- }>(Component: ComponentType<P>): ComponentType<Omit<P, 'backButton'>>;
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useClosingBehaviour.js';
3
- export * from './withClosingBehaviour.js';
@@ -1 +0,0 @@
1
- export type { ClosingBehaviour } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { ClosingBehaviour } from './types.js';
2
- /**
3
- * Returns ClosingBehaviour component instance.
4
- */
5
- export declare function useClosingBehaviour(): ClosingBehaviour;
@@ -1,9 +0,0 @@
1
- import { type ComponentType } from 'react';
2
- import type { ClosingBehaviour } from './types.js';
3
- /**
4
- * HOC which passes ClosingBehaviour SDK component to wrapped React component.
5
- * @param Component - component to wrap.
6
- */
7
- export declare function withClosingBehaviour<P extends {
8
- closingBehaviour?: ClosingBehaviour;
9
- }>(Component: ComponentType<P>): ComponentType<Omit<P, 'closingBehaviour'>>;
@@ -1,14 +0,0 @@
1
- import { type ComponentType } from 'react';
2
- import type { CloudStorage } from '@tma.js/sdk';
3
- /**
4
- * Returns CloudStorage component instance.
5
- */
6
- export declare function useCloudStorage(): CloudStorage;
7
- /**
8
- * HOC which passes CloudStorage SDK component to wrapped React component.
9
- * @param Component - component to wrap.
10
- */
11
- export declare function withCloudStorage<P extends {
12
- cloudStorage?: CloudStorage;
13
- }>(Component: ComponentType<P>): ComponentType<Omit<P, 'cloudStorage'>>;
14
- export type { CloudStorage };
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './useHapticFeedback.js';
3
- export * from './withHapticFeedback.js';
@@ -1 +0,0 @@
1
- export type { HapticFeedback } from '@tma.js/sdk';
@@ -1,5 +0,0 @@
1
- import type { HapticFeedback } from './types.js';
2
- /**
3
- * Returns HapticFeedback component instance.
4
- */
5
- export declare function useHapticFeedback(): HapticFeedback;
@@ -1,9 +0,0 @@
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'>>;
@@ -1,26 +0,0 @@
1
- import type { PostEvent } from '@tma.js/sdk';
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 {};