-
Notifications
You must be signed in to change notification settings - Fork 113
/
Root.vue
132 lines (103 loc) · 2.91 KB
/
Root.vue
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<template>
<root>
<router-view />
<template #overContent>
<back-button :show="showBackButton" @on-click="onBackButton" />
<slot name="overContent" />
</template>
</root>
</template>
<script setup lang="ts">
import { THEME_TOKEN, WAS_INTERACTION_TOKEN } from '@tok/generation/tokens';
import { useI18n } from '@tok/i18n';
import { BackButton } from '@tok/telegram-ui/components/BackButton';
import { useTelegramSdk } from '@tok/telegram-ui/use/sdk';
import { useTheme } from '@tok/telegram-ui/use/theme';
import { Root } from '@tok/ui/components/Root';
import {
computed,
inject,
onBeforeUnmount,
onMounted,
provide,
ref,
watch,
} from 'vue';
import { useRouter } from 'vue-router';
const themeParam = inject(THEME_TOKEN, 'auto');
const theme = useTheme(themeParam);
const tg = useTelegramSdk();
const router = useRouter();
const i18n = useI18n();
const wasInteraction = ref(false);
provide(WAS_INTERACTION_TOKEN, wasInteraction);
const isSupportedLocale = (locale: string) => {
return i18n.available.includes(locale);
};
const tgLocale = tg.initDataUnsafe.user?.language_code;
if (tgLocale && isSupportedLocale(tgLocale)) {
i18n.locale.value = tgLocale;
}
watch(router.currentRoute, (value) => {
const queryLanguageCode = value.query.language_code;
const strLang =
typeof queryLanguageCode === 'string' ? queryLanguageCode : undefined;
const supported = strLang && isSupportedLocale(strLang) ? strLang : undefined;
if (!tgLocale && supported) {
i18n.locale.value = supported;
}
});
const onSetInteraction = () => {
wasInteraction.value = true;
};
onMounted(() => {
tg.expand();
window.addEventListener('mousedown', onSetInteraction);
window.addEventListener('touchstart', onSetInteraction);
window.addEventListener('touchend', onSetInteraction);
});
onBeforeUnmount(() => {
window.removeEventListener('mousedown', onSetInteraction);
window.removeEventListener('touchstart', onSetInteraction);
window.removeEventListener('touchend', onSetInteraction);
});
let ready = false;
const onLoadMessages = (locale: string) => {
if (i18n.available.length === 0) {
tg.ready();
ready = true;
return;
}
i18n
.load(locale)
.then((messages) => {
i18n.setMessages(locale, messages);
})
.finally(() => {
if (!ready) {
tg.ready();
ready = true;
}
});
};
watch(i18n.locale, onLoadMessages, { immediate: true });
const onBackButton = () => {
const hasHistory = window.history.length > 2;
if (hasHistory) {
router.back();
} else {
router.replace('/');
}
};
const showBackButton = computed(() => {
const value = router.currentRoute.value;
if (value.path === '/') {
return !!value.query.page;
}
return true;
});
const setThemeAttribute = (theme: 'dark' | 'light') => {
document.documentElement.setAttribute('data-theme', theme);
};
watch(theme, setThemeAttribute, { immediate: true });
</script>