From 9dfd46538a00e3d126b7634f7d04ff66c76f98f5 Mon Sep 17 00:00:00 2001 From: Devesh Pal Date: Sat, 7 Sep 2024 16:51:13 +0530 Subject: [PATCH 1/2] Add is_premium to WebAppUser --- src/useInitData.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/useInitData.ts b/src/useInitData.ts index 6feff9a..bd80fc0 100644 --- a/src/useInitData.ts +++ b/src/useInitData.ts @@ -21,7 +21,8 @@ export type WebAppUser = { last_name?: string; username?: string; language_code?: string; - photo_url?: true; + photo_url?: string; + is_premium?: boolean; added_to_attachment_menu?: true; allows_write_to_pm?: true; }; From da6aebc687d2a4a1e96514926bc07072be4a6c31 Mon Sep 17 00:00:00 2001 From: Devesh Pal Date: Sat, 7 Sep 2024 17:02:39 +0530 Subject: [PATCH 2/2] feat: Add SettingsButton --- docs/interfaces/SettingsButtonProps.md | 27 ++++++++++++++++++++ src/SettingsButton.tsx | 34 ++++++++++++++++++++++++++ src/WebAppProvider.tsx | 1 + src/core/context.ts | 2 ++ src/index.ts | 4 +++ 5 files changed, 68 insertions(+) create mode 100644 docs/interfaces/SettingsButtonProps.md create mode 100644 src/SettingsButton.tsx diff --git a/docs/interfaces/SettingsButtonProps.md b/docs/interfaces/SettingsButtonProps.md new file mode 100644 index 0000000..609273c --- /dev/null +++ b/docs/interfaces/SettingsButtonProps.md @@ -0,0 +1,27 @@ +[@vkruglikov/react-telegram-web-app](../README.md) / SettingsButtonProps + +# Interface: SettingsButtonProps + +The props type of [`SettingsButton`](../README.md#settingsbutton). + +## Table of contents + +### Properties + +- [onClick](SettingsButtonProps.md#onclick) + +## Properties + +### onClick + +• `Optional` **onClick**: () => `void` + +#### Type declaration + +▸ (): `void` + +The settings button press event handler + +##### Returns + +`void` diff --git a/src/SettingsButton.tsx b/src/SettingsButton.tsx new file mode 100644 index 0000000..5c8de48 --- /dev/null +++ b/src/SettingsButton.tsx @@ -0,0 +1,34 @@ +import { useContext, useEffect, useId } from 'react'; +import { useWebApp, useSmoothButtonsTransition, systemContext } from './core'; + +export interface SettingsButtonProps { + onClick?: () => void; +} + +const SettingsButton = ({ onClick }: SettingsButtonProps): null => { + const system = useContext(systemContext); + const buttonId = useId(); + const WebApp = useWebApp(); + + useEffect(() => { + if (!onClick || !WebApp?.SettingsButton) { + return; + } + + WebApp.SettingsButton.onClick(onClick); + return () => { + WebApp.SettingsButton.offClick(onClick); + }; + }, [onClick, WebApp]); + + useSmoothButtonsTransition({ + show: WebApp?.SettingsButton?.show, + hide: WebApp?.SettingsButton?.hide, + currentShowedIdRef: system.SettingsButton, + id: buttonId, + }); + + return null; +}; + +export default SettingsButton; diff --git a/src/WebAppProvider.tsx b/src/WebAppProvider.tsx index 400aabe..aa393e5 100644 --- a/src/WebAppProvider.tsx +++ b/src/WebAppProvider.tsx @@ -62,6 +62,7 @@ const WebAppProvider = ({ const forceHideButtons = () => { globalWebApp?.MainButton?.hide(); globalWebApp?.BackButton?.hide(); + globalWebApp?.SettingsButton?.hide(); }; window.addEventListener('beforeunload', forceHideButtons); diff --git a/src/core/context.ts b/src/core/context.ts index 81f2909..2eec51e 100644 --- a/src/core/context.ts +++ b/src/core/context.ts @@ -38,11 +38,13 @@ export const optionsContext = createContext(DEFAULT_OPTIONS); type SystemContext = { MainButton: MutableRefObject; BackButton: MutableRefObject; + SettingsButton: MutableRefObject; }; export const createSystemContextValue = () => ({ MainButton: { current: null }, BackButton: { current: null }, + SettingsButton: { current: null }, }); export const systemContext = createContext( diff --git a/src/index.ts b/src/index.ts index 98f7710..58c0269 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,9 @@ export { default as MainButton, MainButtonProps } from './MainButton'; export { default as BackButton, BackButtonProps } from './BackButton'; +export { + default as SettingsButton, + SettingsButtonProps, +} from './SettingsButton'; export { default as useShowPopup, ShowPopupFunction,