import "./AutomationListBox.css"; import { FC, useState } from "react"; import { AutomationData } from "types/automations"; import InputMultiSelect from "components/Inputs/InputMultiSelect"; import InputText from "components/Inputs/InputText"; import { Button } from "components/Inputs/Button"; import { makeGrouping } from "./automationGrouper"; import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; import { useCookies } from "react-cookie"; import { TagDB } from "components/AutomationList/TagDB"; // import { ButtonIcon } from "components/Icons/ButtonIcons"; // import ReadMoreOutlinedIcon from "@mui/icons-material/ReadMoreOutlined"; import { ListData, ListParams } from "apiService/types"; import Chip from "@mui/material/Chip"; import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; export type AutomationListBoxProps = { automations: ListData; selected: number; onSelectAutomation: (i: number) => void; onRemove: (i: number) => void; onAdd: () => void; onUpdateTags: (id: string, tags: Record) => void; onUpdate: (a: AutomationData, i: number) => void; onLoadMore: (p: ListParams) => void; tagsDB: TagDB; }; const useAutomationListBoxState = () => { const [ cookies, setCookies, // eslint-disable-next-line _, ] = useCookies(["albSearchText", "albSelected"]); let initialSelected = []; try { initialSelected = JSON.parse(cookies.albSelected); if (!Array.isArray(initialSelected)) { initialSelected = [initialSelected]; } initialSelected = initialSelected.map(Number); // eslint-disable-next-line } catch (_) {} const [searchText, setSearchText] = useState(cookies.albSearchText ?? ""); const [selectedTagIdx, setSelectedTagIdx] = useState(initialSelected); return { searchText, setSearchText(v: string) { setSearchText(v); setCookies("albSearchText", v); }, setSelectedTagIdx(n: number[]) { setSelectedTagIdx(n); setCookies("albSelected", JSON.stringify(n)); }, selectedTagIdx, }; }; export const AutomationListBox: FC = ({ automations: { data: automations, // params: autoParams, totalItems: totalAutomations, }, selected, onSelectAutomation, onAdd, onRemove, // onLoadMore, onUpdateTags, tagsDB, onUpdate, }) => { const tags = getTagList(automations); const { searchText, setSearchText, selectedTagIdx, setSelectedTagIdx } = useAutomationListBoxState(); const filteredAutomations = filterAutomations( automations, searchText.toLowerCase() ); const grouping = makeGrouping( filteredAutomations.map(([a, _]) => a), selectedTagIdx.map((i) => tags[i]), selected ); return (
{automations.length < totalAutomations ? ( <> } onClick={() => window.open( "https://github.com/asosnovsky/Shortumation/issues/78", "_blank" ) } /> support for more is not yet implemented ) : ( "" )} {/* {autoParams.offset > 0 && ( } onClick={() => onLoadMore()} /> )} */} {/* {filteredAutomations.length} out of {totalAutomations} */} {/* {autoParams.offset + automations.length < totalAutomations && ( } /> )} */}
); }; export const getTagList = (autos: AutomationData[]): string[] => Object.keys( autos .map((a) => Object.keys(a.tags)) .reduce((all: any, keys) => { for (const k of keys) { all[k] = 1; } return all; }, {}) ); const filterAutomations = ( autos: AutomationData[], searchText: string ): Array<[AutomationData, number]> => autos .map<[AutomationData, number]>((a, i) => [a, i]) .filter(([a, i]) => { if (!a.metadata) { return true; } if (searchText.length > 0) { if ( !( (a.metadata.description ?? "") .toLocaleLowerCase() .includes(searchText) || (a.metadata.alias ?? "").toLocaleLowerCase().includes(searchText) ) ) { return false; } } return true; });