0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(console): fix the plan title for subscription plan selector (#6348)

This commit is contained in:
Darcy Ye 2024-07-29 10:49:03 +08:00 committed by GitHub
parent 7659454d8f
commit 918b6f2bbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 3 deletions

View file

@ -2,7 +2,7 @@ import { conditional } from '@silverhand/essentials';
import { type TFuncKey } from 'i18next'; import { type TFuncKey } from 'i18next';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { ReservedPlanName } from '@/types/subscriptions'; import { ReservedPlanName, ReservedSkuId } from '@/types/subscriptions';
const registeredPlanNamePhraseMap: Record< const registeredPlanNamePhraseMap: Record<
string, string,
@ -15,6 +15,16 @@ const registeredPlanNamePhraseMap: Record<
[ReservedPlanName.Enterprise]: 'enterprise', [ReservedPlanName.Enterprise]: 'enterprise',
}; };
const registeredSkuIdNamePhraseMap: Record<
string,
TFuncKey<'translation', 'admin_console.subscription'> | undefined
> = {
quotaKey: undefined,
[ReservedSkuId.Free]: 'free_plan',
[ReservedSkuId.Pro]: 'pro_plan',
[ReservedSkuId.Enterprise]: 'enterprise',
};
type Props = { type Props = {
/** Temporarily use optional for backward compatibility. */ /** Temporarily use optional for backward compatibility. */
readonly skuId?: string; readonly skuId?: string;
@ -26,7 +36,7 @@ type Props = {
function PlanName({ skuId, name }: Props) { function PlanName({ skuId, name }: Props) {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.subscription' }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.subscription' });
const planNamePhrase = const planNamePhrase =
conditional(skuId && registeredPlanNamePhraseMap[skuId]) ?? registeredPlanNamePhraseMap[name]; conditional(skuId && registeredSkuIdNamePhraseMap[skuId]) ?? registeredPlanNamePhraseMap[name];
/** /**
* Note: fallback to the plan name if the phrase is not registered. * Note: fallback to the plan name if the phrase is not registered.

View file

@ -10,11 +10,13 @@ import PlanName from '@/components/PlanName';
import QuotaGuardFooter from '@/components/QuotaGuardFooter'; import QuotaGuardFooter from '@/components/QuotaGuardFooter';
import { isDevFeaturesEnabled } from '@/consts/env'; import { isDevFeaturesEnabled } from '@/consts/env';
import { SubscriptionDataContext } from '@/contexts/SubscriptionDataProvider'; import { SubscriptionDataContext } from '@/contexts/SubscriptionDataProvider';
import { TenantsContext } from '@/contexts/TenantsProvider';
import Button from '@/ds-components/Button'; import Button from '@/ds-components/Button';
import FormField from '@/ds-components/FormField'; import FormField from '@/ds-components/FormField';
import ModalLayout from '@/ds-components/ModalLayout'; import ModalLayout from '@/ds-components/ModalLayout';
import TextInput from '@/ds-components/TextInput'; import TextInput from '@/ds-components/TextInput';
import useApi from '@/hooks/use-api'; import useApi from '@/hooks/use-api';
import useNewSubscriptionScopeUsage from '@/hooks/use-new-subscription-scopes-usage';
import modalStyles from '@/scss/modal.module.scss'; import modalStyles from '@/scss/modal.module.scss';
import { trySubmitSafe } from '@/utils/form'; import { trySubmitSafe } from '@/utils/form';
import { hasReachedQuotaLimit, hasReachedSubscriptionQuotaLimit } from '@/utils/quota'; import { hasReachedQuotaLimit, hasReachedSubscriptionQuotaLimit } from '@/utils/quota';
@ -35,6 +37,10 @@ function CreatePermissionModal({ resourceId, totalResourceCount, onClose }: Prop
currentSubscriptionQuota, currentSubscriptionQuota,
currentSubscriptionScopeResourceUsage, currentSubscriptionScopeResourceUsage,
} = useContext(SubscriptionDataContext); } = useContext(SubscriptionDataContext);
const { currentTenantId } = useContext(TenantsContext);
const {
scopeResourceUsage: { mutate: mutateScopeResourceUsage },
} = useNewSubscriptionScopeUsage(currentTenantId);
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const { const {
@ -55,6 +61,7 @@ function CreatePermissionModal({ resourceId, totalResourceCount, onClose }: Prop
.post(`api/resources/${resourceId}/scopes`, { json: formData }) .post(`api/resources/${resourceId}/scopes`, { json: formData })
.json<Scope>(); .json<Scope>();
void mutateScopeResourceUsage();
onClose(createdScope); onClose(createdScope);
}) })
); );

View file

@ -2,7 +2,7 @@ import type { Resource } from '@logto/schemas';
import { isManagementApi, Theme } from '@logto/schemas'; import { isManagementApi, Theme } from '@logto/schemas';
import { conditionalArray } from '@silverhand/essentials'; import { conditionalArray } from '@silverhand/essentials';
import classNames from 'classnames'; import classNames from 'classnames';
import { useEffect, useState } from 'react'; import { useEffect, useState, useContext } from 'react';
import { toast } from 'react-hot-toast'; import { toast } from 'react-hot-toast';
import { Trans, useTranslation } from 'react-i18next'; import { Trans, useTranslation } from 'react-i18next';
import { Outlet, useLocation, useParams } from 'react-router-dom'; import { Outlet, useLocation, useParams } from 'react-router-dom';
@ -19,11 +19,13 @@ import DetailsPageHeader, { type MenuItem } from '@/components/DetailsPage/Detai
import Drawer from '@/components/Drawer'; import Drawer from '@/components/Drawer';
import PageMeta from '@/components/PageMeta'; import PageMeta from '@/components/PageMeta';
import { ApiResourceDetailsTabs } from '@/consts/page-tabs'; import { ApiResourceDetailsTabs } from '@/consts/page-tabs';
import { TenantsContext } from '@/contexts/TenantsProvider';
import DeleteConfirmModal from '@/ds-components/DeleteConfirmModal'; import DeleteConfirmModal from '@/ds-components/DeleteConfirmModal';
import TabNav, { TabNavItem } from '@/ds-components/TabNav'; import TabNav, { TabNavItem } from '@/ds-components/TabNav';
import type { RequestError } from '@/hooks/use-api'; import type { RequestError } from '@/hooks/use-api';
import useApi from '@/hooks/use-api'; import useApi from '@/hooks/use-api';
import useDocumentationUrl from '@/hooks/use-documentation-url'; import useDocumentationUrl from '@/hooks/use-documentation-url';
import useNewSubscriptionScopeUsage from '@/hooks/use-new-subscription-scopes-usage';
import useTenantPathname from '@/hooks/use-tenant-pathname'; import useTenantPathname from '@/hooks/use-tenant-pathname';
import useTheme from '@/hooks/use-theme'; import useTheme from '@/hooks/use-theme';
@ -41,6 +43,10 @@ const icons = {
function ApiResourceDetails() { function ApiResourceDetails() {
const { pathname } = useLocation(); const { pathname } = useLocation();
const { id, guideId } = useParams(); const { id, guideId } = useParams();
const { currentTenantId } = useContext(TenantsContext);
const {
scopeResourceUsage: { mutate: mutateScopeResourceUsage },
} = useNewSubscriptionScopeUsage(currentTenantId);
const { navigate, match } = useTenantPathname(); const { navigate, match } = useTenantPathname();
const { getDocumentationUrl } = useDocumentationUrl(); const { getDocumentationUrl } = useDocumentationUrl();
const isGuideView = !!id && !!guideId && match(`/api-resources/${id}/guide/${guideId}`); const isGuideView = !!id && !!guideId && match(`/api-resources/${id}/guide/${guideId}`);
@ -75,6 +81,7 @@ function ApiResourceDetails() {
try { try {
await api.delete(`api/resources/${data.id}`); await api.delete(`api/resources/${data.id}`);
void mutateScopeResourceUsage();
toast.success(t('api_resource_details.api_resource_deleted', { name: data.name })); toast.success(t('api_resource_details.api_resource_deleted', { name: data.name }));
navigate(`/api-resources`); navigate(`/api-resources`);
} finally { } finally {

View file

@ -10,6 +10,13 @@ export enum ReservedPlanName {
Enterprise = 'Enterprise', Enterprise = 'Enterprise',
} }
// TODO: use `ReservedPlanId` in the future.
export enum ReservedSkuId {
Free = 'free',
Pro = 'pro',
Enterprise = 'enterprise',
}
export type SubscriptionPlanQuota = Omit< export type SubscriptionPlanQuota = Omit<
SubscriptionPlanResponse['quota'], SubscriptionPlanResponse['quota'],
'builtInEmailConnectorEnabled' 'builtInEmailConnectorEnabled'