-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(init-data): refactor init data utils. Add utility to get init da…
…ta raw representation
- Loading branch information
Showing
6 changed files
with
44 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.