@@ -35,6 +35,7 @@ const MAX_USE_OPTIONS: { label: string; value: number | null }[] = [
3535export const DEFAULT_INVITE_TTL_SECS = TTL_OPTIONS [ 1 ] . value ;
3636
3737type CopyStatus = "idle" | "copying" | "copied" ;
38+ type GenerationStatus = "idle" | "generating" | "failed" ;
3839
3940/**
4041 * Share-with-link footer for the community invite dialog.
@@ -51,18 +52,30 @@ export function InviteLinkSection({
5152 ttlSecs : number ;
5253} ) {
5354 const [ copyStatus , setCopyStatus ] = React . useState < CopyStatus > ( "idle" ) ;
55+ const [ generationStatus , setGenerationStatus ] =
56+ React . useState < GenerationStatus > ( "generating" ) ;
5457 const [ inviteUrl , setInviteUrl ] = React . useState ( "" ) ;
55- const [ isGenerating , setIsGenerating ] = React . useState ( true ) ;
5658 const [ maxUses , setMaxUses ] = React . useState < number | null > ( null ) ;
59+ const generationRequestId = React . useRef ( 0 ) ;
5760 const shouldReduceMotion = useReducedMotion ( ) ;
5861 const ttlLabel =
5962 TTL_OPTIONS . find ( ( option ) => option . value === ttlSecs ) ?. label ?? "3 days" ;
6063 const maxUsesLabel =
6164 MAX_USE_OPTIONS . find ( ( option ) => option . value === maxUses ) ?. label ??
6265 "No limit" ;
63- const copyLabel = copyStatus === "copied" ? "Copied" : "Copy link" ;
64- const isCopying = isGenerating || copyStatus === "copying" ;
65- const copyButtonWidth = copyStatus === "copied" ? "5.25rem" : "4.5rem" ;
66+ const isGenerating = generationStatus === "generating" ;
67+ const hasGenerationFailed = generationStatus === "failed" ;
68+ const isWorking = isGenerating || copyStatus === "copying" ;
69+ const copyLabel = hasGenerationFailed
70+ ? "Retry"
71+ : copyStatus === "copied"
72+ ? "Copied"
73+ : "Copy link" ;
74+ const copyButtonWidth = isWorking
75+ ? "6.25rem"
76+ : copyStatus === "copied"
77+ ? "5.25rem"
78+ : "4.5rem" ;
6679 const copyButtonTransition = shouldReduceMotion
6780 ? { duration : 0 }
6881 : { duration : 0.12 , ease : [ 0.77 , 0 , 0.175 , 1 ] as const } ;
@@ -73,30 +86,37 @@ export function InviteLinkSection({
7386 return ( ) => window . clearTimeout ( resetTimer ) ;
7487 } , [ copyStatus ] ) ;
7588
76- React . useEffect ( ( ) => {
77- let isCurrent = true ;
78-
79- async function generateInviteLink ( ) {
80- setIsGenerating ( true ) ;
81- setInviteUrl ( " ") ;
82- setCopyStatus ( "idle" ) ;
83- try {
84- const invite = await mintInvite ( { ttlSecs , maxUses } ) ;
85- if ( isCurrent ) setInviteUrl ( invite . url ) ;
86- } catch {
87- if ( isCurrent ) {
88- toast . error ( "Couldn’t create an invite link. Try again." ) ;
89- }
90- } finally {
91- if ( isCurrent ) setIsGenerating ( false ) ;
89+ const generateInviteLink = React . useCallback ( async ( ) => {
90+ const requestId = generationRequestId . current + 1 ;
91+ generationRequestId . current = requestId ;
92+ setGenerationStatus ( "generating" ) ;
93+ setInviteUrl ( "" ) ;
94+ setCopyStatus ( "idle ") ;
95+ try {
96+ const invite = await mintInvite ( { ttlSecs , maxUses } ) ;
97+ if ( generationRequestId . current === requestId ) {
98+ setInviteUrl ( invite . url ) ;
99+ setGenerationStatus ( "idle" ) ;
100+ }
101+ } catch {
102+ if ( generationRequestId . current === requestId ) {
103+ setGenerationStatus ( "failed" ) ;
104+ toast . error ( "Couldn’t create an invite link." ) ;
92105 }
93106 }
107+ } , [ maxUses , ttlSecs ] ) ;
94108
109+ React . useEffect ( ( ) => {
95110 void generateInviteLink ( ) ;
96111 return ( ) => {
97- isCurrent = false ;
112+ generationRequestId . current += 1 ;
98113 } ;
99- } , [ maxUses , ttlSecs ] ) ;
114+ } , [ generateInviteLink ] ) ;
115+
116+ function retryInviteGeneration ( ) {
117+ if ( ! hasGenerationFailed ) return ;
118+ void generateInviteLink ( ) ;
119+ }
100120
101121 async function handleCopy ( ) {
102122 if ( ! inviteUrl || isGenerating || copyStatus === "copying" ) return ;
@@ -119,7 +139,11 @@ export function InviteLinkSection({
119139 className = "h-11 pr-28 text-transparent caret-transparent selection:bg-transparent"
120140 data-testid = "invite-link-url"
121141 disabled = { isGenerating }
122- placeholder = "Creating invite link…"
142+ placeholder = {
143+ hasGenerationFailed
144+ ? "Couldn’t create invite link"
145+ : "Creating invite link…"
146+ }
123147 readOnly
124148 value = { inviteUrl }
125149 />
@@ -142,12 +166,17 @@ export function InviteLinkSection({
142166 className = "h-9 w-full px-3"
143167 data-copy-status = { copyStatus }
144168 data-testid = "copy-invite-link"
145- disabled = { isGenerating || ! inviteUrl || copyStatus === "copying" }
146- onClick = { ( ) => void handleCopy ( ) }
169+ disabled = {
170+ ! hasGenerationFailed &&
171+ ( isGenerating || ! inviteUrl || copyStatus === "copying" )
172+ }
173+ onClick = { ( ) =>
174+ hasGenerationFailed ? retryInviteGeneration ( ) : void handleCopy ( )
175+ }
147176 size = "sm"
148177 type = "button"
149178 >
150- { isCopying ? (
179+ { isWorking ? (
151180 < Spinner aria-hidden = "true" className = "h-4 w-4 border-2" />
152181 ) : copyStatus === "copied" ? (
153182 < Check aria-hidden = "true" className = "h-4 w-4" />
0 commit comments