Skip to content

Commit

Permalink
update SV when reloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanfbrito committed Oct 31, 2023
1 parent 50e3064 commit ecd5e6b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
93 changes: 55 additions & 38 deletions src/servers/supportedVersions/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
WEBVIEW_SERVER_SUPPORTED_VERSIONS_SOURCE_UPDATED,
WEBVIEW_READY,
WEBVIEW_SERVER_UNIQUE_ID_UPDATED,
WEBVIEW_SERVER_RELOADED,
} from '../../ui/actions';
import type { Server } from '../common';
import type {
Expand Down Expand Up @@ -347,6 +348,53 @@ export const isServerVersionSupported = async (
return false;
};

const updateSupportedVersionsData = async (
serverUrl: string
): Promise<void> => {
const server = select(({ servers }) => servers).find(
(server) => server.url === serverUrl
);
if (!server) return;
console.log('updateSupportedVersionsData', server.url);
const serverInfo = await getServerInfo(server.url);
if (!serverInfo) return;
dispatch({
type: WEBVIEW_SERVER_VERSION_UPDATED,
payload: {
url: server.url,
version: serverInfo.version,
},
});
const uniqueID = await getUniqueId(server.url);

Check failure on line 368 in src/servers/supportedVersions/main.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'getUniqueId' was used before it was defined

Check failure on line 368 in src/servers/supportedVersions/main.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

'getUniqueId' was used before it was defined

Check failure on line 368 in src/servers/supportedVersions/main.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

'getUniqueId' was used before it was defined
dispatch({
type: WEBVIEW_SERVER_UNIQUE_ID_UPDATED,
payload: {
url: server.url,
uniqueID,
},
});
};

const checkSupportedVersion = async (serverUrl: string): Promise<void> => {
const currentServerUrl = select(({ currentView }) =>
typeof currentView === 'object' ? currentView.url : null
);
const server = select(({ servers }) => servers).find(
(server) => server.url === serverUrl && server.url === currentServerUrl
);
console.log('checkSupportedVersion', serverUrl);
if (!server || server.expirationMessage === null) return;
const { expirationMessage, expirationMessageLastTimeShown } = server;
if (!expirationMessage) return;
if (
expirationMessageLastTimeShown &&
moment().diff(expirationMessageLastTimeShown, 'hours') < 12
)
return;
updateSupportedVersionsData(serverUrl);
dispatch({ type: SUPPORTED_VERSION_DIALOG_OPEN });
};

const getUniqueId = async (serverUrl: string): Promise<string> => {
const response = await fetch(
`${serverUrl}/api/v1/settings.public?query={"_id": "uniqueID"}`
Expand All @@ -357,27 +405,11 @@ const getUniqueId = async (serverUrl: string): Promise<string> => {

export function checkSupportedVersionServers(): void {
listen(WEBVIEW_READY, async (action) => {
const server = select(({ servers }) => servers).find(
(server) => server.url === action.payload.url
);
if (!server) return;
const serverInfo = await getServerInfo(server.url);
if (!serverInfo) return;
dispatch({
type: WEBVIEW_SERVER_VERSION_UPDATED,
payload: {
url: server.url,
version: serverInfo.version,
},
});
const uniqueID = await getUniqueId(server.url);
dispatch({
type: WEBVIEW_SERVER_UNIQUE_ID_UPDATED,
payload: {
url: server.url,
uniqueID,
},
});
updateSupportedVersionsData(action.payload.url);
});

listen(WEBVIEW_SERVER_RELOADED, async (action) => {
updateSupportedVersionsData(action.payload.url);
});

listen(WEBVIEW_SERVER_SUPPORTED_VERSIONS_UPDATED, async (action) => {
Expand All @@ -393,6 +425,7 @@ export function checkSupportedVersionServers(): void {
isSupportedVersion,
},
});
checkSupportedVersion(server.url);
});

listen(WEBVIEW_SERVER_VERSION_UPDATED, async (action) => {
Expand All @@ -413,22 +446,6 @@ export function checkSupportedVersionServers(): void {
});

listen(WEBVIEW_DID_NAVIGATE, async (action) => {
const currentServerUrl = select(({ currentView }) =>
typeof currentView === 'object' ? currentView.url : null
);
const server = select(({ servers }) => servers).find(
(server) =>
server.url === action.payload.url && server.url === currentServerUrl
);
if (!server || server.expirationMessage === null) return;
const { expirationMessage, expirationMessageLastTimeShown } = server;
if (!expirationMessage) return;
if (
expirationMessageLastTimeShown &&
moment().diff(expirationMessageLastTimeShown, 'seconds') < 12
)
return;

dispatch({ type: SUPPORTED_VERSION_DIALOG_OPEN });
checkSupportedVersion(action.payload.url);
});
}
4 changes: 4 additions & 0 deletions src/ui/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const SUPPORTED_VERSION_DIALOG_DISMISS =
'supported-versions-dialog/dismiss';
export const SUPPORTED_VERSION_EXPIRATION_MESSAGE_UPDATED =
'supported-versions/expiration-message-updated';
export const WEBVIEW_SERVER_RELOADED = 'webview/server-reloaded';

export type UiActionTypeToPayloadMap = {
[ABOUT_DIALOG_DISMISSED]: void;
Expand Down Expand Up @@ -215,4 +216,7 @@ export type UiActionTypeToPayloadMap = {
url: Server['url'];
supportedVersionsSource: Server['supportedVersionsSource'];
};
[WEBVIEW_SERVER_RELOADED]: {
url: Server['url'];
};
};
13 changes: 13 additions & 0 deletions src/ui/main/menuBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MENU_BAR_TOGGLE_IS_TRAY_ICON_ENABLED_CLICKED,
SIDE_BAR_DOWNLOADS_BUTTON_CLICKED,
SIDE_BAR_SETTINGS_BUTTON_CLICKED,
WEBVIEW_SERVER_RELOADED,
} from '../actions';
import { askForAppDataReset } from './dialogs';
import { getRootWindow } from './rootWindow';
Expand Down Expand Up @@ -207,6 +208,12 @@ const createViewMenu = createSelector(
? getWebContentsByServerUrl(currentView.url)
: null;
guestWebContents?.reload();
if (typeof currentView === 'object' && !!currentView.url) {
dispatch({
type: WEBVIEW_SERVER_RELOADED,
payload: { url: currentView.url },
});
}
},
},
{
Expand All @@ -225,6 +232,12 @@ const createViewMenu = createSelector(
? getWebContentsByServerUrl(currentView.url)
: null;
guestWebContents?.reloadIgnoringCache();
if (typeof currentView === 'object' && !!currentView.url) {
dispatch({
type: WEBVIEW_SERVER_RELOADED,
payload: { url: currentView.url },
});
}
},
},
{
Expand Down
7 changes: 7 additions & 0 deletions src/ui/main/serverView/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
WEBVIEW_DID_NAVIGATE,
WEBVIEW_DID_START_LOADING,
WEBVIEW_ATTACHED,
WEBVIEW_SERVER_RELOADED,
} from '../../actions';
import { getRootWindow } from '../rootWindow';
import { createPopupMenuForServerView } from './popupMenu';
Expand Down Expand Up @@ -391,6 +392,12 @@ export const attachGuestWebContentsEvents = async (): Promise<void> => {
click: () => {
const guestWebContents = getWebContentsByServerUrl(serverUrl);
guestWebContents?.loadURL(serverUrl);
if (serverUrl) {
dispatch({
type: WEBVIEW_SERVER_RELOADED,
payload: { url: serverUrl },
});
}
},
},
{
Expand Down

0 comments on commit ecd5e6b

Please sign in to comment.