Refactor: Replace hardcoded WEBSITE_URL with dynamic getWebsiteUrl function across multiple components

This commit is contained in:
troyeguo
2025-11-27 15:14:54 +08:00
parent 29f12a10a0
commit 22c53cb1a9
14 changed files with 78 additions and 45 deletions

View File

@@ -2,7 +2,7 @@ import React from "react";
import { Trans } from "react-i18next";
import { AboutDialogProps, AboutDialogState } from "./interface";
import { isElectron } from "react-device-detect";
import { openExternalUrl, WEBSITE_URL } from "../../../utils/common";
import { getWebsiteUrl, openExternalUrl } from "../../../utils/common";
import toast from "react-hot-toast";
import {
exportBooks,
@@ -59,9 +59,9 @@ class AboutDialog extends React.Component<AboutDialogProps, AboutDialogState> {
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
this.handleJump(WEBSITE_URL + "/zh/document");
this.handleJump(getWebsiteUrl() + "/zh/document");
} else {
this.handleJump(WEBSITE_URL + "/en/document");
this.handleJump(getWebsiteUrl() + "/en/document");
}
}}
>
@@ -74,9 +74,9 @@ class AboutDialog extends React.Component<AboutDialogProps, AboutDialogState> {
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
openExternalUrl(WEBSITE_URL + "/zh/support");
openExternalUrl(getWebsiteUrl() + "/zh/support");
} else {
openExternalUrl(WEBSITE_URL + "/en/support");
openExternalUrl(getWebsiteUrl() + "/en/support");
}
}}
>
@@ -85,7 +85,7 @@ class AboutDialog extends React.Component<AboutDialogProps, AboutDialogState> {
<li
className="sort-by-category-list"
onClick={() => {
this.handleJump(WEBSITE_URL);
this.handleJump(getWebsiteUrl());
}}
>
<Trans>Our website</Trans>
@@ -125,7 +125,7 @@ class AboutDialog extends React.Component<AboutDialogProps, AboutDialogState> {
<li
className="sort-by-category-list"
onClick={() => {
this.handleJump(WEBSITE_URL);
this.handleJump(getWebsiteUrl());
}}
style={{ color: "rgb(35, 170, 242)" }}
>

View File

@@ -6,9 +6,9 @@ import Lottie from "react-lottie";
import supportAnimation from "../../../assets/lotties/support.json";
import exitAnimation from "../../../assets/lotties/exit.json";
import {
getWebsiteUrl,
handleContextMenu,
openInBrowser,
WEBSITE_URL,
} from "../../../utils/common";
import {
ConfigService,
@@ -338,7 +338,7 @@ class SupporDialog extends React.Component<
let deviceUuid =
await TokenService.getFingerprint();
openInBrowser(
WEBSITE_URL +
getWebsiteUrl() +
(ConfigService.getReaderConfig(
"lang"
).startsWith("zh")

View File

@@ -5,7 +5,7 @@ import packageInfo from "../../../../package.json";
import { Trans } from "react-i18next";
import Lottie from "react-lottie";
import animationNew from "../../../assets/lotties/new.json";
import { openExternalUrl, WEBSITE_URL } from "../../../utils/common";
import { getWebsiteUrl, openExternalUrl } from "../../../utils/common";
import { isElectron } from "react-device-detect";
import { sleep } from "../../../utils/common";
import {
@@ -207,7 +207,7 @@ class UpdateInfo extends React.Component<UpdateInfoProps, UpdateInfoState> {
lang = "zh";
}
openExternalUrl(
WEBSITE_URL +
getWebsiteUrl() +
"/" +
lang +
"/download" +
@@ -238,7 +238,7 @@ class UpdateInfo extends React.Component<UpdateInfoProps, UpdateInfoState> {
lang = "zh";
}
openExternalUrl(
WEBSITE_URL +
getWebsiteUrl() +
"/" +
lang +
"/download" +

View File

@@ -6,9 +6,9 @@ import Parser from "html-react-parser";
import DOMPurify from "dompurify";
import { Trans } from "react-i18next";
import {
getWebsiteUrl,
handleContextMenu,
openExternalUrl,
WEBSITE_URL,
} from "../../../utils/common";
import toast from "react-hot-toast";
import DatabaseService from "../../../utils/storage/databaseService";
@@ -348,9 +348,9 @@ class PopupAssist extends React.Component<PopupAssistProps, PopupAssistState> {
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
openExternalUrl(WEBSITE_URL + "/zh/plugin");
openExternalUrl(getWebsiteUrl() + "/zh/plugin");
} else {
openExternalUrl(WEBSITE_URL + "/en/plugin");
openExternalUrl(getWebsiteUrl() + "/en/plugin");
}
}}
>

View File

@@ -8,9 +8,9 @@ import axios from "axios";
import DictHistory from "../../../models/DictHistory";
import { Trans } from "react-i18next";
import {
getWebsiteUrl,
handleContextMenu,
openExternalUrl,
WEBSITE_URL,
} from "../../../utils/common";
import toast from "react-hot-toast";
import DatabaseService from "../../../utils/storage/databaseService";
@@ -114,7 +114,7 @@ class PopupDict extends React.Component<PopupDictProps, PopupDictState> {
let moreElement = document.querySelector(".dict-learn-more");
if (moreElement) {
moreElement.addEventListener("click", () => {
openExternalUrl(window.learnMoreUrl || WEBSITE_URL);
openExternalUrl(window.learnMoreUrl || getWebsiteUrl());
});
}
}
@@ -273,9 +273,9 @@ class PopupDict extends React.Component<PopupDictProps, PopupDictState> {
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
openExternalUrl(WEBSITE_URL + "/zh/plugin");
openExternalUrl(getWebsiteUrl() + "/zh/plugin");
} else {
openExternalUrl(WEBSITE_URL + "/en/plugin");
openExternalUrl(getWebsiteUrl() + "/en/plugin");
}
}}
>

View File

@@ -7,9 +7,9 @@ import { Trans } from "react-i18next";
import toast from "react-hot-toast";
import {
getDefaultTransTarget,
getWebsiteUrl,
handleContextMenu,
openExternalUrl,
WEBSITE_URL,
} from "../../../utils/common";
import DatabaseService from "../../../utils/storage/databaseService";
import { checkPlugin } from "../../../utils/common";
@@ -214,9 +214,9 @@ class PopupTrans extends React.Component<PopupTransProps, PopupTransState> {
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
openExternalUrl(WEBSITE_URL + "/zh/plugin");
openExternalUrl(getWebsiteUrl() + "/zh/plugin");
} else {
openExternalUrl(WEBSITE_URL + "/en/plugin");
openExternalUrl(getWebsiteUrl() + "/en/plugin");
}
}}
>

View File

@@ -6,10 +6,10 @@ import { ConfigService } from "../../assets/lib/kookit-extra-browser.min";
import {
checkPlugin,
getAllVoices,
getWebsiteUrl,
handleContextMenu,
sleep,
splitSentences,
WEBSITE_URL,
} from "../../utils/common";
import { isElectron } from "react-device-detect";
import toast from "react-hot-toast";
@@ -536,9 +536,9 @@ class TextToSpeech extends React.Component<
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
openExternalUrl(WEBSITE_URL + "/zh/plugin");
openExternalUrl(getWebsiteUrl() + "/zh/plugin");
} else {
openExternalUrl(WEBSITE_URL + "/en/plugin");
openExternalUrl(getWebsiteUrl() + "/en/plugin");
}
}}
>

View File

@@ -32,9 +32,9 @@ import {
generateSyncRecord,
getChatLocale,
getStorageLocation,
getWebsiteUrl,
removeChatBox,
showTaskProgress,
WEBSITE_URL,
} from "../../utils/common";
import { driveList } from "../../constants/driveList";
import SupportDialog from "../../components/dialogs/supportDialog";
@@ -498,7 +498,7 @@ class Header extends React.Component<HeaderProps, HeaderState> {
onClick={() => {
window.require("electron").ipcRenderer.invoke("new-chat", {
url:
WEBSITE_URL +
getWebsiteUrl() +
(ConfigService.getReaderConfig("lang").startsWith("zh")
? "/zh/faq"
: "/en/faq") +

View File

@@ -7,12 +7,11 @@ import { themeList } from "../../../constants/themeList";
import toast from "react-hot-toast";
import {
formatTimestamp,
getWebsiteUrl,
handleContextMenu,
openInBrowser,
reloadManager,
WEBSITE_URL,
} from "../../../utils/common";
import { getStorageLocation } from "../../../utils/common";
import {
CommonTool,
ConfigService,
@@ -827,7 +826,7 @@ class AccountSetting extends React.Component<
onClick={async () => {
if (!this.props.isAuthed) {
openInBrowser(
WEBSITE_URL +
getWebsiteUrl() +
(ConfigService.getReaderConfig("lang").startsWith("zh")
? "/zh"
: "/en") +
@@ -840,7 +839,7 @@ class AccountSetting extends React.Component<
let tempToken = response.data.access_token;
let deviceUuid = await TokenService.getFingerprint();
openInBrowser(
WEBSITE_URL +
getWebsiteUrl() +
(ConfigService.getReaderConfig("lang").startsWith("zh")
? "/zh"
: "/en") +

View File

@@ -6,9 +6,9 @@ import { themeList } from "../../../constants/themeList";
import toast from "react-hot-toast";
import {
checkPlugin,
getWebsiteUrl,
handleContextMenu,
openExternalUrl,
WEBSITE_URL,
} from "../../../utils/common";
import { getStorageLocation } from "../../../utils/common";
import DatabaseService from "../../../utils/storage/databaseService";
@@ -152,9 +152,9 @@ class SettingDialog extends React.Component<
ConfigService.getReaderConfig("lang") &&
ConfigService.getReaderConfig("lang").startsWith("zh")
) {
openExternalUrl(WEBSITE_URL + "/zh/plugin");
openExternalUrl(getWebsiteUrl() + "/zh/plugin");
} else {
openExternalUrl(WEBSITE_URL + "/en/plugin");
openExternalUrl(getWebsiteUrl() + "/en/plugin");
}
}}
>

View File

@@ -10,13 +10,13 @@ import { themeList } from "../../../constants/themeList";
import toast from "react-hot-toast";
import {
generateSyncRecord,
getWebsiteUrl,
handleContextMenu,
openExternalUrl,
openInBrowser,
showTaskProgress,
testConnection,
testCORS,
WEBSITE_URL,
} from "../../../utils/common";
import { getStorageLocation } from "../../../utils/common";
import { driveInputConfig, driveList } from "../../../constants/driveList";
@@ -545,7 +545,7 @@ class SyncSetting extends React.Component<SettingInfoProps, SettingInfoState> {
className="voice-add-cancel"
style={{ borderWidth: 0, lineHeight: "30px" }}
onClick={() => {
openExternalUrl(WEBSITE_URL + "/zh/add-source");
openExternalUrl(getWebsiteUrl() + "/zh/add-source");
}}
>
{this.props.t("How to fill out")}

View File

@@ -4,7 +4,7 @@ import { sideMenu } from "../../constants/sideMenu";
import { SidebarProps, SidebarState } from "./interface";
import { withRouter } from "react-router-dom";
import { ConfigService } from "../../assets/lib/kookit-extra-browser.min";
import { openInBrowser, WEBSITE_URL } from "../../utils/common";
import { getWebsiteUrl, openInBrowser } from "../../utils/common";
import { Trans } from "react-i18next";
import toast from "react-hot-toast";
class Sidebar extends React.Component<SidebarProps, SidebarState> {
@@ -263,7 +263,7 @@ class Sidebar extends React.Component<SidebarProps, SidebarState> {
}
alt=""
onClick={() => {
this.handleJump(WEBSITE_URL);
this.handleJump(getWebsiteUrl());
}}
style={this.state.isCollapsed ? { display: "none" } : {}}
className="logo"

View File

@@ -580,6 +580,29 @@ export const getDefaultTransTarget = (langList) => {
return langMap[langTarget || "English"];
};
export const WEBSITE_URL = "https://koodoreader.com";
export const CN_WEBSITE_URL = "https://koodoreader.cn";
export const getServerRegion = () => {
let isUseCN = false;
if (ConfigService.getItem("serverRegion")) {
isUseCN = ConfigService.getItem("serverRegion") === "china";
} else {
if (navigator.language && navigator.language === "zh-CN") {
isUseCN = true;
}
}
return isUseCN ? "china" : "global";
};
export const getWebsiteUrl = () => {
let isUseCN = false;
if (ConfigService.getItem("serverRegion")) {
isUseCN = ConfigService.getItem("serverRegion") === "china";
} else {
if (navigator.language && navigator.language === "zh-CN") {
isUseCN = true;
}
}
return isUseCN ? CN_WEBSITE_URL : WEBSITE_URL;
};
export const formatTimestamp = (timestamp) => {
if (!timestamp) return "";

View File

@@ -1,19 +1,30 @@
import axios from "axios";
import toast from "react-hot-toast";
import i18n from "../../i18n";
import { TokenService } from "../../assets/lib/kookit-extra-browser.min";
import {
ConfigService,
TokenService,
} from "../../assets/lib/kookit-extra-browser.min";
import { reloadManager } from "../common";
const PUBLIC_URL = "https://api.960960.xyz";
const CN_PUBLIC_URL = "https://api.koodoreader.cn";
export const getPublicUrl = () => {
let isUseCN = false;
if (ConfigService.getItem("serverRegion")) {
isUseCN = ConfigService.getItem("serverRegion") === "china";
} else {
if (navigator.language && navigator.language === "zh-CN") {
isUseCN = true;
}
}
return isUseCN ? CN_PUBLIC_URL : PUBLIC_URL;
};
export const checkDeveloperUpdate = async () => {
let res = await axios.get(
PUBLIC_URL + `/api/update_dev?name=${navigator.language}`
getPublicUrl() + `/api/update_dev?name=${navigator.language}`
);
return res.data.log;
};
export const getUploadUrl = async () => {
let res = await axios.get(PUBLIC_URL + "/api/get_temp_upload_url");
return res.data;
};
export const uploadFile = async (url: string, file: any) => {
return new Promise<boolean>((resolve) => {
axios
@@ -29,7 +40,7 @@ export const uploadFile = async (url: string, file: any) => {
};
export const checkStableUpdate = async () => {
let res = await axios.get(
PUBLIC_URL + `/api/update?name=${navigator.language}`
getPublicUrl() + `/api/update?name=${navigator.language}`
);
return res.data.log;
};