Skip to content

Commit

Permalink
Move to Telegram SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
heyqbnk committed Jun 24, 2024
1 parent 321476f commit a298044
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 275 deletions.
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Mini Apps platform using the following technologies and libraries:
- [React](https://react.dev/)
- [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript)
- [TON Connect](https://docs.ton.org/develop/dapps/ton-connect/overview)
- [@tma.js SDK](https://docs.telegram-mini-apps.com/packages/tma-js-sdk)
- [Telegram SDK](https://core.telegram.org/bots/webapps#initializing-mini-apps)
- [Telegram UI](https://github.com/Telegram-Mini-Apps/TelegramUI)
- [Vite](https://vitejs.dev/)

Expand Down Expand Up @@ -62,24 +62,15 @@ After this, you will see a similar message in your terminal:
```bash
VITE ready in 275 ms

➜ Local: http://localhost:5173/reactjs-template
➜ Local: http://localhost:5173/reactjs-js-tsdk-template
➜ press h + enter to show help
```

To view the application, you need to open the `Local`
link (`http://localhost:5173/reactjs-template` in this example) in your browser:
link (`http://localhost:5173/reactjs-js-tsdk-template` in this example) in your browser:

![Application](assets/application.png)

It is important to note that some libraries in this template, such as `@tma.js/sdk`, are not
intended for use outside of Telegram.

Nevertheless, they appear to function properly. This is because the `src/mockEnv.js` file, which is
imported in the application's entry point (`src/index.js`), employs the `mockTelegramEnv` function
to simulate the Telegram environment. This trick convinces the application that it is running in a
Telegram-based environment. Therefore, be cautious not to use this function in production mode
unless you fully understand its implications.

### Run Inside Telegram

Although it is possible to run the application outside of Telegram, it is recommended to develop it
Expand All @@ -100,11 +91,11 @@ After uncommenting the function, run the `dev` script again and observe the outp
```bash
VITE ready in 331 ms

➜ Local: https://localhost:5173/reactjs-template
➜ Local: https://localhost:5173/reactjs-js-tsdk-template
➜ press h + enter to show help
```

Visiting the `Local` link (`https://localhost:5173/reactjs-template` in this example) in your
Visiting the `Local` link (`https://localhost:5173/reactjs-js-tsdk-template` in this example) in your
browser, you will see the following warning:

![SSL Warning](assets/ssl-warning.png)
Expand All @@ -131,10 +122,10 @@ Here is an example of the output you will see:
```bash
VITE v5.2.12 ready in 257 ms

➜ Local: https://localhost:5173/reactjs-template
➜ Network: https://172.27.224.1:5173/reactjs-template
➜ Network: https://172.19.32.1:5173/reactjs-template
➜ Network: https://192.168.0.171:5173/reactjs-template
➜ Local: https://localhost:5173/reactjs-js-tsdk-template
➜ Network: https://172.27.224.1:5173/reactjs-js-tsdk-template
➜ Network: https://172.19.32.1:5173/reactjs-js-tsdk-template
➜ Network: https://192.168.0.171:5173/reactjs-js-tsdk-template
```

All `Network` links listed here are accessible to devices on the same network. Using these links,
Expand Down Expand Up @@ -243,5 +234,4 @@ project's information.
## Useful Links

- [Platform documentation](https://docs.telegram-mini-apps.com/)
- [@tma.js/sdk-react documentation](https://docs.telegram-mini-apps.com/packages/tma-js-sdk-react)
- [Telegram developers community chat](https://t.me/devs)
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="theme-color" content="#000000"/>
<title>Vite + React + TS</title>
<title>Telegram Mini App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
73 changes: 28 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "reactjs-js-template",
"name": "reactjs-js-tsdk-template",
"private": true,
"version": "0.0.1",
"type": "module",
"homepage": "https://telegram-mini-apps.github.io/reactjs-js-template",
"homepage": "https://telegram-mini-apps.github.io/reactjs-js-tsdk-template",
"scripts": {
"deploy": "gh-pages -d dist",
"dev": "vite",
Expand All @@ -15,9 +15,9 @@
},
"dependencies": {
"@telegram-apps/telegram-ui": "^2.1.4",
"@tma.js/react-router-integration": "^4.0.3",
"@tma.js/sdk-react": "^2.2.5",
"@tonconnect/ui-react": "^2.0.5",
"@twa-dev/sdk": "^7.0.0",
"@twa-dev/types": "^7.0.0",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"eruda": "^3.0.1",
Expand All @@ -30,6 +30,7 @@
"@babel/eslint-parser": "^7.24.1",
"@babel/preset-env": "^7.24.4",
"@babel/preset-react": "^7.24.1",
"@vitejs/plugin-basic-ssl": "^1.1.0",
"@vitejs/plugin-react-swc": "^3.6.0",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.1",
Expand Down
75 changes: 32 additions & 43 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,57 @@
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 { useEffect, useMemo } from 'react';
import { useEffect } from 'react';
import {
Navigate,
Route,
Router,
BrowserRouter,
Routes,
useLocation,
useNavigate,
} from 'react-router-dom';

import { routes } from '@/navigation/routes.jsx';

/**
* @return {JSX.Element}
*/
export function App() {
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 a 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]);
if (location.pathname === '/') {
WebApp.BackButton.isVisible && WebApp.BackButton.hide();
} else {
!WebApp.BackButton.isVisible && WebApp.BackButton.show();
}
}, [location]);

return null;
}

/**
* @return {JSX.Element}
*/
export function App() {
return (
<AppRoot
appearance={miniApp.isDark ? 'dark' : 'light'}
platform={['macos', 'ios'].includes(lp.platform) ? 'ios' : 'base'}
appearance={WebApp.colorScheme}
platform={['macos', 'ios'].includes(WebApp.platform) ? 'ios' : 'base'}
>
<Router location={location} navigator={reactNavigator}>
<BrowserRouter>
<BackButtonManipulator/>
<Routes>
{routes.map((route) => <Route key={route.path} {...route} />)}
<Route path='*' element={<Navigate to='/'/>}/>
</Routes>
</Router>
</BrowserRouter>
</AppRoot>
);
}
6 changes: 3 additions & 3 deletions src/components/DisplayData/DisplayData.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isRGB } from '@tma.js/sdk-react';
import { Cell, Checkbox, Section } from '@telegram-apps/telegram-ui';

import { RGB } from '@/components/RGB/RGB.jsx';
Expand All @@ -9,10 +8,11 @@ import './DisplayData.css';
/**
* @typedef {object} DisplayDataRow
* @property {string} title
* @property {string | boolean | import('react').ReactNode | import('@tma.js/sdk-react').RGB} [value]
* @property {string | boolean | import('react').ReactNode} [value]
*/

/**
* @param {import('react').ReactNode} header - section header.
* @param {DisplayDataRow[]} rows - list of rows to be displayed.
* @return {JSX.Element}
*/
Expand All @@ -28,7 +28,7 @@ export function DisplayData({ header, rows }) {
if ('type' in item) {
valueNode = <Link to={item.value}>Open</Link>;
} else if (typeof item.value === 'string') {
valueNode = isRGB(item.value)
valueNode = item.value.match(/^#[a-f0-9]{3,6}$/i)
? <RGB color={item.value}/>
: item.value;
} else if (typeof item.value === 'boolean') {
Expand Down
10 changes: 4 additions & 6 deletions src/components/Link/Link.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { classNames, useUtils } from '@tma.js/sdk-react';
import { useCallback } from 'react';
import WebApp from '@twa-dev/sdk';
import { Link as RouterLink } from 'react-router-dom';

import './Link.css';
Expand All @@ -14,8 +14,6 @@ export function Link({
to,
...rest
}) {
const utils = useUtils();

const onClick = useCallback((e) => {
propsOnClick?.(e);

Expand All @@ -36,16 +34,16 @@ export function Link({

if (isExternal) {
e.preventDefault();
return utils.openLink(targetUrl.toString());
return WebApp.openLink(targetUrl.toString());
}
}, [to, propsOnClick, utils]);
}, [to, propsOnClick]);

return (
<RouterLink
{...rest}
to={to}
onClick={onClick}
className={classNames(className, 'link')}
className={[className, 'link'].filter(Boolean).join(' ')}
/>
);
}
Loading

0 comments on commit a298044

Please sign in to comment.