zapishis-client/apps/web/hooks/telegram/use-back-button.ts
vchikalkin 1f168df095 Refactor ProPage and improve back button logic
- Removed the unused PageHeader component from ProPage for a cleaner layout.
- Updated the isRootLevelPage function to enhance path exclusion logic, ensuring it correctly identifies root level pages.
2025-09-19 16:21:12 +03:00

41 lines
901 B
TypeScript

'use client';
import { backButton } from '@telegram-apps/sdk-react';
import { usePathname, useRouter } from 'next/navigation';
import { useCallback, useEffect } from 'react';
const exclude = ['/pro'];
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 (backButton.isMounted()) {
if (isRootLevelPage(pathname)) {
backButton.hide();
} else {
backButton.show();
}
}
}, [pathname]);
}
function isRootLevelPage(pathname: string) {
if (exclude.some((path) => pathname.includes(path))) return false;
return pathname.split('/').filter(Boolean).length === 1;
}