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";
|
2019-11-25 16:23:52 +00:00
|
|
|
import cmp from "semver-compare";
|
2019-11-20 15:58:17 +00:00
|
|
|
|
2019-11-25 16:23:52 +00:00
|
|
|
import { APP_VERSION } from "@saleor/config";
|
|
|
|
import useLocalStorage from "@saleor/hooks/useLocalStorage";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2019-11-25 11:29:07 +00:00
|
|
|
import {
|
|
|
|
getActions,
|
2019-11-25 14:32:10 +00:00
|
|
|
getCatalog,
|
2019-11-25 11:29:07 +00:00
|
|
|
getCustomers,
|
|
|
|
getViews,
|
|
|
|
hasActions,
|
2019-11-25 14:32:10 +00:00
|
|
|
hasCatalog,
|
2019-11-25 11:29:07 +00:00
|
|
|
hasCustomers,
|
|
|
|
hasViews
|
|
|
|
} from "./modes/utils";
|
2019-11-20 15:58:17 +00:00
|
|
|
import NavigatorInput from "./NavigatorInput";
|
|
|
|
import NavigatorSection from "./NavigatorSection";
|
|
|
|
import { QuickSearchAction } from "./types";
|
|
|
|
import useQuickSearch from "./useQuickSearch";
|
|
|
|
|
2019-11-25 15:42:24 +00:00
|
|
|
const navigatorHotkey = "ctrl+k, command+k";
|
2019-11-25 16:23:52 +00:00
|
|
|
const navigatorNotificationStorageKey = "notifiedAboutNavigator";
|
2019-11-20 15:58:17 +00:00
|
|
|
|
2019-11-25 11:29:07 +00:00
|
|
|
function getItemOffset(
|
|
|
|
actions: QuickSearchAction[],
|
|
|
|
cbs: Array<typeof getViews>
|
|
|
|
): number {
|
|
|
|
return cbs.reduce((acc, cb) => cb(actions).length + acc, 0);
|
|
|
|
}
|
|
|
|
|
2019-11-20 15:58:17 +00:00
|
|
|
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 intl = useIntl();
|
2019-11-25 16:23:52 +00:00
|
|
|
const notify = useNotifier();
|
|
|
|
const [notifiedAboutNavigator, setNotifiedAboutNavigator] = useLocalStorage(
|
|
|
|
navigatorNotificationStorageKey,
|
|
|
|
false
|
|
|
|
);
|
2019-11-20 15:58:17 +00:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2019-11-25 15:42:24 +00:00
|
|
|
hotkeys(navigatorHotkey, event => {
|
|
|
|
event.preventDefault();
|
|
|
|
setVisible(!visible);
|
|
|
|
});
|
2019-11-20 15:58:17 +00:00
|
|
|
|
2019-11-25 16:23:52 +00:00
|
|
|
if (cmp(APP_VERSION, "2.1.0") !== 1 && !notifiedAboutNavigator) {
|
|
|
|
notify({
|
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"Our new feature to help you with your daily task. Run Navigator using Ctrl+K shortcut. (Cmd+K for Mac users)",
|
|
|
|
description: "navigator notification"
|
|
|
|
}),
|
|
|
|
title: intl.formatMessage({
|
|
|
|
defaultMessage: "Navigator is here to help",
|
|
|
|
description: "navigator notification title"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
setNotifiedAboutNavigator(true);
|
|
|
|
}
|
|
|
|
|
2019-11-20 15:58:17 +00:00
|
|
|
return () => hotkeys.unbind(navigatorHotkey);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog
|
|
|
|
open={visible}
|
|
|
|
onClose={() => setVisible(false)}
|
|
|
|
fullWidth
|
|
|
|
maxWidth="sm"
|
|
|
|
>
|
|
|
|
<Downshift
|
|
|
|
itemToString={(item: QuickSearchAction) => (item ? item.label : "")}
|
|
|
|
onSelect={(item: QuickSearchAction) => {
|
|
|
|
setVisible(false);
|
2019-11-22 15:39:20 +00:00
|
|
|
item.onClick();
|
2019-11-20 15:58:17 +00:00
|
|
|
}}
|
|
|
|
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-22 15:39:20 +00:00
|
|
|
{hasViews(actions) && (
|
2019-11-21 12:13:41 +00:00
|
|
|
<NavigatorSection
|
|
|
|
label={intl.formatMessage({
|
2019-11-22 15:39:20 +00:00
|
|
|
defaultMessage: "Navigate to",
|
2019-11-21 12:13:41 +00:00
|
|
|
description: "navigator section header"
|
|
|
|
})}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
highlightedIndex={highlightedIndex}
|
2019-11-22 15:39:20 +00:00
|
|
|
items={getViews(actions)}
|
2019-11-22 16:07:16 +00:00
|
|
|
offset={0}
|
2019-11-21 12:13:41 +00:00
|
|
|
/>
|
|
|
|
)}
|
2019-11-22 15:39:20 +00:00
|
|
|
{hasActions(actions) && (
|
2019-11-20 15:58:17 +00:00
|
|
|
<NavigatorSection
|
|
|
|
label={intl.formatMessage({
|
2019-11-22 15:39:20 +00:00
|
|
|
defaultMessage: "Quick Actions",
|
2019-11-20 15:58:17 +00:00
|
|
|
description: "navigator section header"
|
|
|
|
})}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
highlightedIndex={highlightedIndex}
|
2019-11-22 15:39:20 +00:00
|
|
|
items={getActions(actions)}
|
2019-11-25 11:29:07 +00:00
|
|
|
offset={getItemOffset(actions, [getViews])}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{hasCustomers(actions) && (
|
|
|
|
<NavigatorSection
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Search in Customers",
|
|
|
|
description: "navigator section header"
|
|
|
|
})}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
highlightedIndex={highlightedIndex}
|
|
|
|
items={getCustomers(actions)}
|
|
|
|
offset={getItemOffset(actions, [getViews, getActions])}
|
2019-11-20 15:58:17 +00:00
|
|
|
/>
|
|
|
|
)}
|
2019-11-25 14:32:10 +00:00
|
|
|
{hasCatalog(actions) && (
|
|
|
|
<NavigatorSection
|
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Search in Catalog",
|
|
|
|
description: "navigator section header"
|
|
|
|
})}
|
|
|
|
getItemProps={getItemProps}
|
|
|
|
highlightedIndex={highlightedIndex}
|
|
|
|
items={getCatalog(actions)}
|
|
|
|
offset={0}
|
|
|
|
/>
|
|
|
|
)}
|
2019-11-20 15:58:17 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Downshift>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Navigator;
|