UNPKG

@redocly/theme

Version:

Shared UI components lib

47 lines 2.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSearchDialog = useSearchDialog; const react_1 = require("react"); const react_router_dom_1 = require("react-router-dom"); const hotkeys_js_1 = __importDefault(require("hotkeys-js")); const use_theme_hooks_1 = require("../use-theme-hooks"); const use_theme_config_1 = require("../use-theme-config"); const contexts_1 = require("../../contexts"); function useSearchDialog() { var _a, _b; const [isOpen, setIsOpen] = (0, react_1.useState)(false); const themeSettings = (0, use_theme_config_1.useThemeConfig)(); const location = (0, react_router_dom_1.useLocation)(); const { useTelemetry } = (0, use_theme_hooks_1.useThemeHooks)(); const telemetry = useTelemetry(); const { refreshSearchSessionId } = (0, contexts_1.useSearchSession)(); const keyShortcuts = (_b = (_a = themeSettings === null || themeSettings === void 0 ? void 0 : themeSettings.search) === null || _a === void 0 ? void 0 : _a.shortcuts) !== null && _b !== void 0 ? _b : ['⌘+K,CTRL+K']; const hotKeys = keyShortcuts === null || keyShortcuts === void 0 ? void 0 : keyShortcuts.join(','); (0, react_1.useEffect)(() => { if (hotKeys) { (0, hotkeys_js_1.default)(hotKeys, (ev) => { setIsOpen(true); telemetry.sendSearchOpenedMessage([{ object: 'search', method: 'shortcut' }]); ev.preventDefault(); }); return () => hotkeys_js_1.default.unbind(hotKeys); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [hotKeys]); const onOpen = (0, react_1.useCallback)(function () { telemetry.sendSearchOpenedMessage([{ object: 'search', method: 'click' }]); setIsOpen(true); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const onClose = (0, react_1.useCallback)(() => { refreshSearchSessionId(); setIsOpen(false); }, [refreshSearchSessionId]); // eslint-disable-next-line react-hooks/exhaustive-deps (0, react_1.useEffect)(onClose, [location]); return { isOpen, onOpen, onClose }; } //# sourceMappingURL=use-search-dialog.js.map