generated from Telegram-Mini-Apps/reactjs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
110 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,52 @@ | ||
import { useIntegration } from '@tma.js/react-router-integration'; | ||
import { | ||
bindMiniAppCSSVars, | ||
bindThemeParamsCSSVars, | ||
bindViewportCSSVars, | ||
initNavigator, useLaunchParams, | ||
useMiniApp, | ||
useThemeParams, | ||
useViewport, | ||
} from '@tma.js/sdk-react'; | ||
import WebApp from '@twa-dev/sdk'; | ||
import { AppRoot } from '@telegram-apps/telegram-ui'; | ||
import { type FC, useEffect, useMemo } from 'react'; | ||
import { type FC, useEffect } from 'react'; | ||
import { | ||
Navigate, | ||
Route, | ||
Router, | ||
BrowserRouter, | ||
Routes, | ||
useLocation, | ||
useNavigate, | ||
} from 'react-router-dom'; | ||
|
||
import { routes } from '@/navigation/routes.tsx'; | ||
|
||
export const App: FC = () => { | ||
const lp = useLaunchParams(); | ||
const miniApp = useMiniApp(); | ||
const themeParams = useThemeParams(); | ||
const viewport = useViewport(); | ||
|
||
useEffect(() => { | ||
return bindMiniAppCSSVars(miniApp, themeParams); | ||
}, [miniApp, themeParams]); | ||
|
||
useEffect(() => { | ||
return bindThemeParamsCSSVars(themeParams); | ||
}, [themeParams]); | ||
function BackButtonManipulator() { | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
return viewport && bindViewportCSSVars(viewport); | ||
}, [viewport]); | ||
function onClick() { | ||
navigate(-1); | ||
} | ||
WebApp.BackButton.onClick(onClick); | ||
|
||
// Create new application navigator and attach it to the browser history, so it could modify | ||
// it and listen to its changes. | ||
const navigator = useMemo(() => initNavigator('app-navigation-state'), []); | ||
const [location, reactNavigator] = useIntegration(navigator); | ||
return () => WebApp.BackButton.offClick(onClick); | ||
}, [navigate]); | ||
|
||
// Don't forget to attach the navigator to allow it to control the BackButton state as well | ||
// as browser history. | ||
useEffect(() => { | ||
navigator.attach(); | ||
return () => navigator.detach(); | ||
}, [navigator]); | ||
|
||
return ( | ||
<AppRoot | ||
appearance={miniApp.isDark ? 'dark' : 'light'} | ||
platform={['macos', 'ios'].includes(lp.platform) ? 'ios' : 'base'} | ||
> | ||
<Router location={location} navigator={reactNavigator}> | ||
<Routes> | ||
{routes.map((route) => <Route key={route.path} {...route} />)} | ||
<Route path='*' element={<Navigate to='/'/>}/> | ||
</Routes> | ||
</Router> | ||
</AppRoot> | ||
); | ||
}; | ||
if (location.pathname === '/') { | ||
WebApp.BackButton.isVisible && WebApp.BackButton.hide(); | ||
} else { | ||
!WebApp.BackButton.isVisible && WebApp.BackButton.show(); | ||
} | ||
}, [location]); | ||
|
||
return null; | ||
} | ||
|
||
export const App: FC = () => ( | ||
<AppRoot | ||
appearance={WebApp.colorScheme} | ||
platform={['macos', 'ios'].includes(WebApp.platform) ? 'ios' : 'base'} | ||
> | ||
<BrowserRouter> | ||
<BackButtonManipulator/> | ||
<Routes> | ||
{routes.map((route) => <Route key={route.path} {...route} />)} | ||
<Route path='*' element={<Navigate to='/'/>}/> | ||
</Routes> | ||
</BrowserRouter> | ||
</AppRoot> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.