From a55b5cefe0897162814a545030d6d134b6c5cd24 Mon Sep 17 00:00:00 2001 From: Vladislav Kibenko Date: Sun, 5 Nov 2023 02:14:00 +0300 Subject: [PATCH] feat(init-data): refactor init data utils. Add utility to get init data raw representation --- packages/sdk-react/src/lib/index.ts | 2 +- packages/sdk-react/src/lib/init-data.tsx | 43 +++++++++++++++++++ packages/sdk-react/src/lib/init-data/index.ts | 3 -- packages/sdk-react/src/lib/init-data/types.ts | 1 - .../src/lib/init-data/useInitData.ts | 9 ---- .../src/lib/init-data/withInitData.tsx | 18 -------- 6 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 packages/sdk-react/src/lib/init-data.tsx delete mode 100644 packages/sdk-react/src/lib/init-data/index.ts delete mode 100644 packages/sdk-react/src/lib/init-data/types.ts delete mode 100644 packages/sdk-react/src/lib/init-data/useInitData.ts delete mode 100644 packages/sdk-react/src/lib/init-data/withInitData.tsx diff --git a/packages/sdk-react/src/lib/index.ts b/packages/sdk-react/src/lib/index.ts index 715cde57d..1a3d7bc41 100644 --- a/packages/sdk-react/src/lib/index.ts +++ b/packages/sdk-react/src/lib/index.ts @@ -1,7 +1,6 @@ export * from './back-button/index.js'; export * from './closing-behavior/index.js'; export * from './haptic-feedback/index.js'; -export * from './init-data/index.js'; export * from './launch-params/index.js'; export * from './main-button/index.js'; export * from './popup/index.js'; @@ -10,4 +9,5 @@ export * from './viewport/index.js'; export * from './web-app/index.js'; export * from './cloud-storage.js'; export { usePostEvent } from './hooks.js'; +export * from './init-data.js'; export * from './theme-params.js'; diff --git a/packages/sdk-react/src/lib/init-data.tsx b/packages/sdk-react/src/lib/init-data.tsx new file mode 100644 index 000000000..22adc273c --- /dev/null +++ b/packages/sdk-react/src/lib/init-data.tsx @@ -0,0 +1,43 @@ +import React, { type ComponentType } from 'react'; +import type { InitData } from '@tma.js/sdk'; + +import { useUnit } from '../provider/index.js'; + +interface EnhancedProps { + initData: InitData | null; + initDataRaw: string | null; +} + +/** + * Returns InitData component instance. + */ +export function useInitData(): InitData | null { + return useUnit('initData') || null; +} + +/** + * Returns init data raw representation. + */ +export function useInitDataRaw(): string | null { + return useUnit('initDataRaw') || null; +} + +/** + * HOC which passes InitData SDK component to wrapped React component. + * @param Component - component to wrap. + */ +export function withInitData

>( + Component: ComponentType

, +): ComponentType> { + return function WithInitData(props) { + const enhancedProps = { + ...props, + initData: useInitData(), + initDataRaw: useInitDataRaw(), + } as P; + + return ; + }; +} + +export { InitData }; diff --git a/packages/sdk-react/src/lib/init-data/index.ts b/packages/sdk-react/src/lib/init-data/index.ts deleted file mode 100644 index ae6529c05..000000000 --- a/packages/sdk-react/src/lib/init-data/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './types.js'; -export * from './useInitData.js'; -export * from './withInitData.js'; diff --git a/packages/sdk-react/src/lib/init-data/types.ts b/packages/sdk-react/src/lib/init-data/types.ts deleted file mode 100644 index 3b9624b52..000000000 --- a/packages/sdk-react/src/lib/init-data/types.ts +++ /dev/null @@ -1 +0,0 @@ -export type { InitData } from '@tma.js/sdk'; diff --git a/packages/sdk-react/src/lib/init-data/useInitData.ts b/packages/sdk-react/src/lib/init-data/useInitData.ts deleted file mode 100644 index 4df7110db..000000000 --- a/packages/sdk-react/src/lib/init-data/useInitData.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { useUnit } from '../../provider/index.js'; -import type { InitData } from './types.js'; - -/** - * Returns InitData component instance. - */ -export function useInitData(): InitData | null { - return useUnit('initData') || null; -} diff --git a/packages/sdk-react/src/lib/init-data/withInitData.tsx b/packages/sdk-react/src/lib/init-data/withInitData.tsx deleted file mode 100644 index 7e4ce643a..000000000 --- a/packages/sdk-react/src/lib/init-data/withInitData.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import React, { type ComponentType } from 'react'; - -import { useInitData } from './useInitData.js'; -import type { InitData } from './types.js'; - -/** - * HOC which passes InitData SDK component to wrapped React component. - * @param Component - component to wrap. - */ -export function withInitData

( - Component: ComponentType

, -): ComponentType> { - return function WithInitData(props: Omit) { - const p = { ...props, initData: useInitData() } as P; - - return ; - }; -}