diff --git a/packages/console/src/contexts/SubscriptionDataProvider/use-new-subscription-data.ts b/packages/console/src/contexts/SubscriptionDataProvider/use-new-subscription-data.ts index c355a1434..ab71e62c2 100644 --- a/packages/console/src/contexts/SubscriptionDataProvider/use-new-subscription-data.ts +++ b/packages/console/src/contexts/SubscriptionDataProvider/use-new-subscription-data.ts @@ -1,4 +1,4 @@ -import { cond, condString, pick } from '@silverhand/essentials'; +import { cond, pick } from '@silverhand/essentials'; import { useContext, useEffect, useMemo } from 'react'; import useSWR from 'swr'; @@ -21,25 +21,24 @@ import { type NewSubscriptionContext } from './types'; const useNewSubscriptionData: () => NewSubscriptionContext & { isLoading: boolean } = () => { const cloudApi = useCloudApi(); - const { currentTenant, updateTenant } = useContext(TenantsContext); + const { currentTenant, currentTenantId, updateTenant } = useContext(TenantsContext); const { isLoading: isLogtoSkusLoading, data: fetchedLogtoSkus } = useLogtoSkus(); - const tenantId = condString(currentTenant?.id); const { data: currentSubscription, isLoading: isSubscriptionLoading, mutate: mutateSubscription, - } = useSubscription(tenantId); + } = useSubscription(currentTenantId); const { data: subscriptionUsageData, isLoading: isSubscriptionUsageDataLoading, mutate: mutateSubscriptionQuotaAndUsages, } = useSWR( - isCloud && tenantId && `/api/tenants/${tenantId}/subscription-usage`, + isCloud && currentTenantId && `/api/tenants/${currentTenantId}/subscription-usage`, async () => cloudApi.get('/api/tenants/:tenantId/subscription-usage', { - params: { tenantId }, + params: { tenantId: currentTenantId }, }) ); @@ -52,11 +51,11 @@ const useNewSubscriptionData: () => NewSubscriptionContext & { isLoading: boolea useEffect(() => { if (subscriptionUsageData?.quota) { - updateTenant(tenantId, { + updateTenant(currentTenantId, { quota: pick(subscriptionUsageData.quota, 'mauLimit', 'tokenLimit'), }); } - }, [tenantId, subscriptionUsageData?.quota, updateTenant]); + }, [currentTenantId, subscriptionUsageData?.quota, updateTenant]); return useMemo( () => ({ diff --git a/packages/console/src/contexts/TenantsProvider.tsx b/packages/console/src/contexts/TenantsProvider.tsx index 419045a3f..04fe0d02e 100644 --- a/packages/console/src/contexts/TenantsProvider.tsx +++ b/packages/console/src/contexts/TenantsProvider.tsx @@ -118,31 +118,49 @@ function TenantsProvider({ children }: Props) { [currentTenantId, tenants] ); + const resetTenants = useCallback((tenants: TenantResponse[]) => { + setTenants(tenants); + setIsInitComplete(true); + }, []); + + const prependTenant = useCallback((tenant: TenantResponse) => { + setTenants((tenants) => [tenant, ...tenants]); + }, []); + + const removeTenant = useCallback((tenantId: string) => { + setTenants((tenants) => tenants.filter((tenant) => tenant.id !== tenantId)); + }, []); + + const updateTenant = useCallback((tenantId: string, data: Partial) => { + setTenants((tenants) => + tenants.map((tenant) => (tenant.id === tenantId ? { ...tenant, ...data } : tenant)) + ); + }, []); + const memorizedContext = useMemo( () => ({ tenants, - resetTenants: (tenants: TenantResponse[]) => { - setTenants(tenants); - setIsInitComplete(true); - }, - prependTenant: (tenant: TenantResponse) => { - setTenants((tenants) => [tenant, ...tenants]); - }, - removeTenant: (tenantId: string) => { - setTenants((tenants) => tenants.filter((tenant) => tenant.id !== tenantId)); - }, - updateTenant: (tenantId: string, data: Partial) => { - setTenants((tenants) => - tenants.map((tenant) => (tenant.id === tenantId ? { ...tenant, ...data } : tenant)) - ); - }, + resetTenants, + prependTenant, + removeTenant, + updateTenant, isInitComplete, currentTenantId, isDevTenant: currentTenant?.tag === TenantTag.Development, currentTenant, navigateTenant, }), - [currentTenant, currentTenantId, isInitComplete, navigateTenant, tenants] + [ + currentTenant, + currentTenantId, + isInitComplete, + navigateTenant, + tenants, + resetTenants, + prependTenant, + removeTenant, + updateTenant, + ] ); return {children};