Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

158 add hook in sdk react to get init data raw representation #164

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading