UNPKG

@uimkit/uikit-react

Version:

<img style="width:64px" src="https://mgmt.uimkit.chat/media/img/avatar.png"/>

58 lines (55 loc) 2.61 kB
import '../../../context/TranslationContext.js'; import '../../../context/UIKitContext.js'; import '../../../context/ComponentContext.js'; import '../../../context/ChatActionContext.js'; import '../../../context/MessageInputContext.js'; import '../../../context/UIMessageContext.js'; import { useChatStateContext } from '../../../context/ChatStateContext.js'; import { UICommandItem } from '../../UICommandItem/UICommandItem.js'; var useCommandTrigger = function () { var chatConfig = useChatStateContext('useCommandTrigger').chatConfig; var commands = chatConfig === null || chatConfig === void 0 ? void 0 : chatConfig.commands; console.log('commands: ', commands); return { component: UICommandItem, dataProvider: function (query, text, onReady) { if (text.indexOf('/') !== 0 || !commands) { return []; } var selectedCommands = commands.filter(function (command) { var _a; return ((_a = command.name) === null || _a === void 0 ? void 0 : _a.indexOf(query)) !== -1; }); // sort alphabetically unless the you're matching the first char selectedCommands.sort(function (a, b) { var _a, _b; var nameA = (_a = a.name) === null || _a === void 0 ? void 0 : _a.toLowerCase(); var nameB = (_b = b.name) === null || _b === void 0 ? void 0 : _b.toLowerCase(); if ((nameA === null || nameA === void 0 ? void 0 : nameA.indexOf(query)) === 0) { nameA = "0".concat(nameA); } if ((nameB === null || nameB === void 0 ? void 0 : nameB.indexOf(query)) === 0) { nameB = "0".concat(nameB); } // Should confirm possible null / undefined when TS is fully implemented if (nameA != null && nameB != null) { if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } } return 0; }); var result = selectedCommands.slice(0, 10); if (onReady) onReady(result.filter(function (result) { return result.name !== undefined; }), query); return result; }, output: function (entity) { return ({ caretPosition: 'next', key: entity.name, text: "/".concat(entity.name), }); }, }; }; export { useCommandTrigger }; //# sourceMappingURL=useCommandTrigger.js.map