import AsyncStorage from "@react-native-async-storage/async-storage"; import { usePathname } from "expo-router"; import { useEffect, useRef, useState } from "react"; import { Animated, Platform, View } from "react-native"; import { useAppSelector, useAppDispatch } from "@/store/hooks"; import { selectIsMobile, setSidebarCollapsed, } from "@/store/screen/screenSizeSlice"; import { SignOutButton } from "@/components/auth/SignOutButton"; import { useAuthContext } from "@/auth/AuthContext"; import { SidebarToggle } from "@/components/features/navigation/SidebarToggle"; import { SidebarItem } from "@/components/features/navigation/SidebarItem"; const SIDEBAR_STATE_KEY = "sidebarCollapsedState"; const saveCollapsedState = async (isCollapsed: boolean) => { try { if (Platform.OS === "web") { localStorage.setItem(SIDEBAR_STATE_KEY, JSON.stringify(isCollapsed)); } else { await AsyncStorage.setItem( SIDEBAR_STATE_KEY, JSON.stringify(isCollapsed) ); } } catch (error) { console.error("Error saving sidebar state:", error); } }; const loadCollapsedState = async (): Promise => { try { let value; if (Platform.OS === "web") { value = localStorage.getItem(SIDEBAR_STATE_KEY); } else { value = await AsyncStorage.getItem(SIDEBAR_STATE_KEY); } return value ? JSON.parse(value) : null; } catch (error) { console.error("Error loading sidebar state:", error); return null; } }; export const Sidebar = ({ mobileHidden = false, onRequestMobileToggle, }: { mobileHidden?: boolean; onRequestMobileToggle?: () => void; }) => { const { isAuthenticated } = useAuthContext(); const pathname = usePathname(); const [collapsed, setCollapsed] = useState(false); const [labelsVisible, setLabelsVisible] = useState(true); const animatedWidth = useRef(new Animated.Value(200)).current; const textOpacity = useRef(new Animated.Value(1)).current; const iconTranslateX = useRef(new Animated.Value(0)).current; const paddingLeft = useRef(new Animated.Value(16)).current; // 0.5rem = 8px const isMobile = useAppSelector(selectIsMobile); const dispatch = useAppDispatch(); useEffect(() => { const loadSavedState = async () => { if (isMobile) { setCollapsed(true); setLabelsVisible(false); textOpacity.setValue(0); iconTranslateX.setValue(12); paddingLeft.setValue(8); Animated.timing(animatedWidth, { toValue: mobileHidden ? 0 : 80, duration: 250, useNativeDriver: false, }).start(); return; } const savedState = await loadCollapsedState(); if (savedState !== null) { setCollapsed(savedState); dispatch(setSidebarCollapsed(savedState)); setLabelsVisible(!savedState); textOpacity.setValue(savedState ? 0 : 1); iconTranslateX.setValue(savedState ? 12 : 0); paddingLeft.setValue(8); Animated.timing(animatedWidth, { toValue: savedState ? 80 : 200, duration: 300, useNativeDriver: false, }).start(); } }; loadSavedState(); }, [ animatedWidth, textOpacity, iconTranslateX, paddingLeft, isMobile, mobileHidden, dispatch, ]); const isActive = (path: string) => { if (path === "/user-dashboard" || path === "/dashboard") { return ( pathname.includes("/user-dashboard") || pathname === "/(tabs)" || pathname === "/" ); } if (path === "/accounts") { return pathname.includes("/accounts"); } if (path === "/settings") { return ( pathname.startsWith("/settings") || pathname.startsWith("/(tabs)/settings") ); } if (path === "/data-protection") { return ( pathname.startsWith("/data-protection") || pathname.startsWith("/(tabs)/data-protection") ); } return pathname.includes(path); }; const toggleSidebar = () => { if (isMobile) { onRequestMobileToggle?.(); return; } const newCollapsedState = !collapsed; if (!newCollapsedState) { setLabelsVisible(true); } Animated.parallel([ Animated.timing(animatedWidth, { toValue: newCollapsedState ? 80 : 200, duration: 300, useNativeDriver: false, }), Animated.timing(textOpacity, { toValue: newCollapsedState ? 0 : 1, duration: 250, useNativeDriver: true, }), Animated.timing(iconTranslateX, { toValue: newCollapsedState ? 12 : 0, duration: 300, useNativeDriver: true, }), Animated.timing(paddingLeft, { toValue: 8, duration: 300, useNativeDriver: false, }), ]).start(() => { setLabelsVisible(!newCollapsedState); }); if (newCollapsedState) { setLabelsVisible(true); } setCollapsed(newCollapsedState); dispatch(setSidebarCollapsed(newCollapsedState)); saveCollapsedState(newCollapsedState); }; return ( {!(isMobile && mobileHidden) && ( )} {!(isMobile && mobileHidden) && ( )} {!(isMobile && mobileHidden) && ( {isAuthenticated && ( )} )} ); };