From 7e243707700e38d723b461a3bce44f32882f6d0b Mon Sep 17 00:00:00 2001 From: BrickheadJohnny Date: Wed, 23 Oct 2024 10:21:23 +0200 Subject: [PATCH] fix(useReorderRoles): sort roles when mutating the SWR cache --- .../AddAndOrderRoles/hooks/useReorderRoles.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/components/[guild]/AddAndOrderRoles/hooks/useReorderRoles.ts b/src/components/[guild]/AddAndOrderRoles/hooks/useReorderRoles.ts index 71b5570b56..6af81175a5 100644 --- a/src/components/[guild]/AddAndOrderRoles/hooks/useReorderRoles.ts +++ b/src/components/[guild]/AddAndOrderRoles/hooks/useReorderRoles.ts @@ -32,10 +32,15 @@ const useReorderRoles = (onClose) => { ...oldData, // requirements, and rolePlatforms are not returned, so we need to spread older data too // Plus, we don't update all the roles, only the ones that changed, so this also retains those that weren't updated - roles: (oldData?.roles ?? []).map((prevRole) => ({ - ...prevRole, - ...(newRoles ?? []).find((newRole) => newRole.id === prevRole.id), - })), + roles: (oldData?.roles ?? []) + .map((prevRole) => ({ + ...prevRole, + ...(newRoles ?? []).find((newRole) => newRole.id === prevRole.id), + })) + .sort( + (role1, role2) => + (role1.position ?? Infinity) - (role2.position ?? Infinity) + ), }), { revalidate: false,