1- import type { GetDomainsParams , OrganizationEnrollmentMode } from '@clerk/types' ;
1+ import type { GetDomainsParams , OrganizationDomainResource , OrganizationEnrollmentMode } from '@clerk/types' ;
22import type { OrganizationDomainVerificationStatus } from '@clerk/types' ;
33import React , { useMemo } from 'react' ;
44
5- import { withGate } from '../../common' ;
5+ import { stripOrigin , toURL , trimLeadingSlash } from '../../../utils' ;
6+ import { useGate , withGate } from '../../common' ;
67import { useCoreOrganization } from '../../contexts' ;
8+ import type { LocalizationKey } from '../../customizables' ;
79import { Box , Col , localizationKeys , Spinner } from '../../customizables' ;
810import { ArrowBlockButton , BlockWithTrailingComponent , ThreeDotsMenu } from '../../elements' ;
911import { useInView } from '../../hooks' ;
@@ -17,10 +19,61 @@ type DomainListProps = GetDomainsParams & {
1719 * Enables internal links to navigate to the correct page
1820 * based on when this component is used
1921 */
20- redirectSubPath : string ;
22+ redirectSubPath : 'organization-settings/domain' | 'domain' ;
2123 fallback ?: React . ReactNode ;
2224} ;
2325
26+ const useDomainList = ( ) => {
27+ const { isAuthorizedUser : canDeleteDomain } = useGate ( { permission : 'org:sys_domains:delete' } ) ;
28+ const { isAuthorizedUser : canVerifyDomain } = useGate ( { permission : 'org:sys_domains:manage' } ) ;
29+
30+ return {
31+ showDotMenu : canDeleteDomain || canVerifyDomain ,
32+ canVerifyDomain,
33+ canDeleteDomain,
34+ } ;
35+ } ;
36+
37+ const buildDomainListRelativeURL = ( parentPath : string , domainId : string , mode ?: 'verify' | 'remove' ) =>
38+ trimLeadingSlash ( stripOrigin ( toURL ( `${ parentPath } /${ domainId } /${ mode || '' } ` ) ) ) ;
39+
40+ const useMenuActions = (
41+ parentPath : string ,
42+ domainId : string ,
43+ ) : { label : LocalizationKey ; onClick : ( ) => Promise < unknown > ; isDestructive ?: boolean } [ ] => {
44+ const { canDeleteDomain, canVerifyDomain } = useDomainList ( ) ;
45+ const { navigate } = useRouter ( ) ;
46+
47+ const menuActions = [ ] ;
48+
49+ if ( canVerifyDomain ) {
50+ menuActions . push ( {
51+ label : localizationKeys ( 'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__verify' ) ,
52+ onClick : ( ) => navigate ( buildDomainListRelativeURL ( parentPath , domainId , 'verify' ) ) ,
53+ } ) ;
54+ }
55+
56+ if ( canDeleteDomain ) {
57+ menuActions . push ( {
58+ label : localizationKeys ( 'organizationProfile.profilePage.domainSection.unverifiedDomain_menuAction__remove' ) ,
59+ isDestructive : true ,
60+ onClick : ( ) => navigate ( buildDomainListRelativeURL ( parentPath , domainId , 'remove' ) ) ,
61+ } ) ;
62+ }
63+
64+ return menuActions ;
65+ } ;
66+
67+ const DomainListDotMenu = ( {
68+ redirectSubPath,
69+ domainId,
70+ } : Pick < DomainListProps , 'redirectSubPath' > & {
71+ domainId : OrganizationDomainResource [ 'id' ] ;
72+ } ) => {
73+ const actions = useMenuActions ( redirectSubPath , domainId ) ;
74+ return < ThreeDotsMenu actions = { actions } /> ;
75+ } ;
76+
2477export const DomainList = withGate (
2578 ( props : DomainListProps ) => {
2679 const { verificationStatus, enrollmentMode, redirectSubPath, fallback, ...rest } = props ;
@@ -31,6 +84,7 @@ export const DomainList = withGate(
3184 } ,
3285 } ) ;
3386
87+ const { showDotMenu } = useDomainList ( ) ;
3488 const { ref } = useInView ( {
3589 threshold : 0 ,
3690 onChange : inView => {
@@ -69,7 +123,7 @@ export const DomainList = withGate(
69123 < Col >
70124 { domainList . length === 0 && ! domains ?. isLoading && fallback }
71125 { domainList . map ( d => {
72- if ( ! ( d . verification && d . verification . status === 'verified' ) ) {
126+ if ( ! ( d . verification && d . verification . status === 'verified' ) || ! showDotMenu ) {
73127 return (
74128 < BlockWithTrailingComponent
75129 key = { d . id }
@@ -82,23 +136,12 @@ export const DomainList = withGate(
82136 } ) }
83137 badge = { < EnrollmentBadge organizationDomain = { d } /> }
84138 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- />
139+ showDotMenu ? (
140+ < DomainListDotMenu
141+ redirectSubPath = { redirectSubPath }
142+ domainId = { d . id }
143+ />
144+ ) : undefined
102145 }
103146 >
104147 { d . name }
@@ -116,7 +159,7 @@ export const DomainList = withGate(
116159 padding : `${ t . space . $3 } ${ t . space . $4 } ` ,
117160 minHeight : t . sizes . $10 ,
118161 } ) }
119- onClick = { ( ) => navigate ( ` ${ redirectSubPath } ${ d . id } ` ) }
162+ onClick = { ( ) => navigate ( buildDomainListRelativeURL ( redirectSubPath , d . id ) ) }
120163 >
121164 { d . name }
122165 </ ArrowBlockButton >
@@ -154,6 +197,6 @@ export const DomainList = withGate(
154197 ) ;
155198 } ,
156199 {
157- permission : 'org:sys_domains:manage ' ,
200+ permission : 'org:sys_domains:read ' ,
158201 } ,
159202) ;
0 commit comments