Skip to content

Commit

Permalink
feat: common
Browse files Browse the repository at this point in the history
  • Loading branch information
lgz5689 committed Jun 25, 2024
1 parent 539518d commit bf9bfa4
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 20 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
}
},
"dependencies": {
"axios": "^1.7.2",
"i18next": "^23.11.5",
"pretty-bytes": "^6.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-router-dom": "^6.23.1"
},
"devDependencies": {
"@commitlint/cli": "^18.4.4",
Expand Down
64 changes: 58 additions & 6 deletions pnpm-lock.yaml

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

7 changes: 7 additions & 0 deletions src/layout/ContentLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Outlet } from "react-router-dom";

const ContentLayout = () => {
return <Outlet />;
};

export default ContentLayout;
7 changes: 7 additions & 0 deletions src/layout/MainLayout.tsx/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Outlet } from "react-router-dom";

const MainLayout = () => {
return <Outlet />;
};

export default MainLayout;
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./main.css";
import "./styles/main.css";

import React from "react";
import ReactDOM from "react-dom/client";
Expand Down
7 changes: 7 additions & 0 deletions src/pages/login/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const Login = () => {
return <LoginContent />;
};

export const LoginContent = () => {
return <div>LoginContent</div>;
};
5 changes: 5 additions & 0 deletions src/pages/third/login/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LoginContent } from "@/pages/login";

export const Login = () => {
return <LoginContent />;
};
32 changes: 32 additions & 0 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createHashRouter } from "react-router-dom";

import ContentLayout from "../layout/ContentLayout/index.tsx";
import MainLayout from "../layout/MainLayout.tsx";
import thirdRoutes from "./thirdRoutes.ts";

const router = createHashRouter([
{
path: "/",
element: <MainLayout />,
children: [
{
path: "/",
element: <ContentLayout />,
children: [],
},
{
path: "login",
async lazy() {
const { Login } = await import("@/pages/login");
return { Component: Login };
},
},
],
},
{
path: "third",
children: thirdRoutes,
},
]);

export default router;
11 changes: 11 additions & 0 deletions src/routes/thirdRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const thirdRoutes = [
{
path: "login",
async lazy() {
const { Login } = await import("@/pages/third/login");
return { Component: Login };
},
},
];

export default thirdRoutes;
File renamed without changes.
37 changes: 37 additions & 0 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios from "axios";

const createAxiosInstance = (baseURL: string) => {
const serves = axios.create({
baseURL,
timeout: 3000,
});

serves.interceptors.request.use(
(config) => {
return config;
},
(err) => Promise.reject(err),
);

serves.interceptors.response.use(
(res) => {
if (res.data.errCode !== 0) {
return Promise.reject(res.data);
}
return res.data;
},
(err) => {
if (err.message.includes("timeout")) {
console.error("error", err);
}
if (err.message.includes("Network Error")) {
console.error("error", err);
}
return Promise.reject(err);
},
);

return serves;
};

export default createAxiosInstance;
13 changes: 13 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const TOKEN = "USER_TOKEN";
const LOCALE = "USER_LOCALE";

export type LocaleString = "zh-CN" | "en-US";

export const getIMToken = () => localStorage.getItem(TOKEN);
export const getLocale = (): LocaleString =>
(localStorage.getItem(LOCALE) as LocaleString) ||
window.navigator.language ||
"en-US";

export const setTMToken = (token: string) => localStorage.setItem(TOKEN, token);
export const setLocale = (locale: string) => localStorage.setItem(LOCALE, locale);
30 changes: 21 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,36 @@
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"lib": [
"ES2020",
"DOM",
"DOM.Iterable"
],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"baseUrl": "./",
"paths": {
"@/*": [
"src/*"
]
},
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
"include": [
"src"
],
"references": [
{
"path": "./tsconfig.node.json"
}
]
}
6 changes: 4 additions & 2 deletions tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
"include": [
"vite.config.ts"
]
}
8 changes: 7 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { rmSync } from "node:fs";
import path from "node:path";
import { defineConfig } from "vite";

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import electron from "vite-electron-plugin";
import { loadViteEnv } from "vite-electron-plugin/plugin";

rmSync(path.join(__dirname, "dist-electron"), { recursive: true, force: true });

export default defineConfig({
resolve: {
alias: {
"@": path.join(__dirname, "src"),
},
},
plugins: [
react(),
electron({
Expand Down

0 comments on commit bf9bfa4

Please sign in to comment.