Files
Web-Frontend/client/src/components/dashboard/SettingsButton.jsx
T

375 lines
15 KiB
React
Raw Normal View History

import {useState, useMemo, useRef, useEffect, useCallback} from "react";
import {
2026-07-29 13:48:38 -07:00
Button,
Box,
Text,
Flex,
Grid,
Dialog,
Tabs,
Badge,
Menu,
HStack,
2026-07-29 14:18:10 -07:00
useRecipe,
useSlotRecipe
} from "@chakra-ui/react";
import {Settings, LayoutGrid, Move, Save, X, ChevronRight, ChevronLeft} from 'lucide-react';
2026-07-29 13:48:38 -07:00
import {useDashboardStore} from "../../store/useDashboardStore.jsx";
import {WIDGET_REGISTRY} from "../../constants/widgetRegistry.jsx";
2026-08-20 14:45:13 -07:00
// 🚨 NEW: Tiny helper to convert size keys into abbreviations
const getSizeAbbreviation = (key) => {
2026-08-21 12:58:28 -07:00
const mapping = {small: "S", medium: "M", large: "L", xlarge: "XL"};
2026-08-20 14:45:13 -07:00
return mapping[key.toLowerCase()] || key.charAt(0).toUpperCase();
};
export function SettingsButton() {
2026-07-29 13:48:38 -07:00
// 1. ☁Store connections
const isEditing = useDashboardStore(state => state.isEditing);
const enterEditMode = useDashboardStore(state => state.enterEditMode);
const saveEdit = useDashboardStore(state => state.saveEdit);
const cancelEdit = useDashboardStore(state => state.cancelEdit);
const addWidget = useDashboardStore(state => state.addWidget);
2026-07-29 13:48:38 -07:00
const recipe = useRecipe({key: "glassTab"});
const tabStyles = recipe();
2026-08-20 12:51:58 -07:00
// Pull the glass container styles for the dropdown and modal
2026-07-29 13:48:38 -07:00
const panelRecipe = useSlotRecipe({key: "glassPanel"});
2026-07-29 14:18:10 -07:00
const panelStyles = panelRecipe({layout: "dropdown"});
2026-08-20 12:51:58 -07:00
const modalStyles = panelRecipe({layout: "popover"});
2026-07-29 13:48:38 -07:00
const [isLibraryOpen, setIsLibraryOpen] = useState(false);
// ⬅️ ➡️ This is the callbacks and scroll control to add the chevron arrows in the tab list
const tabsRef = useRef(null);
const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(false);
const checkScroll = useCallback(() => {
if (tabsRef.current) {
2026-08-21 12:58:28 -07:00
const {scrollLeft, scrollWidth, clientWidth} = tabsRef.current;
setCanScrollLeft(scrollLeft > 0);
// We use -1 here to account for sub-pixel rendering in some browsers
setCanScrollRight(Math.ceil(scrollLeft + clientWidth) < scrollWidth - 1);
}
}, []);
useEffect(() => {
if (isLibraryOpen) {
// Small timeout ensures the DOM has rendered the tabs before measuring
setTimeout(checkScroll, 50);
window.addEventListener('resize', checkScroll);
return () => window.removeEventListener('resize', checkScroll);
}
}, [isLibraryOpen, checkScroll]);
2026-07-29 13:48:38 -07:00
// Smart Add Handler
const handleAddWidget = (widgetId, sizeKey) => {
enterEditMode();
addWidget(widgetId, sizeKey);
};
2026-07-29 13:48:38 -07:00
const categorizedWidgets = useMemo(() => {
return Object.entries(WIDGET_REGISTRY).reduce((acc, [id, widget]) => {
const cat = widget.category || 'General';
if (!acc[cat]) acc[cat] = [];
acc[cat].push({id, ...widget});
return acc;
}, {});
}, []);
2026-07-29 13:48:38 -07:00
const categories = Object.keys(categorizedWidgets);
2026-07-29 13:48:38 -07:00
// use ref to capture html node for placing menu accordingly
const menuRef = useRef(null);
const getAnchorRect = () => {
if (!menuRef.current) return null;
return menuRef.current.getBoundingClientRect();
};
2026-07-29 13:48:38 -07:00
return (
<>
{isEditing ? (
// 📝 EDIT MODE: The 3-Pill Layout
<HStack gap={2}>
{/* 1. The Add Widget Button */}
2026-07-29 13:48:38 -07:00
<Box
as="button"
css={tabStyles}
data-state="open"
onClick={() => setIsLibraryOpen(true)}
2026-08-21 12:58:28 -07:00
color={{base: "blue.500", _dark: "blue.300"}}
_hover={{bg: "rgba(66, 153, 225, 0.1)"}}
2026-07-29 13:48:38 -07:00
>
2026-08-21 12:58:28 -07:00
<LayoutGrid size={20} style={{flexShrink: 0}}/>
<Box as="span">Widget Library</Box>
2026-07-29 13:48:38 -07:00
</Box>
{/* 2. The Save Button */}
<Box
as="button"
css={tabStyles}
data-state="open"
onClick={saveEdit}
2026-08-21 12:58:28 -07:00
color={{base: "green.600", _dark: "green.400"}}
_hover={{bg: "rgba(72, 187, 120, 0.1)"}}
>
2026-08-21 12:58:28 -07:00
<Save size={20} style={{flexShrink: 0}}/>
<Box as="span">Save Layout</Box>
</Box>
{/* 3. The Discard Button */}
<Box
as="button"
css={tabStyles}
data-state="open"
onClick={cancelEdit}
2026-08-21 12:58:28 -07:00
color={{base: "red.500", _dark: "red.400"}}
_hover={{bg: "rgba(245, 101, 101, 0.1)"}}
>
2026-08-21 12:58:28 -07:00
<X size={20} style={{flexShrink: 0}}/>
<Box as="span">Discard</Box>
</Box>
</HStack>
) : (
// 👀 VIEW MODE: The Static Dropdown Menu
2026-08-21 12:58:28 -07:00
<Menu.Root positioning={{getAnchorRect}}>
<Menu.Trigger asChild>
<Box ref={menuRef} as="button" css={tabStyles}>
{/* size increased to 24 to match tabs. flexShrink: 0 is CRITICAL */}
2026-08-21 12:58:28 -07:00
<Settings size={24} style={{flexShrink: 0}}/>
<Box as="span">Dashboard Settings</Box>
</Box>
</Menu.Trigger>
<Menu.Positioner>
<Menu.Content css={panelStyles.root} zIndex="dropdown" minW="200px">
<Menu.Item
onClick={enterEditMode}
cursor="pointer"
2026-08-21 12:58:28 -07:00
_hover={{WebkitTextStroke: "0.5px currentColor"}}
>
2026-08-21 12:58:28 -07:00
<Move size={16} style={{marginRight: '8px'}}/> Edit Layout
2026-07-29 13:48:38 -07:00
</Menu.Item>
2026-08-21 12:58:28 -07:00
<Menu.Separator my={1} borderColor={{base: "gray.200", _dark: "whiteAlpha.200"}}/>
2026-08-21 12:58:28 -07:00
<Menu.Item
onClick={() => setIsLibraryOpen(true)}
cursor="pointer"
_hover={{
bg: {base: "rgba(0, 0, 0, 0.05)", _dark: "rgba(255, 255, 255, 0.1)"},
WebkitTextStroke: "0.5px currentColor"
}}
>
2026-08-21 12:58:28 -07:00
<LayoutGrid size={16} style={{marginRight: '8px'}}/> Widget Library
</Menu.Item>
</Menu.Content>
</Menu.Positioner>
</Menu.Root>
)}
2026-08-21 12:58:28 -07:00
{/* 📚📚📚 THE WIDGET LIBRARY MODAL (Controlled by isLibraryOpen) */}
<Dialog.Root
open={isLibraryOpen}
onOpenChange={(e) => setIsLibraryOpen(e.open)}
size="xl"
placement="center"
closeOnInteractOutside={true}
closeOnEscapeKeyDown={true}
>
{/* Soft blur can be added to the background behind the modal */}
{/*<Dialog.Backdrop bg="blackAlpha.100" backdropFilter="blur(2px)"/>*/}
<Dialog.Positioner>
<Dialog.Content css={modalStyles.root} overflow="hidden" maxW="60%" minH="80%" p={0}>
2026-07-29 13:48:38 -07:00
2026-08-21 12:58:28 -07:00
<Dialog.Header bg="transparent" color={{base: "gray.800", _dark: "white"}} py={4}
borderColor={{base: "gray.200", _dark: "whiteAlpha.200"}}>
<Dialog.Title fontSize="xl" fontWeight="bold">Widgets Shop</Dialog.Title>
</Dialog.Header>
2026-07-29 13:48:38 -07:00
2026-08-21 12:58:28 -07:00
<Dialog.Body p={0}>
<Tabs.Root defaultValue={categories[0]} variant="unstyled">
2026-08-21 13:30:02 -07:00
<Box position="relative" display="flex" mx="auto" maxWidth="100%" mx={5} mt={4} mb={6}>
2026-08-21 12:58:28 -07:00
{/* LEFT GLASS CRESCENT */}
{canScrollLeft && (
<Flex
position="absolute"
left="0" top="0" bottom="0"
2026-08-21 13:30:02 -07:00
w="50px"
2026-08-21 12:58:28 -07:00
align="center" justify="center"
zIndex={2}
cursor="pointer"
onClick={() => tabsRef.current.scrollBy({left: -200, behavior: 'smooth'})}
bg={{base: "rgba(255, 255, 255, 0.7)", _dark: "rgba(30, 30, 30, 0.8)"}}
2026-08-21 13:30:02 -07:00
backdropFilter="blur(1px)"
2026-08-21 12:58:28 -07:00
borderTopLeftRadius="full"
borderBottomLeftRadius="full"
2026-08-21 13:30:02 -07:00
// THE MAGIC: Bites a transparent circle out of the flat right side!
WebkitMaskImage="radial-gradient(circle at 100% 50%, transparent 30px, black 19px)"
maskImage="radial-gradient(circle at 100% 50%, transparent 30px, black 35px)"
2026-08-21 12:58:28 -07:00
color={{base: "gray.600", _dark: "gray.300"}}
2026-08-21 13:30:02 -07:00
transition="background-color 0.2s"
_hover={{
color: {base: "blue.500", _dark: "blue.300"},
bg: {base: "rgba(255, 255, 255, 0.9)", _dark: "rgba(40, 40, 40, 0.95)"}
}}
2026-08-21 12:58:28 -07:00
>
2026-08-21 13:30:02 -07:00
{/* Shifted slightly left to optically center it in the crescent mass */}
<ChevronLeft size={18} style={{ marginLeft: "-30px" }}/>
2026-08-21 12:58:28 -07:00
</Flex>
)}
<Tabs.List
ref={tabsRef}
onScroll={checkScroll}
bg={{base: "rgba(0, 0, 0, 0.04)", _dark: "rgba(255, 255, 255, 0.05)"}}
p={1}
borderRadius="full"
display="flex"
flexWrap="nowrap"
overflowX="auto"
maxWidth="100%"
border="1px solid"
borderColor={{base: "blackAlpha.50", _dark: "whiteAlpha.100"}}
css={{
scrollbarWidth: "none",
"&::-webkit-scrollbar": {display: "none"},
}}
// This is for desktop users not on trackpad to scroll the
onWheel={(e) => {
if (e.currentTarget) {
e.currentTarget.scrollLeft += e.deltaY;
}
}}
>
{categories.map(cat => (
<Tabs.Trigger
key={cat}
value={cat}
px={5}
py={2}
borderRadius="full"
whiteSpace="nowrap"
flexShrink={0}
color={{base: "gray.500", _dark: "gray.400"}}
fontWeight="medium"
transition="background-color 0.2s, color 0.2s, box-shadow 0.2s"
_hover={{
color: {base: "gray.800", _dark: "gray.200"}
}}
_selected={{
bg: {base: "white", _dark: "rgba(255, 255, 255, 0.12)"},
color: {base: "blue.600", _dark: "blue.200"},
boxShadow: "sm",
fontWeight: "bold",
}}
>
{cat}
</Tabs.Trigger>
))}
</Tabs.List>
{canScrollRight && (
<Flex
position="absolute"
right="0" top="0" bottom="0"
2026-08-21 13:30:02 -07:00
w="50px"
2026-08-21 12:58:28 -07:00
align="center" justify="center"
zIndex={2}
cursor="pointer"
onClick={() => tabsRef.current.scrollBy({left: 200, behavior: 'smooth'})}
bg={{base: "rgba(255, 255, 255, 0.7)", _dark: "rgba(30, 30, 30, 0.8)"}}
2026-08-21 13:30:02 -07:00
backdropFilter="blur(1px)"
2026-08-21 12:58:28 -07:00
borderTopRightRadius="full"
borderBottomRightRadius="full"
2026-08-21 13:30:02 -07:00
// THE MAGIC: Bites a transparent circle out of the flat left side!
WebkitMaskImage="radial-gradient(circle at 0% 50%, transparent 30px, black 19px)"
maskImage="radial-gradient(circle at 0% 50%, transparent 30px, black 35px)"
2026-08-21 12:58:28 -07:00
color={{base: "gray.600", _dark: "gray.300"}}
2026-08-21 13:30:02 -07:00
transition="background-color 0.2s"
_hover={{
color: {base: "blue.500", _dark: "blue.300"},
bg: {base: "rgba(255, 255, 255, 0.9)", _dark: "rgba(40, 40, 40, 0.95)"}
}}
2026-08-21 12:58:28 -07:00
>
2026-08-21 13:30:02 -07:00
{/* Shifted slightly right to optically center it in the crescent mass */}
<ChevronRight size={18} style={{ marginRight: "-30px" }}/>
2026-08-21 12:58:28 -07:00
</Flex>
)}
</Box>
{categories.map(cat => (
<Tabs.Content key={cat} value={cat} p={6}>
<Grid templateColumns="repeat(auto-fill, minmax(300px, 1fr))" gap={4}>
{categorizedWidgets[cat].map(widget => (
<Flex
key={widget.id}
direction="column"
bg={{base: "rgba(255, 255, 255, 0.6)", _dark: "rgba(0, 0, 0, 0.2)"}}
p={4}
borderRadius="xl"
border="1px solid"
borderColor={{base: "rgba(255, 255, 255, 0.9)", _dark: "whiteAlpha.200"}}
boxShadow="sm"
>
<Text fontWeight="bold" fontSize="md" mb={1}>{widget.name}</Text>
<Text fontSize="sm" color="gray.500" mb={4} flex="1">{widget.description}</Text>
<Box borderTop="1px solid" borderColor={{base: "gray.200", _dark: "whiteAlpha.200"}}
pt={3}>
<Text fontSize="xs" fontWeight="bold" color="gray.400" textTransform="uppercase"
mb={2}>
Add to Dashboard:
</Text>
<HStack gap={2} flexWrap="wrap">
{Object.keys(widget.allowedSizes).map(sizeKey => (
<Box
key={sizeKey}
as="button"
css={tabStyles}
onClick={() => handleAddWidget(widget.id, sizeKey)}
color={{base: "blue.600", _dark: "blue.300"}}
2026-07-29 13:48:38 -07:00
>
2026-08-21 13:30:02 -07:00
{/* THE ABBREVIATION ICON: Always visible */}
2026-08-21 12:58:28 -07:00
<Flex
align="center"
justify="center"
w="24px"
h="24px"
flexShrink={0}
borderRadius="full"
bg={{base: "blue.50", _dark: "whiteAlpha.200"}}
fontWeight="black"
fontSize="xs"
bg='transparent'
>
{getSizeAbbreviation(sizeKey)}
</Flex>
2026-08-20 12:51:58 -07:00
2026-08-21 13:30:02 -07:00
{/* THE EXPANDED TEXT: Only visible on hover */}
2026-08-21 12:58:28 -07:00
<Box as="span">Add Widget</Box>
</Box>
2026-07-29 13:48:38 -07:00
))}
2026-08-21 12:58:28 -07:00
</HStack>
</Box>
</Flex>
))}
</Grid>
</Tabs.Content>
))}
</Tabs.Root>
</Dialog.Body>
<Dialog.CloseTrigger position="absolute" top={3} right={3}
color={{base: "gray.500", _dark: "whiteAlpha.800"}} _hover={{color: "red.400"}}/>
</Dialog.Content>
</Dialog.Positioner>
</Dialog.Root>
2026-07-29 13:48:38 -07:00
</>
);
}