@angelerator/uuics-react
Version:
Universal UI Context System - React hooks and components
161 lines (158 loc) • 5.65 kB
JavaScript
'use strict';
var react = require('react');
var uuicsCore = require('@angelerator/uuics-core');
var jsxRuntime = require('react/jsx-runtime');
// src/hooks/useUICS.ts
function useUICS(config) {
const [context, setContext] = react.useState(null);
const [isInitialized, setIsInitialized] = react.useState(false);
const engineRef = react.useRef(null);
react.useEffect(() => {
const engine = new uuicsCore.UUICSEngine(config);
engineRef.current = engine;
engine.initialize().then(() => {
setIsInitialized(true);
const currentContext = engine.getContext();
if (currentContext) {
setContext(currentContext);
}
});
const unsubscribe = engine.subscribe((newContext) => {
setContext(newContext);
});
return () => {
unsubscribe();
engine.destroy();
engineRef.current = null;
};
}, []);
const execute = react.useCallback(async (command) => {
if (!engineRef.current) {
return {
success: false,
message: "Engine not initialized",
error: "UUICS engine is not available"
};
}
return engineRef.current.execute(command);
}, []);
const executeBatch = react.useCallback(async (commands) => {
if (!engineRef.current) {
return [];
}
return engineRef.current.executeBatch(commands);
}, []);
const serialize = react.useCallback((format) => {
if (!engineRef.current) {
return "";
}
return engineRef.current.serialize(format);
}, []);
const scan = react.useCallback(async () => {
if (!engineRef.current) {
return null;
}
return engineRef.current.scan();
}, []);
return {
context,
isInitialized,
execute,
executeBatch,
serialize,
scan,
engine: engineRef.current
};
}
function useUIElement(selector) {
const { context } = useUICS();
const element = react.useMemo(() => {
if (!context) {
return null;
}
return context.elements.find((el) => el.selector === selector) ?? null;
}, [context, selector]);
return element;
}
function useUIElements(type) {
const { context } = useUICS();
const elements = react.useMemo(() => {
if (!context) {
return [];
}
return context.elements.filter((el) => el.type === type);
}, [context, type]);
return elements;
}
var UUICSContext = react.createContext(null);
function UUICSProvider({ children, config }) {
const uuics = useUICS(config);
return /* @__PURE__ */ jsxRuntime.jsx(UUICSContext.Provider, { value: uuics, children });
}
function useUUICSContext() {
const context = react.useContext(UUICSContext);
if (!context) {
throw new Error("useUUICSContext must be used within a UUICSProvider");
}
return context;
}
function DebugPanel({
format = "natural",
className = "",
style = {}
}) {
const { context, serialize, isInitialized } = useUUICSContext();
const [isExpanded, setIsExpanded] = react.useState(true);
const [selectedFormat, setSelectedFormat] = react.useState(format);
if (!isInitialized) {
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `uuics-debug-panel loading ${className}`, style, children: [
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "uuics-debug-header", children: /* @__PURE__ */ jsxRuntime.jsx("h3", { children: "UUICS Debug Panel" }) }),
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "uuics-debug-content", children: "Initializing..." })
] });
}
const serializedContext = serialize(selectedFormat);
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `uuics-debug-panel ${className}`, style, children: [
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "uuics-debug-header", children: [
/* @__PURE__ */ jsxRuntime.jsx("h3", { children: "UUICS Debug Panel" }),
/* @__PURE__ */ jsxRuntime.jsx("button", { onClick: () => setIsExpanded(!isExpanded), children: isExpanded ? "Collapse" : "Expand" })
] }),
isExpanded && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "uuics-debug-controls", children: [
/* @__PURE__ */ jsxRuntime.jsxs("label", { children: [
"Format:",
/* @__PURE__ */ jsxRuntime.jsxs(
"select",
{
value: selectedFormat,
onChange: (e) => setSelectedFormat(e.target.value),
children: [
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "natural", children: "Natural Language" }),
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "json", children: "JSON" }),
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "openapi", children: "OpenAPI" })
]
}
)
] }),
context && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "uuics-debug-stats", children: [
/* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
"Elements: ",
context.elements.length
] }),
/* @__PURE__ */ jsxRuntime.jsxs("span", { children: [
"Actions: ",
context.actions.length
] })
] })
] }),
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "uuics-debug-content", children: /* @__PURE__ */ jsxRuntime.jsx("pre", { children: serializedContext }) })
] })
] });
}
exports.DebugPanel = DebugPanel;
exports.UUICSProvider = UUICSProvider;
exports.useUICS = useUICS;
exports.useUIElement = useUIElement;
exports.useUIElements = useUIElements;
exports.useUUICSContext = useUUICSContext;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map