From 54531e38bc331662cfcb1bdcbb2cdb0e647439b3 Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:31:15 +0200 Subject: [PATCH] fix(admin-ui): Patch admin path issue (#5154) --- .changeset/tasty-experts-kick.md | 6 +++++ .../src/components/templates/user-table.tsx | 7 +++--- packages/admin-ui/ui/src/i18n/index.ts | 7 +++--- .../admin-ui/ui/src/utils/get-admin-path.ts | 22 +++++++++++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 .changeset/tasty-experts-kick.md create mode 100644 packages/admin-ui/ui/src/utils/get-admin-path.ts diff --git a/.changeset/tasty-experts-kick.md b/.changeset/tasty-experts-kick.md new file mode 100644 index 0000000000..645648681b --- /dev/null +++ b/.changeset/tasty-experts-kick.md @@ -0,0 +1,6 @@ +--- +"@medusajs/admin-ui": patch +"@medusajs/admin": patch +--- + +fix(admin-ui): Patch admin path issue diff --git a/packages/admin-ui/ui/src/components/templates/user-table.tsx b/packages/admin-ui/ui/src/components/templates/user-table.tsx index b009b88a50..9902aeb824 100644 --- a/packages/admin-ui/ui/src/components/templates/user-table.tsx +++ b/packages/admin-ui/ui/src/components/templates/user-table.tsx @@ -14,6 +14,7 @@ import Table from "../molecules/table" import DeletePrompt from "../organisms/delete-prompt" import EditUser from "../organisms/edit-user-modal" import { useTranslation } from "react-i18next" +import { getAdminPath, getFullAdminPath } from "../../utils/get-admin-path" type UserListElement = { entity: any @@ -110,9 +111,9 @@ const UserTable: React.FC = ({ return store.invite_link_template } - return `${window.location.origin}${ - process.env.ADMIN_PATH ? `${process.env.ADMIN_PATH}/` : "/" - }invite?token={invite_token}` + const adminPath = getFullAdminPath() + + return `${adminPath}invite?token={invite_token}` }, [store]) const getInviteTableRow = (invite: Invite, index: number) => { diff --git a/packages/admin-ui/ui/src/i18n/index.ts b/packages/admin-ui/ui/src/i18n/index.ts index c0635b076a..3c86d03e39 100644 --- a/packages/admin-ui/ui/src/i18n/index.ts +++ b/packages/admin-ui/ui/src/i18n/index.ts @@ -2,6 +2,7 @@ import i18n from "i18next" import LanguageDetector from "i18next-browser-languagedetector" import Backend from "i18next-http-backend" import { initReactI18next } from "react-i18next" +import { getFullAdminPath } from "../utils/get-admin-path" export const supportedLanguages = [ { @@ -22,10 +23,8 @@ export const supportedLanguages = [ }, ] -const backendUrl = window.location.origin -const adminPath = process.env.ADMIN_PATH ? `${process.env.ADMIN_PATH}/` : "/" - -const pathToLoadFrom = `${backendUrl}${adminPath}public/locales/{{lng}}/{{ns}}.json` +const adminPath = getFullAdminPath() +const pathToLoadFrom = `${adminPath}public/locales/{{lng}}/{{ns}}.json` void i18n .use(Backend) diff --git a/packages/admin-ui/ui/src/utils/get-admin-path.ts b/packages/admin-ui/ui/src/utils/get-admin-path.ts new file mode 100644 index 0000000000..fed5f2a8be --- /dev/null +++ b/packages/admin-ui/ui/src/utils/get-admin-path.ts @@ -0,0 +1,22 @@ +/** + * returns the full admin path using the window object + * @returns {string} - admin path + */ +export function getFullAdminPath() { + if (window) { + const origin = window.location.origin // returns base url with trailing slash + + const adminPath = process.env.ADMIN_PATH ? `${process.env.ADMIN_PATH}/` : "" + + let fullPath = `${origin}${adminPath}` + + // safeguard against double slashes, which happens in case path is set to `/` + if (fullPath.endsWith("//")) { + fullPath = adminPath.slice(0, -1) + } + + return fullPath + } + + return "" +}