-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from Telegram-Mini-Apps/feature/settings-button
Feature/settings button
- Loading branch information
Showing
12 changed files
with
172 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tma.js/sdk": minor | ||
--- | ||
|
||
Implement SettingsButton component |
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
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,25 @@ | ||
import { SettingsButton } from '~/settings-button/index.js'; | ||
import { getStorageValue, saveStorageValue } from '~/storage.js'; | ||
import type { PostEvent } from '~/bridge/index.js'; | ||
|
||
/** | ||
* Creates SettingsButton instance using last locally saved data also saving each state in | ||
* the storage. | ||
* @param isPageReload - was current page reloaded. | ||
* @param version - platform version. | ||
* @param postEvent - Bridge postEvent function | ||
*/ | ||
export function createSettingsButton( | ||
isPageReload: boolean, | ||
version: string, | ||
postEvent: PostEvent, | ||
): SettingsButton { | ||
const { isVisible = false } = isPageReload ? getStorageValue('settings-button') || {} : {}; | ||
const component = new SettingsButton(isVisible, version, postEvent); | ||
|
||
component.on('change', () => { | ||
saveStorageValue('settings-button', { isVisible: component.isVisible }); | ||
}); | ||
|
||
return component; | ||
} |
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
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
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,85 @@ | ||
import { | ||
off, | ||
on, | ||
type PostEvent, | ||
postEvent as defaultPostEvent, | ||
} from '~/bridge/index.js'; | ||
import { EventEmitter } from '~/event-emitter/index.js'; | ||
import { State } from '~/state/index.js'; | ||
import { createSupportsFunc, type SupportsFunc } from '~/supports/index.js'; | ||
import type { Version } from '~/version/index.js'; | ||
|
||
import type { SettingsButtonEvents, SettingsButtonState } from './types.js'; | ||
|
||
type Emitter = EventEmitter<SettingsButtonEvents>; | ||
|
||
export class SettingsButton { | ||
private readonly ee: Emitter = new EventEmitter(); | ||
|
||
private readonly state: State<SettingsButtonState>; | ||
|
||
constructor( | ||
isVisible: boolean, | ||
version: Version, | ||
private readonly postEvent: PostEvent = defaultPostEvent, | ||
) { | ||
this.state = new State({ isVisible }, this.ee); | ||
this.supports = createSupportsFunc(version, { | ||
show: 'web_app_setup_settings_button', | ||
hide: 'web_app_setup_settings_button', | ||
}); | ||
} | ||
|
||
private set isVisible(visible: boolean) { | ||
this.state.set('isVisible', visible); | ||
this.postEvent('web_app_setup_settings_button', { is_visible: visible }); | ||
} | ||
|
||
/** | ||
* True if SettingsButton is currently visible. | ||
*/ | ||
get isVisible(): boolean { | ||
return this.state.get('isVisible'); | ||
} | ||
|
||
/** | ||
* Hides the SettingsButton. | ||
*/ | ||
hide(): void { | ||
this.isVisible = false; | ||
} | ||
|
||
/** | ||
* Adds event listener. | ||
* @param event - event name. | ||
* @param listener - event listener. | ||
*/ | ||
on: Emitter['on'] = (event, listener) => ( | ||
event === 'click' | ||
? on('settings_button_pressed', listener) | ||
: this.ee.on(event, listener) | ||
); | ||
|
||
/** | ||
* Removes event listener. | ||
* @param event - event name. | ||
* @param listener - event listener. | ||
*/ | ||
off: Emitter['off'] = (event, listener) => ( | ||
event === 'click' | ||
? off('settings_button_pressed', listener) | ||
: this.ee.off(event, listener) | ||
); | ||
|
||
/** | ||
* Shows the SettingsButton. | ||
*/ | ||
show(): void { | ||
this.isVisible = true; | ||
} | ||
|
||
/** | ||
* Checks if specified method is supported by current component. | ||
*/ | ||
supports: SupportsFunc<'show' | 'hide'>; | ||
} |
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,2 @@ | ||
export * from './SettingsButton.js'; | ||
export * from './types.js'; |
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,15 @@ | ||
import type { MiniAppsEventListener } from '~/bridge/index.js'; | ||
import type { StateEvents } from '~/state/index.js'; | ||
|
||
export interface SettingsButtonState { | ||
isVisible: boolean; | ||
} | ||
|
||
export interface SettingsButtonEvents extends StateEvents<SettingsButtonState> { | ||
click: MiniAppsEventListener<'settings_button_pressed'>; | ||
} | ||
|
||
export type SettingsButtonEventName = keyof SettingsButtonEvents; | ||
|
||
export type SettingsButtonEventListener<E extends SettingsButtonEventName> = | ||
SettingsButtonEvents[E]; |
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