Skip to content

Commit bf6298d

Browse files
committed
fix(SidebarCollapseButton): handle optimistic update rollback on error
- Revert sidebar collapse state if the server action fails - Update comments for clarity on background state updates
1 parent 05daca6 commit bf6298d

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

apps/app/src/components/sidebar-collapse-button.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import { updateSidebarState } from "@/actions/sidebar";
44
import { useSidebar } from "@/context/sidebar-context";
55
import { Button } from "@comp/ui/button";
6-
import { cn } from "@comp/ui/cn";
76
import { Icons } from "@comp/ui/icons";
87
import { useAction } from "next-safe-action/hooks";
9-
import { useRouter } from "next/navigation";
108

119
interface SidebarCollapseButtonProps {
1210
isCollapsed: boolean;
@@ -15,19 +13,19 @@ interface SidebarCollapseButtonProps {
1513
export function SidebarCollapseButton({
1614
isCollapsed,
1715
}: SidebarCollapseButtonProps) {
18-
const router = useRouter();
1916
const { setIsCollapsed } = useSidebar();
2017

2118
const { execute } = useAction(updateSidebarState, {
22-
onSuccess: () => {
23-
router.refresh();
19+
onError: () => {
20+
// Revert the optimistic update if the server action fails
21+
setIsCollapsed(isCollapsed);
2422
},
2523
});
2624

2725
const handleToggle = () => {
2826
// Update local state immediately for responsive UI
2927
setIsCollapsed(!isCollapsed);
30-
// Update server state (cookie)
28+
// Update server state (cookie) in the background
3129
execute({ isCollapsed: !isCollapsed });
3230
};
3331

0 commit comments

Comments
 (0)