// @ts-strict-ignore import useLocalStorage from "@dashboard/hooks/useLocalStorage"; import useNotifier from "@dashboard/hooks/useNotifier"; import { Divider, Fade, Modal, Paper } from "@material-ui/core"; import { makeStyles, useTheme } from "@saleor/macaw-ui"; import Downshift from "downshift"; import hotkeys from "hotkeys-js"; import React from "react"; import { useIntl } from "react-intl"; import { getActions, getCatalog, getCustomers, getViews, hasActions, hasCatalog, hasCustomers, hasViews, } from "./modes/utils"; import NavigatorInput from "./NavigatorInput"; import NavigatorSection from "./NavigatorSection"; import { QuickSearchAction } from "./types"; import useQuickSearch from "./useQuickSearch"; const navigatorHotkey = "ctrl+k, command+k"; const navigatorNotificationStorageKey = "notifiedAboutNavigator"; function getItemOffset( actions: QuickSearchAction[], cbs: Array, ): number { return cbs.reduce((acc, cb) => cb(actions).length + acc, 0); } const useStyles = makeStyles( theme => ({ modal: { alignItems: "center", display: "flex", justifyContent: "center", padding: theme.spacing(3), }, paper: { overflow: "hidden", }, root: { [theme.breakpoints.down("sm")]: { height: "auto", }, height: 500, maxWidth: 900, outline: 0, width: "100%", }, }), { name: "Navigator", }, ); export interface NavigatorProps { visible: boolean; setVisibility: (state: boolean) => void; } const Navigator: React.FC = ({ visible, setVisibility }) => { const input = React.useRef(null); const [query, mode, change, actions] = useQuickSearch(visible, input); const intl = useIntl(); const notify = useNotifier(); const [notifiedAboutNavigator, setNotifiedAboutNavigator] = useLocalStorage( navigatorNotificationStorageKey, false, ); const classes = useStyles({}); const theme = useTheme(); React.useEffect(() => { hotkeys(navigatorHotkey, event => { event.preventDefault(); setVisibility(!visible); }); if (!notifiedAboutNavigator) { notify({ text: intl.formatMessage( { id: "EM+30g", defaultMessage: "Our new feature to help you with your daily tasks. Run Navigator using {keyboardShortcut} shortcut.", description: "navigator notification", }, { keyboardShortcut: navigator.platform.toLowerCase().includes("mac") ? "⌘+K" : "Ctrl+K", }, ), title: intl.formatMessage({ id: "Gxm7Qx", defaultMessage: "Navigator is here to help", description: "navigator notification title", }), }); setNotifiedAboutNavigator(true); } return () => hotkeys.unbind(navigatorHotkey); }, []); const hasAnything = hasViews(actions) || hasActions(actions) || hasCustomers(actions) || hasCatalog(actions); return ( setVisibility(false)} >
item ? item.label : "" } onSelect={(item: QuickSearchAction) => { const shouldRemainVisible = item.onClick(); if (!shouldRemainVisible) { setVisibility(false); } }} onInputValueChange={value => change({ target: { name: "query", value, }, }) } defaultHighlightedIndex={0} > {({ getInputProps, getItemProps, highlightedIndex }) => (
)} ref={input} /> {hasAnything && } {hasViews(actions) && ( )} {hasActions(actions) && ( )} {hasCustomers(actions) && ( )} {hasCatalog(actions) && ( )}
)}
); }; export default Navigator;