import React, { useCallback, useEffect, useState } from 'react'; import * as Dialog from '@radix-ui/react-dialog'; import { Button } from 'components/shared/Button'; import { CrossIcon, SearchIcon } from 'components/shared/CustomIcon'; import { Input } from 'components/shared/Input'; import { useGQLClient } from 'context/GQLClientContext'; import { Project } from 'gql-client'; import { useDebounce } from 'usehooks-ts'; import { ProjectSearchBarItem } from './ProjectSearchBarItem'; import { ProjectSearchBarEmpty } from './ProjectSearchBarEmpty'; import { useNavigate } from 'react-router-dom'; import { useCombobox } from 'downshift'; interface ProjectSearchBarDialogProps extends Dialog.DialogProps { onClose?: () => void; onClickItem?: (data: Project) => void; } export const ProjectSearchBarDialog = ({ onClose, onClickItem, ...props }: ProjectSearchBarDialogProps) => { const [items, setItems] = useState([]); const [selectedItem, setSelectedItem] = useState(null); const client = useGQLClient(); const navigate = useNavigate(); const { getInputProps, getItemProps, inputValue, setInputValue } = useCombobox({ items, itemToString(item) { return item ? item.name : ''; }, selectedItem, onSelectedItemChange: ({ selectedItem: newSelectedItem }) => { if (newSelectedItem) { setSelectedItem(newSelectedItem); onClickItem?.(newSelectedItem); navigate( `/${newSelectedItem.organization.slug}/projects/${newSelectedItem.id}`, ); } }, }); const debouncedInputValue = useDebounce(inputValue, 500); const fetchProjects = useCallback( async (inputValue: string) => { const { searchProjects } = await client.searchProjects(inputValue); setItems(searchProjects); }, [client], ); useEffect(() => { if (debouncedInputValue) { fetchProjects(debouncedInputValue); } }, [fetchProjects, debouncedInputValue]); const handleClose = () => { setInputValue(''); setItems([]); onClose?.(); }; return (
} placeholder="Search" appearance="borderless" autoFocus />
{/* Content */}
{items.length > 0 ? items.map((item, index) => ( <>

Suggestions

)) : inputValue && }
); };