From 303e086df3037542f59fc823ea1161885bc087d2 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 1 Mar 2023 23:46:30 +0800 Subject: [PATCH] refactor: optimize tenant requests --- packages/console/src/App.tsx | 9 ++- .../console/src/cloud/pages/Main/Tenants.tsx | 17 +++-- .../console/src/cloud/pages/Main/index.tsx | 8 +-- .../src/components/AppLoading/Offline.tsx | 3 + .../src/contexts/AppEndpointsProvider.tsx | 15 ++-- .../console/src/contexts/TenantsProvider.tsx | 18 ++++- packages/console/src/hooks/use-api.ts | 10 ++- packages/core/src/middleware/koa-body-etag.ts | 29 ++++++++ packages/core/src/oidc/init.ts | 2 + packages/core/src/routes/well-known.ts | 69 ++++++++----------- 10 files changed, 116 insertions(+), 64 deletions(-) create mode 100644 packages/core/src/middleware/koa-body-etag.ts diff --git a/packages/console/src/App.tsx b/packages/console/src/App.tsx index 9d094b01e..b3261f093 100644 --- a/packages/console/src/App.tsx +++ b/packages/console/src/App.tsx @@ -10,11 +10,12 @@ import './scss/overlayscrollbars.scss'; // eslint-disable-next-line import/no-unassigned-import import '@fontsource/roboto-mono'; + import CloudApp from '@/cloud/App'; import { cloudApi, getManagementApi, meApi } from '@/consts/resources'; import initI18n from '@/i18n/init'; -import { adminTenantEndpoint, getUserTenantId } from './consts'; +import { adminTenantEndpoint } from './consts'; import { isCloud } from './consts/cloud'; import AppConfirmModalProvider from './contexts/AppConfirmModalProvider'; import AppEndpointsProvider from './contexts/AppEndpointsProvider'; @@ -24,10 +25,12 @@ import Main from './pages/Main'; void initI18n(); const Content = () => { - const { tenants, isSettle } = useContext(TenantsContext); - const currentTenantId = getUserTenantId(); + const { tenants, isSettle, currentTenantId } = useContext(TenantsContext); const resources = deduplicate([ + // Explicitly add `currentTenantId` and deduplicate since the user may directly + // access a URL with Tenant ID, adding the ID from the URL here can possibly remove one + // additional redirect. ...(currentTenantId && [getManagementApi(currentTenantId).indicator]), ...(tenants ?? []).map(({ id }) => getManagementApi(id).indicator), ...(isCloud ? [cloudApi.indicator] : []), diff --git a/packages/console/src/cloud/pages/Main/Tenants.tsx b/packages/console/src/cloud/pages/Main/Tenants.tsx index 67eaa69f7..421b7dac4 100644 --- a/packages/console/src/cloud/pages/Main/Tenants.tsx +++ b/packages/console/src/cloud/pages/Main/Tenants.tsx @@ -1,10 +1,11 @@ import type { TenantInfo } from '@logto/schemas'; -import { useCallback, useEffect } from 'react'; +import { useCallback, useContext, useEffect } from 'react'; import { useCloudApi } from '@/cloud/hooks/use-cloud-api'; import { AppLoadingOffline } from '@/components/AppLoading/Offline'; import Button from '@/components/Button'; import DangerousRaw from '@/components/DangerousRaw'; +import { TenantsContext } from '@/contexts/TenantsProvider'; import * as styles from './index.module.scss'; @@ -15,6 +16,7 @@ type Props = { const Tenants = ({ data, onAdd }: Props) => { const api = useCloudApi(); + const { navigate } = useContext(TenantsContext); const createTenant = useCallback(async () => { onAdd(await api.post('api/tenants').json()); @@ -26,18 +28,25 @@ const Tenants = ({ data, onAdd }: Props) => { } if (data[0]) { - window.location.assign('/' + data[0].id); + navigate(data[0].id); } else { void createTenant(); } - }, [createTenant, data]); + }, [createTenant, data, navigate]); if (data.length > 1) { return (

Choose a tenant

{data.map(({ id }) => ( - + { + event.preventDefault(); + navigate(id); + }} + >