///////////////////////////////////////////////////////////// // // pgAdmin 4 - PostgreSQL Tools // // Copyright (C) 2013 - 2023, The pgAdmin Development Team // This software is released under the PostgreSQL Licence // ////////////////////////////////////////////////////////////// import React, {useRef,useState, useEffect} from 'react'; import { makeStyles } from '@material-ui/core'; import clsx from 'clsx'; import {useDelayDebounce} from 'sources/custom_hooks'; import {onlineHelpSearch} from './online_help'; import {menuSearch} from './menuitems_help'; import $ from 'jquery'; import gettext from 'sources/gettext'; import PropTypes from 'prop-types'; function HelpArticleContents({isHelpLoading, isMenuLoading, helpSearchResult}) { return (isHelpLoading && !(isMenuLoading??true)) ? (
 HELP ARTICLES {Object.keys(helpSearchResult.data).length > 10 ? '(10 of ' + Object.keys(helpSearchResult.data).length + ')' : '(' + Object.keys(helpSearchResult.data).length + ')' } { Object.keys(helpSearchResult.data).length > 10 ? Show all   : '' }
{gettext('Searching...')}
) : ''; } HelpArticleContents.propTypes = { helpSearchResult: PropTypes.object, isHelpLoading: PropTypes.bool, isMenuLoading: PropTypes.bool }; const useModalStyles = makeStyles(() => ({ setTop: { marginTop: '-20px', } })); export function Search({closeModal}) { const classes = useModalStyles(); const wrapperRef = useRef(null); const firstEleRef = useRef(); const [searchTerm, setSearchTerm] = useState(''); const [isShowMinLengthMsg, setIsShowMinLengthMsg] = useState(false); const [isMenuLoading, setIsMenuLoading] = useState(false); const [isHelpLoading, setIsHelpLoading] = useState(false); const [menuSearchResult, setMenuSearchResult] = useState({ fetched: false, data: [], }); const [helpSearchResult, setHelpSearchResult] = useState({ fetched: false, clearedPooling: true, url: '', data: [], }); const [showResults, setShowResults] = useState(false); const resetSearchState = () => { setMenuSearchResult(state => ({ ...state, fetched: false, data: [], })); setHelpSearchResult(state => ({ ...state, fetched: false, clearedPooling: true, url: '', data: {}, })); }; // Below will be called when any changes has been made to state useEffect(() => { if(menuSearchResult.fetched){ setIsMenuLoading(false); } if(helpSearchResult.fetched){ setIsHelpLoading(false); } }, [menuSearchResult, helpSearchResult]); const initSearch = (param) => { setIsMenuLoading(true); setIsHelpLoading(true); onlineHelpSearch(param, { state: helpSearchResult, setState: setHelpSearchResult, }); menuSearch(param, { state: menuSearchResult, setState: setMenuSearchResult, }); }; // Debounse logic to avoid multiple re-render with each keypress useDelayDebounce(initSearch, searchTerm, 1000); const toggleDropdownMenu = () => { let pooling = window.pooling; if(pooling){ window.clearInterval(pooling); } document.getElementsByClassName('live-search-field')[0].value = ''; setTimeout(function(){ document.getElementById('live-search-field').focus(); },100); resetSearchState(); setShowResults(!showResults); setIsMenuLoading(false); setIsHelpLoading(false); setIsShowMinLengthMsg(false); }; const refactorMenuItems = (items) => { if(items.length > 0){ let menuItemsHtmlElement = []; items.forEach((i) => { menuItemsHtmlElement.push(
  • { closeModal(); i.callback(); } }> {i.label} {i.path}
  • ); }); $('[data-toggle="tooltip"]').tooltip(); return menuItemsHtmlElement; } }; const onInputValueChange = (value) => { let pooling = window.pooling; if(pooling){ window.clearInterval(pooling); } resetSearchState(); setSearchTerm(''); if(value.length >= 3){ setSearchTerm(value); setIsMenuLoading(true); setIsHelpLoading(true); setIsShowMinLengthMsg(false); }else{ setIsMenuLoading(false); setIsHelpLoading(false); } if(value.length < 3 && value.length > 0){ setIsShowMinLengthMsg(true); } if(value.length == 0){ setIsShowMinLengthMsg(false); } }; const useOutsideAlerter = (ref) => { useEffect(() => { /** * Alert if clicked on outside of element */ function handleClickOutside(event) { if (ref.current && !ref.current.contains(event.target)) { let input_element = document.getElementById('live-search-field'); if(input_element == null){ return; } let input_value = input_element.value; if(input_value && input_value.length > 0){ toggleDropdownMenu(); } } } // Bind the event listener document.addEventListener('mousedown', handleClickOutside); return () => { // Unbind the event listener on clean up document.removeEventListener('mousedown', handleClickOutside); }; }, [ref]); }; useOutsideAlerter(wrapperRef); const showLoader = (loading) => { return loading ?
    {gettext('Searching...')}
    : ''; }; useEffect(() => { setTimeout(() => { firstEleRef.current && firstEleRef.current.focus(); }, 350); }, [firstEleRef.current]); return (
    , ); } Search.propTypes = { closeModal: PropTypes.func };