stackchain-dashboard/frontend/commands.js
timmy 7651e5b090
All checks were successful
CI / lint (pull_request) Successful in 15s
CI / build-frontend (pull_request) Successful in 5s
feat: add global work search (#195)
2026-08-07 12:27:53 +00:00

50 lines
1.7 KiB
JavaScript

(function (root, factory) {
const filterCommands = factory();
if (typeof module === 'object' && module.exports) module.exports = filterCommands;
if (root) root.filterCommands = filterCommands;
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
function filterCommands(commands, filter) {
const term = String(filter || '').toLowerCase();
return commands.filter(command => command.name.toLowerCase().includes(term));
}
filterCommands.nextSelection = function nextSelection(current, key, count) {
if (count < 1) return -1;
if (key === 'ArrowDown') return (current + 1 + count) % count;
if (key === 'ArrowUp') return (current - 1 + count) % count;
return current;
};
filterCommands.createGlobalSearchController = function createGlobalSearchController(options) {
const search = options.search;
const onState = options.onState;
const delay = options.delay === undefined ? 250 : options.delay;
let timer = null;
let generation = 0;
return {
setQuery(value) {
const query = String(value || '').trim();
generation += 1;
const current = generation;
if (timer !== null) clearTimeout(timer);
if (query.length < 2) {
onState({ status: 'idle', query, items: [] });
return;
}
onState({ status: 'loading', query, items: [] });
timer = setTimeout(async () => {
try {
const items = await search(query);
if (current === generation) onState({ status: 'ready', query, items });
} catch (error) {
if (current === generation) onState({ status: 'error', query, items: [], error });
}
}, delay);
},
};
};
return filterCommands;
});