1- import type { GetDomainsParams , OrganizationEnrollmentMode } from '@clerk/types' ;
2- import type { OrganizationDomainVerificationStatus } from '@clerk/types' ;
1+ import type {
2+ GetDomainsParams ,
3+ OrganizationDomainResource ,
4+ OrganizationDomainVerificationStatus ,
5+ OrganizationEnrollmentMode ,
6+ } from '@clerk/types' ;
37import React , { useMemo } from 'react' ;
48
5- import { withGate } from '../../common' ;
9+ import { stripOrigin , toURL , trimLeadingSlash } from '../../../utils' ;
10+ import { useGate , withGate } from '../../common' ;
611import { useCoreOrganization } from '../../contexts' ;
12+ import type { LocalizationKey } from '../../customizables' ;
713import { Box , Col , localizationKeys , Spinner } from '../../customizables' ;
814import { ArrowBlockButton , BlockWithTrailingComponent , ThreeDotsMenu } from '../../elements' ;
915import { useInView } from '../../hooks' ;
@@ -17,10 +23,61 @@ type DomainListProps = GetDomainsParams & {
1723 * Enables internal links to navigate to the correct page
1824 * based on when this component is used
1925 */
20- redirectSubPath : string ;
26+ redirectSubPath : 'organization-settings/domain' | 'domain' ;
2127 fallback ?: React . ReactNode ;
2228} ;
2329
30+ const useDomainList = ( ) => {
31+ const { isAuthorizedUser : canDeleteDomain } = useGate ( { permission : 'org:sys_domains:delete' } ) ;
32+ const { isAuthorizedUser : canVerifyDomain } = useGate ( { permission : 'org:sys_domains:manage' } ) ;
33+
34+ return {
35+ showDotMenu : canDeleteDomain || canVerifyDomain ,
36+ canVerifyDomain,
37+ canDeleteDomain,
38+ } ;
39+ } ;
40+
41+ const buildDomainListRelativeURL = ( parentPath : string , domainId : string , mode ?: 'verify' | 'remove' ) =>
42+ trimLeadingSlash ( stripOrigin ( toURL ( `${ parentPath } /${ domainId } /${ mode || '' } ` ) ) ) ;
43+
44+ const useMenuActions = (
45+ parentPath : string ,
46+ domainId : string ,
47+ ) : { label : LocalizationKey ; onClick : ( ) => Promise < unknown > ; isDestructive ?: boolean } [ ] => {
48+ const { canDeleteDomain, canVerifyDomain } = useDomainList ( ) ;
49+ const { navigate } = useRouter ( ) ;
50+
51+ const menuActions = [ ] ;
52+
53+ if ( canVerifyDomain ) {
54+ menuActions . push ( {
55+ label : localizationKeys ( 'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__verify' ) ,
56+ onClick : ( ) => navigate ( buildDomainListRelativeURL ( parentPath , domainId , 'verify' ) ) ,
57+ } ) ;
58+ }
59+
60+ if ( canDeleteDomain ) {
61+ menuActions . push ( {
62+ label : localizationKeys ( 'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__remove' ) ,
63+ isDestructive : true ,
64+ onClick : ( ) => navigate ( buildDomainListRelativeURL ( parentPath , domainId , 'remove' ) ) ,
65+ } ) ;
66+ }
67+
68+ return menuActions ;
69+ } ;
70+
71+ const DomainListDotMenu = ( {
72+ redirectSubPath,
73+ domainId,
74+ } : Pick < DomainListProps , 'redirectSubPath' > & {
75+ domainId : OrganizationDomainResource [ 'id' ] ;
76+ } ) => {
77+ const actions = useMenuActions ( redirectSubPath , domainId ) ;
78+ return < ThreeDotsMenu actions = { actions } /> ;
79+ } ;
80+
2481export const DomainList = withGate (
2582 ( props : DomainListProps ) => {
2683 const { verificationStatus, enrollmentMode, redirectSubPath, fallback, ...rest } = props ;
@@ -31,6 +88,7 @@ export const DomainList = withGate(
3188 } ,
3289 } ) ;
3390
91+ const { showDotMenu } = useDomainList ( ) ;
3492 const { ref } = useInView ( {
3593 threshold : 0 ,
3694 onChange : inView => {
@@ -69,7 +127,7 @@ export const DomainList = withGate(
69127 < Col >
70128 { domainList . length === 0 && ! domains ?. isLoading && fallback }
71129 { domainList . map ( d => {
72- if ( ! ( d . verification && d . verification . status === 'verified' ) ) {
130+ if ( ! ( d . verification && d . verification . status === 'verified' ) || ! showDotMenu ) {
73131 return (
74132 < BlockWithTrailingComponent
75133 key = { d . id }
@@ -82,23 +140,12 @@ export const DomainList = withGate(
82140 } ) }
83141 badge = { < EnrollmentBadge organizationDomain = { d } /> }
84142 trailingComponent = {
85- < ThreeDotsMenu
86- actions = { [
87- {
88- label : localizationKeys (
89- 'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__verify' ,
90- ) ,
91- onClick : ( ) => navigate ( `${ redirectSubPath } ${ d . id } /verify` ) ,
92- } ,
93- {
94- label : localizationKeys (
95- 'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__remove' ,
96- ) ,
97- isDestructive : true ,
98- onClick : ( ) => navigate ( `${ redirectSubPath } ${ d . id } /remove` ) ,
99- } ,
100- ] }
101- />
143+ showDotMenu ? (
144+ < DomainListDotMenu
145+ redirectSubPath = { redirectSubPath }
146+ domainId = { d . id }
147+ />
148+ ) : undefined
102149 }
103150 >
104151 { d . name }
@@ -116,7 +163,7 @@ export const DomainList = withGate(
116163 padding : `${ t . space . $3 } ${ t . space . $4 } ` ,
117164 minHeight : t . sizes . $10 ,
118165 } ) }
119- onClick = { ( ) => navigate ( ` ${ redirectSubPath } ${ d . id } ` ) }
166+ onClick = { ( ) => navigate ( buildDomainListRelativeURL ( redirectSubPath , d . id ) ) }
120167 >
121168 { d . name }
122169 </ ArrowBlockButton >
@@ -154,6 +201,6 @@ export const DomainList = withGate(
154201 ) ;
155202 } ,
156203 {
157- permission : 'org:sys_domains:manage ' ,
204+ permission : 'org:sys_domains:read ' ,
158205 } ,
159206) ;
0 commit comments