Skip to content

Commit

Permalink
refactor(console): refactor tag component
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Aug 16, 2024
1 parent 04c533e commit 626482f
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function CreateForm({
planId === ReservedPlanId.Pro &&
ReservedPlanId.Pro
)}
hasAddOnTag={isDevFeaturesEnabled && watch('type') === ApplicationType.MachineToMachine}
size={defaultCreateType ? 'medium' : 'large'}
footer={
<Footer
Expand Down
19 changes: 0 additions & 19 deletions packages/console/src/components/FeatureTag/AddOnTag.tsx

This file was deleted.

40 changes: 30 additions & 10 deletions packages/console/src/components/FeatureTag/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { ReservedPlanId } from '@logto/schemas';
import classNames from 'classnames';
import { useContext } from 'react';

import { isDevFeaturesEnabled } from '@/consts/env';
import { isCloud, isDevFeaturesEnabled } from '@/consts/env';
import { SubscriptionDataContext } from '@/contexts/SubscriptionDataProvider';
import { TenantsContext } from '@/contexts/TenantsProvider';

import AddOnTag from './AddOnTag';
import styles from './index.module.scss';

export { default as BetaTag } from './BetaTag';
Expand Down Expand Up @@ -53,9 +52,6 @@ export type Props = {
function FeatureTag(props: Props) {
const { className } = props;
const { isDevTenant } = useContext(TenantsContext);
const {
currentSubscription: { planId },
} = useContext(SubscriptionDataContext);

const { isVisible, plan } = props;

Expand All @@ -65,12 +61,36 @@ function FeatureTag(props: Props) {
return null;
}

// Show the add-on tag for Pro plan when dev features are enabled.
if (isDevFeaturesEnabled && planId === ReservedPlanId.Pro) {
return <AddOnTag className={className} />;
}

return <div className={classNames(styles.tag, className)}>{plan}</div>;
}

export default FeatureTag;

type CombinedAddOnAndFeatureTagProps = {
readonly hasAddOnTag?: boolean;
readonly className?: string;
/** The minimum plan required to use the feature. */
readonly paywall?: Props['plan'];
};

/**
* When `hasAddOnTag` is `true`, the tag will be `AddOnTag` if the plan is `ReservedPlanId.Pro`
* and dev features are enabled. Otherwise, it will be `FeatureTag` with the `paywall` prop.
*/
export function CombinedAddOnAndFeatureTag(props: CombinedAddOnAndFeatureTagProps) {
const { hasAddOnTag, className, paywall } = props;
const {
currentSubscription: { planId },
} = useContext(SubscriptionDataContext);

// Show the "Add-on" tag for Pro plan when dev features enabled.
if (hasAddOnTag && isDevFeaturesEnabled && planId === ReservedPlanId.Pro) {
return <div className={classNames(styles.tag, styles.beta, className)}>Add-on</div>;
}

if (paywall && isCloud) {
return <FeatureTag isVisible plan={paywall} />;
}

return null;
}
7 changes: 4 additions & 3 deletions packages/console/src/ds-components/CardTitle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import classNames from 'classnames';
import type { ReactElement } from 'react';
import { useTranslation } from 'react-i18next';

import FeatureTag from '@/components/FeatureTag';
import { isCloud } from '@/consts/env';
import { CombinedAddOnAndFeatureTag } from '@/components/FeatureTag';
import type { Props as TextLinkProps } from '@/ds-components/TextLink';

import type DangerousRaw from '../DangerousRaw';
Expand All @@ -27,6 +26,7 @@ export type Props = {
* If not provided, no paywall tag will be shown.
*/
readonly paywall?: Exclude<ReservedPlanId, ReservedPlanId.Free | ReservedPlanId.Development>;
readonly hasAddOnTag?: boolean;
};

/**
Expand All @@ -40,14 +40,15 @@ function CardTitle({
learnMoreLink,
className,
paywall,
hasAddOnTag,
}: Props) {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });

return (
<div className={classNames(styles.container, styles[size], className)}>
<div className={classNames(styles.title, !isWordWrapEnabled && styles.titleEllipsis)}>
{typeof title === 'string' ? <DynamicT forKey={title} /> : title}
{paywall && isCloud && <FeatureTag isVisible plan={paywall} />}
<CombinedAddOnAndFeatureTag hasAddOnTag={hasAddOnTag} paywall={paywall} />
</div>
{Boolean(subtitle ?? learnMoreLink) && (
<div className={styles.subtitle}>
Expand Down
5 changes: 4 additions & 1 deletion packages/console/src/ds-components/ModalLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export type Props = {
readonly className?: string;
readonly size?: 'medium' | 'large' | 'xlarge';
readonly headerIcon?: ReactElement;
} & Pick<CardTitleProps, 'learnMoreLink' | 'title' | 'subtitle' | 'isWordWrapEnabled' | 'paywall'>;
} & Pick<
CardTitleProps,
'learnMoreLink' | 'title' | 'subtitle' | 'isWordWrapEnabled' | 'paywall' | 'hasAddOnTag'
>;

function ModalLayout({
children,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function CreateForm({ onClose }: Props) {
paywall={conditional(
isDevFeaturesEnabled && planId === ReservedPlanId.Pro && ReservedPlanId.Pro
)}
hasAddOnTag={isDevFeaturesEnabled}
footer={<Footer isCreationLoading={isSubmitting} onClickCreate={onSubmit} />}
onClose={onClose}
>
Expand Down
11 changes: 3 additions & 8 deletions packages/console/src/pages/EnterpriseSso/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ function EnterpriseSso() {
const { navigate } = useTenantPathname();
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const { isDevTenant } = useContext(TenantsContext);
const {
currentPlan,
currentSubscription: { planId },
currentSubscriptionQuota,
} = useContext(SubscriptionDataContext);
const { currentPlan, currentSubscriptionQuota } = useContext(SubscriptionDataContext);

const [{ page }, updateSearchParameters] = useSearchParametersWatcher({
page: 1,
Expand All @@ -67,11 +63,10 @@ function EnterpriseSso() {
return (
<ListPage
title={{
paywall: isDevFeaturesEnabled
? conditional(planId === ReservedPlanId.Pro && ReservedPlanId.Pro)
: conditional((!isSsoEnabled || isDevTenant) && ReservedPlanId.Pro),
paywall: conditional((!isSsoEnabled || isDevTenant) && ReservedPlanId.Pro),
title: 'enterprise_sso.title',
subtitle: 'enterprise_sso.subtitle',
hasAddOnTag: isDevFeaturesEnabled,
}}
pageMeta={{ titleKey: 'enterprise_sso.page_title' }}
createButton={conditional(
Expand Down
8 changes: 2 additions & 6 deletions packages/console/src/pages/Mfa/PageWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function PageWrapper({ children }: Props) {
const { isDevTenant } = useContext(TenantsContext);
const {
currentPlan,
currentSubscription: { planId },
currentSubscriptionQuota: { mfaEnabled },
} = useContext(SubscriptionDataContext);
const isMfaEnabled =
Expand All @@ -28,11 +27,8 @@ function PageWrapper({ children }: Props) {
<div className={styles.container}>
<PageMeta titleKey="mfa.title" />
<CardTitle
paywall={
isDevFeaturesEnabled
? cond(planId === ReservedPlanId.Pro && ReservedPlanId.Pro)
: cond((!isMfaEnabled || isDevTenant) && ReservedPlanId.Pro)
}
paywall={cond((!isMfaEnabled || isDevTenant) && ReservedPlanId.Pro)}
hasAddOnTag={isDevFeaturesEnabled}
title="mfa.title"
subtitle="mfa.description"
className={styles.cardTitle}
Expand Down
12 changes: 2 additions & 10 deletions packages/console/src/pages/OrganizationTemplate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ const basePathname = '/organization-template';
function OrganizationTemplate() {
const { getDocumentationUrl } = useDocumentationUrl();
const [isGuideDrawerOpen, setIsGuideDrawerOpen] = useState(false);
const {
currentPlan,
currentSubscription: { planId },
currentSubscriptionQuota,
} = useContext(SubscriptionDataContext);
const { currentPlan, currentSubscriptionQuota } = useContext(SubscriptionDataContext);
const { isDevTenant } = useContext(TenantsContext);
const isOrganizationsDisabled =
isCloud &&
Expand All @@ -60,11 +56,7 @@ function OrganizationTemplate() {
href: getDocumentationUrl(organizationTemplateLink),
targetBlank: 'noopener',
}}
paywall={
isDevFeaturesEnabled
? cond(planId === ReservedPlanId.Pro && ReservedPlanId.Pro)
: cond((isOrganizationsDisabled || isDevTenant) && ReservedPlanId.Pro)
}
paywall={cond((isOrganizationsDisabled || isDevTenant) && ReservedPlanId.Pro)}
/>
<Button
title="application_details.check_guide"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function CreateOrganizationModal({ isOpen, onClose }: Props) {
paywall={conditional(
isDevFeaturesEnabled && planId === ReservedPlanId.Pro && ReservedPlanId.Pro
)}
hasAddOnTag={isDevFeaturesEnabled}
footer={
cond(
isDevFeaturesEnabled &&
Expand Down
13 changes: 3 additions & 10 deletions packages/console/src/pages/Organizations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ const organizationsPathname = '/organizations';

function Organizations() {
const { getDocumentationUrl } = useDocumentationUrl();
const {
currentPlan,
currentSubscription: { planId },
currentSubscriptionQuota,
} = useContext(SubscriptionDataContext);
const { currentPlan, currentSubscriptionQuota } = useContext(SubscriptionDataContext);
const { isDevTenant } = useContext(TenantsContext);

const { navigate } = useTenantPathname();
Expand Down Expand Up @@ -64,11 +60,8 @@ function Organizations() {
<PageMeta titleKey="organizations.page_title" />
<div className={pageLayout.headline}>
<CardTitle
paywall={
isDevFeaturesEnabled
? cond(planId === ReservedPlanId.Pro && ReservedPlanId.Pro)
: cond((isOrganizationsDisabled || isDevTenant) && ReservedPlanId.Pro)
}
paywall={cond((isOrganizationsDisabled || isDevTenant) && ReservedPlanId.Pro)}
hasAddOnTag={isDevFeaturesEnabled}
title="organizations.title"
subtitle="organizations.subtitle"
learnMoreLink={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function InviteMemberModal({ isOpen, onClose }: Props) {
paywall={conditional(
isDevFeaturesEnabled && planId === ReservedPlanId.Pro && ReservedPlanId.Pro
)}
hasAddOnTag={isDevFeaturesEnabled}
subtitle="tenant_members.invite_modal.subtitle"
footer={
conditional(
Expand Down

0 comments on commit 626482f

Please sign in to comment.