import React, { useState } from "react"; import PropTypes from "prop-types"; import { filter, includes } from "lodash"; import Button from "components/buttons/Button"; import Modal from "components/modals/Modal"; import InputField from "components/forms/fields/InputField"; import queryInterface from "interfaces/query"; import hostInterface from "interfaces/host"; import helpers from "../helpers"; import OpenNewTabIcon from "../../../../../assets/images/open-new-tab-12x12@2x.png"; import ErrorIcon from "../../../../../assets/images/icon-error-16x16@2x.png"; const baseClass = "select-query-modal"; const onQueryHostCustom = (host, dispatch) => { const { queryHostCustom } = helpers; queryHostCustom(dispatch, host); return false; }; const onQueryHostSaved = (host, selectedQuery, dispatch) => { const { queryHostSaved } = helpers; queryHostSaved(dispatch, host, selectedQuery); return false; }; const SelectQueryModal = (props) => { const { host, toggleQueryHostModal, dispatch, queries, queryErrors } = props; const [queriesFilter, setQueriesFilter] = useState(""); const getQueries = () => { if (!queriesFilter) { return queries; } const lowerQueryFilter = queriesFilter.toLowerCase(); return filter(queries, (query) => { if (!query.name) { return false; } const lowerQueryName = query.name.toLowerCase(); return includes(lowerQueryName, lowerQueryFilter); }); }; const customQueryButton = () => { return ( ); }; const onFilterQueries = (event) => { setQueriesFilter(event); return false; }; const queriesFiltered = getQueries(); const queriesCount = queriesFiltered.length; const results = () => { if (queryErrors) { return (
error icon Something's gone wrong. Refresh the page or log in again. If this keeps happening, please{" "} file an issue. open new tab {customQueryButton()}
); } if (!queriesFilter && queriesCount === 0) { return (
You have no saved queries. Expecting to see queries? Try again in a few seconds as the system catches up. {customQueryButton()}
); } if (queriesCount > 0) { const queryList = queriesFiltered.map((query) => { return ( ); }); return (
OR {customQueryButton()}
{queryList}
); } if (queriesFilter && queriesCount === 0) { return (
OR {customQueryButton()}
No queries match the current search criteria. Expecting to see queries? Try again in a few seconds as the system catches up.
); } return null; }; return ( {results()} ); }; SelectQueryModal.propTypes = { dispatch: PropTypes.func, host: hostInterface, queries: PropTypes.arrayOf(queryInterface), toggleQueryHostModal: PropTypes.func, queryErrors: PropTypes.object, // eslint-disable-line react/forbid-prop-types }; export default SelectQueryModal;