From e070523e94c89c1eb50e231ec165e9301db04d27 Mon Sep 17 00:00:00 2001 From: Rohit Date: Mon, 19 Oct 2026 15:17:49 +0530 Subject: [PATCH] fix: follow several people in a row without cancelling the earlier follows Follow buttons were plain Inertia visits, and Inertia cancels the visit in flight when the next one starts. Following a second person before the first answered left the first button on "Follow". Follows are now async visits, and each person's follow waits for the one before it to finish. Fixes #559 --- resources/js/components/follow-button.tsx | 15 +++++++ tests/Unit/FollowState.test.tsx | 49 +++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/resources/js/components/follow-button.tsx b/resources/js/components/follow-button.tsx index 56f03748a7..0c65dd9125 100644 --- a/resources/js/components/follow-button.tsx +++ b/resources/js/components/follow-button.tsx @@ -37,6 +37,9 @@ export function useIsFollowing( return useSharedState(isFollowingKey(user), isFollowing); } +const isPendingKey = (user: Pick): string => + `follow_pending:${user.id}`; + const hasBlockKey = (user: Pick): string => `has_block:${user.id}`; /** @@ -102,6 +105,9 @@ export function FollowLabel({ ); } +/** + * Follows are async visits, so following several people in a row never cancels the follows still on their way. Each person's follow waits for the one before it to answer, so a double click can't follow and then unfollow. + */ export function FollowButton({ user, isFollowing: initialIsFollowing, @@ -121,6 +127,10 @@ export function FollowButton({ user, initialIsFollowing, ); + const [isPending, setIsPending] = useSharedState( + isPendingKey(user), + false, + ); const { isJustToggled, markToggled, onMouseLeave } = useFollowHover(); if (auth.user === null) { @@ -152,6 +162,11 @@ export function FollowButton({ } as="button" preserveScroll + async + onBefore={() => !isPending} + onStart={() => setIsPending(true)} + onFinish={() => setIsPending(false)} + aria-disabled={isPending} onHttpException={(response) => onFollowHttpException(user, response) } diff --git a/tests/Unit/FollowState.test.tsx b/tests/Unit/FollowState.test.tsx index 11a56864ed..f8be4a131a 100644 --- a/tests/Unit/FollowState.test.tsx +++ b/tests/Unit/FollowState.test.tsx @@ -332,3 +332,52 @@ test('a preview unfollow that fails after a block keeps who to follow unfollowed assert.equal(suggestionLabel(user), 'Follow @raced'); }); + +function suggestionFollowLink(user: SuggestedUser): Props { + const button = find( + SuggestedUserRow({ user }), + (props) => props['data-test'] === 'suggested-follow-button', + ); + assert.ok(button); + + const link = find(render(button), (props) => 'aria-label' in props); + assert.ok(link); + + return link.props; +} + +test('following several suggestions in a row sends every follow without cancelling the others', () => { + const first = suggestion('pauline'); + const second = suggestion('brent'); + + const firstFollow = suggestionFollowLink(first); + (firstFollow.onStart as () => void)(); + const secondFollow = suggestionFollowLink(second); + + assert.equal(firstFollow.async, true); + assert.equal(secondFollow.async, true); + assert.equal((secondFollow.onBefore as () => boolean)(), true); + + (secondFollow.onStart as () => void)(); + for (const follow of [secondFollow, firstFollow]) { + (follow.onSuccess as () => void)(); + (follow.onFinish as () => void)(); + } + + assert.equal(suggestionLabel(first), 'Unfollow @pauline'); + assert.equal(suggestionLabel(second), 'Unfollow @brent'); +}); + +test('a second click on a follow still on its way sends nothing', () => { + const user = suggestion('matthieu'); + + (suggestionFollowLink(user).onStart as () => void)(); + + const pending = suggestionFollowLink(user); + assert.equal(pending['aria-disabled'], true); + assert.equal((pending.onBefore as () => boolean)(), false); + + (pending.onFinish as () => void)(); + + assert.equal((suggestionFollowLink(user).onBefore as () => boolean)(), true); +});