import React, { type JSX, useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'next-i18next'; import { Collapse } from 'reactstrap'; import { SORT_AXIS, SORT_ORDER } from '~/interfaces/search'; import type { ISearchConditions, ISearchConfigurations } from '~/stores/search'; import { SearchModalTriggerinput } from './SearchModalTriggerinput'; import { SearchOptionModalLazyLoaded } from './SearchOptionModal'; import SortControl from './SortControl'; type Props = { isEnableSort: boolean; isEnableFilter: boolean; isHidingUserPages: boolean; initialSearchConditions: Partial; onSearchInvoked?: ( keyword: string, configurations: Partial, ) => void; extraControls: React.ReactNode; collapseContents?: React.ReactNode; isCollapsed?: boolean; }; const SearchControl = React.memo((props: Props): JSX.Element => { const { isEnableSort, isEnableFilter, isHidingUserPages, initialSearchConditions, onSearchInvoked, extraControls, collapseContents, isCollapsed, } = props; const keywordOnInit = initialSearchConditions.keyword ?? ''; const [keyword, setKeyword] = useState(keywordOnInit); 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 searchBySearchControlHandler = useCallback( (newKeyword: string) => { setKeyword(newKeyword); onSearchInvoked?.(newKeyword, { sort, order, includeUserPages, includeTrashPages, }); }, [includeTrashPages, includeUserPages, onSearchInvoked, order, sort], ); const changeSortHandler = useCallback( (nextSort: SORT_AXIS, nextOrder: SORT_ORDER) => { setSort(nextSort); setOrder(nextOrder); onSearchInvoked?.(keyword, { sort: nextSort, order: nextOrder, includeUserPages, includeTrashPages, }); }, [includeTrashPages, includeUserPages, keyword, onSearchInvoked], ); const changeIncludeUserPagesHandler = useCallback( (include: boolean) => { setIncludeUserPages(include); onSearchInvoked?.(keyword, { sort, order, includeUserPages: include, includeTrashPages, }); }, [includeTrashPages, keyword, onSearchInvoked, order, sort], ); const changeIncludeTrashPagesHandler = useCallback( (include: boolean) => { setIncludeTrashPages(include); onSearchInvoked?.(keyword, { sort, order, includeUserPages, includeTrashPages: include, }); }, [includeUserPages, keyword, onSearchInvoked, order, sort], ); useEffect(() => { setKeyword(keywordOnInit); }, [keywordOnInit]); return (
{/* TODO: replace the following elements deleteAll button , relevance button and include specificPath button component */}
{/* sort option */} {isEnableSort && (
)} {/* filter option */} {isEnableFilter && ( <>
{isHidingUserPages === false && (
changeIncludeUserPagesHandler(e.target.checked) } />
)}
changeIncludeTrashPagesHandler(e.target.checked) } />
)} {extraControls}
{collapseContents != null && ( {collapseContents} )} setIsFileterOptionModalShown(false)} isHidingUserPages={isHidingUserPages} includeUserPages={includeUserPages} includeTrashPages={includeTrashPages} onIncludeUserPagesSwitched={setIncludeUserPages} onIncludeTrashPagesSwitched={setIncludeTrashPages} />
); }); SearchControl.displayName = 'SearchControl'; export default SearchControl;