Merge branch 'develop' into fix/2336-settings-select-value-type

This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-06-01 11:16:20 -07:00
commit 8075d3c63c
15 files changed
+149 -120

No files matched your search

@@ -0,0 +1,16 @@
import i18n from "../../Utils/i18n";
import { useSelector } from "react-redux";
import { useEffect } from "react";
const I18nLoader = () => {
const language = useSelector((state) => state.ui.language);
useEffect(() => {
if (language && i18n.language !== language) {
i18n.changeLanguage(language);
}
}, [language]);
return null;
};
export default I18nLoader;
@@ -164,7 +164,9 @@ Select.propTypes = {
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired, value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired,
items: PropTypes.arrayOf( items: PropTypes.arrayOf(
PropTypes.shape({ PropTypes.shape({
_id: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]).isRequired, _id: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool])
.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
}) })
).isRequired, ).isRequired,
+4 -3
View File
@@ -3,15 +3,17 @@ import { Box, MenuItem, Select, Stack } from "@mui/material";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import "flag-icons/css/flag-icons.min.css"; import "flag-icons/css/flag-icons.min.css";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { useDispatch } from "react-redux";
import { setLanguage } from "../Features/UI/uiSlice";
const LanguageSelector = () => { const LanguageSelector = () => {
const { i18n } = useTranslation(); const { i18n } = useTranslation();
const theme = useTheme(); const theme = useTheme();
const { language } = useSelector((state) => state.ui); const { language } = useSelector((state) => state.ui);
const dispatch = useDispatch();
const handleChange = (event) => { const handleChange = (event) => {
const newLang = event.target.value; const newLang = event.target.value;
i18n.changeLanguage(newLang); dispatch(setLanguage(newLang));
}; };
const languages = Object.keys(i18n.options.resources || {}); const languages = Object.keys(i18n.options.resources || {});
@@ -32,7 +34,6 @@ const LanguageSelector = () => {
} }
if (parsedLang.includes("-")) { if (parsedLang.includes("-")) {
parsedLang = parsedLang.split("-")[1].toLowerCase(); parsedLang = parsedLang.split("-")[1].toLowerCase();
console.log("parsedLang", parsedLang);
} }
const flag = parsedLang ? `fi fi-${parsedLang}` : null; const flag = parsedLang ? `fi fi-${parsedLang}` : null;
@@ -5,7 +5,6 @@ const initialState = {
isLoading: false, isLoading: false,
apiBaseUrl: "", apiBaseUrl: "",
logLevel: "debug", logLevel: "debug",
language: "gb",
pagespeedApiKey: "", pagespeedApiKey: "",
}; };
+2 -2
View File
@@ -24,7 +24,7 @@ const initialState = {
greeting: { index: 0, lastUpdate: null }, greeting: { index: 0, lastUpdate: null },
timezone: "America/Toronto", timezone: "America/Toronto",
distributedUptimeEnabled: false, distributedUptimeEnabled: false,
language: "gb", language: "en",
starPromptOpen: true, starPromptOpen: true,
}; };
@@ -57,7 +57,7 @@ const uiSlice = createSlice({
setTimezone(state, action) { setTimezone(state, action) {
state.timezone = action.payload.timezone; state.timezone = action.payload.timezone;
}, },
setLanguage(state, action) { setLanguage: (state, action) => {
state.language = action.payload; state.language = action.payload;
}, },
setStarPromptOpen: (state, action) => { setStarPromptOpen: (state, action) => {
+1 -1
View File
@@ -22,7 +22,7 @@ const SettingsURL = ({ HEADING_SX, handleChange, showURL = false }) => {
<Select <Select
name="showURL" name="showURL"
label={t("settingsURLSelectTitle")} label={t("settingsURLSelectTitle")}
value={showURL} value={showURL || ""}
onChange={handleChange} onChange={handleChange}
items={[ items={[
{ _id: true, name: t("settingsURLEnabled") }, { _id: true, name: t("settingsURLEnabled") },
-1
View File
@@ -93,7 +93,6 @@ const Settings = () => {
if (name === "language") { if (name === "language") {
dispatch(setLanguage(value)); dispatch(setLanguage(value));
i18n.changeLanguage(value);
} }
if (name === "deleteStats") { if (name === "deleteStats") {
@@ -17,14 +17,17 @@ const StatusBoxes = ({ shouldRender, monitorsSummary }) => {
> >
<StatusBox <StatusBox
title={t("monitorStatus.up")} title={t("monitorStatus.up")}
status="up"
value={monitorsSummary?.upMonitors ?? 0} value={monitorsSummary?.upMonitors ?? 0}
/> />
<StatusBox <StatusBox
title={t("monitorStatus.down")} title={t("monitorStatus.down")}
status="down"
value={monitorsSummary?.downMonitors ?? 0} value={monitorsSummary?.downMonitors ?? 0}
/> />
<StatusBox <StatusBox
title={t("monitorStatus.paused")} title={t("monitorStatus.paused")}
status="paused"
value={monitorsSummary?.pausedMonitors ?? 0} value={monitorsSummary?.pausedMonitors ?? 0}
/> />
</Stack> </Stack>
@@ -5,9 +5,8 @@ import Arrow from "../../../../../assets/icons/top-right-arrow.svg?react";
import Background from "../../../../../assets/Images/background-grid.svg?react"; import Background from "../../../../../assets/Images/background-grid.svg?react";
import ClockSnooze from "../../../../../assets/icons/clock-snooze.svg?react"; import ClockSnooze from "../../../../../assets/icons/clock-snooze.svg?react";
const StatusBox = ({ title, value }) => { const StatusBox = ({ title, value, status }) => {
const theme = useTheme(); const theme = useTheme();
let sharedStyles = { let sharedStyles = {
position: "absolute", position: "absolute",
right: 8, right: 8,
@@ -17,21 +16,21 @@ const StatusBox = ({ title, value }) => {
let color; let color;
let icon; let icon;
if (title === "up") { if (status === "up") {
color = theme.palette.success.lowContrast; color = theme.palette.success.lowContrast;
icon = ( icon = (
<Box sx={{ ...sharedStyles, top: 8 }}> <Box sx={{ ...sharedStyles, top: 8 }}>
<Arrow /> <Arrow />
</Box> </Box>
); );
} else if (title === "down") { } else if (status === "down") {
color = theme.palette.error.lowContrast; color = theme.palette.error.lowContrast;
icon = ( icon = (
<Box sx={{ ...sharedStyles, transform: "rotate(180deg)", top: 5 }}> <Box sx={{ ...sharedStyles, transform: "rotate(180deg)", top: 5 }}>
<Arrow /> <Arrow />
</Box> </Box>
); );
} else if (title === "paused") { } else if (status === "paused") {
color = theme.palette.warning.lowContrast; color = theme.palette.warning.lowContrast;
icon = ( icon = (
<Box sx={{ ...sharedStyles, top: 12, right: 12 }}> <Box sx={{ ...sharedStyles, top: 12, right: 12 }}>
@@ -100,8 +99,9 @@ const StatusBox = ({ title, value }) => {
}; };
StatusBox.propTypes = { StatusBox.propTypes = {
title: PropTypes.oneOf(["up", "down", "paused"]).isRequired, title: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
status: PropTypes.string,
}; };
export default StatusBox; export default StatusBox;
-1
View File
@@ -4,7 +4,6 @@ const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
const FALLBACK_BASE_URL = "http://localhost:5000/api/v1"; const FALLBACK_BASE_URL = "http://localhost:5000/api/v1";
import { clearAuthState } from "../Features/Auth/authSlice"; import { clearAuthState } from "../Features/Auth/authSlice";
import { clearUptimeMonitorState } from "../Features/UptimeMonitors/uptimeMonitorsSlice"; import { clearUptimeMonitorState } from "../Features/UptimeMonitors/uptimeMonitorsSlice";
class NetworkService { class NetworkService {
constructor(store, dispatch, navigate) { constructor(store, dispatch, navigate) {
this.store = store; this.store = store;
+2 -10
View File
@@ -1,10 +1,9 @@
import i18n from "i18next"; import i18n from "i18next";
import { initReactI18next } from "react-i18next"; import { initReactI18next } from "react-i18next";
import { setLanguage } from "../Features/UI/uiSlice";
import store from "../store";
const primaryLanguage = "en"; const primaryLanguage = "en";
// Load all translation files eagerly
const translations = import.meta.glob("../locales/*.json", { eager: true }); const translations = import.meta.glob("../locales/*.json", { eager: true });
const resources = {}; const resources = {};
@@ -15,12 +14,9 @@ Object.keys(translations).forEach((path) => {
}; };
}); });
const savedLanguage = store.getState()?.ui?.language;
const initialLanguage = savedLanguage;
i18n.use(initReactI18next).init({ i18n.use(initReactI18next).init({
resources, resources,
lng: initialLanguage, lng: primaryLanguage,
fallbackLng: primaryLanguage, fallbackLng: primaryLanguage,
debug: import.meta.env.MODE === "development", debug: import.meta.env.MODE === "development",
ns: ["translation"], ns: ["translation"],
@@ -30,8 +26,4 @@ i18n.use(initReactI18next).init({
}, },
}); });
i18n.on("languageChanged", (lng) => {
store.dispatch(setLanguage(lng));
});
export default i18n; export default i18n;
+1
View File
@@ -277,6 +277,7 @@ const settingsValidation = joi.object({
}), }),
pagespeedApiKey: joi.string().allow("").optional(), pagespeedApiKey: joi.string().allow("").optional(),
language: joi.string().required(), language: joi.string().required(),
timezone: joi.string().allow("").optional(),
systemEmailHost: joi.string().allow(""), systemEmailHost: joi.string().allow(""),
systemEmailPort: joi.number().allow(null, ""), systemEmailPort: joi.number().allow(null, ""),
systemEmailAddress: joi.string().allow(""), systemEmailAddress: joi.string().allow(""),
+2 -1
View File
@@ -1,7 +1,6 @@
import ReactDOM from "react-dom/client"; import ReactDOM from "react-dom/client";
import App from "./App.jsx"; import App from "./App.jsx";
import "./index.css"; import "./index.css";
import "./Utils/i18n";
import { BrowserRouter as Router } from "react-router-dom"; import { BrowserRouter as Router } from "react-router-dom";
import { Provider } from "react-redux"; import { Provider } from "react-redux";
import { persistor, store } from "./store"; import { persistor, store } from "./store";
@@ -9,6 +8,7 @@ import { PersistGate } from "redux-persist/integration/react";
import NetworkServiceProvider from "./Utils/NetworkServiceProvider.jsx"; import NetworkServiceProvider from "./Utils/NetworkServiceProvider.jsx";
import { networkService } from "./Utils/NetworkService"; import { networkService } from "./Utils/NetworkService";
export { networkService }; export { networkService };
import I18nLoader from "./Components/I18nLoader";
ReactDOM.createRoot(document.getElementById("root")).render( ReactDOM.createRoot(document.getElementById("root")).render(
<Provider store={store}> <Provider store={store}>
@@ -16,6 +16,7 @@ ReactDOM.createRoot(document.getElementById("root")).render(
loading={null} loading={null}
persistor={persistor} persistor={persistor}
> >
<I18nLoader />
<Router> <Router>
<NetworkServiceProvider> <NetworkServiceProvider>
<App /> <App />
+108 -93
View File
@@ -1,11 +1,12 @@
const SERVICE_NAME = "NotificationService"; const SERVICE_NAME = "NotificationService";
const TELEGRAM_API_BASE_URL = "https://api.telegram.org/bot"; const TELEGRAM_API_BASE_URL = "https://api.telegram.org/bot";
const PLATFORM_TYPES = ["telegram", "slack", "discord"]; const PLATFORM_TYPES = ["telegram", "slack", "discord", "webhook"];
const MESSAGE_FORMATTERS = { const MESSAGE_FORMATTERS = {
telegram: (messageText, chatId) => ({ chat_id: chatId, text: messageText }), telegram: (messageText, chatId) => ({ chat_id: chatId, text: messageText }),
slack: (messageText) => ({ text: messageText }), slack: (messageText) => ({ text: messageText }),
discord: (messageText) => ({ content: messageText }), discord: (messageText) => ({ content: messageText }),
webhook: (messageText) => ({ text: messageText }),
}; };
class NotificationService { class NotificationService {
@@ -42,49 +43,57 @@ class NotificationService {
formatNotificationMessage(monitor, status, platform, chatId, code, timestamp) { formatNotificationMessage(monitor, status, platform, chatId, code, timestamp) {
// Format timestamp using the local system timezone // Format timestamp using the local system timezone
const formatTime = (timestamp) => { const formatTime = (timestamp) => {
const date = new Date(timestamp); const date = new Date(timestamp);
// Get timezone abbreviation and format the date // Get timezone abbreviation and format the date
const timeZoneAbbr = date.toLocaleTimeString('en-US', { timeZoneName: 'short' }) const timeZoneAbbr = date
.split(' ').pop(); .toLocaleTimeString("en-US", { timeZoneName: "short" })
.split(" ")
// Format the date with readable format .pop();
return date.toLocaleString('en-US', {
year: 'numeric', // Format the date with readable format
month: '2-digit', return (
day: '2-digit', date
hour: '2-digit', .toLocaleString("en-US", {
minute: '2-digit', year: "numeric",
second: '2-digit', month: "2-digit",
hour12: false day: "2-digit",
}).replace(/(\d+)\/(\d+)\/(\d+),\s/, '$3-$1-$2 ') + ' ' + timeZoneAbbr; hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
})
.replace(/(\d+)\/(\d+)\/(\d+),\s/, "$3-$1-$2 ") +
" " +
timeZoneAbbr
);
}; };
// Get formatted time // Get formatted time
const formattedTime = timestamp const formattedTime = timestamp
? formatTime(timestamp) ? formatTime(timestamp)
: formatTime(new Date().getTime()); : formatTime(new Date().getTime());
// Create different messages based on status with extra spacing // Create different messages based on status with extra spacing
let messageText; let messageText;
if (status === true) { if (status === true) {
messageText = this.stringService.monitorUpAlert messageText = this.stringService.monitorUpAlert
.replace("{monitorName}", monitor.name) .replace("{monitorName}", monitor.name)
.replace("{time}", formattedTime) .replace("{time}", formattedTime)
.replace("{code}", code || 'Unknown'); .replace("{code}", code || "Unknown");
} else { } else {
messageText = this.stringService.monitorDownAlert messageText = this.stringService.monitorDownAlert
.replace("{monitorName}", monitor.name) .replace("{monitorName}", monitor.name)
.replace("{time}", formattedTime) .replace("{time}", formattedTime)
.replace("{code}", code || 'Unknown'); .replace("{code}", code || "Unknown");
} }
if (!PLATFORM_TYPES.includes(platform)) { if (!PLATFORM_TYPES.includes(platform)) {
return undefined; return undefined;
} }
return MESSAGE_FORMATTERS[platform](messageText, chatId); return MESSAGE_FORMATTERS[platform](messageText, chatId);
} }
/** /**
* Sends a webhook notification to a specified platform. * Sends a webhook notification to a specified platform.
@@ -105,56 +114,56 @@ class NotificationService {
const { monitor, status, code } = networkResponse; const { monitor, status, code } = networkResponse;
const { platform } = notification; const { platform } = notification;
const { webhookUrl, botToken, chatId } = notification.config; const { webhookUrl, botToken, chatId } = notification.config;
// Early return if platform is not supported // Early return if platform is not supported
if (!PLATFORM_TYPES.includes(platform)) { if (!PLATFORM_TYPES.includes(platform)) {
this.logger.warn({ this.logger.warn({
message: this.stringService.getWebhookUnsupportedPlatform(platform), message: this.stringService.getWebhookUnsupportedPlatform(platform),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "sendWebhookNotification", method: "sendWebhookNotification",
details: { platform } details: { platform },
}); });
return false; return false;
} }
// Early return for telegram if required fields are missing // Early return for telegram if required fields are missing
if (platform === "telegram" && (!botToken || !chatId)) { if (platform === "telegram" && (!botToken || !chatId)) {
this.logger.warn({ this.logger.warn({
message: "Missing required fields for Telegram notification", message: "Missing required fields for Telegram notification",
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "sendWebhookNotification", method: "sendWebhookNotification",
details: { platform } details: { platform },
}); });
return false; return false;
} }
let url = webhookUrl; let url = webhookUrl;
if (platform === "telegram") { if (platform === "telegram") {
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
} }
const message = this.formatNotificationMessage( const message = this.formatNotificationMessage(
monitor, monitor,
status, status,
platform, platform,
chatId, chatId,
code, // Pass the code field directly code, // Pass the code field directly
networkResponse.timestamp networkResponse.timestamp
); );
try { try {
const response = await this.networkService.requestWebhook(platform, url, message); const response = await this.networkService.requestWebhook(platform, url, message);
return response.status; return response.status;
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
message: this.stringService.getWebhookSendError(platform), message: this.stringService.getWebhookSendError(platform),
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "sendWebhookNotification", method: "sendWebhookNotification",
stack: error.stack, stack: error.stack,
}); });
return false; return false;
} }
} }
/** /**
* Sends an email notification for hardware infrastructure alerts * Sends an email notification for hardware infrastructure alerts
@@ -197,33 +206,33 @@ class NotificationService {
async handleStatusNotifications(networkResponse) { async handleStatusNotifications(networkResponse) {
try { try {
// If status hasn't changed, we're done // If status hasn't changed, we're done
if (networkResponse.statusChanged === false) return false; if (networkResponse.statusChanged === false) return false;
// if prevStatus is undefined, monitor is resuming, we're done // if prevStatus is undefined, monitor is resuming, we're done
if (networkResponse.prevStatus === undefined) return false; if (networkResponse.prevStatus === undefined) return false;
const notifications = await this.db.getNotificationsByMonitorId( const notifications = await this.db.getNotificationsByMonitorId(
networkResponse.monitorId networkResponse.monitorId
); );
for (const notification of notifications) { for (const notification of notifications) {
if (notification.type === "email") { if (notification.type === "email") {
await this.sendEmail(networkResponse, notification.address); await this.sendEmail(networkResponse, notification.address);
} else if (notification.type === "webhook") { } else if (notification.type === "webhook") {
await this.sendWebhookNotification(networkResponse, notification); await this.sendWebhookNotification(networkResponse, notification);
}
// Handle other types of notifications here
} }
// Handle other types of notifications here return true;
}
return true;
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
message: error.message, message: error.message,
service: this.SERVICE_NAME, service: this.SERVICE_NAME,
method: "handleNotifications", method: "handleNotifications",
stack: error.stack, stack: error.stack,
}); });
} }
} }
/** /**
* Handles status change notifications for a monitor * Handles status change notifications for a monitor
* *
@@ -257,7 +266,13 @@ class NotificationService {
const alerts = { const alerts = {
cpu: cpuThreshold !== -1 && cpuUsage > cpuThreshold ? true : false, cpu: cpuThreshold !== -1 && cpuUsage > cpuThreshold ? true : false,
memory: memoryThreshold !== -1 && memoryUsage > memoryThreshold ? true : false, memory: memoryThreshold !== -1 && memoryUsage > memoryThreshold ? true : false,
disk: disk?.some(d => diskThreshold !== -1 && typeof d?.usage_percent === "number" && d?.usage_percent > diskThreshold) ?? false, disk:
disk?.some(
(d) =>
diskThreshold !== -1 &&
typeof d?.usage_percent === "number" &&
d?.usage_percent > diskThreshold
) ?? false,
}; };
const notifications = await this.db.getNotificationsByMonitorId( const notifications = await this.db.getNotificationsByMonitorId(
+1
View File
@@ -427,6 +427,7 @@ const updateAppSettingsBodyValidation = joi.object({
checkTTL: joi.number().allow(""), checkTTL: joi.number().allow(""),
pagespeedApiKey: joi.string().allow(""), pagespeedApiKey: joi.string().allow(""),
language: joi.string().allow(""), language: joi.string().allow(""),
timezone: joi.string().allow(""),
// showURL: joi.bool().required(), // showURL: joi.bool().required(),
systemEmailHost: joi.string().allow(""), systemEmailHost: joi.string().allow(""),
systemEmailPort: joi.number().allow(""), systemEmailPort: joi.number().allow(""),