Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions resources/js/components/guild-preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -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();
Expand Down Expand Up @@ -62,6 +67,8 @@ export function GuildPreferences({ guild }: { guild: Guild }) {
only: ['guilds'],
preserveScroll: true,
onSuccess: () => router.flushAll(),
onHttpException: (response) =>
!toastWhenThrottled(response),
onFinish: () => setProcessing(false),
},
);
Expand All @@ -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 ? (
Expand All @@ -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')}
>
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/GuildControls.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});