import React, { FC, Ref, CSSProperties, useRef, useState, useCallback, } from "react"; import { observer } from "mobx-react-lite"; import { useFloating, shift, flip, offset } from "@floating-ui/react-dom"; import type { OnChangeValue } from "react-select"; import AsyncSelect from "react-select/async"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faCaretDown } from "@fortawesome/free-solid-svg-icons/faCaretDown"; import type { AlertStore } from "Stores/AlertStore"; import type { Settings } from "Stores/Settings"; import type { APIGridT } from "Models/APITypes"; import { StringToOption, OptionT } from "Common/Select"; import { DropdownSlide } from "Components/Animations/DropdownSlide"; import { ThemeContext } from "Components/Theme"; import { useOnClickOutside } from "Hooks/useOnClickOutside"; const specialLabels: OptionT[] = [ { label: "Automatic selection", value: "@auto", wasCreated: false }, { label: "@alertmanager", value: "@alertmanager", wasCreated: false }, { label: "@cluster", value: "@cluster", wasCreated: false }, { label: "@receiver", value: "@receiver", wasCreated: false }, ]; const NullContainer: FC = () => null; const GridLabelNameSelect: FC<{ alertStore: AlertStore; settingsStore: Settings; grid: APIGridT; onClose: () => void; }> = ({ alertStore, settingsStore, grid, onClose }) => { const loadOptions = ( inputValue: string, callback: (options: OptionT[]) => void ) => { const autoEnabled = settingsStore.multiGridConfig.config.gridLabel === "@auto"; const options = [ ...specialLabels.filter( (val) => val.value !== "@auto" || (val.value === "@auto" && !autoEnabled) ), ...alertStore.data.labelNames .filter( (labelName) => autoEnabled === true || (autoEnabled === false && labelName !== grid.labelName) ) .sort() .map((key) => StringToOption(key)), ]; callback(options); }; const context = React.useContext(ThemeContext); return ( ) => { settingsStore.multiGridConfig.setGridLabel((option as OptionT).value); onClose(); }} menuIsOpen={true} components={{ ClearIndicator: NullContainer, IndicatorSeparator: null, DropdownIndicator: null, ValueContainer: NullContainer, Control: NullContainer, }} /> ); }; const Dropdown: FC<{ x: number | null; y: number | null; floating: Ref | null; strategy: CSSProperties["position"]; alertStore: AlertStore; settingsStore: Settings; grid: APIGridT; onClose: () => void; }> = ({ x, y, floating, strategy, alertStore, settingsStore, grid, onClose, }) => { return (
); }; const GridLabelSelect: FC<{ alertStore: AlertStore; settingsStore: Settings; grid: APIGridT; }> = observer(({ alertStore, settingsStore, grid }) => { const [isVisible, setIsVisible] = useState(false); const hide = useCallback(() => setIsVisible(false), []); const toggle = useCallback(() => { setIsVisible(!isVisible); }, [isVisible]); const ref = useRef(null); useOnClickOutside(ref, hide, isVisible); const { x, y, reference, floating, strategy } = useFloating({ placement: "bottom", middleware: [shift(), flip(), offset(5)], }); return (
); }); export { GridLabelSelect };