2019-11-20 15:58:17 +00:00
|
|
|
import Dialog from "@material-ui/core/Dialog";
|
|
|
|
import Downshift from "downshift";
|
|
|
|
import hotkeys from "hotkeys-js";
|
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
2019-11-21 12:13:41 +00:00
|
|
|
import { getActions, hasActions } from "./modes/default";
|
|
|
|
import { getViews, hasViews } from "./modes/default/views";
|
2019-11-20 15:58:17 +00:00
|
|
|
import NavigatorInput from "./NavigatorInput";
|
|
|
|
import NavigatorSection from "./NavigatorSection";
|
|
|
|
import { QuickSearchAction } from "./types";
|
|
|
|
import useQuickSearch from "./useQuickSearch";
|
|
|
|
|
|
|
|
const navigatorHotkey = "ctrl+m, command+m";
|
|
|
|
|
|
|
|
const Navigator: React.FC = () => {
|
|
|
|
const [visible, setVisible] = React.useState(false);
|
2019-11-21 12:13:41 +00:00
|
|
|
const input = React.useRef(null);
|
|
|
|
const [query, mode, change, actions] = useQuickSearch(visible, input);
|
2019-11-20 15:58:17 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
hotkeys(navigatorHotkey, () => setVisible(true));
|
|
|
|
|
|
|
|
return () => hotkeys.unbind(navigatorHotkey);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
open={visible}
|
|
|
|
onClose={() => setVisible(false)}
|
|
|
|
fullWidth
|
|
|
|
maxWidth="sm"
|
|
|
|
>
|
|
|
|
<Downshift
|
|
|
|
itemToString={(item: QuickSearchAction) => (item ? item.label : "")}
|
|
|
|
onSelect={(item: QuickSearchAction) => {
|
|
|
|
navigate(item.url);
|
|
|
|
setVisible(false);
|
|
|
|
}}
|
|
|
|
onInputValueChange={value =>
|
|
|
|
change({
|
|
|
|
target: {
|
|
|
|
name: "query",
|
|
|
|
value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
defaultHighlightedIndex={0}
|
|
|
|
>
|
|
|
|
{({ getInputProps, getItemProps, highlightedIndex }) => (
|
|
|
|
<div>
|
|
|
|
<NavigatorInput
|
2019-11-21 12:13:41 +00:00
|
|
|
mode={mode}
|
2019-11-20 15:58:17 +00:00
|
|
|
value={query}
|
|
|
|
{...getInputProps({
|
|
|
|
value: query
|
|
|
|
})}
|
2019-11-21 12:13:41 +00:00
|
|
|
ref={input}
|
2019-11-20 15:58:17 +00:00
|
|
|
/>
|
2019-11-21 12:13:41 +00:00
|
|
|
{hasActions(actions) && (
|
|
|
|
<NavigatorSection
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Quick Actions",
|
|
|
|
description: "navigator section header"
|
|
|
|
})}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
highlightedIndex={highlightedIndex}
|
|
|
|
items={getActions(actions)}
|
|
|
|
offset={0}
|
|
|
|
/>
|
|
|
|
)}
|
2019-11-20 15:58:17 +00:00
|
|
|
{hasViews(actions) && (
|
|
|
|
<NavigatorSection
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Navigate to",
|
|
|
|
description: "navigator section header"
|
|
|
|
})}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
highlightedIndex={highlightedIndex}
|
|
|
|
items={getViews(actions)}
|
2019-11-21 12:13:41 +00:00
|
|
|
offset={getActions(actions).length}
|
2019-11-20 15:58:17 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Downshift>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Navigator;
|