2020-09-01 17:49:12 -07:00
|
|
|
import React from "react";
|
2020-09-01 18:59:05 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/client";
|
2020-09-01 17:49:12 -07:00
|
|
|
import {
|
2020-09-01 18:59:05 -07:00
|
|
|
Box,
|
2020-09-01 17:49:12 -07:00
|
|
|
IconButton,
|
|
|
|
Input,
|
|
|
|
InputGroup,
|
2020-09-01 18:59:05 -07:00
|
|
|
InputLeftAddon,
|
2020-09-01 17:49:12 -07:00
|
|
|
InputLeftElement,
|
|
|
|
InputRightElement,
|
|
|
|
useColorModeValue,
|
|
|
|
} from "@chakra-ui/core";
|
|
|
|
import { CloseIcon, SearchIcon } from "@chakra-ui/icons";
|
2020-09-01 19:40:53 -07:00
|
|
|
import { css, cx } from "emotion";
|
2020-09-01 17:59:04 -07:00
|
|
|
import Autosuggest from "react-autosuggest";
|
2020-09-01 17:49:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SearchToolbar is rendered above both the ItemsPanel and the SearchPanel,
|
|
|
|
* and contains the search field where the user types their query.
|
|
|
|
*
|
|
|
|
* It has some subtle keyboard interaction support, like DownArrow to go to the
|
|
|
|
* first search result, and Escape to clear the search and go back to the
|
|
|
|
* ItemsPanel. (The SearchPanel can also send focus back to here, with Escape
|
|
|
|
* from anywhere, or UpArrow from the first result!)
|
|
|
|
*/
|
|
|
|
function SearchToolbar({
|
|
|
|
query,
|
|
|
|
searchQueryRef,
|
|
|
|
firstSearchResultRef,
|
|
|
|
onChange,
|
|
|
|
}) {
|
2020-09-01 17:59:04 -07:00
|
|
|
const [suggestions, setSuggestions] = React.useState([]);
|
|
|
|
|
2020-09-01 18:59:05 -07:00
|
|
|
// NOTE: This query should always load ~instantly, from the client cache.
|
|
|
|
const { data } = useQuery(gql`
|
|
|
|
query SearchToolbarZones {
|
|
|
|
allZones {
|
|
|
|
id
|
|
|
|
label
|
|
|
|
depth
|
|
|
|
isCommonlyUsedByItems
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
const zones = data?.allZones || [];
|
|
|
|
const itemZones = zones.filter((z) => z.isCommonlyUsedByItems);
|
|
|
|
|
|
|
|
let zoneLabels = itemZones.map((z) => z.label);
|
|
|
|
zoneLabels = [...new Set(zoneLabels)];
|
|
|
|
zoneLabels.sort();
|
|
|
|
|
2020-09-01 17:49:12 -07:00
|
|
|
const onMoveFocusDownToResults = (e) => {
|
|
|
|
if (firstSearchResultRef.current) {
|
|
|
|
firstSearchResultRef.current.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-01 19:40:53 -07:00
|
|
|
const suggestionBgColor = useColorModeValue("transparent", "whiteAlpha.100");
|
|
|
|
const highlightedBgColor = useColorModeValue("gray.100", "whiteAlpha.300");
|
|
|
|
|
2020-09-01 18:59:05 -07:00
|
|
|
const renderSuggestion = React.useCallback(
|
2020-11-08 15:04:17 -08:00
|
|
|
({ text }, { isHighlighted }) => (
|
2020-09-01 19:40:53 -07:00
|
|
|
<Box
|
|
|
|
fontWeight={isHighlighted ? "bold" : "normal"}
|
|
|
|
background={isHighlighted ? highlightedBgColor : suggestionBgColor}
|
|
|
|
padding="2"
|
|
|
|
paddingLeft="2.5rem"
|
|
|
|
fontSize="sm"
|
|
|
|
>
|
2020-11-08 15:04:17 -08:00
|
|
|
{text}
|
2020-09-01 19:40:53 -07:00
|
|
|
</Box>
|
2020-09-01 18:59:05 -07:00
|
|
|
),
|
2020-09-01 19:40:53 -07:00
|
|
|
[suggestionBgColor, highlightedBgColor]
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderSuggestionsContainer = React.useCallback(
|
|
|
|
({ containerProps, children }) => {
|
|
|
|
const { className, ...otherContainerProps } = containerProps;
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
{...otherContainerProps}
|
|
|
|
borderBottomRadius="md"
|
|
|
|
boxShadow="md"
|
|
|
|
overflow="hidden"
|
|
|
|
transition="all 0.4s"
|
|
|
|
className={cx(
|
|
|
|
className,
|
|
|
|
css`
|
|
|
|
li {
|
|
|
|
list-style: none;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
},
|
2020-09-01 18:59:05 -07:00
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-11-08 15:04:17 -08:00
|
|
|
// When we change the query filters, clear out the suggestions.
|
2020-09-01 19:40:53 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
setSuggestions([]);
|
2020-11-08 15:04:17 -08:00
|
|
|
}, [query.filterToItemKind, query.filterToZoneLabel]);
|
|
|
|
|
|
|
|
const queryFilterText = getQueryFilterText(query);
|
2020-09-01 19:40:53 -07:00
|
|
|
|
2020-09-01 17:49:12 -07:00
|
|
|
const focusBorderColor = useColorModeValue("green.600", "green.400");
|
|
|
|
|
|
|
|
return (
|
2020-09-01 17:59:04 -07:00
|
|
|
<Autosuggest
|
|
|
|
suggestions={suggestions}
|
2020-09-01 18:59:05 -07:00
|
|
|
onSuggestionsFetchRequested={({ value }) =>
|
2020-11-08 15:04:17 -08:00
|
|
|
setSuggestions(getSuggestions(value, query, zoneLabels))
|
2020-09-01 18:59:05 -07:00
|
|
|
}
|
|
|
|
onSuggestionsClearRequested={() => setSuggestions([])}
|
|
|
|
onSuggestionSelected={(e, { suggestion }) => {
|
|
|
|
const valueWithoutLastWord = query.value.match(/^(.*?)\s*\S+$/)[1];
|
|
|
|
onChange({
|
|
|
|
...query,
|
|
|
|
value: valueWithoutLastWord,
|
2020-11-08 15:04:17 -08:00
|
|
|
filterToZoneLabel: suggestion.zoneLabel || query.filterToZoneLabel,
|
|
|
|
filterToItemKind: suggestion.itemKind || query.filterToItemKind,
|
2020-09-01 18:59:05 -07:00
|
|
|
});
|
2020-09-01 17:59:04 -07:00
|
|
|
}}
|
2020-09-01 18:59:05 -07:00
|
|
|
getSuggestionValue={(zl) => zl}
|
2020-09-01 19:40:53 -07:00
|
|
|
highlightFirstSuggestion={true}
|
2020-09-01 18:59:05 -07:00
|
|
|
renderSuggestion={renderSuggestion}
|
2020-09-01 19:40:53 -07:00
|
|
|
renderSuggestionsContainer={renderSuggestionsContainer}
|
2020-09-01 17:59:04 -07:00
|
|
|
renderInputComponent={(props) => (
|
|
|
|
<InputGroup>
|
2020-11-08 15:04:17 -08:00
|
|
|
{queryFilterText ? (
|
2020-09-01 18:59:05 -07:00
|
|
|
<InputLeftAddon>
|
|
|
|
<SearchIcon color="gray.400" marginRight="3" />
|
2020-11-08 15:04:17 -08:00
|
|
|
<Box fontSize="sm">{queryFilterText}</Box>
|
2020-09-01 18:59:05 -07:00
|
|
|
</InputLeftAddon>
|
|
|
|
) : (
|
|
|
|
<InputLeftElement>
|
|
|
|
<SearchIcon color="gray.400" />
|
|
|
|
</InputLeftElement>
|
|
|
|
)}
|
2020-09-01 17:59:04 -07:00
|
|
|
<Input {...props} />
|
2020-11-08 15:04:17 -08:00
|
|
|
{(query.value || queryFilterText) && (
|
2020-09-01 17:59:04 -07:00
|
|
|
<InputRightElement>
|
|
|
|
<IconButton
|
|
|
|
icon={<CloseIcon />}
|
|
|
|
color="gray.400"
|
|
|
|
variant="ghost"
|
|
|
|
colorScheme="green"
|
|
|
|
aria-label="Clear search"
|
2020-09-01 18:59:05 -07:00
|
|
|
onClick={() => {
|
|
|
|
onChange(null);
|
|
|
|
}}
|
2020-09-01 17:59:04 -07:00
|
|
|
// Big style hacks here!
|
|
|
|
height="calc(100% - 2px)"
|
|
|
|
marginRight="2px"
|
|
|
|
/>
|
|
|
|
</InputRightElement>
|
|
|
|
)}
|
|
|
|
</InputGroup>
|
|
|
|
)}
|
|
|
|
inputProps={{
|
2020-09-01 18:59:05 -07:00
|
|
|
// placeholder: "Search for items to add…",
|
2020-09-01 17:59:04 -07:00
|
|
|
"aria-label": "Search for items to add…",
|
|
|
|
focusBorderColor: focusBorderColor,
|
2020-09-01 19:11:33 -07:00
|
|
|
value: query.value || "",
|
2020-09-01 17:59:04 -07:00
|
|
|
ref: searchQueryRef,
|
2020-09-01 18:59:05 -07:00
|
|
|
minWidth: 0,
|
2020-09-01 19:40:53 -07:00
|
|
|
borderBottomRadius: suggestions.length > 0 ? "0" : "md",
|
2020-09-01 18:59:05 -07:00
|
|
|
// HACK: Chakra isn't noticing the InputLeftElement swapping out
|
2020-09-01 19:11:33 -07:00
|
|
|
// for the InputLeftAddon, so the styles aren't updating...
|
|
|
|
// Hard override!
|
|
|
|
className: css`
|
2020-11-08 15:04:17 -08:00
|
|
|
padding-left: ${queryFilterText ? "1rem" : "2.5rem"} !important;
|
|
|
|
border-bottom-left-radius: ${queryFilterText
|
2020-09-01 19:11:33 -07:00
|
|
|
? "0"
|
|
|
|
: "0.25rem"} !important;
|
2020-11-08 15:04:17 -08:00
|
|
|
border-top-left-radius: ${queryFilterText
|
2020-09-01 19:11:33 -07:00
|
|
|
? "0"
|
|
|
|
: "0.25rem"} !important;
|
|
|
|
`,
|
2020-09-01 18:59:05 -07:00
|
|
|
onChange: (e, { newValue, method }) => {
|
|
|
|
// The Autosuggest tries to change the _entire_ value of the element
|
|
|
|
// when navigating suggestions, which isn't actually what we want.
|
|
|
|
// Only accept value changes that are typed by the user!
|
|
|
|
if (method === "type") {
|
|
|
|
onChange({ ...query, value: newValue });
|
|
|
|
}
|
|
|
|
},
|
2020-09-01 17:59:04 -07:00
|
|
|
onKeyDown: (e) => {
|
2020-09-01 17:49:12 -07:00
|
|
|
if (e.key === "Escape") {
|
2020-09-01 18:59:05 -07:00
|
|
|
if (suggestions.length > 0) {
|
|
|
|
setSuggestions([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange(null);
|
2020-09-01 17:49:12 -07:00
|
|
|
e.target.blur();
|
|
|
|
} else if (e.key === "ArrowDown") {
|
2020-09-01 18:59:05 -07:00
|
|
|
if (suggestions.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-01 17:49:12 -07:00
|
|
|
onMoveFocusDownToResults(e);
|
2020-09-01 19:11:33 -07:00
|
|
|
} else if (e.key === "Backspace" && e.target.selectionStart === 0) {
|
2020-11-08 15:04:17 -08:00
|
|
|
onChange({
|
|
|
|
...query,
|
|
|
|
filterToItemKind: null,
|
|
|
|
filterToZoneLabel: null,
|
|
|
|
});
|
2020-09-01 17:49:12 -07:00
|
|
|
}
|
2020-09-01 17:59:04 -07:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
2020-09-01 17:49:12 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:04:17 -08:00
|
|
|
function getSuggestions(value, query, zoneLabels) {
|
2020-09-01 18:59:05 -07:00
|
|
|
const words = value.split(/\s+/);
|
|
|
|
const lastWord = words[words.length - 1];
|
|
|
|
if (lastWord.length < 2) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:04:17 -08:00
|
|
|
const suggestions = [];
|
|
|
|
|
|
|
|
if (query.filterToItemKind == null) {
|
|
|
|
if (wordMatches("NC", lastWord) || wordMatches("Neocash", lastWord)) {
|
|
|
|
suggestions.push({ itemKind: "NC", text: "Neocash items" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wordMatches("NP", lastWord) || wordMatches("Neopoints", lastWord)) {
|
|
|
|
suggestions.push({ itemKind: "NP", text: "Neopoint items" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wordMatches("PB", lastWord) || wordMatches("Paintbrush", lastWord)) {
|
|
|
|
suggestions.push({ itemKind: "PB", text: "Paintbrush items" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.filterToZoneLabel == null) {
|
|
|
|
for (const zoneLabel of zoneLabels) {
|
|
|
|
if (wordMatches(zoneLabel, lastWord)) {
|
|
|
|
suggestions.push({ zoneLabel, text: zoneLabel });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wordMatches(target, word) {
|
|
|
|
return target.toLowerCase().includes(word.toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
function getQueryFilterText(query) {
|
|
|
|
const textWords = [];
|
|
|
|
|
|
|
|
if (query.filterToItemKind) {
|
|
|
|
textWords.push(query.filterToItemKind);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.filterToZoneLabel) {
|
|
|
|
textWords.push(query.filterToZoneLabel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return textWords.join(" ");
|
2020-09-01 18:59:05 -07:00
|
|
|
}
|
|
|
|
|
2020-09-01 17:49:12 -07:00
|
|
|
export default SearchToolbar;
|