Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(console): refactor TenantxContext util methods #6710

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<NewSubscriptionUsageResponse, Error>(
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 },
})
);

Expand All @@ -52,11 +51,14 @@ const useNewSubscriptionData: () => NewSubscriptionContext & { isLoading: boolea

useEffect(() => {
if (subscriptionUsageData?.quota) {
updateTenant(tenantId, {
updateTenant(currentTenantId, {
quota: pick(subscriptionUsageData.quota, 'mauLimit', 'tokenLimit'),
});
}
}, [tenantId, subscriptionUsageData?.quota, updateTenant]);
// Since `updateTenant` updates `tenants` in TenantsContext and triggers re-renders (`updateTenant` is re-rendered as well),
// we can not add `updateTenant` to the dependency list, otherwise it will cause a infinite loop.
darcyYe marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentTenantId, subscriptionUsageData?.quota]);

return useMemo(
() => ({
Expand Down
Loading