Skip to content

Commit

Permalink
Refactoring code in 'public/src/admin/appearance/themes.js' to resolv…
Browse files Browse the repository at this point in the history
…e issue with nested functions.

In the src/.../themes.js file, there are a number of nested functions which make the code difficult for developers to understand and maintain. This not only makes maintenance less efficient but also more expensive, as it breaks the normal linear reading flow.
  • Loading branch information
pebble-fish authored Jan 23, 2025
1 parent 206ca56 commit 79e90d1
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions public/src/admin/appearance/themes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';


define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function (bootbox, translator, alerts) {
const Themes = {};

Expand All @@ -18,30 +17,7 @@ define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function
if (config['theme:id'] === themeId) {
return;
}
socket.emit('admin.themes.set', {
type: themeType,
id: themeId,
src: cssSrc,
}, function (err) {
if (err) {
return alerts.error(err);
}
config['theme:id'] = themeId;
highlightSelectedTheme(themeId);

alerts.alert({
alert_id: 'admin:theme',
type: 'info',
title: '[[admin/appearance/themes:theme-changed]]',
message: '[[admin/appearance/themes:restart-to-activate]]',
timeout: 5000,
clickfn: function () {
require(['admin/modules/instance'], function (instance) {
instance.rebuildAndRestart();
});
},
});
});
setTheme(themeType, themeId, cssSrc, alerts);
}
});

Expand Down Expand Up @@ -91,7 +67,43 @@ define('admin/appearance/themes', ['bootbox', 'translator', 'alerts'], function
}
});
};
// helper functions generated with gpt
// made revisions such as defining theme, fixing tabs, changing varnames
function setTheme(themeType, themeId, cssSrc, alerts) {
// 1. Send the socket event
socket.emit('admin.themes.set', {
type: themeType,
id: themeId,
src: cssSrc,
}, function (err) {
if (err) {
return alerts.error(err);
}
// 2. Perform immediate post-set actions
config['theme:id'] = themeId;
highlightSelectedTheme(themeId);
// 3. Show alert & bind its click handler
showThemeChangedAlert();
});
}

function showThemeChangedAlert() {
alerts.alert({
alert_id: 'admin:theme',
type: 'info',
title: '[[admin/appearance/themes:theme-changed]]',
message: '[[admin/appearance/themes:restart-to-activate]]',
timeout: 5000,
clickfn: onThemeAlertClick,
});
}

function onThemeAlertClick() {
// Require asynchronously so we don’t block the main flow
require(['admin/modules/instance'], function (instance) {
instance.rebuildAndRestart();
});
}
function highlightSelectedTheme(themeId) {
translator.translate('[[admin/appearance/themes:select-theme]] || [[admin/appearance/themes:current-theme]]', function (text) {
text = text.split(' || ');
Expand Down

0 comments on commit 79e90d1

Please sign in to comment.