@@ -8,16 +8,12 @@ import { Button } from "@/shared/ui/button";
88import {
99 DropdownMenu ,
1010 DropdownMenuContent ,
11- DropdownMenuLabel ,
1211 DropdownMenuRadioGroup ,
1312 DropdownMenuRadioItem ,
14- DropdownMenuSeparator ,
1513 DropdownMenuTrigger ,
1614} from "@/shared/ui/dropdown-menu" ;
17- import { Input } from "@/shared/ui/input" ;
1815import { Separator } from "@/shared/ui/separator" ;
1916import { Spinner } from "@/shared/ui/spinner" ;
20- import { Switch } from "@/shared/ui/switch" ;
2117
2218const TTL_OPTIONS : { label : string ; value : number } [ ] = [
2319 { label : "1 day" , value : 24 * 60 * 60 } ,
@@ -26,6 +22,15 @@ const TTL_OPTIONS: { label: string; value: number }[] = [
2622 { label : "30 days" , value : 30 * 24 * 60 * 60 } ,
2723] ;
2824
25+ const MAX_USE_OPTIONS : { label : string ; value : number | null } [ ] = [
26+ { label : "No limit" , value : null } ,
27+ { label : "1 use" , value : 1 } ,
28+ { label : "3 uses" , value : 3 } ,
29+ { label : "5 uses" , value : 5 } ,
30+ { label : "10 uses" , value : 10 } ,
31+ { label : "25 uses" , value : 25 } ,
32+ ] ;
33+
2934export const DEFAULT_INVITE_TTL_SECS = TTL_OPTIONS [ 1 ] . value ;
3035
3136type CopyStatus = "idle" | "copying" | "copied" ;
@@ -45,16 +50,12 @@ export function InviteLinkSection({
4550 ttlSecs : number ;
4651} ) {
4752 const [ copyStatus , setCopyStatus ] = React . useState < CopyStatus > ( "idle" ) ;
48- const [ maxUsesEnabled , setMaxUsesEnabled ] = React . useState ( true ) ;
49- const [ maxUsesInput , setMaxUsesInput ] = React . useState ( "3" ) ;
50- const parsedMaxUses = Number ( maxUsesInput ) ;
51- const maxUsesValid =
52- ! maxUsesEnabled ||
53- ( Number . isInteger ( parsedMaxUses ) &&
54- parsedMaxUses >= 1 &&
55- parsedMaxUses <= 10000 ) ;
53+ const [ maxUses , setMaxUses ] = React . useState < number | null > ( null ) ;
5654 const ttlLabel =
5755 TTL_OPTIONS . find ( ( option ) => option . value === ttlSecs ) ?. label ?? "3 days" ;
56+ const maxUsesLabel =
57+ MAX_USE_OPTIONS . find ( ( option ) => option . value === maxUses ) ?. label ??
58+ "No limit" ;
5859 const copyLabel =
5960 copyStatus === "copying"
6061 ? "Copying…"
@@ -69,13 +70,10 @@ export function InviteLinkSection({
6970 } , [ copyStatus ] ) ;
7071
7172 async function handleCopy ( ) {
72- if ( copyStatus === "copying" || ! maxUsesValid ) return ;
73+ if ( copyStatus === "copying" ) return ;
7374 setCopyStatus ( "copying" ) ;
7475 try {
75- const invite = await mintInvite ( {
76- ttlSecs,
77- maxUses : maxUsesEnabled ? parsedMaxUses : null ,
78- } ) ;
76+ const invite = await mintInvite ( { ttlSecs, maxUses } ) ;
7977 await writeTextToClipboard ( invite . url ) ;
8078 setCopyStatus ( "copied" ) ;
8179 toast . success ( "Invite link copied" ) ;
@@ -87,90 +85,87 @@ export function InviteLinkSection({
8785
8886 return (
8987 < section className = "pt-2" data-testid = "community-invite-link-section" >
90- < div className = "flex items-center gap-3" >
91- < span className = "flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-muted text-muted-foreground" >
92- < Link2 aria-hidden = "true" className = "h-4 w-4" />
93- </ span >
94- < div className = "min-w-0 flex-1" >
95- < h3 className = "text-sm font-medium" > Share with a link</ h3 >
96- < p className = "text-xs text-secondary-foreground/75" >
97- Anyone with the link can join this community.
98- </ p >
88+ < div className = "space-y-5" >
89+ < div className = "flex items-center justify-between gap-4" >
90+ < span className = "text-sm font-medium" > Expires after</ span >
91+ < DropdownMenu >
92+ < DropdownMenuTrigger asChild >
93+ < Button
94+ aria-label = "Choose invite expiry"
95+ className = "h-8 shrink-0 gap-1.5 px-2 text-sm text-muted-foreground"
96+ data-testid = "invite-link-ttl-trigger"
97+ disabled = { copyStatus === "copying" }
98+ size = "sm"
99+ type = "button"
100+ variant = "ghost"
101+ >
102+ { ttlLabel }
103+ < ChevronDown aria-hidden = "true" className = "h-3.5 w-3.5" />
104+ </ Button >
105+ </ DropdownMenuTrigger >
106+ < DropdownMenuContent align = "end" className = "w-40" >
107+ < DropdownMenuRadioGroup
108+ onValueChange = { ( value ) => onTtlSecsChange ( Number ( value ) ) }
109+ value = { String ( ttlSecs ) }
110+ >
111+ { TTL_OPTIONS . map ( ( option ) => (
112+ < DropdownMenuRadioItem
113+ data-testid = { `invite-link-ttl-${ option . value } ` }
114+ key = { option . value }
115+ value = { String ( option . value ) }
116+ >
117+ { option . label }
118+ </ DropdownMenuRadioItem >
119+ ) ) }
120+ </ DropdownMenuRadioGroup >
121+ </ DropdownMenuContent >
122+ </ DropdownMenu >
123+ </ div >
124+ < div className = "flex items-center justify-between gap-4" >
125+ < span className = "text-sm font-medium" > Limit number of uses</ span >
126+ < DropdownMenu >
127+ < DropdownMenuTrigger asChild >
128+ < Button
129+ aria-label = "Choose maximum invite uses"
130+ className = "h-8 shrink-0 gap-1.5 px-2 text-sm text-muted-foreground"
131+ data-testid = "invite-link-max-uses-trigger"
132+ disabled = { copyStatus === "copying" }
133+ size = "sm"
134+ type = "button"
135+ variant = "ghost"
136+ >
137+ { maxUsesLabel }
138+ < ChevronDown aria-hidden = "true" className = "h-3.5 w-3.5" />
139+ </ Button >
140+ </ DropdownMenuTrigger >
141+ < DropdownMenuContent align = "end" className = "w-40" >
142+ < DropdownMenuRadioGroup
143+ onValueChange = { ( value ) =>
144+ setMaxUses ( value === "no-limit" ? null : Number ( value ) )
145+ }
146+ value = { String ( maxUses ?? "no-limit" ) }
147+ >
148+ { MAX_USE_OPTIONS . map ( ( option ) => (
149+ < DropdownMenuRadioItem
150+ data-testid = { `invite-link-max-uses-${ option . value ?? "no-limit" } ` }
151+ key = { option . value ?? "no-limit" }
152+ value = { String ( option . value ?? "no-limit" ) }
153+ >
154+ { option . label }
155+ </ DropdownMenuRadioItem >
156+ ) ) }
157+ </ DropdownMenuRadioGroup >
158+ </ DropdownMenuContent >
159+ </ DropdownMenu >
99160 </ div >
100- < DropdownMenu >
101- < DropdownMenuTrigger asChild >
102- < Button
103- aria-label = "Choose invite expiry"
104- className = "h-8 shrink-0 gap-1.5 px-2 text-muted-foreground"
105- data-testid = "invite-link-ttl-trigger"
106- disabled = { copyStatus === "copying" }
107- size = "sm"
108- type = "button"
109- variant = "ghost"
110- >
111- { ttlLabel }
112- < ChevronDown aria-hidden = "true" className = "h-3.5 w-3.5" />
113- </ Button >
114- </ DropdownMenuTrigger >
115- < DropdownMenuContent align = "end" className = "w-40" >
116- < DropdownMenuLabel > Expires after</ DropdownMenuLabel >
117- < DropdownMenuSeparator />
118- < DropdownMenuRadioGroup
119- onValueChange = { ( value ) => onTtlSecsChange ( Number ( value ) ) }
120- value = { String ( ttlSecs ) }
121- >
122- { TTL_OPTIONS . map ( ( option ) => (
123- < DropdownMenuRadioItem
124- data-testid = { `invite-link-ttl-${ option . value } ` }
125- key = { option . value }
126- value = { String ( option . value ) }
127- >
128- { option . label }
129- </ DropdownMenuRadioItem >
130- ) ) }
131- </ DropdownMenuRadioGroup >
132- </ DropdownMenuContent >
133- </ DropdownMenu >
134- </ div >
135- < div className = "mt-3 flex items-center gap-2 text-xs" >
136- < Switch
137- checked = { maxUsesEnabled }
138- data-testid = "invite-link-max-uses-switch"
139- id = "invite-max-uses"
140- onCheckedChange = { setMaxUsesEnabled }
141- />
142- < label className = "text-muted-foreground" htmlFor = "invite-max-uses" >
143- Limit uses
144- </ label >
145- { maxUsesEnabled ? (
146- < Input
147- className = "h-7 w-20"
148- data-testid = "invite-link-max-uses-input"
149- inputMode = "numeric"
150- max = { 10000 }
151- min = { 1 }
152- onChange = { ( event ) => setMaxUsesInput ( event . target . value ) }
153- placeholder = "3"
154- type = "number"
155- value = { maxUsesInput }
156- />
157- ) : null }
158- { maxUsesEnabled && ! maxUsesValid ? (
159- < span
160- className = "text-destructive"
161- data-testid = "invite-link-max-uses-error"
162- >
163- Enter a whole number from 1 to 10,000
164- </ span >
165- ) : null }
166161 </ div >
167162 < Separator className = "my-4 bg-input/40" />
168163 < div className = "flex justify-end" >
169164 < Button
170165 className = "shrink-0 border-border shadow-none"
171166 data-copy-status = { copyStatus }
172167 data-testid = "copy-invite-link"
173- disabled = { copyStatus === "copying" || ! maxUsesValid }
168+ disabled = { copyStatus === "copying" }
174169 onClick = { ( ) => void handleCopy ( ) }
175170 size = "sm"
176171 type = "button"
0 commit comments