@@ -45,6 +45,13 @@ import {
4545 PopoverContent ,
4646 PopoverTrigger ,
4747} from "@/components/ui/popover" ;
48+ import {
49+ Select ,
50+ SelectContent ,
51+ SelectItem ,
52+ SelectTrigger ,
53+ SelectValue ,
54+ } from "@/components/ui/select" ;
4855import { Switch } from "@/components/ui/switch" ;
4956import { api } from "@/utils/api" ;
5057
@@ -780,6 +787,78 @@ function HandleCustomRole({
780787 ) ;
781788}
782789
790+ const DefaultRoleSection = ( { customRoles } : { customRoles : string [ ] } ) => {
791+ const utils = api . useUtils ( ) ;
792+ const { data : auth } = api . user . get . useQuery ( ) ;
793+ const { data : activeOrganization } = api . organization . active . useQuery ( ) ;
794+ const { mutateAsync : updateOrganization , isPending : isUpdating } =
795+ api . organization . update . useMutation ( ) ;
796+ const [ selectedRole , setSelectedRole ] = useState < string > ( ) ;
797+
798+ if ( auth ?. role !== "owner" || ! activeOrganization ) {
799+ return null ;
800+ }
801+
802+ const currentRole = activeOrganization . defaultRole ?? "member" ;
803+ const value = selectedRole ?? currentRole ;
804+
805+ const onSave = async ( ) => {
806+ await updateOrganization ( {
807+ organizationId : activeOrganization . id ,
808+ name : activeOrganization . name ,
809+ logo : activeOrganization . logo ?? undefined ,
810+ defaultRole : value ,
811+ } )
812+ . then ( ( ) => {
813+ toast . success ( "Default role updated" ) ;
814+ utils . organization . active . invalidate ( ) ;
815+ } )
816+ . catch ( ( error ) => {
817+ toast . error (
818+ error instanceof Error
819+ ? error . message
820+ : "Error updating default role" ,
821+ ) ;
822+ } ) ;
823+ } ;
824+
825+ return (
826+ < div className = "flex flex-col sm:flex-row sm:items-center justify-between gap-3 rounded-lg border bg-muted/20 p-4" >
827+ < div className = "space-y-0.5" >
828+ < p className = "text-sm font-medium" > Default role for new members</ p >
829+ < p className = "text-xs text-muted-foreground" >
830+ Assigned automatically to users joining through SSO and preselected
831+ when creating invitations.
832+ </ p >
833+ </ div >
834+ < div className = "flex items-center gap-2" >
835+ < Select value = { value } onValueChange = { setSelectedRole } >
836+ < SelectTrigger className = "w-40" >
837+ < SelectValue placeholder = "Select a role" />
838+ </ SelectTrigger >
839+ < SelectContent >
840+ < SelectItem value = "member" > Member</ SelectItem >
841+ < SelectItem value = "admin" > Admin</ SelectItem >
842+ { customRoles . map ( ( role ) => (
843+ < SelectItem key = { role } value = { role } >
844+ { role }
845+ </ SelectItem >
846+ ) ) }
847+ </ SelectContent >
848+ </ Select >
849+ < Button
850+ size = "sm"
851+ isLoading = { isUpdating }
852+ disabled = { value === currentRole }
853+ onClick = { onSave }
854+ >
855+ Save
856+ </ Button >
857+ </ div >
858+ </ div >
859+ ) ;
860+ } ;
861+
783862const CustomRolesContent = ( ) => {
784863 const {
785864 data : customRoles ,
@@ -821,6 +900,9 @@ const CustomRolesContent = () => {
821900
822901 return (
823902 < div className = "space-y-4" >
903+ < DefaultRoleSection
904+ customRoles = { customRoles ?. map ( ( role ) => role . role ) ?? [ ] }
905+ />
824906 < div className = "flex justify-end" >
825907 < HandleCustomRole onSuccess = { refetch } />
826908 </ div >
0 commit comments