2023-04-30 19:09:37 -04:00
|
|
|
import { useContext } from "react";
|
2025-02-18 16:16:53 -08:00
|
|
|
import { SettingsContext } from "utils/contexts/settings";
|
2023-04-30 19:09:37 -04:00
|
|
|
|
2022-10-22 23:00:51 -07:00
|
|
|
import Error from "./error";
|
|
|
|
|
2025-04-05 23:54:48 -07:00
|
|
|
const ALIASED_WIDGETS = {
|
|
|
|
pialert: "netalertx",
|
|
|
|
hoarder: "karakeep",
|
|
|
|
};
|
|
|
|
|
2022-09-29 21:15:25 -07:00
|
|
|
export default function Container({ error = false, children, service }) {
|
2023-04-30 19:09:37 -04:00
|
|
|
const { settings } = useContext(SettingsContext);
|
|
|
|
|
2022-08-25 16:29:26 +03:00
|
|
|
if (error) {
|
2023-04-30 19:09:37 -04:00
|
|
|
if (settings.hideErrors || service.widget.hide_errors) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:26:55 -07:00
|
|
|
return <Error service={service} error={error} />;
|
2022-08-25 16:29:26 +03:00
|
|
|
}
|
|
|
|
|
2023-06-02 14:57:27 +02:00
|
|
|
const childrenArray = Array.isArray(children) ? children : [children];
|
|
|
|
|
|
|
|
let visibleChildren = childrenArray;
|
2023-07-20 23:03:15 -07:00
|
|
|
let fields = service?.widget?.fields;
|
2023-10-17 23:26:55 -07:00
|
|
|
if (typeof fields === "string") fields = JSON.parse(service.widget.fields);
|
2022-09-29 21:15:25 -07:00
|
|
|
const type = service?.widget?.type;
|
|
|
|
if (fields && type) {
|
2023-02-06 13:43:40 -08:00
|
|
|
// if the field contains a "." then it most likely contains a common loc value
|
|
|
|
// logic now allows a fields array that can look like:
|
|
|
|
// fields: [ "resources.cpu", "resources.mem", "field"]
|
|
|
|
// or even
|
|
|
|
// fields: [ "resources.cpu", "widget_type.field" ]
|
2023-10-17 23:26:55 -07:00
|
|
|
visibleChildren = childrenArray?.filter((child) =>
|
|
|
|
fields.some((field) => {
|
|
|
|
let fullField = field;
|
|
|
|
if (!field.includes(".")) {
|
|
|
|
fullField = `${type}.${field}`;
|
|
|
|
}
|
2025-04-05 23:54:48 -07:00
|
|
|
let matches = fullField === child?.props?.label;
|
|
|
|
// check if the field is an 'alias'
|
|
|
|
if (matches) {
|
|
|
|
return true;
|
|
|
|
} else if (ALIASED_WIDGETS[type]) {
|
|
|
|
matches = fullField.replace(type, ALIASED_WIDGETS[type]) === child?.props?.label;
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
// no match
|
|
|
|
return false;
|
2023-10-17 23:26:55 -07:00
|
|
|
}),
|
|
|
|
);
|
2022-09-29 21:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-09-10 23:36:54 +02:00
|
|
|
return <div className="relative flex flex-row w-full service-container">{visibleChildren}</div>;
|
2022-08-25 16:29:26 +03:00
|
|
|
}
|