import React, { useState, useEffect, useRef, useContext } from 'react'; import {getTheme} from '../theme.jsx'; import { useNavigate, Link, useParams } from "react-router-dom"; import { toast } from "react-toastify" import { Chip, IconButton, TextField, InputAdornment, List, Card, ListItem, ListItemAvatar, ListItemText, Avatar, Grid, Typography, Tooltip, Divider, Button, } from '@mui/material'; import ArticleIcon from '@mui/icons-material/Article'; import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight'; import ManageSearchIcon from '@mui/icons-material/ManageSearch'; import { VerifiedUser as VerifiedUserIcon, } from '@mui/icons-material' import { AvatarGroup, } from "@mui/material" import { Context } from '../context/ContextApi.jsx'; import { Search as SearchIcon, Close as CloseIcon, Folder as FolderIcon, Code as CodeIcon, LibraryBooks as LibraryBooksIcon } from '@mui/icons-material' import algoliasearch from 'algoliasearch/lite'; import aa from 'search-insights' import { InstantSearch, Configure, connectSearchBox, connectHits, Index } from 'react-instantsearch-dom'; //import { InstantSearch, SearchBox, Hits, connectSearchBox, connectHits, Index } from 'react-instantsearch-dom'; // https://www.algolia.com/doc/api-reference/widgets/search-box/react/ const chipStyle = { backgroundColor: "#3d3f43", height: 30, marginRight: 5, paddingLeft: 5, paddingRight: 5, height: 28, cursor: "pointer", borderColor: "#3d3f43", color: "white", } const searchClient = algoliasearch("JNSS5CFDZZ", "c8f882473ff42d41158430be09ec2b4e") const SearchData = props => { const { serverside, globalUrl, userdata } = props let navigate = useNavigate(); const { searchBarModalOpen, setSearchBarModalOpen, isDocSearchModalOpen } = useContext(Context); const borderRadius = 3 const node = useRef() const [searchOpen, setSearchOpen] = useState(false) const [oldPath, setOldPath] = useState("") const [value, setValue] = useState(""); const isDocSearchModal = isDocSearchModalOpen; const {themeMode, supportEmail} = useContext(Context); const theme = getTheme(themeMode) const handleLinkClick = () => { if (searchBarModalOpen) { setSearchBarModalOpen(false); // Assuming setModalOpen is defined correctly } else { console.log("Condition not met, staying on the same page"); } }; if (serverside === true) { return null } //if (window !== undefined && window.location !== undefined && window.location.pathname === "/search") { // return null //} const isCloud = window.location.host === "localhost:3002" || window.location.host === "shuffler.io"; // if (window.location.pathname === "/docs" || window.location.pathname === "/apps" || window.location.pathname === "/usecases" ) { // setModalOpen(false) // } // useEffect(() => { // if (searchOpen) { // var tarfield = document.getElementById("shuffle_search_field") // tarfield.focus() // } // }, searchOpen) const SearchBox = ({ currentRefinement, refine, isSearchStalled, }) => { const [inputValue, setInputValue] = useState(currentRefinement); const textFieldRef = useRef(null); const debounceTimeoutRef = useRef(null); const keyPressHandler = (e) => { if (e.key === "Enter") { e.preventDefault(); // navigate(`/search?q=${currentRefinement}`, { state: value, replace: true }); // setModalOpen(false); const trimmedValue = inputValue.trim(); if (trimmedValue !== '') { navigate(`/search?q=${trimmedValue}`, { state: trimmedValue, replace: true }); setSearchBarModalOpen(false); } } }; useEffect(() => { if (searchOpen && textFieldRef.current) { textFieldRef.current.focus(); } }, [searchOpen]); useEffect(() => { // Clear any existing timeout to prevent multiple executions if (debounceTimeoutRef.current) { clearTimeout(debounceTimeoutRef.current); } // Set a new timeout that will execute the search after a 200ms delay debounceTimeoutRef.current = setTimeout(() => { refine(inputValue); setSearchOpen(inputValue.trim() !== ''); }, 200); // Cleanup function that runs when component unmounts or when dependencies change // This ensures we don't have any hanging timeouts return () => { if (debounceTimeoutRef.current) { clearTimeout(debounceTimeoutRef.current); } }; }, [inputValue, refine]); // Effect runs when inputValue or refine function changes return (