/** @jsx jsx */
import { jsx, css } from '@emotion/react';
import { useCallback, useMemo, useState } from 'react';
import { useSelector } from 'react-redux';
import { memoize, noop, keyBy } from 'lodash';
import { CategoriesCheckboxTree, Link, Loading, IconAlt } from '@veupathdb/wdk-client/lib/Components';
import { LinksPosition, CheckboxTreeStyleSpec } from '@veupathdb/coreui/dist/components/inputs/checkboxes/CheckboxTree/CheckboxTree';
import { RootState } from '@veupathdb/wdk-client/lib/Core/State/Types';
import { useSessionBackedState } from '@veupathdb/wdk-client/lib/Hooks/SessionBackedState';
import { CategoryTreeNode, getDisplayName, getTargetType, getRecordClassUrlSegment, isIndividual, getFormattedTooltipContent } from '@veupathdb/wdk-client/lib/Utils/CategoryUtils';
import { makeClassNameHelper, wrappable } from '@veupathdb/wdk-client/lib/Utils/ComponentUtils';
import { decode, arrayOf, string } from '@veupathdb/wdk-client/lib/Utils/Json';
import { Question } from '@veupathdb/wdk-client/lib/Utils/WdkModel';
import { HtmlTooltip } from '@veupathdb/components/lib/components/widgets/Tooltip';
import { combineClassNames, useAlphabetizedSearchTree } from 'ebrc-client/components/homepage/Utils';
import './SearchPane.scss';
const cx = makeClassNameHelper('ebrc-SearchPane');
const EXPANDED_BRANCHES_SESSION_KEY = 'homepage-left-panel-expanded-branch-ids';
const searchPaneStyleOverrides: CheckboxTreeStyleSpec = {
searchBox: {
clearSearchButton: {top: '3px'},
input: {
borderRadius: '0.5em',
fontSize: '0.9em',
borderColor: '#999',
background: '#dfdfdf',
},
optionalIcon: {
top: 0,
cursor: 'text',
}
},
treeNode: {
topLevelNodeWrapper: {
backgroundColor: '#dfdfdf',
margin: '.25em 0',
border: '.0625rem solid #ddd',
borderRadius: '.5em',
padding: '.35em',
cursor: 'pointer',
},
},
treeSection: {
ul: {
padding: '0 1em 0 0.5em',
}
}
};
const headerMenuItemStyleOverrides: CheckboxTreeStyleSpec = {
searchBox: {
...searchPaneStyleOverrides.searchBox,
input: {
...searchPaneStyleOverrides.searchBox?.input,
background: '#fff',
},
optionalIcon: {
fontSize: '1.25em',
cursor: 'text',
top: '3px',
}
},
treeSection: {
container: {
whiteSpace: 'normal',
width: 'max-content',
},
ul: {
padding: 0,
}
}
};
export type Props = {
containerClassName?: string,
searchTree: CategoryTreeNode | undefined
};
export const SearchPane = (props: Props) => {
const [ searchTerm, setSearchTerm ] = useState('');
const [ expandedBranches, setExpandedBranches ] = useSessionBackedState(
[ ],
EXPANDED_BRANCHES_SESSION_KEY,
JSON.stringify,
memoize((s: string) => decode(arrayOf(string), s))
);
const alphabetizedSearchTree = useAlphabetizedSearchTree(props.searchTree);
return (
);
}
export type SearchCheckboxTreeProps = {
searchTree?: CategoryTreeNode,
searchTerm: string,
expandedBranches: string[],
setSearchTerm: (newSearchTerm: string) => void,
setExpandedBranches: (newExpandedBranches: string[]) => void,
linksPosition?: LinksPosition,
showSearchBox?: boolean,
type?: 'searchPane' | 'headerMenu',
};
export const SearchCheckboxTree = wrappable((
{
searchTree,
searchTerm,
expandedBranches,
setSearchTerm,
setExpandedBranches,
linksPosition,
showSearchBox,
type = 'searchPane'
}: SearchCheckboxTreeProps
) => {
const noSelectedLeaves = useMemo(
() => [] as string[],
[]
);
const styleOverrides = type === 'searchPane' ? searchPaneStyleOverrides : headerMenuItemStyleOverrides;
const questionsByUrlSegment = useSelector((state: RootState) => keyBy(state.globalData.questions, 'urlSegment'));
const renderNode = useCallback(
(node: CategoryTreeNode, path: number[] | undefined) => (
We could not find any searches matching "{searchTerm}".
Perhaps you meant to {' '} run a site-wide search for "{searchTerm}" ?