import React, { useEffect, useRef, useState } from "react"; import { AppsNavBarLeft, AppsNavBarParent, AppsNavBarRight, } from "./Apps-styles"; import NavBack from "../../assets/svgs/NavBack.svg"; import NavAdd from "../../assets/svgs/NavAdd.svg"; import NavMoreMenu from "../../assets/svgs/NavMoreMenu.svg"; import { ButtonBase, ListItemIcon, ListItemText, Menu, MenuItem, Tab, Tabs, } from "@mui/material"; import { executeEvent, subscribeToEvent, unsubscribeFromEvent, } from "../../utils/events"; import TabComponent from "./TabComponent"; import PushPinIcon from "@mui/icons-material/PushPin"; import RefreshIcon from "@mui/icons-material/Refresh"; import { useRecoilState, useSetRecoilState } from "recoil"; import { settingsLocalLastUpdatedAtom, sortablePinnedAppsAtom, } from "../../atoms/global"; export function saveToLocalStorage(key, subKey, newValue) { try { // Fetch existing data const existingData = localStorage.getItem(key); let combinedData = {}; if (existingData) { // Parse the existing data const parsedData = JSON.parse(existingData); // Merge with the new data under the subKey combinedData = { ...parsedData, timestamp: Date.now(), // Update the root timestamp [subKey]: newValue, // Assuming the data is an array }; } else { // If no existing data, just use the new data under the subKey combinedData = { timestamp: Date.now(), // Set the initial root timestamp [subKey]: newValue, }; } // Save combined data back to localStorage const serializedValue = JSON.stringify(combinedData); localStorage.setItem(key, serializedValue); } catch (error) { console.error("Error saving to localStorage:", error); } } export const AppsNavBarDesktop = () => { const [tabs, setTabs] = useState([]); const [selectedTab, setSelectedTab] = useState(null); const [isNewTabWindow, setIsNewTabWindow] = useState(false); const tabsRef = useRef(null); const [anchorEl, setAnchorEl] = useState(null); const open = Boolean(anchorEl); const [sortablePinnedApps, setSortablePinnedApps] = useRecoilState( sortablePinnedAppsAtom ); const setSettingsLocalLastUpdated = useSetRecoilState( settingsLocalLastUpdatedAtom ); const handleClick = (event) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; useEffect(() => { // Scroll to the last tab whenever the tabs array changes (e.g., when a new tab is added) if (tabsRef.current) { const tabElements = tabsRef.current.querySelectorAll(".MuiTab-root"); if (tabElements.length > 0) { const lastTab = tabElements[tabElements.length - 1]; lastTab.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "end", }); } } }, [tabs.length]); // Dependency on the number of tabs const setTabsToNav = (e) => { const { tabs, selectedTab, isNewTabWindow } = e.detail?.data; setTabs([...tabs]); setSelectedTab(!selectedTab ? nulll : { ...selectedTab }); setIsNewTabWindow(isNewTabWindow); }; useEffect(() => { subscribeToEvent("setTabsToNav", setTabsToNav); return () => { unsubscribeFromEvent("setTabsToNav", setTabsToNav); }; }, []); const isSelectedAppPinned = !!sortablePinnedApps?.find( (item) => item?.name === selectedTab?.name && item?.service === selectedTab?.service ); return ( { executeEvent("navigateBack", {}); }} > {tabs?.map((tab) => ( } // Pass custom component sx={{ "&.Mui-selected": { color: "white", }, padding: "0px", margin: "0px", minWidth: "0px", width: "50px", }} /> ))} {selectedTab && ( { setSelectedTab(null); executeEvent("newTabWindow", {}); }} > { if (!selectedTab) return; handleClick(e); }} > )} { if (!selectedTab) return; setSortablePinnedApps((prev) => { let updatedApps; if (isSelectedAppPinned) { // Remove the selected app if it is pinned updatedApps = prev.filter( (item) => !( item?.name === selectedTab?.name && item?.service === selectedTab?.service ) ); } else { // Add the selected app if it is not pinned updatedApps = [ ...prev, { name: selectedTab?.name, service: selectedTab?.service, }, ]; } saveToLocalStorage( "ext_saved_settings", "sortablePinnedApps", updatedApps ); return updatedApps; }); setSettingsLocalLastUpdated(Date.now()); handleClose(); }} > { executeEvent("refreshApp", { tabId: selectedTab?.tabId, }); handleClose(); }} > ); };