mirror of
https://github.com/logto-io/logto.git
synced 2025-01-27 21:39:16 -05:00
Merge pull request #4224 from logto-io/gao-fix-tenant-switching
fix(console): use SWR for user endpoint
This commit is contained in:
commit
5fd22f394f
2 changed files with 23 additions and 22 deletions
|
@ -56,10 +56,17 @@ export default function TenantAccess() {
|
|||
/**
|
||||
* The official cache clean method, see {@link https://github.com/vercel/swr/issues/1887#issuecomment-1171269211 | this comment}.
|
||||
*
|
||||
* We need to exclude the `me` key because it's not tenant-aware. If don't, we
|
||||
* need to manually revalidate the `me` key to make console work again.
|
||||
* Exceptions:
|
||||
* - Exclude the `me` key because it's not tenant-aware. If don't, we need to manually
|
||||
* revalidate the `me` key to make console work again.
|
||||
* - Exclude keys that include `/.well-known/` because they are usually static and
|
||||
* should not be revalidated.
|
||||
*/
|
||||
void mutate((key) => key !== 'me', undefined, { rollbackOnError: false, throwOnError: false });
|
||||
void mutate(
|
||||
(key) => typeof key !== 'string' || (key !== 'me' && !key.includes('/.well-known/')),
|
||||
undefined,
|
||||
{ rollbackOnError: false, throwOnError: false }
|
||||
);
|
||||
}, [mutate, currentTenantId]);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import ky from 'ky';
|
||||
import type { ReactNode } from 'react';
|
||||
import { useContext, useMemo, useEffect, createContext, useState } from 'react';
|
||||
import { useContext, useMemo, createContext } from 'react';
|
||||
import useSWRImmutable from 'swr/immutable';
|
||||
|
||||
import { adminTenantEndpoint } from '@/consts';
|
||||
|
||||
|
@ -23,9 +24,18 @@ export const AppDataContext = createContext<AppData>({});
|
|||
|
||||
/** The context provider for the global app data. */
|
||||
function AppDataProvider({ children }: Props) {
|
||||
const [userEndpoint, setUserEndpoint] = useState<URL>();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const { currentTenantId } = useContext(TenantsContext);
|
||||
|
||||
const { data: userEndpoint } = useSWRImmutable(
|
||||
`api/.well-known/endpoints/${currentTenantId}`,
|
||||
async (pathname) => {
|
||||
const { user } = await ky
|
||||
.get(new URL(pathname, adminTenantEndpoint))
|
||||
.json<{ user: string }>();
|
||||
return new URL(user);
|
||||
}
|
||||
);
|
||||
|
||||
const memorizedContext = useMemo(
|
||||
() =>
|
||||
({
|
||||
|
@ -34,22 +44,6 @@ function AppDataProvider({ children }: Props) {
|
|||
[userEndpoint]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const getEndpoint = async () => {
|
||||
setIsLoading(true);
|
||||
const { user } = await ky
|
||||
.get(new URL(`api/.well-known/endpoints/${currentTenantId}`, adminTenantEndpoint))
|
||||
.json<{ user: string }>();
|
||||
setUserEndpoint(new URL(user));
|
||||
};
|
||||
|
||||
if (!currentTenantId || isLoading || userEndpoint) {
|
||||
return;
|
||||
}
|
||||
|
||||
void getEndpoint();
|
||||
}, [currentTenantId, isLoading, userEndpoint]);
|
||||
|
||||
return <AppDataContext.Provider value={memorizedContext}>{children}</AppDataContext.Provider>;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue