diff --git a/resources/js/components/guild-preferences.tsx b/resources/js/components/guild-preferences.tsx index 59b39494bb..96606ca3f3 100644 --- a/resources/js/components/guild-preferences.tsx +++ b/resources/js/components/guild-preferences.tsx @@ -9,6 +9,7 @@ import { update as mute } from '@/actions/App/Http/Controllers/GuildMuteControll import { useFollowHover } from '@/components/follow-button'; import { Button } from '@/components/ui/button'; import { formatCount, formatExactCount } from '@/lib/numbers'; +import { toastWhenThrottled } from '@/lib/throttle'; import type { Guild } from '@/types/guild'; export function GuildMemberCount({ guild }: { guild: Guild }) { @@ -35,6 +36,10 @@ export function GuildPreferences({ guild }: { guild: Guild }) { const { isJustToggled, markToggled, onMouseLeave } = useFollowHover(); function update(kind: 'joined' | 'muted') { + if (processing) { + return; + } + const key = kind === 'joined' ? 'is_joined' : 'is_muted'; const value = !guild[key]; markToggled(); @@ -62,6 +67,8 @@ export function GuildPreferences({ guild }: { guild: Guild }) { only: ['guilds'], preserveScroll: true, onSuccess: () => router.flushAll(), + onHttpException: (response) => + !toastWhenThrottled(response), onFinish: () => setProcessing(false), }, ); @@ -82,7 +89,7 @@ export function GuildPreferences({ guild }: { guild: Guild }) { ? 'Unmute this guild topic' : 'Mute this guild topic in logs and search' } - disabled={processing} + aria-disabled={processing} onClick={() => update('muted')} > {guild.is_muted ? ( @@ -97,7 +104,7 @@ export function GuildPreferences({ guild }: { guild: Guild }) { className="group min-h-9 min-w-[5.5rem]" aria-label={`${guild.is_joined ? 'Leave' : 'Join'} ${guild.name}`} aria-pressed={guild.is_joined} - disabled={processing} + aria-disabled={processing} onMouseLeave={onMouseLeave} onClick={() => update('joined')} > diff --git a/tests/Unit/GuildControls.test.tsx b/tests/Unit/GuildControls.test.tsx index ecfac9df70..050934b043 100644 --- a/tests/Unit/GuildControls.test.tsx +++ b/tests/Unit/GuildControls.test.tsx @@ -299,3 +299,53 @@ test('keeps the mute slot still but hides it from joined users and keyboard navi ); } }); + +test('keeps busy guild buttons focusable and ignores clicks until their request ends', () => { + const optimistic = vi.spyOn(router, 'optimistic').mockReturnValue(router); + const put = vi.spyOn(router, 'put').mockImplementation(() => {}); + const guild = { + id: 'rust-id', + name: 'Rust', + is_joined: false, + is_muted: false, + } as Guild; + const button = (label: string) => { + cursor = 0; + + return elements(GuildPreferences({ guild })).find( + (element) => element.props['aria-label'] === label, + )!; + }; + + (button('Join Rust').props.onClick as () => void)(); + + for (const label of ['Join Rust', 'Mute Rust']) { + assert.equal(button(label).props['aria-disabled'], true); + assert.equal(button(label).props.disabled, undefined); + } + + (button('Join Rust').props.onClick as () => void)(); + (button('Mute Rust').props.onClick as () => void)(); + assert.equal(optimistic.mock.calls.length, 1); + assert.equal(put.mock.calls.length, 1); + + put.mock.calls[0][2]?.onFinish?.({} as never); + assert.equal(button('Join Rust').props['aria-disabled'], false); +}); + +test('toasts instead of showing the error page when joining a guild is throttled', () => { + vi.spyOn(router, 'optimistic').mockReturnValue(router); + const put = vi.spyOn(router, 'put').mockImplementation(() => {}); + cursor = 0; + const join = elements( + GuildPreferences({ + guild: { id: 'rust-id', name: 'Rust' } as Guild, + }), + ).find((element) => element.props['aria-label'] === 'Join Rust')!; + + (join.props.onClick as () => void)(); + const options = put.mock.calls[0][2]; + + assert.equal(options?.onHttpException?.({ status: 429 } as never), false); + assert.equal(options?.onHttpException?.({ status: 500 } as never), true); +});