@copilotkit/react-core
Version:
<div align="center"> <a href="https://copilotkit.ai" target="_blank"> <img src="https://github.com/copilotkit/copilotkit/raw/main/assets/banner.png" alt="CopilotKit Logo"> </a>
70 lines (69 loc) • 1.9 kB
JavaScript
// src/hooks/use-flat-category-store.ts
import { useCallback, useReducer } from "react";
import { randomId } from "@copilotkit/shared";
var useFlatCategoryStore = () => {
const [elements, dispatch] = useReducer(flatCategoryStoreReducer, /* @__PURE__ */ new Map());
const addElement = useCallback((value, categories) => {
const newId = randomId();
dispatch({
type: "ADD_ELEMENT",
value,
id: newId,
categories
});
return newId;
}, []);
const removeElement = useCallback((id) => {
dispatch({ type: "REMOVE_ELEMENT", id });
}, []);
const allElements = useCallback(
(categories) => {
const categoriesSet = new Set(categories);
const result = [];
elements.forEach((element) => {
if (setsHaveIntersection(categoriesSet, element.categories)) {
result.push(element.value);
}
});
return result;
},
[elements]
);
return { addElement, removeElement, allElements };
};
var use_flat_category_store_default = useFlatCategoryStore;
function flatCategoryStoreReducer(state, action) {
switch (action.type) {
case "ADD_ELEMENT": {
const { value, id, categories } = action;
const newElement = {
id,
value,
categories: new Set(categories)
};
const newState = new Map(state);
newState.set(id, newElement);
return newState;
}
case "REMOVE_ELEMENT": {
const newState = new Map(state);
newState.delete(action.id);
return newState;
}
default:
return state;
}
}
function setsHaveIntersection(setA, setB) {
const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
for (let item of smallerSet) {
if (largerSet.has(item)) {
return true;
}
}
return false;
}
export {
use_flat_category_store_default
};
//# sourceMappingURL=chunk-5FHSUKQL.mjs.map