Skip to content

Commit 2aef2cb

Browse files
committed
1. SonarQube code complexity issue fixed for menu.js
2. If auto_update_url is not present in versions.json then show classic update notifier and hide auto-update menus in app menu 3. IPC event sendDataForAppUpdate changed from invoke to send type
1 parent 1261ea3 commit 2aef2cb

File tree

6 files changed

+134
-94
lines changed

6 files changed

+134
-94
lines changed

runtime/src/js/menu.js

Lines changed: 94 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,100 +14,108 @@ const isLinux = process.platform == 'linux';
1414
let mainMenu;
1515
let cachedMenus;
1616

17+
// Binds click events to all menu and submenu items recursively.
18+
function bindMenuClicks(pgadminMenus, pgAdminMainScreen) {
19+
return pgadminMenus.map((menuItem) => ({
20+
...menuItem,
21+
submenu: menuItem.submenu?.map((subMenuItem) => {
22+
const smName = `${menuItem.name}_${subMenuItem.name}`;
23+
return {
24+
...subMenuItem,
25+
click: () => {
26+
pgAdminMainScreen.webContents.send('menu-click', smName);
27+
},
28+
submenu: subMenuItem.submenu?.map((deeperSubMenuItem) => ({
29+
...deeperSubMenuItem,
30+
click: () => {
31+
pgAdminMainScreen.webContents.send('menu-click', `${smName}_${deeperSubMenuItem.name}`);
32+
},
33+
})),
34+
};
35+
}),
36+
}));
37+
}
38+
39+
// Handles auto-update related menu items for macOS.
40+
// Adds or disables update menu items based on config state.
41+
function handleAutoUpdateMenu(menuFile, configStore, callbacks) {
42+
if (!configStore.get('auto_update_enabled')) return;
43+
if (configStore.get('update_downloaded')) {
44+
// Add "Restart to Update" if update is downloaded
45+
menuFile.submenu.unshift({
46+
name: 'mnu_restart_to_update',
47+
id: 'mnu_restart_to_update',
48+
label: 'Restart to Update...',
49+
enabled: true,
50+
priority: 998,
51+
click: callbacks['restart_to_update'],
52+
});
53+
} else {
54+
// Add "Check for Updates" if update is not downloaded
55+
menuFile.submenu.unshift({
56+
name: 'mnu_check_updates',
57+
id: 'mnu_check_updates',
58+
label: 'Check for Updates...',
59+
enabled: true,
60+
priority: 998,
61+
click: callbacks['check_for_updates'],
62+
});
63+
}
64+
// Disable "Check for Updates" if update is downloading
65+
if (configStore.get('update_downloading')) {
66+
menuFile.submenu.forEach((item) => {
67+
if (item.id == 'mnu_check_updates') item.enabled = false;
68+
});
69+
}
70+
}
71+
72+
// Remove About pgAdmin 4 from help menu and add it to the top of menuFile submenu.
73+
function moveAboutMenuToTop(pgadminMenus, menuFile) {
74+
const helpMenu = pgadminMenus.find((menu) => menu.name == 'help');
75+
if (!helpMenu) return;
76+
const aboutItem = helpMenu.submenu.find((item) => item.name === 'mnu_about');
77+
if (!aboutItem) return;
78+
helpMenu.submenu = helpMenu.submenu.filter((item) => item.name !== 'mnu_about');
79+
menuFile.submenu.unshift(aboutItem);
80+
menuFile.submenu.splice(2, 0, { type: 'separator' });
81+
}
82+
83+
// Builds the application menu template and binds menu click events.
84+
// Handles platform-specific menu structure and dynamic menu items.
1785
function buildMenu(pgadminMenus, pgAdminMainScreen, configStore, callbacks) {
1886
const template = [];
1987

20-
// bind all menus click event.
21-
pgadminMenus = pgadminMenus.map((menuItem)=>{
22-
return {
23-
...menuItem,
24-
submenu: menuItem.submenu?.map((subMenuItem)=>{
25-
const smName = `${menuItem.name}_${subMenuItem.name}`;
26-
return {
27-
...subMenuItem,
28-
click: ()=>{
29-
pgAdminMainScreen.webContents.send('menu-click', smName);
30-
},
31-
submenu: subMenuItem.submenu?.map((deeperSubMenuItem)=>{
32-
return {
33-
...deeperSubMenuItem,
34-
click: ()=>{
35-
pgAdminMainScreen.webContents.send('menu-click', `${smName}_${deeperSubMenuItem.name}`);
36-
},
37-
};
38-
}),
39-
};
40-
}),
41-
};
42-
});
88+
pgadminMenus = bindMenuClicks(pgadminMenus, pgAdminMainScreen);
4389

4490
let menuFile = pgadminMenus.shift();
4591

92+
// macOS-specific menu modifications
4693
if (isMac) {
47-
if (configStore.get('update_downloaded')) {
48-
//Add Restart to update menu item in the app menu if update is downloaded.
49-
menuFile.submenu.unshift({
50-
name:'mnu_restart_to_update',
51-
id: 'mnu_restart_to_update',
52-
label: 'Restart to Update...',
53-
enabled: true,
54-
priority: 998,
55-
click: callbacks['restart_to_update'],
56-
});
57-
} else {
58-
// Add Check for Updates menu item in the app menu.
59-
menuFile.submenu.unshift({
60-
name:'mnu_check_updates',
61-
id: 'mnu_check_updates',
62-
label: 'Check for Updates...',
63-
enabled: true,
64-
priority: 998,
65-
click: callbacks['check_for_updates'],
66-
});
67-
}
68-
69-
// Disable the Check for updates menu item if update is downloading.
70-
if(configStore.get('update_downloading')) {
71-
menuFile.submenu.forEach((item)=> {
72-
if (item.id == 'mnu_check_updates') item.enabled=false;
73-
});
74-
}
75-
76-
// Remove About pgAdmin 4 from help menu and add it to the top of menuFile submenu.
77-
const helpMenu = pgadminMenus.find((menu) => menu.name == 'help');
78-
if (helpMenu) {
79-
const aboutItem = helpMenu.submenu.find((item) => item.name === 'mnu_about');
80-
if (aboutItem) {
81-
helpMenu.submenu = helpMenu.submenu.filter((item) => item.name !== 'mnu_about');
82-
menuFile.submenu.unshift(aboutItem);
83-
menuFile.submenu.splice(2, 0, { type: 'separator' });
84-
}
85-
}
94+
handleAutoUpdateMenu(menuFile, configStore, callbacks);
95+
moveAboutMenuToTop(pgadminMenus, menuFile);
8696
}
87-
97+
8898
template.push({
8999
...menuFile,
90100
submenu: [
91101
...menuFile.submenu,
92102
{ type: 'separator' },
93-
{
94-
label: 'View Logs...', click: callbacks['view_logs'],
95-
},
96-
{
97-
label: 'Configure runtime...', click: callbacks['configure'],
98-
},
103+
{ label: 'View Logs...', click: callbacks['view_logs'] },
104+
{ label: 'Configure runtime...', click: callbacks['configure'] },
99105
{ type: 'separator' },
100-
...(isMac ? [
101-
{ role: 'hide' },
102-
{ role: 'hideOthers' },
103-
{ role: 'unhide' },
104-
{ type: 'separator' },
105-
] : []),
106+
...(isMac
107+
? [
108+
{ role: 'hide' },
109+
{ role: 'hideOthers' },
110+
{ role: 'unhide' },
111+
{ type: 'separator' },
112+
]
113+
: []),
106114
{ role: 'quit' },
107115
],
108116
});
109117

110-
if(isMac) {
118+
if (isMac) {
111119
template[0].label = app.name;
112120
}
113121

@@ -119,19 +127,25 @@ function buildMenu(pgadminMenus, pgAdminMainScreen, configStore, callbacks) {
119127
{
120128
label: 'View',
121129
submenu: [
122-
{ label: 'Reload', click: callbacks['reloadApp']},
123-
{ label: 'Toggle Developer Tools', click: ()=>BrowserWindow.getFocusedWindow().webContents.openDevTools({ mode: 'bottom' })},
130+
{ label: 'Reload', click: callbacks['reloadApp'] },
131+
{
132+
label: 'Toggle Developer Tools',
133+
click: () =>
134+
BrowserWindow.getFocusedWindow().webContents.openDevTools({
135+
mode: 'bottom',
136+
}),
137+
},
124138
{ type: 'separator' },
125139
{ role: 'resetZoom' },
126140
{ role: 'zoomIn' },
127141
{ role: 'zoomOut' },
128142
{ type: 'separator' },
129143
].concat(isLinux ? [] : [{ role: 'togglefullscreen' }]),
130144
},
131-
{ role: 'windowMenu' },
145+
{ role: 'windowMenu' }
132146
);
133147

134-
template.push(pgadminMenus[pgadminMenus.length-1]);
148+
template.push(pgadminMenus[pgadminMenus.length - 1]);
135149

136150
return Menu.buildFromTemplate(template);
137151
}

runtime/src/js/pgadmin.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ function setConfigAndRefreshMenu(event) {
209209
}
210210
}
211211

212+
// Remove auto_update_enabled from configStore on app close or quit
213+
function cleanupAutoUpdateFlag() {
214+
if (configStore.has('auto_update_enabled')) {
215+
configStore.delete('auto_update_enabled');
216+
}
217+
}
218+
212219
// This function will force quit and install update and restart the app
213220
function forceQuitAndInstallUpdate() {
214221
// Disable beforeunload handlers
@@ -219,6 +226,7 @@ function forceQuitAndInstallUpdate() {
219226
pgAdminMainScreen.webContents.on('will-prevent-unload', preventUnload);
220227
// Set flag to show notification after restart
221228
configStore.set('update_installed', true);
229+
cleanupAutoUpdateFlag();
222230
autoUpdater.quitAndInstall();
223231
}
224232

@@ -419,6 +427,7 @@ function launchPgAdminWindow() {
419427
});
420428

421429
pgAdminMainScreen.on('closed', ()=>{
430+
cleanupAutoUpdateFlag();
422431
misc.cleanupAndQuitApp();
423432
});
424433

@@ -522,8 +531,17 @@ if (process.platform === 'darwin') {
522531
pgAdminMainScreen.webContents.send('appUpdateNotifier', {error: true, errMsg: message});
523532
});
524533

525-
ipcMain.handle('sendDataForAppUpdate', (_, data) => {
526-
if (data.check_for_updates) {
534+
ipcMain.on('sendDataForAppUpdate', (_, data) => {
535+
// Update auto-update enabled flag and refresh menus only if the setting is changed or not set
536+
if (typeof data.check_for_updates !== 'undefined') {
537+
const currentFlag = configStore.get('auto_update_enabled');
538+
if (typeof currentFlag === 'undefined' || currentFlag !== data.check_for_updates) {
539+
configStore.set('auto_update_enabled', data.check_for_updates);
540+
refreshMenus(pgAdminMainScreen, configStore, menuCallbacks);
541+
}
542+
}
543+
544+
if (data.auto_update_url && data.upgrade_version && data.upgrade_version_int && data.current_version_int && data.product_name) {
527545
const ftpUrl = encodeURIComponent(`${data.auto_update_url}/pgadmin4-${data.upgrade_version}-${process.arch}.zip`);
528546
let serverUrl = `http://127.0.0.1:${serverPort}/misc/auto_update/${data.current_version_int}/${data.upgrade_version}/${data.upgrade_version_int}/${data.product_name}/${ftpUrl}/?key=${UUID}`;
529547

runtime/src/js/pgadmin_preload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ contextBridge.exposeInMainWorld('electronUI', {
3232
downloadStreamSaveEnd: (...args) => ipcRenderer.send('download-stream-save-end', ...args),
3333
downloadBase64UrlData: (...args) => ipcRenderer.invoke('download-base64-url-data', ...args),
3434
downloadTextData: (...args) => ipcRenderer.invoke('download-text-data', ...args),
35-
sendDataForAppUpdate: (data) => ipcRenderer.invoke('sendDataForAppUpdate', data),
35+
sendDataForAppUpdate: (data) => ipcRenderer.send('sendDataForAppUpdate', data),
3636
appUpdateNotifier: (callback) => {
3737
ipcRenderer.removeAllListeners('appUpdateNotifier'); // Clean up previous listeners
3838
ipcRenderer.on('appUpdateNotifier', (_, data) => callback(data));

web/pgadmin/browser/static/js/UpdateCheckNotify.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
//
88
//////////////////////////////////////////////////////////////
99

10+
import React from 'react';
1011
import { Box } from '@mui/material';
1112
import { styled } from '@mui/material/styles';
1213
import CloseIcon from '@mui/icons-material/CloseRounded';
13-
import { DefaultButton, PgIconButton } from '../../../static/js/components/Buttons';
1414
import PropTypes from 'prop-types';
15+
import { DefaultButton, PgIconButton } from '../../../static/js/components/Buttons';
1516
import pgAdmin from 'sources/pgadmin';
1617

1718
const StyledBox = styled(Box)(({theme}) => ({

web/pgadmin/browser/static/js/browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,14 @@ define('pgadmin.browser', [
276276
url_for('misc.upgrade_check') + '?trigger_update_check=' + trigger_update_check
277277
).then((res)=> {
278278
const data = res.data.data;
279+
window.electronUI?.sendDataForAppUpdate({
280+
'check_for_updates': data.check_for_auto_updates,
281+
});
279282
if (pgAdmin.server_mode == 'False' && (data.check_for_auto_updates && data.auto_update_url!=='')) {
280283
// This is for desktop installers whose auto_update_url is mentioned in https://www.pgadmin.org/versions.json
281284
const message = `${gettext('You are currently running version %s of %s, however the current version is %s.', data.current_version, data.product_name, data.upgrade_version)}`;
282285
function downloadUpdate() {
283286
window.electronUI?.sendDataForAppUpdate({
284-
'check_for_updates': data.check_for_auto_updates,
285287
'current_version':data.current_version,
286288
'upgrade_version': data.upgrade_version,
287289
'current_version_int':data.current_version_int,

web/pgadmin/misc/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,21 @@ def upgrade_check():
385385
platform = 'macos'
386386
elif sys.platform == 'win32':
387387
platform = 'windows'
388-
if not config.SERVER_MODE:
388+
# Determine if auto-update is available and construct the
389+
# response accordingly. If running in desktop mode
390+
# (not SERVER_MODE) and an auto-update URL is provided for the
391+
# platform, return a response indicating that auto-update is
392+
# available, including relevant update details.
393+
# Otherwise, return a response indicating that auto-update is
394+
# not available, but an update exists.
395+
if not config.SERVER_MODE and data[config.UPGRADE_CHECK_KEY][
396+
'auto_update_url'][platform] != '':
389397
ret = {
390398
"outdated": True,
391-
"check_for_auto_updates":
392-
data[config.UPGRADE_CHECK_KEY]
393-
.get('auto_update_url') is not None,
399+
"check_for_auto_updates": True,
394400
"auto_update_url": data[config.UPGRADE_CHECK_KEY][
395-
'auto_update_url'][platform]
396-
if data[config.UPGRADE_CHECK_KEY][
397-
'auto_update_url'] is not None else '',
398-
"platform":platform,
401+
'auto_update_url'][platform],
402+
"platform": platform,
399403
"installer_type": config.UPGRADE_CHECK_KEY,
400404
"current_version": config.APP_VERSION,
401405
"upgrade_version": data[config.UPGRADE_CHECK_KEY][
@@ -410,6 +414,7 @@ def upgrade_check():
410414
else:
411415
ret = {
412416
"outdated": True,
417+
"check_for_auto_updates": False,
413418
"current_version": config.APP_VERSION,
414419
"upgrade_version": data[config.UPGRADE_CHECK_KEY][
415420
'version'],
@@ -429,7 +434,7 @@ def upgrade_check():
429434
def auto_update(current_version_int, latest_version, latest_version_int,
430435
product_name, ftp_url):
431436
"""
432-
Endpoint to provide auto-update information for the desktop app.
437+
Get auto-update information for the desktop app.
433438
434439
Returns update metadata (download URL and version name)
435440
if a newer version is available. Responds with HTTP 204

0 commit comments

Comments
 (0)