From da58dec022fef6693cfe156c3910b1442a1095e8 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Tue, 13 Aug 2024 13:22:26 +0800 Subject: [PATCH] fix: update tenant selector dropdown data source (#6438) --- packages/console/package.json | 2 +- .../TenantDropdownItem/TenantStatusTag.tsx | 31 +++------------- .../TenantDropdownItem/index.tsx | 8 +--- packages/console/src/consts/tenants.ts | 5 ++- packages/core/package.json | 2 +- packages/core/src/libraries/quota.ts | 17 +++------ packages/core/src/utils/subscription/index.ts | 22 +++-------- pnpm-lock.yaml | 37 ++++++++++++------- 8 files changed, 47 insertions(+), 77 deletions(-) diff --git a/packages/console/package.json b/packages/console/package.json index 19fa4be98..90e5120dd 100644 --- a/packages/console/package.json +++ b/packages/console/package.json @@ -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", diff --git a/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/TenantStatusTag.tsx b/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/TenantStatusTag.tsx index 6dd5b94bb..5f14f5e18 100644 --- a/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/TenantStatusTag.tsx +++ b/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/TenantStatusTag.tsx @@ -1,10 +1,4 @@ -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'; @@ -12,16 +6,13 @@ 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: @@ -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 ( diff --git a/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/index.tsx b/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/index.tsx index 1c9fc627a..eccfe22fc 100644 --- a/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/index.tsx +++ b/packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/index.tsx @@ -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] @@ -51,7 +46,6 @@ function TenantDropdownItem({ tenantData, isSelected, onClick }: Props) { diff --git a/packages/console/src/consts/tenants.ts b/packages/console/src/consts/tenants.ts index 133aa531b..8ff6fb29d 100644 --- a/packages/console/src/consts/tenants.ts +++ b/packages/console/src/consts/tenants.ts @@ -34,8 +34,9 @@ export const defaultTenantResponse: TenantResponse = { }, usage: { activeUsers: 0, - cost: 0, - tokenUsage: 0, + }, + quota: { + mauLimit: null, }, openInvoices: [], isSuspended: false, diff --git a/packages/core/package.json b/packages/core/package.json index 404e5cac2..146bee851 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/libraries/quota.ts b/packages/core/src/libraries/quota.ts index f3338877a..967ac083a 100644 --- a/packages/core/src/libraries/quota.ts +++ b/packages/core/src/libraries/quota.ts @@ -8,7 +8,6 @@ import assertThat from '#src/utils/assert-that.js'; import { getTenantSubscriptionPlan, getTenantSubscriptionData, - getTenantSubscriptionScopeUsage, reportSubscriptionUpdates, isReportSubscriptionUpdatesUsageKey, } from '#src/utils/subscription/index.js'; @@ -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( diff --git a/packages/core/src/utils/subscription/index.ts b/packages/core/src/utils/subscription/index.ts index 303cb8a95..7ccfcb322 100644 --- a/packages/core/src/utils/subscription/index.ts +++ b/packages/core/src/utils/subscription/index.ts @@ -43,28 +43,16 @@ export const getTenantSubscriptionData = async ( planId: string; quota: SubscriptionQuota; usage: SubscriptionUsage; + resources: Record; + roles: Record; }> => { 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> => { - 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 ( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f895e49cd..345e217ec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2458,8 +2458,8 @@ importers: specifier: ^29.5.0 version: 29.5.0 '@logto/cloud': - specifier: 0.2.5-3452c56 - version: 0.2.5-3452c56(zod@3.23.8) + specifier: 0.2.5-a6cff75 + version: 0.2.5-a6cff75(zod@3.23.8) '@logto/connector-kit': specifier: workspace:^4.0.0 version: link:../toolkit/connector-kit @@ -2948,8 +2948,8 @@ importers: version: 3.23.8 devDependencies: '@logto/cloud': - specifier: 0.2.5-3452c56 - version: 0.2.5-3452c56(zod@3.23.8) + specifier: 0.2.5-a6cff75 + version: 0.2.5-a6cff75(zod@3.23.8) '@silverhand/eslint-config': specifier: 6.0.1 version: 6.0.1(eslint@8.57.0)(prettier@3.0.0)(typescript@5.5.3) @@ -5080,6 +5080,10 @@ packages: resolution: {integrity: sha512-19MGifwYGxjQMPrm6monfoQyOp9UTL/chtZE0JugppNwvvLyqr3Nx0maCHuwrydLt0ImBSgVmPW1cJVvu2tVPg==} engines: {node: ^20.9.0} + '@logto/cloud@0.2.5-a6cff75': + resolution: {integrity: sha512-VlW8MI8RU5dWbHOXY6HjcaC4cqN+I0FIplZQnQjsf00R7K1EFvWfdzNqMcPsiK0ljnyEkRBH4GO77zJ/MYsNdg==} + engines: {node: ^20.9.0} + '@logto/js@4.1.4': resolution: {integrity: sha512-6twud1nFBQmj89/aflzej6yD1QwXfPiYmRtyYuN4a7O9OaaW3X/kJBVwjKUn5NC9IUt+rd+jXsI3QJXENfaLAw==} @@ -14718,6 +14722,13 @@ snapshots: transitivePeerDependencies: - zod + '@logto/cloud@0.2.5-a6cff75(zod@3.23.8)': + dependencies: + '@silverhand/essentials': 2.9.1 + '@withtyped/server': 0.13.6(zod@3.23.8) + transitivePeerDependencies: + - zod + '@logto/js@4.1.4': dependencies: '@silverhand/essentials': 2.9.1 @@ -15155,10 +15166,10 @@ snapshots: eslint-config-prettier: 9.1.0(eslint@8.57.0) eslint-config-xo: 0.44.0(eslint@8.57.0) eslint-config-xo-typescript: 4.0.0(@typescript-eslint/eslint-plugin@7.7.0(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0)(typescript@5.5.3))(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0)(typescript@5.5.3) - eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0) + eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1)(eslint@8.57.0) eslint-plugin-consistent-default-export-name: 0.0.15 eslint-plugin-eslint-comments: 3.2.0(eslint@8.57.0) - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) eslint-plugin-n: 17.2.1(eslint@8.57.0) eslint-plugin-no-use-extend-native: 0.5.0 eslint-plugin-prettier: 5.1.3(eslint-config-prettier@9.1.0(eslint@8.57.0))(eslint@8.57.0)(prettier@3.0.0) @@ -18096,13 +18107,13 @@ snapshots: transitivePeerDependencies: - supports-color - eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0): + eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1)(eslint@8.57.0): dependencies: debug: 4.3.4 enhanced-resolve: 5.16.0 eslint: 8.57.0 - eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) - eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) + eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0) + eslint-plugin-import: 2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0) fast-glob: 3.3.2 get-tsconfig: 4.7.3 is-core-module: 2.13.1 @@ -18113,14 +18124,14 @@ snapshots: - eslint-import-resolver-webpack - supports-color - eslint-module-utils@2.8.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0): + eslint-module-utils@2.8.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0): dependencies: debug: 3.2.7(supports-color@5.5.0) optionalDependencies: '@typescript-eslint/parser': 7.7.0(eslint@8.57.0)(typescript@5.5.3) eslint: 8.57.0 eslint-import-resolver-node: 0.3.9 - eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0) + eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1)(eslint@8.57.0) transitivePeerDependencies: - supports-color @@ -18142,7 +18153,7 @@ snapshots: eslint: 8.57.0 ignore: 5.3.1 - eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0): + eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-typescript@3.6.1)(eslint@8.57.0): dependencies: array-includes: 3.1.8 array.prototype.findlastindex: 1.2.5 @@ -18152,7 +18163,7 @@ snapshots: doctrine: 2.1.0 eslint: 8.57.0 eslint-import-resolver-node: 0.3.9 - eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint@8.57.0))(eslint@8.57.0))(eslint@8.57.0) + eslint-module-utils: 2.8.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-import-resolver-node@0.3.9)(eslint-import-resolver-typescript@3.6.1(@typescript-eslint/parser@7.7.0(eslint@8.57.0)(typescript@5.5.3))(eslint-plugin-import@2.29.1)(eslint@8.57.0))(eslint@8.57.0) hasown: 2.0.2 is-core-module: 2.13.1 is-glob: 4.0.3