Skip to content

Commit

Permalink
feat(init-data): refactor init data utils. Add utility to get init da…
Browse files Browse the repository at this point in the history
…ta raw representation
  • Loading branch information
heyqbnk committed Nov 4, 2023
1 parent f4fc226 commit a55b5ce
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-react/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
43 changes: 43 additions & 0 deletions packages/sdk-react/src/lib/init-data.tsx
Original file line number Diff line number Diff line change
@@ -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<P extends Partial<EnhancedProps>>(
Component: ComponentType<P>,
): ComponentType<Omit<P, keyof EnhancedProps>> {
return function WithInitData(props) {
const enhancedProps = {
...props,
initData: useInitData(),
initDataRaw: useInitDataRaw(),
} as P;

return <Component {...enhancedProps} />;
};
}

export { InitData };
3 changes: 0 additions & 3 deletions packages/sdk-react/src/lib/init-data/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/sdk-react/src/lib/init-data/types.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/sdk-react/src/lib/init-data/useInitData.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/sdk-react/src/lib/init-data/withInitData.tsx

This file was deleted.

0 comments on commit a55b5ce

Please sign in to comment.