11'use client' ;
22
3- import { useState } from 'react' ;
3+ import { useState , useEffect } from 'react' ;
44import { Link , useLocation } from 'react-router-dom' ;
55import {
66 Container ,
@@ -13,6 +13,7 @@ import {
1313} from 'lucide-react' ;
1414import { cn } from '../../lib/utils' ;
1515import { Button } from '../ui/button' ;
16+ import { useConfig } from '../../hooks/use-config' ;
1617
1718const navigation = [
1819 { name : 'Containers' , href : '/' , icon : Container } ,
@@ -23,9 +24,39 @@ const navigation = [
2324] ;
2425
2526export function Sidebar ( ) {
27+ const { config, updateSidebarCollapsed } = useConfig ( ) ;
2628 const [ collapsed , setCollapsed ] = useState ( false ) ;
29+ const [ isSaving , setIsSaving ] = useState ( false ) ;
2730 const location = useLocation ( ) ;
2831
32+ // Initialize from config whenever config changes
33+ useEffect ( ( ) => {
34+ if ( config ) {
35+ setCollapsed ( config . sidebar_collapsed ) ;
36+ }
37+ } , [ config ] ) ;
38+
39+ const handleToggleCollapsed = async ( ) => {
40+ // Prevent rapid clicks while saving is in progress
41+ if ( isSaving ) {
42+ return ;
43+ }
44+
45+ setIsSaving ( true ) ;
46+
47+ try {
48+ // Use functional update to get the latest state value
49+ await updateSidebarCollapsed ( ! collapsed ) ;
50+ // Update local state only after save succeeds
51+ setCollapsed ( prevCollapsed => ! prevCollapsed ) ;
52+ } catch ( error ) {
53+ console . error ( 'Failed to persist sidebar state:' , error ) ;
54+ // Keep current state on error - don't update local state
55+ } finally {
56+ setIsSaving ( false ) ;
57+ }
58+ } ;
59+
2960 return (
3061 < div
3162 className = { cn (
@@ -50,7 +81,12 @@ export function Sidebar() {
5081 < Button
5182 variant = "ghost"
5283 size = "sm"
53- onClick = { ( ) => setCollapsed ( ! collapsed ) }
84+ onClick = { handleToggleCollapsed }
85+ disabled = { isSaving }
86+ aria-busy = { isSaving }
87+ aria-pressed = { ! collapsed }
88+ aria-expanded = { ! collapsed }
89+ aria-label = { collapsed ? 'Expand sidebar' : 'Collapse sidebar' }
5490 draggable = "false"
5591 >
5692 { collapsed ? (
0 commit comments