- 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.
41 lines
901 B
TypeScript
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;
|
|
}
|