-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console,core,phrases): add quota guard for cloud collaboration i…
…n console (#5644)
- Loading branch information
1 parent
a6a32c5
commit cfeb98c
Showing
75 changed files
with
713 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,6 @@ | |
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"@logto/cloud": "0.2.5-1807f9c" | ||
"@logto/cloud": "0.2.5-ab8a489" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...console/src/pages/TenantSettings/TenantMembers/InviteMemberModal/Footer/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@use '@/scss/underscore' as _; | ||
|
||
.container { | ||
display: flex; | ||
align-items: center; | ||
gap: _.unit(6); | ||
padding: _.unit(6); | ||
background-color: var(--color-info-container); | ||
margin: 0 _.unit(-6) _.unit(-6); | ||
|
||
.description { | ||
flex: 1; | ||
flex-shrink: 0; | ||
font: var(--font-body-2); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/console/src/pages/TenantSettings/TenantMembers/InviteMemberModal/Footer/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ReservedPlanId } from '@logto/schemas'; | ||
import { useContext } from 'react'; | ||
import { Trans, useTranslation } from 'react-i18next'; | ||
|
||
import ContactUsPhraseLink from '@/components/ContactUsPhraseLink'; | ||
import QuotaGuardFooter from '@/components/QuotaGuardFooter'; | ||
import { contactEmailLink } from '@/consts'; | ||
import { SubscriptionDataContext } from '@/contexts/SubscriptionDataProvider'; | ||
import Button, { LinkButton } from '@/ds-components/Button'; | ||
|
||
import useTenantMembersUsage from '../../hooks'; | ||
|
||
import * as styles from './index.module.scss'; | ||
|
||
type Props = { | ||
newInvitationCount?: number; | ||
isLoading: boolean; | ||
onSubmit: () => void; | ||
}; | ||
|
||
function Footer({ newInvitationCount = 0, isLoading, onSubmit }: Props) { | ||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.upsell.paywall' }); | ||
|
||
const { currentPlan } = useContext(SubscriptionDataContext); | ||
const { id: planId, quota } = currentPlan; | ||
|
||
const { hasTenantMembersReachedLimit, limit, usage } = useTenantMembersUsage(); | ||
|
||
if (planId === ReservedPlanId.Free && hasTenantMembersReachedLimit) { | ||
return ( | ||
<QuotaGuardFooter> | ||
<Trans | ||
components={{ | ||
a: <ContactUsPhraseLink />, | ||
}} | ||
> | ||
{t('tenant_members')} | ||
</Trans> | ||
</QuotaGuardFooter> | ||
); | ||
} | ||
|
||
if ( | ||
planId === ReservedPlanId.Development && | ||
(hasTenantMembersReachedLimit || usage + newInvitationCount > limit) | ||
) { | ||
// Display a custom "Contact us" footer instead of asking for upgrade | ||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.description}> | ||
{t('tenant_members_dev_plan', { limit: quota.tenantMembersLimit })} | ||
</div> | ||
<LinkButton | ||
size="large" | ||
type="primary" | ||
title="general.contact_us_action" | ||
href={contactEmailLink} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<Button | ||
size="large" | ||
type="primary" | ||
title="tenant_members.invite_members" | ||
isLoading={isLoading} | ||
onClick={onSubmit} | ||
/> | ||
); | ||
} | ||
|
||
export default Footer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/console/src/pages/TenantSettings/TenantMembers/hooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { OrganizationInvitationStatus } from '@logto/schemas'; | ||
import { useContext, useMemo } from 'react'; | ||
import useSWR from 'swr'; | ||
|
||
import { useAuthedCloudApi } from '@/cloud/hooks/use-cloud-api'; | ||
import { type TenantInvitationResponse, type TenantMemberResponse } from '@/cloud/types/router'; | ||
import { SubscriptionDataContext } from '@/contexts/SubscriptionDataProvider'; | ||
import { TenantsContext } from '@/contexts/TenantsProvider'; | ||
import { type RequestError } from '@/hooks/use-api'; | ||
import { hasReachedQuotaLimit, hasSurpassedQuotaLimit } from '@/utils/quota'; | ||
|
||
const useTenantMembersUsage = () => { | ||
const { currentPlan } = useContext(SubscriptionDataContext); | ||
const { currentTenantId } = useContext(TenantsContext); | ||
|
||
const cloudApi = useAuthedCloudApi(); | ||
|
||
const { data: members } = useSWR<TenantMemberResponse[], RequestError>( | ||
`api/tenants/:tenantId/members`, | ||
async () => | ||
cloudApi.get('/api/tenants/:tenantId/members', { params: { tenantId: currentTenantId } }) | ||
); | ||
const { data: invitations } = useSWR<TenantInvitationResponse[], RequestError>( | ||
'api/tenants/:tenantId/invitations', | ||
async () => | ||
cloudApi.get('/api/tenants/:tenantId/invitations', { params: { tenantId: currentTenantId } }) | ||
); | ||
|
||
const pendingInvitations = useMemo( | ||
() => invitations?.filter(({ status }) => status === OrganizationInvitationStatus.Pending), | ||
[invitations] | ||
); | ||
|
||
const usage = useMemo(() => { | ||
return (members?.length ?? 0) + (pendingInvitations?.length ?? 0); | ||
}, [members?.length, pendingInvitations?.length]); | ||
|
||
const hasTenantMembersReachedLimit = useMemo( | ||
() => | ||
hasReachedQuotaLimit({ | ||
quotaKey: 'tenantMembersLimit', | ||
plan: currentPlan, | ||
usage, | ||
}), | ||
[currentPlan, usage] | ||
); | ||
|
||
const hasTenantMembersSurpassedLimit = useMemo( | ||
() => | ||
hasSurpassedQuotaLimit({ | ||
quotaKey: 'tenantMembersLimit', | ||
plan: currentPlan, | ||
usage, | ||
}), | ||
[currentPlan, usage] | ||
); | ||
|
||
return { | ||
hasTenantMembersReachedLimit, | ||
hasTenantMembersSurpassedLimit, | ||
usage, | ||
limit: currentPlan.quota.tenantMembersLimit ?? Number.POSITIVE_INFINITY, | ||
}; | ||
}; | ||
|
||
export default useTenantMembersUsage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.