Skip to content

Commit

Permalink
Prepare for UI + i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
madhead committed Oct 23, 2023
1 parent c3cf7f2 commit a1ab1a4
Show file tree
Hide file tree
Showing 46 changed files with 759 additions and 26 deletions.
366 changes: 366 additions & 0 deletions mini-app/.pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion mini-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
"packageManager": "[email protected]",
"dependencies": {
"@twa-dev/sdk": "^6.9.0",
"i18next": "^23.6.0",
"i18next-browser-languagedetector": "^7.1.0",
"i18next-http-backend": "^2.2.2",
"normalize.css": "^8.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-i18next": "^13.3.1"
},
"devDependencies": {
"@types/node": "18.18.2",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@types/webpack": "^5.28.3",
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.8.1",
"html-webpack-plugin": "^5.5.3",
"less": "^4.2.0",
Expand Down
5 changes: 5 additions & 0 deletions mini-app/src/common/error/Error.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#error {
color: red;
font-size: large;
text-align: center;
}
11 changes: 11 additions & 0 deletions mini-app/src/common/error/Error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "./Error.less";

type ErrorProps = {
error: string;
};

function Error({ error }: ErrorProps) {
return <div id="error">{error}</div>;
}

export default Error;
27 changes: 7 additions & 20 deletions mini-app/src/history/HistoryApp.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import { useEffect, useState } from "react";
import "./HistoryApp.less";
import WebApp from "@twa-dev/sdk";
import Error from "../common/error/Error";

function HistoryApp() {
console.log(WebApp.initData);
console.log(WebApp.initDataUnsafe);

const [data, setData] = useState(null);
import { useTranslation } from "react-i18next";

useEffect(() => {
async function fetchData() {
const data = await fetch("/app/api/test");
const json = await data.json();
setData(json);
}

fetchData();
}, []);
function HistoryApp() {
const { t, i18n } = useTranslation();

return (
<div>
<h1>History</h1>
Init Data: {WebApp.initData}
</div>
<>
<Error error={t("errors.outsideOfTelegram")} />
</>
);
}

Expand Down
15 changes: 15 additions & 0 deletions mini-app/src/history/history.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@import url(https://fonts.googleapis.com/css2?family=Roboto);
@import '~normalize.css/normalize.css';

body {
font-family: "Roboto", sans-serif;
background-color: var(--tg-theme-bg-color);
padding: 80px;
height: var(--tg-viewport-height);
}

#root {
display: flex;
align-items: center;
justify-content: center;
}
5 changes: 5 additions & 0 deletions mini-app/src/history/history.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from "react";
import ReactDOM from "react-dom/client";
import "@twa-dev/sdk";

import "../i18n/i18n"

import "./history.less";

import HistoryApp from "./HistoryApp";

const root = ReactDOM.createRoot(
Expand Down
5 changes: 5 additions & 0 deletions mini-app/src/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errors": {
"outsideOfTelegram": "This app must be executed inside a Telegram client"
}
}
21 changes: 21 additions & 0 deletions mini-app/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import HttpBackend from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import WebApp from "@twa-dev/sdk";

document.documentElement.lang = WebApp.initDataUnsafe.user?.language_code || "en";

const httpBackend = new HttpBackend(null, {
loadPath: "/app/i18n/{{lng}}.json",
});

const languageDetector = new LanguageDetector(null, {
order: ["htmlTag"],
});

i18next.use(httpBackend).use(languageDetector).use(initReactI18next).init({
fallbackLng: "en",
});

export default i18next;
Empty file added mini-app/src/i18n/it.json
Empty file.
Empty file added mini-app/src/i18n/pt.json
Empty file.
5 changes: 5 additions & 0 deletions mini-app/src/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errors": {
"outsideOfTelegram": "Это приложение нужно запускать внутри Telegram клиента"
}
}
1 change: 1 addition & 0 deletions mini-app/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Tyzenhaus</title>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions mini-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"sourceMap": true,
"strict": true,
"lib": ["dom", "es5"],
"jsx": "react-jsx"
Expand Down
23 changes: 22 additions & 1 deletion mini-app/webpack/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import path from "path";
import webpack from "webpack";
import "webpack-dev-server";
import HtmlWebpackPlugin from "html-webpack-plugin";
import CopyPlugin from "copy-webpack-plugin";

const root = path.resolve(__dirname, "..");
const src = path.resolve(root, "src");
const build = path.resolve(root, "build");

const configuration: webpack.Configuration = {
entry: {
history: [path.resolve(src, "history/history.tsx")],
expense: [path.resolve(src, "expense/expense.tsx")],
},
output: {
path: path.resolve(root, "build"),
path: build,
publicPath: "/app",
},
optimization: {
Expand Down Expand Up @@ -61,6 +63,17 @@ const configuration: webpack.Configuration = {
},
],
},
{
test: /\.css$/,
use: [
{
loader: "style-loader",
},
{
loader: "css-loader",
},
],
},
],
},
plugins: [
Expand All @@ -76,6 +89,14 @@ const configuration: webpack.Configuration = {
filename: "expense.html",
chunks: ["expense"],
}),
new CopyPlugin({
patterns: [
{
from: path.join(src, "/i18n"),
to: path.join(build, "/i18n"),
},
],
}),
],
};

Expand Down
Loading

0 comments on commit a1ab1a4

Please sign in to comment.