File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,9 +4,8 @@ import { updateSidebarState } from "@/actions/sidebar";
44import { useSidebar } from "@/context/sidebar-context" ;
55import { Button } from "@comp/ui/button" ;
66import { cn } from "@comp/ui/cn" ;
7- import { ArrowLeftFromLine , ChevronLeft } from "lucide-react" ;
7+ import { ArrowLeftFromLine } from "lucide-react" ;
88import { useAction } from "next-safe-action/hooks" ;
9- import { useRouter } from "next/navigation" ;
109
1110interface SidebarCollapseButtonProps {
1211 isCollapsed : boolean ;
@@ -15,19 +14,19 @@ interface SidebarCollapseButtonProps {
1514export function SidebarCollapseButton ( {
1615 isCollapsed,
1716} : SidebarCollapseButtonProps ) {
18- const router = useRouter ( ) ;
1917 const { setIsCollapsed } = useSidebar ( ) ;
2018
2119 const { execute } = useAction ( updateSidebarState , {
22- onSuccess : ( ) => {
23- router . refresh ( ) ;
20+ onError : ( ) => {
21+ // Revert the optimistic update if the server action fails
22+ setIsCollapsed ( isCollapsed ) ;
2423 } ,
2524 } ) ;
2625
2726 const handleToggle = ( ) => {
2827 // Update local state immediately for responsive UI
2928 setIsCollapsed ( ! isCollapsed ) ;
30- // Update server state (cookie)
29+ // Update server state (cookie) in the background
3130 execute ( { isCollapsed : ! isCollapsed } ) ;
3231 } ;
3332
You can’t perform that action at this time.
0 commit comments