Refactored Code
This commit is contained in:
1 parent
8bacbcde89
commit
73a6277feb
1 file changed
+33
-50
@@ -94,76 +94,59 @@ const CreateInfrastructureMonitor = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isCreate) {
|
if (isCreate) {
|
||||||
// If global settings are not loaded yet, use default thresholds
|
|
||||||
if (globalSettingsLoading) return;
|
if (globalSettingsLoading) return;
|
||||||
|
|
||||||
// Create mode: use global thresholds
|
const gt = globalSettings?.data?.settings?.globalThresholds || {};
|
||||||
const globalThresholds = globalSettings?.data?.settings?.globalThresholds || {};
|
|
||||||
const defaultThresholds = {
|
|
||||||
cpu: "",
|
|
||||||
memory: "",
|
|
||||||
disk: "",
|
|
||||||
temperature: "",
|
|
||||||
};
|
|
||||||
|
|
||||||
setInfrastructureMonitor((prev) => ({
|
setHttps(false);
|
||||||
...prev,
|
|
||||||
|
setInfrastructureMonitor({
|
||||||
url: "",
|
url: "",
|
||||||
name: "",
|
name: "",
|
||||||
notifications: [],
|
notifications: [],
|
||||||
interval: 0.25,
|
interval: 0.25,
|
||||||
cpu: globalThresholds.cpu != null,
|
cpu: gt.cpu !== undefined,
|
||||||
usage_cpu:
|
usage_cpu: gt.cpu !== undefined ? gt.cpu.toString() : "",
|
||||||
globalThresholds.cpu != null
|
memory: gt.memory !== undefined,
|
||||||
? globalThresholds.cpu.toString()
|
usage_memory: gt.memory !== undefined ? gt.memory.toString() : "",
|
||||||
: defaultThresholds.cpu,
|
disk: gt.disk !== undefined,
|
||||||
memory: globalThresholds.memory != null,
|
usage_disk: gt.disk !== undefined ? gt.disk.toString() : "",
|
||||||
usage_memory:
|
temperature: gt.temperature !== undefined,
|
||||||
globalThresholds.memory != null
|
usage_temperature: gt.temperature !== undefined ? gt.temperature.toString() : "",
|
||||||
? globalThresholds.memory.toString()
|
secret: "",
|
||||||
: defaultThresholds.memory,
|
});
|
||||||
disk: globalThresholds.disk != null,
|
|
||||||
usage_disk:
|
|
||||||
globalThresholds.disk != null
|
|
||||||
? globalThresholds.disk.toString()
|
|
||||||
: defaultThresholds.disk,
|
|
||||||
temperature: globalThresholds.temperature != null,
|
|
||||||
usage_temperature:
|
|
||||||
globalThresholds.temperature != null
|
|
||||||
? globalThresholds.temperature.toString()
|
|
||||||
: defaultThresholds.temperature,
|
|
||||||
}));
|
|
||||||
} else if (monitor) {
|
} else if (monitor) {
|
||||||
// Edit mode: use monitor only, ignore global thresholds
|
const { thresholds = {} } = monitor;
|
||||||
setInfrastructureMonitor((prev) => ({
|
|
||||||
...prev,
|
setHttps(monitor.url.startsWith("https"));
|
||||||
url: monitor.url?.replace(/^https?:\/\//, "") || "",
|
|
||||||
|
setInfrastructureMonitor({
|
||||||
|
url: monitor.url.replace(/^https?:\/\//, ""),
|
||||||
name: monitor.name || "",
|
name: monitor.name || "",
|
||||||
notifications: monitor.notifications || [],
|
notifications: monitor.notifications || [],
|
||||||
interval: monitor.interval / MS_PER_MINUTE,
|
interval: monitor.interval / MS_PER_MINUTE,
|
||||||
cpu: monitor.thresholds?.usage_cpu != null,
|
cpu: thresholds.usage_cpu !== undefined,
|
||||||
usage_cpu:
|
usage_cpu:
|
||||||
monitor.thresholds?.usage_cpu != null
|
thresholds.usage_cpu !== undefined
|
||||||
? (monitor.thresholds.usage_cpu * 100).toString()
|
? (thresholds.usage_cpu * 100).toString()
|
||||||
: "",
|
: "",
|
||||||
memory: monitor.thresholds?.usage_memory != null,
|
memory: thresholds.usage_memory !== undefined,
|
||||||
usage_memory:
|
usage_memory:
|
||||||
monitor.thresholds?.usage_memory != null
|
thresholds.usage_memory !== undefined
|
||||||
? (monitor.thresholds.usage_memory * 100).toString()
|
? (thresholds.usage_memory * 100).toString()
|
||||||
: "",
|
: "",
|
||||||
disk: monitor.thresholds?.usage_disk != null,
|
disk: thresholds.usage_disk !== undefined,
|
||||||
usage_disk:
|
usage_disk:
|
||||||
monitor.thresholds?.usage_disk != null
|
thresholds.usage_disk !== undefined
|
||||||
? (monitor.thresholds.usage_disk * 100).toString()
|
? (thresholds.usage_disk * 100).toString()
|
||||||
: "",
|
: "",
|
||||||
temperature: monitor.thresholds?.usage_temperature != null,
|
temperature: thresholds.usage_temperature !== undefined,
|
||||||
usage_temperature:
|
usage_temperature:
|
||||||
monitor.thresholds?.usage_temperature != null
|
thresholds.usage_temperature !== undefined
|
||||||
? (monitor.thresholds.usage_temperature * 100).toString()
|
? (thresholds.usage_temperature * 100).toString()
|
||||||
: "",
|
: "",
|
||||||
secret: monitor.secret || "",
|
secret: monitor.secret || "",
|
||||||
}));
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}, [isCreate, monitor, globalSettings, globalSettingsLoading]);
|
}, [isCreate, monitor, globalSettings, globalSettingsLoading]);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user