Add watcher polling settings.

This commit is contained in:
Patrick Fic
2025-03-26 11:32:41 -07:00
parent 791c518920
commit e2ccbf7007
11 changed files with 247 additions and 65 deletions

View File

@@ -7,15 +7,39 @@ import {
selectWatcherStatus,
} from "@renderer/redux/app.slice";
import { useAppSelector } from "@renderer/redux/reduxHooks";
import { Alert, Button, Card, Space, Typography } from "antd";
import { Alert, Button, Card, InputNumber, Space, Switch } from "antd";
import { useTranslation } from "react-i18next";
import ipcTypes from "../../../../util/ipcTypes.json";
import { useEffect, useState } from "react";
const SettingsWatcher: React.FC = () => {
const { t } = useTranslation();
const isWatcherStarted = useAppSelector(selectWatcherStatus);
const watcherError = useAppSelector(selectWatcherError);
const [pollingState, setPollingState] = useState<{
enabled: boolean;
interval: number;
}>({
enabled: false,
interval: 0,
});
console.log("*** ~ pollingState:", pollingState);
const getPollingStateFromStore = (): void => {
window.electron.ipcRenderer
.invoke(ipcTypes.toMain.settings.watcher.getpolling)
.then((storePollingState: { enabled: boolean; interval: number }) => {
console.log("*** ~ .then ~ storePollingState:", storePollingState);
setPollingState(storePollingState);
});
};
//Get state first time it renders.
useEffect(() => {
getPollingStateFromStore();
}, []);
const handleStart = (): void => {
window.electron.ipcRenderer.send(ipcTypes.toMain.watcher.start);
};
@@ -24,6 +48,31 @@ const SettingsWatcher: React.FC = () => {
window.electron.ipcRenderer.send(ipcTypes.toMain.watcher.stop);
};
const toggleWatcherMode = (checked: boolean): void => {
window.electron.ipcRenderer
.invoke(ipcTypes.toMain.settings.watcher.setpolling, {
enabled: !checked,
interval: pollingState.interval,
})
.then((storePollingState: { enabled: boolean; interval: number }) => {
setPollingState(storePollingState);
});
};
const handlePollingIntervalChange = (value: number | null): void => {
if (value) {
window.electron.ipcRenderer
.invoke(ipcTypes.toMain.settings.watcher.setpolling, {
enabled: pollingState.enabled,
interval: value,
})
.then((storePollingState: { enabled: boolean; interval: number }) => {
setPollingState(storePollingState);
});
}
getPollingStateFromStore();
};
return (
<Card title={t("settings.labels.watcherstatus")}>
<Space>
@@ -47,6 +96,19 @@ const SettingsWatcher: React.FC = () => {
{t("settings.labels.stopped")}
</Space>
)}
<Switch
checked={!pollingState.enabled}
onChange={toggleWatcherMode}
checkedChildren={t("settings.labels.watchermoderealtime")}
unCheckedChildren={t("settings.labels.watchermodepolling")}
/>
<InputNumber
title={t("settings.labels.pollinginterval")}
disabled={!pollingState.enabled}
min={1000}
value={pollingState.interval}
onChange={handlePollingIntervalChange}
/>
{watcherError && <Alert message={watcherError} />}
</Space>
</Card>

View File

@@ -1,13 +1,15 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import log from "electron-log/renderer";
import type { RootState } from "./redux-store";
import { update } from "lodash";
import { notification } from "antd";
interface AppState {
value: number;
watcher: {
started: boolean;
error: string | null;
polling: {
enabled: boolean;
interval: number;
};
};
updates: {
available: boolean;
@@ -24,6 +26,10 @@ const initialState: AppState = {
watcher: {
started: false,
error: null,
polling: {
enabled: false,
interval: 30000,
},
},
updates: {
available: false,
@@ -58,16 +64,26 @@ export const appSlice = createSlice({
state.updates.available = true;
state.updates.checking = false;
},
updateProgress: (state, action) => {
updateProgress: (
state,
action: PayloadAction<{ progress: number; speed: number }>,
) => {
state.updates.available = true;
state.updates.progress = action?.progress;
state.updates.speed = action?.speed;
state.updates.progress = action.payload.progress;
state.updates.speed = action.payload.speed;
},
updateDownloaded: (state) => {
state.updates.completed = true;
state.updates.progress = 100;
state.updates.speed = 0;
},
setWatcherPolling: (
state,
action: PayloadAction<{ enabled: boolean; interval: number }>,
) => {
state.watcher.polling.enabled = action.payload.enabled;
state.watcher.polling.interval = action.payload.interval;
},
},
});
@@ -79,6 +95,7 @@ export const {
updateChecking,
updateDownloaded,
updateProgress,
setWatcherPolling,
} = appSlice.actions;
// Other code such as selectors can use the imported `RootState` type
@@ -100,6 +117,13 @@ export const selectAppUpdateSpeed = (state: RootState): number =>
export const selectAppUpdateCompleted = (state: RootState): boolean =>
state.app.updates.completed;
export const selectWatcherPolling = (
state: RootState,
): {
enabled: boolean;
interval: number;
} => state.app.watcher.polling;
//Async Functions - Thunks
// Define a thunk that dispatches those action creators
// const fetchUsers = () => async (dispatch) => {
@@ -108,12 +132,4 @@ export const selectAppUpdateCompleted = (state: RootState): boolean =>
// // dispatch(incrementByAmount(100));
// };
const updateAvailableThunk = () => async (dispatch) => {
notification.info({
message: "Update Available",
key: "app-update",
description: "An update is available for download.",
});
};
export default appSlice.reducer;

View File

@@ -1,5 +1,7 @@
//Set up all of the IPC handlers.
import {
selectWatcherPolling,
setWatcherPolling,
updateAvailable,
updateChecking,
updateDownloaded,
@@ -84,3 +86,7 @@ ipcRenderer.on(
dispatch(updateDownloaded());
},
);
ipcRenderer.on(ipcTypes.toRenderer.watcher.polling, (event, arg) => {
dispatch(setWatcherPolling({ enabled: arg.enabled, interval: arg.interval }));
});