Skip to content

Commit c394fce

Browse files
authored
Merge pull request #855 from trycompai/lewis/comp-sidebar-opportunistic-ui
[dev] [carhartlewis] lewis/comp-sidebar-opportunistic-ui
2 parents d7e5258 + 0bed3a4 commit c394fce

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { updateSidebarState } from "@/actions/sidebar";
44
import { useSidebar } from "@/context/sidebar-context";
55
import { Button } from "@comp/ui/button";
66
import { cn } from "@comp/ui/cn";
7-
import { ArrowLeftFromLine, ChevronLeft } from "lucide-react";
7+
import { ArrowLeftFromLine } from "lucide-react";
88
import { useAction } from "next-safe-action/hooks";
9-
import { useRouter } from "next/navigation";
109

1110
interface SidebarCollapseButtonProps {
1211
isCollapsed: boolean;
@@ -15,19 +14,19 @@ interface SidebarCollapseButtonProps {
1514
export 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

0 commit comments

Comments
 (0)