code review updates

Summary of what we fixed based on AI review feedback:

Formatting - Ran Prettier to fix code formatting
Redux state rollback - Moved Redux dispatch to after successful save in settingsHooks.js
Performance - Added caching layer in settingsModule (1-minute TTL) to prevent DB query on every status page request
Architecture - Inject settingsModule instead of raw AppSettings model for better encapsulation
This commit is contained in:
Craig Lawson committed 2026-01-11 20:51:25 +00:00
1 parent 7b619e6b31
commit 7556086f71
5 files changed
+47 -18

No files matched your search

+8
View File
@@ -56,6 +56,14 @@ const useSaveSettings = ({
setEmailPasswordHasBeenReset(false); setEmailPasswordHasBeenReset(false);
} }
setSettingsData(settingsResponse.data.data); setSettingsData(settingsResponse.data.data);
// Update Redux state after successful save
if (typeof settings.showURL !== "undefined") {
const { setShowURL } = await import("../../Features/UI/uiSlice.js");
const { default: store } = await import("../../store.js");
store.dispatch(setShowURL(settings.showURL));
}
createToast({ body: t("settingsSuccessSaved") }); createToast({ body: t("settingsSuccessSaved") });
} catch (error) { } catch (error) {
createToast({ body: t("settingsFailedToSave") }); createToast({ body: t("settingsFailedToSave") });
-5
View File
@@ -168,11 +168,6 @@ const Settings = () => {
} }
setSettingsData(newSettingsData); setSettingsData(newSettingsData);
// Update Redux immediately for UI feedback
if (name === "showURL") {
dispatch(setShowURL(value));
}
}; };
const handleSave = () => { const handleSave = () => {
+2 -2
View File
@@ -83,7 +83,8 @@ export const initializeServices = async ({ logger, envSettings, settingsService
// Create DB // Create DB
const checkModule = new CheckModule({ logger, Check, Monitor, User }); const checkModule = new CheckModule({ logger, Check, Monitor, User });
const inviteModule = new InviteModule({ InviteToken, crypto, stringService }); const inviteModule = new InviteModule({ InviteToken, crypto, stringService });
const statusPageModule = new StatusPageModule({ StatusPage, NormalizeData, stringService, AppSettings }); const settingsModule = new SettingsModule({ AppSettings });
const statusPageModule = new StatusPageModule({ StatusPage, NormalizeData, stringService, settingsModule });
const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService }); const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService });
const maintenanceWindowModule = new MaintenanceWindowModule({ MaintenanceWindow }); const maintenanceWindowModule = new MaintenanceWindowModule({ MaintenanceWindow });
const monitorModule = new MonitorModule({ const monitorModule = new MonitorModule({
@@ -100,7 +101,6 @@ export const initializeServices = async ({ logger, envSettings, settingsService
}); });
const notificationModule = new NotificationModule({ Notification, Monitor }); const notificationModule = new NotificationModule({ Notification, Monitor });
const recoveryModule = new RecoveryModule({ User, RecoveryToken, crypto, stringService }); const recoveryModule = new RecoveryModule({ User, RecoveryToken, crypto, stringService });
const settingsModule = new SettingsModule({ AppSettings });
const incidentModule = new IncidentModule({ logger, Incident, Monitor, User }); const incidentModule = new IncidentModule({ logger, Incident, Monitor, User });
const db = new MongoDB({ const db = new MongoDB({
@@ -4,8 +4,29 @@ const SERVICE_NAME = "SettingsModule";
class SettingsModule { class SettingsModule {
constructor({ AppSettings }) { constructor({ AppSettings }) {
this.AppSettings = AppSettings; this.AppSettings = AppSettings;
this.settingsCache = null;
this.cacheTimestamp = null;
this.CACHE_TTL = 60000; // 1 minute cache
} }
getAppSettings = async () => {
try {
const now = Date.now();
if (this.settingsCache && this.cacheTimestamp && (now - this.cacheTimestamp) < this.CACHE_TTL) {
return this.settingsCache;
}
const settings = await this.AppSettings.findOne({ singleton: true }).select("-__v -_id -createdAt -updatedAt -singleton").lean();
this.settingsCache = settings;
this.cacheTimestamp = now;
return settings;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getAppSettings";
throw error;
}
};
updateAppSettings = async (newSettings) => { updateAppSettings = async (newSettings) => {
try { try {
const update = { $set: { ...newSettings } }; const update = { $set: { ...newSettings } };
@@ -24,6 +45,11 @@ class SettingsModule {
upsert: true, upsert: true,
}); });
const settings = await this.AppSettings.findOne().select("-__v -_id -createdAt -updatedAt -singleton").lean(); const settings = await this.AppSettings.findOne().select("-__v -_id -createdAt -updatedAt -singleton").lean();
// Invalidate cache after update
this.settingsCache = settings;
this.cacheTimestamp = Date.now();
return settings; return settings;
} catch (error) { } catch (error) {
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
+11 -11
View File
@@ -5,11 +5,11 @@
const SERVICE_NAME = "statusPageModule"; const SERVICE_NAME = "statusPageModule";
class StatusPageModule { class StatusPageModule {
constructor({ StatusPage, NormalizeData, stringService, AppSettings }) { constructor({ StatusPage, NormalizeData, stringService, settingsModule }) {
this.StatusPage = StatusPage; this.StatusPage = StatusPage;
this.NormalizeData = NormalizeData; this.NormalizeData = NormalizeData;
this.stringService = stringService; this.stringService = stringService;
this.AppSettings = AppSettings; this.settingsModule = settingsModule;
} }
createStatusPage = async ({ statusPageData, image, userId, teamId }) => { createStatusPage = async ({ statusPageData, image, userId, teamId }) => {
@@ -260,19 +260,19 @@ class StatusPageModule {
const { statusPage, monitors } = statusPageQuery[0]; const { statusPage, monitors } = statusPageQuery[0];
const appSettings = await this.AppSettings.findOne({ singleton: true }).lean(); const appSettings = await this.settingsModule.getAppSettings();
const showURL = appSettings?.showURL === true; const showURL = appSettings?.showURL === true;
const normalizedMonitors = monitors.map((monitor) => { const normalizedMonitors = monitors.map((monitor) => {
const normalizedChecks = this.NormalizeData(monitor.checks, 10, 100); const normalizedChecks = this.NormalizeData(monitor.checks, 10, 100);
if (showURL !== true) { if (showURL !== true) {
const { url, port, secret, notifications, ...rest } = monitor; const { url, port, secret, notifications, ...rest } = monitor;
return { ...rest, checks: normalizedChecks }; return { ...rest, checks: normalizedChecks };
} }
return { ...monitor, checks: normalizedChecks }; return { ...monitor, checks: normalizedChecks };
}); });
return { statusPage, monitors: normalizedMonitors }; return { statusPage, monitors: normalizedMonitors };
} catch (error) { } catch (error) {
error.service = SERVICE_NAME; error.service = SERVICE_NAME;
error.method = "getStatusPageByUrl"; error.method = "getStatusPageByUrl";