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:
1 parent
7b619e6b31
commit
7556086f71
5 files changed
+47
-18
No files matched your search
@@ -56,6 +56,14 @@ const useSaveSettings = ({
|
||||
setEmailPasswordHasBeenReset(false);
|
||||
}
|
||||
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") });
|
||||
} catch (error) {
|
||||
createToast({ body: t("settingsFailedToSave") });
|
||||
|
||||
@@ -168,11 +168,6 @@ const Settings = () => {
|
||||
}
|
||||
|
||||
setSettingsData(newSettingsData);
|
||||
|
||||
// Update Redux immediately for UI feedback
|
||||
if (name === "showURL") {
|
||||
dispatch(setShowURL(value));
|
||||
}
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
|
||||
@@ -83,7 +83,8 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
||||
// Create DB
|
||||
const checkModule = new CheckModule({ logger, Check, Monitor, User });
|
||||
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 maintenanceWindowModule = new MaintenanceWindowModule({ MaintenanceWindow });
|
||||
const monitorModule = new MonitorModule({
|
||||
@@ -100,7 +101,6 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
||||
});
|
||||
const notificationModule = new NotificationModule({ Notification, Monitor });
|
||||
const recoveryModule = new RecoveryModule({ User, RecoveryToken, crypto, stringService });
|
||||
const settingsModule = new SettingsModule({ AppSettings });
|
||||
const incidentModule = new IncidentModule({ logger, Incident, Monitor, User });
|
||||
|
||||
const db = new MongoDB({
|
||||
|
||||
@@ -4,8 +4,29 @@ const SERVICE_NAME = "SettingsModule";
|
||||
class SettingsModule {
|
||||
constructor({ 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) => {
|
||||
try {
|
||||
const update = { $set: { ...newSettings } };
|
||||
@@ -24,6 +45,11 @@ class SettingsModule {
|
||||
upsert: true,
|
||||
});
|
||||
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;
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
const SERVICE_NAME = "statusPageModule";
|
||||
|
||||
class StatusPageModule {
|
||||
constructor({ StatusPage, NormalizeData, stringService, AppSettings }) {
|
||||
constructor({ StatusPage, NormalizeData, stringService, settingsModule }) {
|
||||
this.StatusPage = StatusPage;
|
||||
this.NormalizeData = NormalizeData;
|
||||
this.stringService = stringService;
|
||||
this.AppSettings = AppSettings;
|
||||
this.settingsModule = settingsModule;
|
||||
}
|
||||
|
||||
createStatusPage = async ({ statusPageData, image, userId, teamId }) => {
|
||||
@@ -260,19 +260,19 @@ class StatusPageModule {
|
||||
|
||||
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 normalizedMonitors = monitors.map((monitor) => {
|
||||
const normalizedChecks = this.NormalizeData(monitor.checks, 10, 100);
|
||||
if (showURL !== true) {
|
||||
const { url, port, secret, notifications, ...rest } = monitor;
|
||||
return { ...rest, checks: normalizedChecks };
|
||||
}
|
||||
return { ...monitor, checks: normalizedChecks };
|
||||
});
|
||||
const normalizedChecks = this.NormalizeData(monitor.checks, 10, 100);
|
||||
if (showURL !== true) {
|
||||
const { url, port, secret, notifications, ...rest } = monitor;
|
||||
return { ...rest, checks: normalizedChecks };
|
||||
}
|
||||
return { ...monitor, checks: normalizedChecks };
|
||||
});
|
||||
|
||||
return { statusPage, monitors: normalizedMonitors };
|
||||
return { statusPage, monitors: normalizedMonitors };
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
error.method = "getStatusPageByUrl";
|
||||
|
||||
Reference in new issue
Block a user