Skip to content

Commit 99dcf41

Browse files
authored
Merge pull request #650 from Telegram-Mini-Apps/feature/location-manager-open-settings
feat(sdk): implement locationManager.openSettings. Export it and the unmount function
2 parents 7566bdc + c36aa39 commit 99dcf41

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@telegram-apps/sdk": minor
3+
---
4+
5+
Implement `locationManager.openSettings` method. Exports location manager's `unmount` method. Fix invalid method name for `requestLocation`

packages/sdk/src/scopes/components/location-manager/exports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ export {
1212
requestLocation,
1313
mountPromise as locationManagerMountPromise,
1414
isAvailable as isLocationManagerAvailable,
15+
openSettings as openLocationManagerSettings,
16+
unmount as unmountLocationManager,
1517
} from './exports.variable.js';
1618
export * as locationManager from './exports.variable.js';

packages/sdk/src/scopes/components/location-manager/exports.variable.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ export {
1212
requestLocation,
1313
mountPromise,
1414
isAvailable,
15+
openSettings,
16+
unmount,
1517
} from './location-manager.js';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { beforeEach, describe, vi } from 'vitest';
2+
3+
import { testSafety } from '@test-utils/predefined/testSafety.js';
4+
import {
5+
openSettings,
6+
requestLocation,
7+
mount,
8+
_isMounted,
9+
} from '@/scopes/components/location-manager/location-manager.js';
10+
import { mockPostEvent, resetPackageState } from '@test-utils/utils.js';
11+
12+
beforeEach(() => {
13+
resetPackageState();
14+
vi.restoreAllMocks();
15+
mockPostEvent();
16+
});
17+
18+
describe.each([
19+
['requestLocation', requestLocation, _isMounted],
20+
['openSettings', openSettings, undefined],
21+
['mount', mount, undefined],
22+
] as const)('%s', (name, fn, isMounted) => {
23+
testSafety(fn, name, {
24+
component: 'locationManager',
25+
minVersion: '8.0',
26+
isMounted,
27+
});
28+
});

packages/sdk/src/scopes/components/location-manager/location-manager.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { EventPayload } from '@telegram-apps/bridge';
55
import type { Computed } from '@telegram-apps/signals';
66

77
import { defineMountFn } from '@/scopes/defineMountFn.js';
8-
import { request } from '@/globals.js';
8+
import { postEvent, request } from '@/globals.js';
99
import { createWrapComplete } from '@/scopes/wrappers/createWrapComplete.js';
1010
import { createWrapSupported } from '@/scopes/wrappers/createWrapSupported.js';
1111
import { NotAvailableError } from '@/errors.js';
@@ -16,6 +16,7 @@ import { createComputed, createSignal } from '@/signals-registry.js';
1616

1717
const COMPONENT_NAME = 'locationManager';
1818
const CHECK_LOCATION_METHOD = 'web_app_check_location';
19+
const OPEN_SETTINGS_METHOD = 'web_app_open_location_settings';
1920

2021
export interface State {
2122
/**
@@ -70,7 +71,6 @@ export const isAccessRequested = fromState('accessRequested');
7071
* @see location_checked
7172
*/
7273
function eventToState(event: EventPayload<'location_checked'>): State {
73-
console.log(event);
7474
let available = false;
7575
let accessRequested: Maybe<boolean>;
7676
let accessGranted: Maybe<boolean>;
@@ -159,10 +159,28 @@ const [
159159
* const location = await requestLocation();
160160
* }
161161
*/
162-
export const requestLocation = wrapComplete('getLocation', reqLocationFn);
162+
export const requestLocation = wrapComplete('requestLocation', reqLocationFn);
163163
export const [, requestLocationPromise, isRequestingLocation] = tReqLocationPromise;
164164
export const [, requestLocationError] = tReqLocationError;
165165

166+
/**
167+
* Opens the location access settings for bots. Useful when you need to request location access
168+
* from users who haven't granted it yet.
169+
*
170+
* Note that this method can be called only in response to user interaction with the Mini App
171+
* interface (e.g., a click inside the Mini App or on the main button).
172+
* @since Mini Apps v8.0
173+
* @throws {FunctionNotAvailableError} The environment is unknown
174+
* @throws {FunctionNotAvailableError} The SDK is not initialized
175+
* @throws {FunctionNotAvailableError} The function is not supported
176+
* @example
177+
* if (openSettings.isAvailable()) {
178+
* openSettings();
179+
* }
180+
*/
181+
export const openSettings = wrapSupported('openSettings', () => {
182+
postEvent(OPEN_SETTINGS_METHOD);
183+
}, OPEN_SETTINGS_METHOD);
166184

167185
/**
168186
* Unmounts the component.

0 commit comments

Comments
 (0)