-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseTMAStore.ts
More file actions
69 lines (60 loc) · 1.57 KB
/
useTMAStore.ts
File metadata and controls
69 lines (60 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { RetrieveLaunchParamsResult } from '@tma.js/sdk-vue';
import {
initData,
init as initSDK,
retrieveLaunchParams,
setDebug,
useSignal,
} from '@tma.js/sdk-vue';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
// Extend Window interface to include Telegram
declare global {
interface Window {
Telegram?: {
WebApp?: unknown;
};
}
}
export const useTMAStore = defineStore('tmaStore', () => {
const isInitialized = ref(false);
const launchParams = ref<RetrieveLaunchParamsResult | null>(null);
const initDataSignal = useSignal(initData.state);
const error = ref<string | null>(null);
const debugLogs = ref<string[]>([]);
const initialize = async (): Promise<void> => {
try {
setDebug(true);
initSDK();
// Mount components first
initData.restore();
// Then retrieve data
const params = retrieveLaunchParams();
launchParams.value = params;
isInitialized.value = true;
error.value = null;
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
debugLogs.value.push(errorMessage);
error.value = errorMessage;
}
};
const reset = () => {
isInitialized.value = false;
launchParams.value = null;
error.value = null;
debugLogs.value = [];
};
return {
isAvailable: computed(
() => typeof window !== 'undefined' && Boolean(window.Telegram?.WebApp),
),
isInitialized,
launchParams,
initData: initDataSignal,
error,
debugLogs,
initialize,
reset,
};
});