UNPKG

@stolostron/multicluster-sdk

Version:

Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.

171 lines 10.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); /* Copyright Contributors to the Open Cluster Management project */ const react_1 = require("react"); const react_i18next_1 = require("react-i18next"); const _ = __importStar(require("lodash")); const api_1 = require("../../api"); const utils_1 = require("./utils"); const constants_1 = require("./constants"); const react_core_1 = require("@patternfly/react-core"); const react_styles_1 = require("@patternfly/react-styles"); const TogglePlay_1 = __importDefault(require("./TogglePlay")); const EventStreamList_1 = require("./EventStreamList"); const EventComponent_1 = __importDefault(require("./EventComponent")); const ResourceEventStream = ({ resource }) => { var _a, _b, _c; const [active, setActive] = (0, react_1.useState)(true); const hubCluster = (0, api_1.useHubClusterName)(); const { t } = (0, react_i18next_1.useTranslation)('public'); const [sortedEvents, setSortedEvents] = (0, react_1.useState)([]); const [error, setError] = (0, react_1.useState)(false); const [loading, setLoading] = (0, react_1.useState)(true); const ws = (0, react_1.useRef)(); const [backendAPIPath, loaded] = (0, api_1.useFleetK8sAPIPath)(resource === null || resource === void 0 ? void 0 : resource.cluster); const fieldSelector = `involvedObject.uid=${(_a = resource === null || resource === void 0 ? void 0 : resource.metadata) === null || _a === void 0 ? void 0 : _a.uid},involvedObject.name=${(_b = resource === null || resource === void 0 ? void 0 : resource.metadata) === null || _b === void 0 ? void 0 : _b.name},involvedObject.kind=${resource === null || resource === void 0 ? void 0 : resource.kind}`; const namespace = (_c = resource === null || resource === void 0 ? void 0 : resource.metadata) === null || _c === void 0 ? void 0 : _c.namespace; // Handle websocket setup and teardown when dependent props change (0, react_1.useEffect)(() => { var _a; if (!resource.cluster || resource.cluster === hubCluster || !loaded) return; (_a = ws.current) === null || _a === void 0 ? void 0 : _a.close(); const watchURLOptions = Object.assign(Object.assign({ cluster: resource.cluster }, (namespace ? { ns: namespace } : {})), (fieldSelector ? { fieldSelector, } : {})); if (!ws.current) { ws.current = (0, api_1.fleetWatch)(utils_1.EventModel, watchURLOptions, backendAPIPath); if (ws.current === undefined) return; ws.current.onmessage = (message) => { if (!active) return; const eventdataParsed = JSON.parse(message.data); if (!eventdataParsed) return; const eventType = eventdataParsed.type; const object = eventdataParsed.object; setSortedEvents((currentSortedEvents) => { var _a; const topEvents = currentSortedEvents.slice(0, constants_1.MAX_MESSAGES); const uid = ((_a = object === null || object === void 0 ? void 0 : object.metadata) === null || _a === void 0 ? void 0 : _a.uid) || ''; switch (eventType) { case 'ADDED': case 'MODIFIED': const eventAlreadyExists = topEvents.find((e) => { var _a; return ((_a = e === null || e === void 0 ? void 0 : e.metadata) === null || _a === void 0 ? void 0 : _a.uid) === uid; }); if (eventAlreadyExists && (eventAlreadyExists === null || eventAlreadyExists === void 0 ? void 0 : eventAlreadyExists.count) !== undefined && (object === null || object === void 0 ? void 0 : object.count) !== undefined && eventAlreadyExists.count > object.count) { // We already have a more recent version of this message stored, so skip this one return topEvents; } return (0, utils_1.sortEvents)([...topEvents, object]); case 'DELETED': return topEvents.filter((e) => { var _a; return ((_a = e === null || e === void 0 ? void 0 : e.metadata) === null || _a === void 0 ? void 0 : _a.uid) !== uid; }); default: // eslint-disable-next-line no-console console.error(`UNHANDLED EVENT: ${eventType}`); return topEvents; } }); }; ws.current.onopen = () => { setActive(true); setError(false); setLoading(false); }; ws.current.onclose = (evt) => { setActive(false); if ((evt === null || evt === void 0 ? void 0 : evt.wasClean) === false) { setError(evt.reason || t('public~Connection did not close cleanly.')); } }; ws.current.onerror = () => { setActive(false); setError(true); }; } return () => { var _a; (_a = ws.current) === null || _a === void 0 ? void 0 : _a.close(); }; }, [namespace, fieldSelector, active, t]); const count = sortedEvents.length; const noEvents = count === 0; const noMatches = count > 0 && count === 0; let sysEventStatus, statusBtnTxt; if (noEvents || (noMatches && resource)) { sysEventStatus = (0, jsx_runtime_1.jsx)(react_core_1.EmptyState, { title: t('public~No events') }); } if (noMatches && !resource) { sysEventStatus = ((0, jsx_runtime_1.jsx)(react_core_1.EmptyState, { title: t('public~No matching events'), children: count >= constants_1.MAX_MESSAGES ? t('public~{{count}}+ event exist, but none match the current filter', { count: constants_1.MAX_MESSAGES, }) : t('public~{{count}} event exist, but none match the current filter', { count, }) })); } if (error) { statusBtnTxt = ((0, jsx_runtime_1.jsx)("span", { className: "co-sysevent-stream__connection-error", children: _.isString(error) ? t('public~Error connecting to event stream: { error }', { error, }) : t('public~Error connecting to event stream') })); sysEventStatus = ((0, jsx_runtime_1.jsx)(react_core_1.EmptyState, { title: t('public~Error loading events'), children: t('public~An error occurred during event retrieval. Attempting to reconnect...') })); } else if (loading) { statusBtnTxt = (0, jsx_runtime_1.jsx)("span", { children: t('public~Loading events...') }); sysEventStatus = (0, jsx_runtime_1.jsx)(react_core_1.Spinner, {}); } else if (active) { statusBtnTxt = (0, jsx_runtime_1.jsx)("span", { children: t('public~Streaming events...') }); } else { statusBtnTxt = (0, jsx_runtime_1.jsx)("span", { children: t('public~Event stream is paused.') }); } const klass = (0, react_styles_1.css)('co-sysevent-stream__timeline', { 'co-sysevent-stream__timeline--empty': !count, }); const messageCount = count < constants_1.MAX_MESSAGES ? t('public~Showing {{count}} event', { count }) : t('public~Showing most recent {{count}} event', { count }); const toggleStream = () => { setActive((prev) => !prev); }; if (!resource.cluster || resource.cluster === hubCluster) return (0, jsx_runtime_1.jsx)(ResourceEventStream, { resource: resource }); return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(react_core_1.PageSection, { children: (0, jsx_runtime_1.jsxs)("div", { className: "co-sysevent-stream", children: [(0, jsx_runtime_1.jsxs)("div", { className: "co-sysevent-stream__status", children: [(0, jsx_runtime_1.jsx)("div", { className: "co-sysevent-stream__timeline__btn-text", children: statusBtnTxt }), (0, jsx_runtime_1.jsx)("div", { className: "co-sysevent-stream__totals pf-v6-u-text-color-subtle", "data-test": "event-totals", children: messageCount })] }), (0, jsx_runtime_1.jsxs)("div", { className: klass, children: [(0, jsx_runtime_1.jsx)(TogglePlay_1.default, { active: active, onClick: toggleStream, className: "co-sysevent-stream__timeline__btn" }), (0, jsx_runtime_1.jsx)("div", { className: "co-sysevent-stream__timeline__end-message", children: t('public~Older events are not stored.') })] }), count > 0 && (0, jsx_runtime_1.jsx)(EventStreamList_1.EventStreamList, { events: sortedEvents, EventComponent: EventComponent_1.default }), sysEventStatus] }) }) })); }; exports.default = ResourceEventStream; //# sourceMappingURL=EventStream.js.map