@@ -4,58 +4,41 @@ import { useState, useEffect } from "react"
44import { Button } from "@/components/ui/button"
55import { Input } from "@/components/ui/input"
66import { Switch } from "@/components/ui/switch"
7- import { Copy , Check , Loader2 } from "lucide-react"
7+ import { Copy , Check } from "lucide-react"
88import { useToast } from "@/components/hooks/use-toast"
99import { SINGLE_TENANT_ORG_DOMAIN } from "@/lib/constants"
10- import { getOrgInviteId , getInviteLinkEnabled , setInviteLinkEnabled } from "@/actions"
10+ import { getOrgInviteId , setInviteLinkEnabled } from "@/actions"
1111import { isServiceError } from "@/lib/utils"
1212
13- export function InviteLinkToggle ( ) {
14- const [ enabled , setEnabled ] = useState ( false )
13+ interface InviteLinkToggleProps {
14+ inviteLinkEnabled : boolean
15+ }
16+
17+ export function InviteLinkToggle ( { inviteLinkEnabled } : InviteLinkToggleProps ) {
18+ const [ enabled , setEnabled ] = useState ( inviteLinkEnabled )
1519 const [ isLoading , setIsLoading ] = useState ( false )
16- const [ isInitializing , setIsInitializing ] = useState ( true )
1720 const [ inviteLink , setInviteLink ] = useState ( "" )
1821 const [ copied , setCopied ] = useState ( false )
1922 const { toast } = useToast ( )
2023
21- // Fetch initial values on component mount
24+ // Fetch invite link when component mounts if enabled
2225 useEffect ( ( ) => {
23- const fetchInitialValues = async ( ) => {
24- try {
25- const enabledResult = await getInviteLinkEnabled ( SINGLE_TENANT_ORG_DOMAIN )
26-
27- if ( isServiceError ( enabledResult ) ) {
28- toast ( {
29- title : "Error" ,
30- description : "Failed to load invite link setting" ,
31- variant : "destructive" ,
32- } )
33- return
34- }
35-
36- setEnabled ( enabledResult )
37-
38- // If enabled, also fetch the invite link
39- if ( enabledResult ) {
26+ const fetchInviteLink = async ( ) => {
27+ if ( inviteLinkEnabled ) {
28+ try {
4029 const inviteIdResult = await getOrgInviteId ( SINGLE_TENANT_ORG_DOMAIN )
4130 if ( ! isServiceError ( inviteIdResult ) ) {
4231 setInviteLink ( `${ window . location . origin } /invite?id=${ inviteIdResult } ` )
4332 }
33+ } catch ( error ) {
34+ setInviteLink ( "" )
35+ console . error ( "Error fetching invite link:" , error )
4436 }
45- } catch ( error ) {
46- console . error ( "Error fetching invite link setting:" , error )
47- toast ( {
48- title : "Error" ,
49- description : "Failed to load invite link setting" ,
50- variant : "destructive" ,
51- } )
52- } finally {
53- setIsInitializing ( false )
5437 }
5538 }
5639
57- fetchInitialValues ( )
58- } , [ toast ] )
40+ fetchInviteLink ( )
41+ } , [ inviteLinkEnabled ] )
5942
6043 const handleToggle = async ( checked : boolean ) => {
6144 setIsLoading ( true )
@@ -124,56 +107,48 @@ export function InviteLinkToggle() {
124107 </ div >
125108 </ div >
126109 < div className = "flex-shrink-0" >
127- { isInitializing ? (
128- < div className = "flex items-center justify-center w-11 h-6" >
129- < Loader2 className = "animate-spin h-4 w-4 text-[var(--muted-foreground)]" />
130- </ div >
131- ) : (
132- < Switch
133- checked = { enabled }
134- onCheckedChange = { handleToggle }
135- disabled = { isLoading }
136- />
137- ) }
110+ < Switch
111+ checked = { enabled }
112+ onCheckedChange = { handleToggle }
113+ disabled = { isLoading }
114+ />
138115 </ div >
139116 </ div >
140117
141- { ! isInitializing && (
142- < div className = { `transition-all duration-300 ease-in-out ${
143- enabled
144- ? 'max-h-96 opacity-100 transform translate-y-0 mt-4'
145- : 'max-h-0 opacity-0 transform -translate-y-2 overflow-hidden'
146- } `} >
147- < div className = "space-y-4 pt-4 border-t border-[var(--border)]" >
148- < div className = "space-y-2" >
149- < div className = "flex gap-2" >
150- < Input
151- value = { inviteLink }
152- readOnly
153- className = "flex-1 bg-[var(--muted)] border-[var(--border)] text-[var(--foreground)]"
154- />
155- < Button
156- onClick = { handleCopy }
157- variant = "outline"
158- size = "icon"
159- className = "shrink-0 border-[var(--border)] hover:bg-[var(--muted)]"
160- disabled = { ! inviteLink }
161- >
162- { copied ? (
163- < Check className = "h-4 w-4 text-[var(--chart-2)]" />
164- ) : (
165- < Copy className = "h-4 w-4" />
166- ) }
167- </ Button >
168- </ div >
118+ < div className = { `transition-all duration-300 ease-in-out ${
119+ enabled
120+ ? 'max-h-96 opacity-100 transform translate-y-0 mt-4'
121+ : 'max-h-0 opacity-0 transform -translate-y-2 overflow-hidden'
122+ } `} >
123+ < div className = "space-y-4 pt-4 border-t border-[var(--border)]" >
124+ < div className = "space-y-2" >
125+ < div className = "flex gap-2" >
126+ < Input
127+ value = { inviteLink }
128+ readOnly
129+ className = "flex-1 bg-[var(--muted)] border-[var(--border)] text-[var(--foreground)]"
130+ />
131+ < Button
132+ onClick = { handleCopy }
133+ variant = "outline"
134+ size = "icon"
135+ className = "shrink-0 border-[var(--border)] hover:bg-[var(--muted)]"
136+ disabled = { ! inviteLink }
137+ >
138+ { copied ? (
139+ < Check className = "h-4 w-4 text-[var(--chart-2)]" />
140+ ) : (
141+ < Copy className = "h-4 w-4" />
142+ ) }
143+ </ Button >
169144 </ div >
170-
171- < p className = "text-sm text-[var(--muted-foreground)]" >
172- You can find this link again in the < strong > Settings → Members</ strong > page.
173- </ p >
174145 </ div >
146+
147+ < p className = "text-sm text-[var(--muted-foreground)]" >
148+ You can find this link again in the < strong > Settings → Members</ strong > page.
149+ </ p >
175150 </ div >
176- ) }
151+ </ div >
177152 </ div >
178153 )
179154}
0 commit comments