mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix(console): fix cloud CI failed issue (#6710)
chore: remove useNewSubscriptionData manual updateTenant
This commit is contained in:
parent
83e942a098
commit
b906db730c
2 changed files with 41 additions and 24 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { cond, condString, pick } from '@silverhand/essentials';
|
import { cond, pick } from '@silverhand/essentials';
|
||||||
import { useContext, useEffect, useMemo } from 'react';
|
import { useContext, useEffect, useMemo } from 'react';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
|
|
||||||
|
@ -21,25 +21,24 @@ import { type NewSubscriptionContext } from './types';
|
||||||
const useNewSubscriptionData: () => NewSubscriptionContext & { isLoading: boolean } = () => {
|
const useNewSubscriptionData: () => NewSubscriptionContext & { isLoading: boolean } = () => {
|
||||||
const cloudApi = useCloudApi();
|
const cloudApi = useCloudApi();
|
||||||
|
|
||||||
const { currentTenant, updateTenant } = useContext(TenantsContext);
|
const { currentTenant, currentTenantId, updateTenant } = useContext(TenantsContext);
|
||||||
const { isLoading: isLogtoSkusLoading, data: fetchedLogtoSkus } = useLogtoSkus();
|
const { isLoading: isLogtoSkusLoading, data: fetchedLogtoSkus } = useLogtoSkus();
|
||||||
const tenantId = condString(currentTenant?.id);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: currentSubscription,
|
data: currentSubscription,
|
||||||
isLoading: isSubscriptionLoading,
|
isLoading: isSubscriptionLoading,
|
||||||
mutate: mutateSubscription,
|
mutate: mutateSubscription,
|
||||||
} = useSubscription(tenantId);
|
} = useSubscription(currentTenantId);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: subscriptionUsageData,
|
data: subscriptionUsageData,
|
||||||
isLoading: isSubscriptionUsageDataLoading,
|
isLoading: isSubscriptionUsageDataLoading,
|
||||||
mutate: mutateSubscriptionQuotaAndUsages,
|
mutate: mutateSubscriptionQuotaAndUsages,
|
||||||
} = useSWR<NewSubscriptionUsageResponse, Error>(
|
} = useSWR<NewSubscriptionUsageResponse, Error>(
|
||||||
isCloud && tenantId && `/api/tenants/${tenantId}/subscription-usage`,
|
isCloud && currentTenantId && `/api/tenants/${currentTenantId}/subscription-usage`,
|
||||||
async () =>
|
async () =>
|
||||||
cloudApi.get('/api/tenants/:tenantId/subscription-usage', {
|
cloudApi.get('/api/tenants/:tenantId/subscription-usage', {
|
||||||
params: { tenantId },
|
params: { tenantId: currentTenantId },
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -52,11 +51,11 @@ const useNewSubscriptionData: () => NewSubscriptionContext & { isLoading: boolea
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (subscriptionUsageData?.quota) {
|
if (subscriptionUsageData?.quota) {
|
||||||
updateTenant(tenantId, {
|
updateTenant(currentTenantId, {
|
||||||
quota: pick(subscriptionUsageData.quota, 'mauLimit', 'tokenLimit'),
|
quota: pick(subscriptionUsageData.quota, 'mauLimit', 'tokenLimit'),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [tenantId, subscriptionUsageData?.quota, updateTenant]);
|
}, [currentTenantId, subscriptionUsageData?.quota, updateTenant]);
|
||||||
|
|
||||||
return useMemo(
|
return useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
|
|
|
@ -118,31 +118,49 @@ function TenantsProvider({ children }: Props) {
|
||||||
[currentTenantId, tenants]
|
[currentTenantId, tenants]
|
||||||
);
|
);
|
||||||
|
|
||||||
const memorizedContext = useMemo(
|
const resetTenants = useCallback((tenants: TenantResponse[]) => {
|
||||||
() => ({
|
|
||||||
tenants,
|
|
||||||
resetTenants: (tenants: TenantResponse[]) => {
|
|
||||||
setTenants(tenants);
|
setTenants(tenants);
|
||||||
setIsInitComplete(true);
|
setIsInitComplete(true);
|
||||||
},
|
}, []);
|
||||||
prependTenant: (tenant: TenantResponse) => {
|
|
||||||
|
const prependTenant = useCallback((tenant: TenantResponse) => {
|
||||||
setTenants((tenants) => [tenant, ...tenants]);
|
setTenants((tenants) => [tenant, ...tenants]);
|
||||||
},
|
}, []);
|
||||||
removeTenant: (tenantId: string) => {
|
|
||||||
|
const removeTenant = useCallback((tenantId: string) => {
|
||||||
setTenants((tenants) => tenants.filter((tenant) => tenant.id !== tenantId));
|
setTenants((tenants) => tenants.filter((tenant) => tenant.id !== tenantId));
|
||||||
},
|
}, []);
|
||||||
updateTenant: (tenantId: string, data: Partial<TenantResponse>) => {
|
|
||||||
|
const updateTenant = useCallback((tenantId: string, data: Partial<TenantResponse>) => {
|
||||||
setTenants((tenants) =>
|
setTenants((tenants) =>
|
||||||
tenants.map((tenant) => (tenant.id === tenantId ? { ...tenant, ...data } : tenant))
|
tenants.map((tenant) => (tenant.id === tenantId ? { ...tenant, ...data } : tenant))
|
||||||
);
|
);
|
||||||
},
|
}, []);
|
||||||
|
|
||||||
|
const memorizedContext = useMemo(
|
||||||
|
() => ({
|
||||||
|
tenants,
|
||||||
|
resetTenants,
|
||||||
|
prependTenant,
|
||||||
|
removeTenant,
|
||||||
|
updateTenant,
|
||||||
isInitComplete,
|
isInitComplete,
|
||||||
currentTenantId,
|
currentTenantId,
|
||||||
isDevTenant: currentTenant?.tag === TenantTag.Development,
|
isDevTenant: currentTenant?.tag === TenantTag.Development,
|
||||||
currentTenant,
|
currentTenant,
|
||||||
navigateTenant,
|
navigateTenant,
|
||||||
}),
|
}),
|
||||||
[currentTenant, currentTenantId, isInitComplete, navigateTenant, tenants]
|
[
|
||||||
|
currentTenant,
|
||||||
|
currentTenantId,
|
||||||
|
isInitComplete,
|
||||||
|
navigateTenant,
|
||||||
|
tenants,
|
||||||
|
resetTenants,
|
||||||
|
prependTenant,
|
||||||
|
removeTenant,
|
||||||
|
updateTenant,
|
||||||
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
return <TenantsContext.Provider value={memorizedContext}>{children}</TenantsContext.Provider>;
|
return <TenantsContext.Provider value={memorizedContext}>{children}</TenantsContext.Provider>;
|
||||||
|
|
Loading…
Reference in a new issue