Vlad Chikalkin 49df4365ca
Feature/telegram integration (#4)
* add files from official template

* remove all official template trash
2024-12-11 16:00:55 +03:00

46 lines
1.3 KiB
TypeScript

/* eslint-disable sonarjs/function-return-type */
'use client';
import { useClientOnce, useDidMount, useTelegramMock } from '@/hooks/telegram';
import { setLocale } from '@/utils/i18n/locale';
import { init } from '@/utils/init';
import { initData, useLaunchParams, useSignal } from '@telegram-apps/sdk-react';
import { type PropsWithChildren, useEffect } from 'react';
export function Root(props: Readonly<PropsWithChildren>) {
// Unfortunately, Telegram Mini Apps does not allow us to use all features of
// the Server Side Rendering. That's why we are showing loader on the server
// side.
const didMount = useDidMount();
if (!didMount) return <div>Loading</div>;
return <RootInner {...props} />;
}
function RootInner({ children }: PropsWithChildren) {
const isDevelopment = process.env.NODE_ENV === 'development';
// Mock Telegram environment in development mode if needed.
if (isDevelopment) {
// eslint-disable-next-line react-hooks/rules-of-hooks
useTelegramMock();
}
const lp = useLaunchParams();
const debug = isDevelopment || lp.startParam === 'debug';
// Initialize the library.
useClientOnce(() => {
init(debug);
});
const initDataUser = useSignal(initData.user);
// Set the user locale.
useEffect(() => {
if (initDataUser) setLocale(initDataUser.languageCode);
}, [initDataUser]);
return children;
}