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,
|
2021-01-21 17:23:34 -08:00
|
|
|
Tooltip,
|
2020-09-01 17:49:12 -07:00
|
|
|
useColorModeValue,
|
2020-12-25 09:08:33 -08:00
|
|
|
} from "@chakra-ui/react";
|
2021-01-21 17:23:34 -08:00
|
|
|
import {
|
|
|
|
ChevronDownIcon,
|
|
|
|
ChevronUpIcon,
|
|
|
|
CloseIcon,
|
|
|
|
SearchIcon,
|
|
|
|
} from "@chakra-ui/icons";
|
2021-01-03 19:11:46 -08:00
|
|
|
import { ClassNames } from "@emotion/react";
|
2020-09-01 17:59:04 -07:00
|
|
|
import Autosuggest from "react-autosuggest";
|
2020-09-01 17:49:12 -07:00
|
|
|
|
2021-01-21 15:58:24 -08:00
|
|
|
import useCurrentUser from "../components/useCurrentUser";
|
2021-01-22 14:12:07 -08:00
|
|
|
import { logAndCapture } from "../util";
|
2021-01-21 15:58:24 -08:00
|
|
|
|
2021-01-18 15:56:24 -08:00
|
|
|
export const emptySearchQuery = {
|
|
|
|
value: "",
|
|
|
|
filterToZoneLabel: null,
|
|
|
|
filterToItemKind: null,
|
2021-01-21 15:58:24 -08:00
|
|
|
filterToCurrentUserOwnsOrWants: null,
|
2021-01-18 15:56:24 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
export function searchQueryIsEmpty(query) {
|
|
|
|
return Object.values(query).every((value) => !value);
|
|
|
|
}
|
|
|
|
|
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,
|
2021-01-21 16:38:08 -08:00
|
|
|
autoFocus,
|
2021-01-18 15:56:24 -08:00
|
|
|
showItemsLabel = false,
|
|
|
|
background = null,
|
|
|
|
boxShadow = null,
|
2020-09-01 17:49:12 -07:00
|
|
|
}) {
|
2020-09-01 17:59:04 -07:00
|
|
|
const [suggestions, setSuggestions] = React.useState([]);
|
2021-01-21 17:23:34 -08:00
|
|
|
const [advancedSearchIsOpen, setAdvancedSearchIsOpen] = React.useState(false);
|
2021-01-21 15:58:24 -08:00
|
|
|
const { isLoggedIn } = useCurrentUser();
|
2020-09-01 17:59:04 -07:00
|
|
|
|
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 (
|
2021-01-03 19:11:46 -08:00
|
|
|
<ClassNames>
|
|
|
|
{({ css, cx }) => (
|
|
|
|
<Box
|
|
|
|
{...otherContainerProps}
|
|
|
|
borderBottomRadius="md"
|
|
|
|
boxShadow="md"
|
2021-01-21 17:23:34 -08:00
|
|
|
overflow="auto"
|
2021-01-03 19:11:46 -08:00
|
|
|
transition="all 0.4s"
|
2021-01-21 17:23:34 -08:00
|
|
|
maxHeight="48"
|
2021-01-03 19:11:46 -08:00
|
|
|
className={cx(
|
|
|
|
className,
|
|
|
|
css`
|
|
|
|
li {
|
|
|
|
list-style: none;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{children}
|
2021-01-21 17:23:34 -08:00
|
|
|
{!children && advancedSearchIsOpen && (
|
|
|
|
<Box
|
|
|
|
padding="4"
|
|
|
|
fontSize="sm"
|
|
|
|
fontStyle="italic"
|
|
|
|
textAlign="center"
|
|
|
|
>
|
|
|
|
No more filters available!
|
|
|
|
</Box>
|
|
|
|
)}
|
2021-01-03 19:11:46 -08:00
|
|
|
</Box>
|
2020-09-01 19:40:53 -07:00
|
|
|
)}
|
2021-01-03 19:11:46 -08:00
|
|
|
</ClassNames>
|
2020-09-01 19:40:53 -07:00
|
|
|
);
|
|
|
|
},
|
2021-01-21 17:23:34 -08:00
|
|
|
[advancedSearchIsOpen]
|
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([]);
|
2021-01-21 15:58:24 -08:00
|
|
|
}, [
|
|
|
|
query.filterToItemKind,
|
|
|
|
query.filterToZoneLabel,
|
|
|
|
query.filterToCurrentUserOwnsOrWants,
|
|
|
|
]);
|
2020-11-08 15:04:17 -08:00
|
|
|
|
2021-01-18 15:56:24 -08:00
|
|
|
let queryFilterText = getQueryFilterText(query);
|
|
|
|
if (showItemsLabel) {
|
|
|
|
queryFilterText = queryFilterText ? (
|
|
|
|
<>
|
|
|
|
<Box as="span" fontWeight="600">
|
|
|
|
Items:
|
|
|
|
</Box>{" "}
|
|
|
|
{queryFilterText}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<Box as="span" fontWeight="600">
|
|
|
|
Items
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
2020-09-01 19:40:53 -07:00
|
|
|
|
2021-01-21 17:23:34 -08:00
|
|
|
const allSuggestions = getSuggestions(null, query, zoneLabels, isLoggedIn, {
|
|
|
|
showAll: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Once you remove the final suggestion available, close Advanced Search. We
|
|
|
|
// have placeholder text available, sure, but this feels more natural!
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (allSuggestions.length === 0) {
|
|
|
|
setAdvancedSearchIsOpen(false);
|
|
|
|
}
|
|
|
|
}, [allSuggestions.length]);
|
|
|
|
|
2020-09-01 17:49:12 -07:00
|
|
|
const focusBorderColor = useColorModeValue("green.600", "green.400");
|
|
|
|
|
|
|
|
return (
|
2021-01-19 14:27:55 -08:00
|
|
|
<Autosuggest
|
2021-01-21 17:23:34 -08:00
|
|
|
suggestions={advancedSearchIsOpen ? allSuggestions : suggestions}
|
2021-01-19 14:34:30 -08:00
|
|
|
onSuggestionsFetchRequested={({ value }) => {
|
|
|
|
// HACK: I'm not sure why, but apparently this gets called with value
|
|
|
|
// set to the _chosen suggestion_ after choosing it? Has that
|
|
|
|
// always happened? Idk? Let's just, gate around it, I guess?
|
|
|
|
if (typeof value === "string") {
|
2021-01-21 15:58:24 -08:00
|
|
|
setSuggestions(getSuggestions(value, query, zoneLabels, isLoggedIn));
|
2021-01-19 14:34:30 -08:00
|
|
|
}
|
|
|
|
}}
|
2021-01-19 14:27:55 -08:00
|
|
|
onSuggestionSelected={(e, { suggestion }) => {
|
|
|
|
onChange({
|
|
|
|
...query,
|
2021-01-22 14:14:31 -08:00
|
|
|
// If the suggestion was from typing, remove the last word of the
|
|
|
|
// query value. Or, if it was from Advanced Search, leave it alone!
|
|
|
|
value: advancedSearchIsOpen
|
|
|
|
? query.value
|
|
|
|
: removeLastWord(query.value),
|
2021-01-19 14:27:55 -08:00
|
|
|
filterToZoneLabel: suggestion.zoneLabel || query.filterToZoneLabel,
|
|
|
|
filterToItemKind: suggestion.itemKind || query.filterToItemKind,
|
2021-01-21 15:58:24 -08:00
|
|
|
filterToCurrentUserOwnsOrWants:
|
|
|
|
suggestion.userOwnsOrWants || query.filterToCurrentUserOwnsOrWants,
|
2021-01-19 14:27:55 -08:00
|
|
|
});
|
|
|
|
}}
|
|
|
|
getSuggestionValue={(zl) => zl}
|
|
|
|
alwaysRenderSuggestions={true}
|
|
|
|
highlightFirstSuggestion={true}
|
|
|
|
renderSuggestion={renderSuggestion}
|
|
|
|
renderSuggestionsContainer={renderSuggestionsContainer}
|
|
|
|
renderInputComponent={(inputProps) => (
|
|
|
|
<InputGroup boxShadow={boxShadow} borderRadius="md">
|
|
|
|
{queryFilterText ? (
|
|
|
|
<InputLeftAddon>
|
|
|
|
<SearchIcon color="gray.400" marginRight="3" />
|
|
|
|
<Box fontSize="sm">{queryFilterText}</Box>
|
|
|
|
</InputLeftAddon>
|
|
|
|
) : (
|
|
|
|
<InputLeftElement>
|
|
|
|
<SearchIcon color="gray.400" />
|
|
|
|
</InputLeftElement>
|
|
|
|
)}
|
2021-01-21 16:38:08 -08:00
|
|
|
<Input
|
|
|
|
background={background}
|
|
|
|
autoFocus={autoFocus}
|
|
|
|
{...inputProps}
|
|
|
|
/>
|
2021-01-21 17:23:34 -08:00
|
|
|
<InputRightElement
|
|
|
|
width="auto"
|
|
|
|
justifyContent="flex-end"
|
|
|
|
paddingRight="2px"
|
|
|
|
paddingY="2px"
|
|
|
|
>
|
|
|
|
{!searchQueryIsEmpty(query) && (
|
|
|
|
<Tooltip label="Clear">
|
|
|
|
<IconButton
|
|
|
|
icon={<CloseIcon fontSize="0.6em" />}
|
|
|
|
color="gray.400"
|
|
|
|
variant="ghost"
|
|
|
|
height="100%"
|
|
|
|
marginLeft="1"
|
|
|
|
aria-label="Clear search"
|
|
|
|
onClick={() => {
|
|
|
|
setSuggestions([]);
|
|
|
|
onChange(emptySearchQuery);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
<Tooltip label="Advanced search">
|
2021-01-19 14:27:55 -08:00
|
|
|
<IconButton
|
2021-01-21 17:23:34 -08:00
|
|
|
icon={
|
|
|
|
advancedSearchIsOpen ? (
|
|
|
|
<ChevronUpIcon fontSize="1.5em" />
|
|
|
|
) : (
|
|
|
|
<ChevronDownIcon fontSize="1.5em" />
|
|
|
|
)
|
|
|
|
}
|
2021-01-19 14:27:55 -08:00
|
|
|
color="gray.400"
|
|
|
|
variant="ghost"
|
2021-01-21 17:23:34 -08:00
|
|
|
height="100%"
|
|
|
|
aria-label="Open advanced search"
|
|
|
|
onClick={() => setAdvancedSearchIsOpen((isOpen) => !isOpen)}
|
2021-01-19 14:27:55 -08:00
|
|
|
/>
|
2021-01-21 17:23:34 -08:00
|
|
|
</Tooltip>
|
|
|
|
</InputRightElement>
|
2021-01-19 14:27:55 -08:00
|
|
|
</InputGroup>
|
|
|
|
)}
|
|
|
|
inputProps={{
|
2021-02-04 22:23:34 -08:00
|
|
|
placeholder: "Search all items…",
|
2021-01-19 14:27:55 -08:00
|
|
|
focusBorderColor: focusBorderColor,
|
|
|
|
value: query.value || "",
|
|
|
|
ref: searchQueryRef,
|
|
|
|
minWidth: 0,
|
2021-02-04 23:34:43 -08:00
|
|
|
"data-test-id": "item-search-input",
|
2021-01-19 14:27:55 -08: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 18:59:05 -07:00
|
|
|
}
|
2021-01-19 14:27:55 -08:00
|
|
|
},
|
|
|
|
onKeyDown: (e) => {
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
if (suggestions.length > 0) {
|
|
|
|
setSuggestions([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onChange(null);
|
|
|
|
e.target.blur();
|
|
|
|
} else if (e.key === "ArrowDown") {
|
|
|
|
if (suggestions.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onMoveFocusDownToResults(e);
|
|
|
|
} else if (e.key === "Backspace" && e.target.selectionStart === 0) {
|
2020-11-08 15:04:17 -08:00
|
|
|
onChange({
|
|
|
|
...query,
|
2021-01-19 14:27:55 -08:00
|
|
|
filterToItemKind: null,
|
|
|
|
filterToZoneLabel: null,
|
2021-01-21 15:58:24 -08:00
|
|
|
filterToCurrentUserOwnsOrWants: null,
|
2020-11-08 15:04:17 -08:00
|
|
|
});
|
2021-01-19 14:27:55 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
2020-09-01 17:49:12 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:34 -08:00
|
|
|
function getSuggestions(
|
|
|
|
value,
|
|
|
|
query,
|
|
|
|
zoneLabels,
|
|
|
|
isLoggedIn,
|
|
|
|
{ showAll = false } = {}
|
|
|
|
) {
|
|
|
|
if (!value && !showAll) {
|
2021-01-19 14:34:30 -08:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:34 -08:00
|
|
|
const words = (value || "").split(/\s+/);
|
2020-09-01 18:59:05 -07:00
|
|
|
const lastWord = words[words.length - 1];
|
2021-01-21 17:23:34 -08:00
|
|
|
if (lastWord.length < 2 && !showAll) {
|
2020-09-01 18:59:05 -07:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:04:17 -08:00
|
|
|
const suggestions = [];
|
|
|
|
|
|
|
|
if (query.filterToItemKind == null) {
|
2021-01-21 17:23:34 -08:00
|
|
|
if (
|
|
|
|
wordMatches("NC", lastWord) ||
|
|
|
|
wordMatches("Neocash", lastWord) ||
|
|
|
|
showAll
|
|
|
|
) {
|
2020-11-08 15:04:17 -08:00
|
|
|
suggestions.push({ itemKind: "NC", text: "Neocash items" });
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:34 -08:00
|
|
|
if (
|
|
|
|
wordMatches("NP", lastWord) ||
|
|
|
|
wordMatches("Neopoints", lastWord) ||
|
|
|
|
showAll
|
|
|
|
) {
|
2020-11-08 15:04:17 -08:00
|
|
|
suggestions.push({ itemKind: "NP", text: "Neopoint items" });
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:34 -08:00
|
|
|
if (
|
|
|
|
wordMatches("PB", lastWord) ||
|
|
|
|
wordMatches("Paintbrush", lastWord) ||
|
|
|
|
showAll
|
|
|
|
) {
|
2020-11-08 15:04:17 -08:00
|
|
|
suggestions.push({ itemKind: "PB", text: "Paintbrush items" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:58:24 -08:00
|
|
|
if (isLoggedIn && query.filterToCurrentUserOwnsOrWants == null) {
|
2021-01-21 17:23:34 -08:00
|
|
|
if (wordMatches("Items you own", lastWord) || showAll) {
|
2021-01-21 15:58:24 -08:00
|
|
|
suggestions.push({ userOwnsOrWants: "OWNS", text: "Items you own" });
|
|
|
|
}
|
|
|
|
|
2021-01-21 17:23:34 -08:00
|
|
|
if (wordMatches("Items you want", lastWord) || showAll) {
|
2021-01-21 15:58:24 -08:00
|
|
|
suggestions.push({ userOwnsOrWants: "WANTS", text: "Items you want" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:04:17 -08:00
|
|
|
if (query.filterToZoneLabel == null) {
|
|
|
|
for (const zoneLabel of zoneLabels) {
|
2021-01-21 17:23:34 -08:00
|
|
|
if (wordMatches(zoneLabel, lastWord) || showAll) {
|
2021-01-21 16:46:23 -08:00
|
|
|
suggestions.push({ zoneLabel, text: `Zone: ${zoneLabel}` });
|
2020-11-08 15:04:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-01-21 16:46:23 -08:00
|
|
|
textWords.push(pluralizeZoneLabel(query.filterToZoneLabel));
|
2020-11-08 15:04:17 -08:00
|
|
|
}
|
|
|
|
|
2021-01-21 15:58:24 -08:00
|
|
|
if (query.filterToCurrentUserOwnsOrWants === "OWNS") {
|
2021-01-21 17:35:03 -08:00
|
|
|
if (!query.filterToItemKind && !query.filterToZoneLabel) {
|
2021-01-21 15:58:24 -08:00
|
|
|
textWords.push("Items");
|
2021-01-21 17:35:03 -08:00
|
|
|
} else if (query.filterToItemKind && !query.filterToZoneLabel) {
|
|
|
|
textWords.push("items");
|
2021-01-21 15:58:24 -08:00
|
|
|
}
|
|
|
|
textWords.push("you own");
|
|
|
|
} else if (query.filterToCurrentUserOwnsOrWants === "WANTS") {
|
2021-01-21 17:35:03 -08:00
|
|
|
if (!query.filterToItemKind && !query.filterToZoneLabel) {
|
2021-01-21 15:58:24 -08:00
|
|
|
textWords.push("Items");
|
2021-01-21 17:35:03 -08:00
|
|
|
} else if (query.filterToItemKind && !query.filterToZoneLabel) {
|
|
|
|
textWords.push("items");
|
2021-01-21 15:58:24 -08:00
|
|
|
}
|
|
|
|
textWords.push("you want");
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:04:17 -08:00
|
|
|
return textWords.join(" ");
|
2020-09-01 18:59:05 -07:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:46:23 -08:00
|
|
|
/**
|
|
|
|
* pluralizeZoneLabel hackily tries to convert a zone name to a plural noun!
|
|
|
|
*
|
|
|
|
* HACK: It'd be more reliable and more translatable to do this by just
|
|
|
|
* manually creating the plural for each zone. But, ehh! ¯\_ (ツ)_/¯
|
|
|
|
*/
|
|
|
|
function pluralizeZoneLabel(zoneLabel) {
|
|
|
|
if (zoneLabel.endsWith("ss")) {
|
|
|
|
return zoneLabel + "es";
|
|
|
|
} else if (zoneLabel.endsWith("s")) {
|
|
|
|
return zoneLabel;
|
|
|
|
} else {
|
|
|
|
return zoneLabel + "s";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:12:07 -08:00
|
|
|
/**
|
|
|
|
* removeLastWord returns a copy of the text, with the last word and any
|
|
|
|
* preceding space removed.
|
|
|
|
*/
|
|
|
|
function removeLastWord(text) {
|
|
|
|
// This regex matches the full text, and assigns the last word and any
|
|
|
|
// preceding text to subgroup 2, and all preceding text to subgroup 1. If
|
|
|
|
// there's no last word, we'll still match, and the full string will be in
|
|
|
|
// subgroup 1, including any space - no changes made!
|
|
|
|
const match = text.match(/^(.*?)(\s*\S+)?$/);
|
|
|
|
if (!match) {
|
|
|
|
logAndCapture(
|
|
|
|
new Error(
|
|
|
|
`Assertion failure: pattern should match any input text, ` +
|
|
|
|
`but failed to match ${JSON.stringify(text)}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return match[1];
|
|
|
|
}
|
|
|
|
|
2020-09-01 17:49:12 -07:00
|
|
|
export default SearchToolbar;
|