فهرست منبع

:construction: add pdfjs setting & localstorage token support

Jeremy Zheng 3 سال پیش
والد
کامیت
e8f16b4338
3فایلهای تغییر یافته به همراه36 افزوده شده و 12 حذف شده
  1. 1 0
      dashboard/.env.orig
  2. 21 11
      dashboard/src/App.tsx
  3. 14 1
      dashboard/src/reducers/current-user.ts

+ 1 - 0
dashboard/.env.orig

@@ -8,3 +8,4 @@ PORT=20080
 REACT_APP_DEFAULT_LOCALE=zh-Hans
 REACT_APP_LANGUAGES=en-US,zh-Hans,zh-Hant
 REACT_APP_API_HOST=https://jeremy.spring.wikipali.org
+REACT_APP_ENABLE_LOCAL_TOKEN=true

+ 21 - 11
dashboard/src/App.tsx

@@ -1,31 +1,41 @@
 import { BrowserRouter } from "react-router-dom";
 import { ConfigProvider } from "antd";
 import { IntlProvider } from "react-intl";
+import { Provider } from "react-redux";
+import { pdfjs } from "react-pdf";
 
 import Router from "./Router";
+import store from "./store";
 import locales, {
   get as getLocale,
   DEFAULT as DEFAULT_LOCALE,
 } from "./locales";
+import { API_HOST } from "./request";
+import onLoad from "./load";
 
 import "./App.css";
 
+pdfjs.GlobalWorkerOptions.workerSrc = `${API_HOST}/assets/pdf.worker.min.js`;
+
+onLoad();
 const lang = getLocale();
 const i18n = locales(lang);
 
 function Widget() {
   return (
-    <IntlProvider
-      messages={i18n.messages}
-      locale={lang}
-      defaultLocale={DEFAULT_LOCALE}
-    >
-      <ConfigProvider locale={i18n.antd}>
-        <BrowserRouter basename={process.env.PUBLIC_URL}>
-          <Router />
-        </BrowserRouter>
-      </ConfigProvider>
-    </IntlProvider>
+    <Provider store={store}>
+      <IntlProvider
+        messages={i18n.messages}
+        locale={lang}
+        defaultLocale={DEFAULT_LOCALE}
+      >
+        <ConfigProvider locale={i18n.antd}>
+          <BrowserRouter basename={process.env.PUBLIC_URL}>
+            <Router />
+          </BrowserRouter>
+        </ConfigProvider>
+      </IntlProvider>
+    </Provider>
   );
 }
 

+ 14 - 1
dashboard/src/reducers/current-user.ts

@@ -11,16 +11,29 @@ export const TO_PROFILE = "/dashboard/users/logs";
 const KEY = "token";
 export const DURATION = 60 * 60 * 24;
 
+const IS_LOCAL_ENABLE = process.env.REACT_APP_ENABLE_LOCAL_TOKEN === "true";
+
 export const get = (): string | null => {
-  return sessionStorage.getItem(KEY);
+  const token = sessionStorage.getItem(KEY);
+  if (token) {
+    return token;
+  }
+  if (IS_LOCAL_ENABLE) {
+    return localStorage.getItem(KEY);
+  }
+  return null;
 };
 
 const set = (token: string) => {
   sessionStorage.setItem(KEY, token);
+  if (IS_LOCAL_ENABLE) {
+    localStorage.setItem(KEY, token);
+  }
 };
 
 const remove = () => {
   sessionStorage.removeItem(KEY);
+  localStorage.removeItem(KEY);
 };
 
 export interface IUser {