UNPKG

@atlaskit/editor-plugin-quick-insert

Version:

Quick insert plugin for @atlaskit/editor-core

93 lines • 3.66 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { useCallback, useEffect, useRef } from 'react'; import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '@atlaskit/editor-common/analytics/types/enums'; var SEARCH_DEBOUNCE_MS = 1000; export var useQuickInsertSearchAnalytics = function useQuickInsertSearchAnalytics(_ref) { var query = _ref.query, resultCount = _ref.resultCount, surface = _ref.surface, category = _ref.category, _ref$isOpen = _ref.isOpen, isOpen = _ref$isOpen === void 0 ? true : _ref$isOpen, fireAnalyticsEvent = _ref.fireAnalyticsEvent; var latestOptions = useRef({ query: query, resultCount: resultCount, surface: surface, category: category, fireAnalyticsEvent: fireAnalyticsEvent }); latestOptions.current = { query: query, resultCount: resultCount, surface: surface, category: category, fireAnalyticsEvent: fireAnalyticsEvent }; var pendingSearch = useRef(null); var effectVersion = useRef(0); var flush = useCallback(function () { var pending = pendingSearch.current; if (!pending) { return; } clearTimeout(pending.timer); pendingSearch.current = null; var current = latestOptions.current; if (current.query.trim() !== pending.query || !current.fireAnalyticsEvent) { return; } var attributes = _objectSpread(_objectSpread({}, current.surface === 'element-browser' && current.category !== undefined ? { category: current.category } : {}), {}, { isEmpty: current.resultCount === 0, queryLength: current.query.trim().length, resultCount: current.resultCount, surface: current.surface }); var payload = { action: ACTION.SEARCHED, actionSubject: ACTION_SUBJECT.QUICK_INSERT, eventType: EVENT_TYPE.TRACK, attributes: attributes }; current.fireAnalyticsEvent(payload); }, []); useEffect(function () { var currentVersion = ++effectVersion.current; if (!isOpen) { flush(); return; } var trimmedQuery = query.trim(); if (!trimmedQuery) { if (pendingSearch.current) { clearTimeout(pendingSearch.current.timer); pendingSearch.current = null; } return; } if (pendingSearch.current) { clearTimeout(pendingSearch.current.timer); } var timer = setTimeout(flush, SEARCH_DEBOUNCE_MS); pendingSearch.current = { query: trimmedQuery, timer: timer }; return function () { if (pendingSearch.current) { clearTimeout(pendingSearch.current.timer); } Promise.resolve().then(function () { // oxlint-disable-next-line react-hooks/exhaustive-deps -- The live effect version distinguishes dependency cleanup from a real unmount. if (effectVersion.current === currentVersion) { flush(); } }); }; }, [query, surface, category, isOpen, flush]); return flush; };