feat(layout): integrate TelegramProvider and BackButton into main layout for enhanced navigation

This commit is contained in:
vchikalkin 2025-08-02 12:37:29 +03:00
parent 278da049a5
commit c9cc19c704
6 changed files with 52 additions and 7 deletions

View File

@ -1,13 +1,15 @@
import { UpdateProfile } from '@/components/auth';
import { BottomNav } from '@/components/navigation';
import { BackButton, BottomNav } from '@/components/navigation';
import { TelegramProvider } from '@/providers/telegram';
import { type PropsWithChildren } from 'react';
export default async function Layout({ children }: Readonly<PropsWithChildren>) {
return (
<>
<TelegramProvider>
<UpdateProfile />
<main className="grow">{children}</main>
<BottomNav />
</>
<BackButton />
</TelegramProvider>
);
}

View File

@ -0,0 +1,9 @@
'use client';
import { useBackButton } from '@/hooks/telegram';
export function BackButton() {
useBackButton();
return null;
}

View File

@ -1,13 +1,11 @@
'use client';
import { BackButton } from './back-button';
type Props = { title: string | undefined };
export function PageHeader(props: Readonly<Props>) {
return (
<div className="sticky top-0 z-50 flex h-12 items-center rounded-b-lg bg-transparent px-2 font-bold tracking-wide backdrop-blur-md">
<BackButton />
<div className="sticky top-0 z-50 flex h-12 items-center rounded-b-lg bg-transparent px-4 font-bold tracking-wide backdrop-blur-md">
{/* <BackButton /> */}
{props.title}
</div>
);

View File

@ -1,2 +1,3 @@
export * from './back-button';
export * from './bottom-nav';
export * from './header';

View File

@ -1,2 +1,3 @@
export * from './use-back-button';
export * from './use-client-once';
export * from './use-did-mount';

View File

@ -0,0 +1,34 @@
'use client';
import { backButton } from '@telegram-apps/sdk-react';
import { usePathname, useRouter } from 'next/navigation';
import { useCallback, useEffect } from 'react';
export function useBackButton() {
const { back } = useRouter();
const pathname = usePathname();
const onBackClick = useCallback(() => {
if (pathname !== '/') {
back();
}
}, [pathname, back]);
useEffect(() => {
const off = backButton.onClick(onBackClick);
return off;
}, [onBackClick]);
useEffect(() => {
if (isRootLevelPage(pathname)) {
backButton.hide();
} else {
backButton.show();
}
}, [pathname]);
}
function isRootLevelPage(path: string) {
return path.split('/').filter(Boolean).length === 1;
}