import React, { FC, useCallback, useEffect, useState, } from 'react'; import { useTranslation } from 'react-i18next'; import { SORT_AXIS, SORT_ORDER } from '~/interfaces/search'; import { ISearchConditions, ISearchConfigurations } from '~/stores/search'; import SearchOptionModal from './SearchOptionModal'; import SortControl from './SortControl'; import SearchForm from '../SearchForm'; type Props = { isSearchServiceReachable: boolean, initialSearchConditions: Partial, onSearchInvoked: (keyword: string, configurations: Partial) => void, deleteAllControl: React.ReactNode, } const SearchControl: FC = React.memo((props: Props) => { const { isSearchServiceReachable, initialSearchConditions, onSearchInvoked, deleteAllControl, } = props; const [keyword, setKeyword] = useState(initialSearchConditions.keyword ?? ''); const [sort, setSort] = useState(initialSearchConditions.sort ?? SORT_AXIS.RELATION_SCORE); const [order, setOrder] = useState(initialSearchConditions.order ?? SORT_ORDER.DESC); const [includeUserPages, setIncludeUserPages] = useState(initialSearchConditions.includeUserPages ?? false); const [includeTrashPages, setIncludeTrashPages] = useState(initialSearchConditions.includeTrashPages ?? false); const [isFileterOptionModalShown, setIsFileterOptionModalShown] = useState(false); const { t } = useTranslation(''); const invokeSearch = useCallback(() => { if (onSearchInvoked == null) { return; } onSearchInvoked(keyword, { sort, order, includeUserPages, includeTrashPages, }); }, [keyword, sort, order, includeTrashPages, includeUserPages, onSearchInvoked]); const searchFormSubmittedHandler = useCallback((input: string) => { setKeyword(input); }, []); const changeSortHandler = useCallback((nextSort: SORT_AXIS, nextOrder: SORT_ORDER) => { setSort(nextSort); setOrder(nextOrder); }, []); useEffect(() => { invokeSearch(); }, [invokeSearch]); return (
{/* sort option: show when screen is larger than lg */}
{/* TODO: replace the following elements deleteAll button , relevance button and include specificPath button component */}
{deleteAllControl}
{/* sort option: show when screen is smaller than lg */}
{/* filter option */}
setIncludeUserPages(e.target.checked)} />
setIncludeTrashPages(e.target.checked)} />
setIsFileterOptionModalShown(false)} includeUserPages={includeUserPages} includeTrashPages={includeTrashPages} onIncludeUserPagesSwitched={setIncludeUserPages} onIncludeTrashPagesSwitched={setIncludeTrashPages} />
); }); export default SearchControl;