import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { cn } from "@/lib/utils"; import { useCallback, useEffect, useState } from "react"; import { isDesktop } from "react-device-detect"; import { useTranslation } from "react-i18next"; type AttributeSelectDialogProps = { open: boolean; setOpen: (open: boolean) => void; title: string; description: string; onSave: (selectedAttributes: string[]) => void; selectedAttributes: Record; // model -> selected attribute modelAttributes: Record; // model -> available attributes className?: string; }; export default function AttributeSelectDialog({ open, setOpen, title, description, onSave, selectedAttributes, modelAttributes, className, }: AttributeSelectDialogProps) { const { t } = useTranslation(); const [internalSelection, setInternalSelection] = useState< Record >({}); useEffect(() => { if (open) { setInternalSelection({ ...selectedAttributes }); } }, [open, selectedAttributes]); const handleSave = useCallback(() => { // Convert from model->attribute map to flat list of attributes const attributes = Object.values(internalSelection).filter( (attr): attr is string => attr !== null, ); onSave(attributes); }, [internalSelection, onSave]); const handleToggle = useCallback((modelName: string, attribute: string) => { setInternalSelection((prev) => { const currentSelection = prev[modelName]; // If clicking the currently selected attribute, deselect it if (currentSelection === attribute) { return { ...prev, [modelName]: null }; } // Otherwise, select this attribute for this model return { ...prev, [modelName]: attribute }; }); }, []); return ( e.preventDefault()} > {title} {description}
{Object.entries(modelAttributes).map(([modelName, attributes]) => (
{modelName}
{attributes.map((attribute) => (
handleToggle(modelName, attribute) } />
))}
))}
); }