File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import { updateSidebarState } from "@/actions/sidebar" ;
44import { useSidebar } from "@/context/sidebar-context" ;
55import { Button } from "@comp/ui/button" ;
6- import { cn } from "@comp/ui/cn" ;
76import { Icons } from "@comp/ui/icons" ;
87import { useAction } from "next-safe-action/hooks" ;
9- import { useRouter } from "next/navigation" ;
108
119interface SidebarCollapseButtonProps {
1210 isCollapsed : boolean ;
@@ -15,19 +13,19 @@ interface SidebarCollapseButtonProps {
1513export 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
You can’t perform that action at this time.
0 commit comments