Skip to content

Commit

Permalink
Merge pull request #164 from Telegram-Mini-Apps/158-add-hook-in-sdk-r…
Browse files Browse the repository at this point in the history
…eact-to-get-init-data-raw-representation

158 add hook in sdk react to get init data raw representation
  • Loading branch information
heyqbnk authored Nov 4, 2023
2 parents 8df377c + 2978287 commit 336cd5e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-waves-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tma.js/sdk-react": minor
---

Add `useInitDataRaw` utility to retrieve init data raw representation.
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.

1 comment on commit 336cd5e

@vercel
Copy link

@vercel vercel bot commented on 336cd5e Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.