Skip to content

Commit

Permalink
fix: update tenant selector dropdown data source (#6438)
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe authored Aug 13, 2024
1 parent fe054d5 commit da58dec
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@fontsource/roboto-mono": "^5.0.0",
"@jest/types": "^29.5.0",
"@logto/cloud": "0.2.5-3452c56",
"@logto/cloud": "0.2.5-a6cff75",
"@logto/connector-kit": "workspace:^4.0.0",
"@logto/core-kit": "workspace:^2.5.0",
"@logto/elements": "workspace:^0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { conditional } from '@silverhand/essentials';

import {
type TenantResponse,
type NewSubscriptionUsage,
type NewSubscriptionQuota,
} from '@/cloud/types/router';
import { type TenantResponse } from '@/cloud/types/router';
import { isDevFeaturesEnabled } from '@/consts/env';
import DynamicT from '@/ds-components/DynamicT';
import Tag from '@/ds-components/Tag';
import { type SubscriptionPlan } from '@/types/subscriptions';

type Props = {
readonly tenantData: TenantResponse;
/** @deprecated */
readonly tenantSubscriptionPlan: SubscriptionPlan;
readonly tenantStatus: {
usage: NewSubscriptionUsage;
quota: NewSubscriptionQuota;
};
readonly className?: string;
};

function TenantStatusTag({ tenantData, tenantSubscriptionPlan, tenantStatus, className }: Props) {
const { usage, openInvoices, isSuspended } = tenantData;
function TenantStatusTag({ tenantData, tenantSubscriptionPlan, className }: Props) {
const { usage, quota, openInvoices, isSuspended } = tenantData;

/**
* Tenant status priority:
Expand All @@ -46,21 +37,11 @@ function TenantStatusTag({ tenantData, tenantSubscriptionPlan, tenantStatus, cla
);
}

const { usage: tenantUsage, quota: tenantQuota } = tenantStatus;

const { activeUsers } = usage;

const {
quota: { mauLimit },
} = tenantSubscriptionPlan;
const mauLimit = isDevFeaturesEnabled ? quota.mauLimit : tenantSubscriptionPlan.quota.mauLimit;

const isMauExceeded =
conditional(
isDevFeaturesEnabled &&
tenantQuota.mauLimit !== null &&
tenantUsage.mauLimit >= tenantQuota.mauLimit
) ??
(mauLimit !== null && activeUsers >= mauLimit);
const isMauExceeded = mauLimit !== null && activeUsers >= mauLimit;

if (isMauExceeded) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ function TenantDropdownItem({ tenantData, isSelected, onClick }: Props) {
subscription: { planId },
} = tenantData;

const {
currentPlan,
subscriptionPlans,
currentSubscriptionUsage: usage,
currentSubscriptionQuota: quota,
} = useContext(SubscriptionDataContext);
const { currentPlan, subscriptionPlans } = useContext(SubscriptionDataContext);
const tenantSubscriptionPlan = useMemo(
() => subscriptionPlans.find((plan) => plan.id === planId),
[subscriptionPlans, planId]
Expand All @@ -51,7 +46,6 @@ function TenantDropdownItem({ tenantData, isSelected, onClick }: Props) {
<TenantEnvTag tag={tag} />
<TenantStatusTag
tenantData={tenantData}
tenantStatus={{ usage, quota }}
tenantSubscriptionPlan={tenantSubscriptionPlan}
className={styles.statusTag}
/>
Expand Down
5 changes: 3 additions & 2 deletions packages/console/src/consts/tenants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ export const defaultTenantResponse: TenantResponse = {
},
usage: {
activeUsers: 0,
cost: 0,
tokenUsage: 0,
},
quota: {
mauLimit: null,
},
openInvoices: [],
isSuspended: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@logto/cloud": "0.2.5-3452c56",
"@logto/cloud": "0.2.5-a6cff75",
"@silverhand/eslint-config": "6.0.1",
"@silverhand/ts-config": "6.0.0",
"@types/adm-zip": "^0.5.5",
Expand Down
17 changes: 6 additions & 11 deletions packages/core/src/libraries/quota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import assertThat from '#src/utils/assert-that.js';
import {
getTenantSubscriptionPlan,
getTenantSubscriptionData,
getTenantSubscriptionScopeUsage,
reportSubscriptionUpdates,
isReportSubscriptionUpdatesUsageKey,
} from '#src/utils/subscription/index.js';
Expand Down Expand Up @@ -235,16 +234,12 @@ export const createQuotaLibrary = (
return;
}

const [
{
quota: { scopesPerResourceLimit, scopesPerRoleLimit },
},
scopeUsages,
] = await Promise.all([
getTenantSubscriptionData(cloudConnection),
getTenantSubscriptionScopeUsage(cloudConnection, entityName),
]);
const usage = scopeUsages[entityId] ?? 0;
const {
quota: { scopesPerResourceLimit, scopesPerRoleLimit },
resources,
roles,
} = await getTenantSubscriptionData(cloudConnection);
const usage = (entityName === 'resources' ? resources[entityId] : roles[entityId]) ?? 0;

if (entityName === 'resources') {
assertThat(
Expand Down
22 changes: 5 additions & 17 deletions packages/core/src/utils/subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,16 @@ export const getTenantSubscriptionData = async (
planId: string;
quota: SubscriptionQuota;
usage: SubscriptionUsage;
resources: Record<string, number>;
roles: Record<string, number>;
}> => {
const client = await cloudConnection.getClient();
const [{ planId }, quota, usage] = await Promise.all([
const [{ planId }, { quota, usage, resources, roles }] = await Promise.all([
client.get('/api/tenants/my/subscription'),
client.get('/api/tenants/my/subscription/quota'),
client.get('/api/tenants/my/subscription/usage'),
client.get('/api/tenants/my/subscription-usage'),
]);

return { planId, quota, usage };
};

export const getTenantSubscriptionScopeUsage = async (
cloudConnection: CloudConnectionLibrary,
entityName: 'resources' | 'roles'
): Promise<Record<string, number>> => {
const client = await cloudConnection.getClient();
const scopeUsages = await client.get('/api/tenants/my/subscription/usage/:entityName/scopes', {
params: { entityName },
search: {},
});

return scopeUsages;
return { planId, quota, usage, resources, roles };
};

export const reportSubscriptionUpdates = async (
Expand Down
37 changes: 24 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da58dec

Please sign in to comment.