From e598e04172c38f06ca6b054f257979e39c95ece1 Mon Sep 17 00:00:00 2001 From: Marcin Cichocki Date: Sun, 22 Oct 2023 12:45:04 +0200 Subject: [PATCH] feat(client-electron): add ability clear stats --- src/electron/common/actions.ts | 8 ++++++++ src/electron/main/main.ts | 25 +++++++++++++++++++++++++ src/electron/main/store/reducer.ts | 19 ++++++++++--------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/electron/common/actions.ts b/src/electron/common/actions.ts index 1d81077c..33212420 100644 --- a/src/electron/common/actions.ts +++ b/src/electron/common/actions.ts @@ -4,6 +4,7 @@ import { Analysis, AnalysisResult, AppSettings, + AppStats, HistoryEntry, UpdateStatus, WorkerStatus, @@ -20,6 +21,7 @@ export const ActionTypes = { SET_ANALYSIS: 'SET_ANALYSIS', CLEAR_ANALYSIS: 'CLEAR_ANALYSIS', ADD_ANALYSIS_RESULTS: 'ADD_ANALYSIS_RESULTS', + SET_STATS: 'SET_STATS', } as const; export class SetStatusAction implements Action { @@ -83,3 +85,9 @@ export class AddAnalysisResultsAction implements Action { constructor(public readonly payload: AnalysisResult) {} } + +export class SetStatsAction implements Action { + readonly type = ActionTypes.SET_STATS; + + constructor(public readonly payload: AppStats) {} +} diff --git a/src/electron/main/main.ts b/src/electron/main/main.ts index 1eccb543..a46794e5 100644 --- a/src/electron/main/main.ts +++ b/src/electron/main/main.ts @@ -26,6 +26,7 @@ import { Action, ActionTypes, AnalysisInput, + AppStats, BreachProtocolCommands, BreachProtocolKeyBinds, COMMANDS, @@ -33,6 +34,7 @@ import { DropZoneFileValidationErrors, KEY_BINDS, PackageDetails, + SetStatsAction, UpdateSettingsAction, WorkerStatus, } from '../common'; @@ -125,6 +127,12 @@ export class Main { this.restoreDefaultSettings(); }, }, + { + label: 'Clear stats', + click: () => { + this.clearStats(); + }, + }, ]); private trayMenu: MenuItemConstructorOptions[] = [ @@ -577,4 +585,21 @@ export class Main { this.store.dispatch(action, true); } + + async clearStats() { + const result = await nativeDialog.confirm({ + message: 'Do you want to clear stats?', + }); + + if (!result) { + return; + } + + const { stats } = this.store.getState(); + const entries = Object.entries(stats).map(([key]) => [key, 0]); + const clearedStats = Object.fromEntries(entries) as AppStats; + const action = new SetStatsAction(clearedStats); + + this.store.dispatch(action, true); + } } diff --git a/src/electron/main/store/reducer.ts b/src/electron/main/store/reducer.ts index d2ca0488..bac71480 100644 --- a/src/electron/main/store/reducer.ts +++ b/src/electron/main/store/reducer.ts @@ -2,17 +2,13 @@ import { isDaemonsFragment } from '@/core'; import { Action, ActionTypes, - Analysis, AnalysisResult, AppSettings, AppStats, BreachProtocolStatus, HistoryEntry, State, - UpdateStatus, - WorkerStatus, } from '@/electron/common'; -import type { ScreenshotDisplayOutput } from 'screenshot-desktop'; type Handler = (state: S, action: Action) => State; @@ -28,12 +24,14 @@ export function createReducer( }; } -function createSetHandler(prop: keyof State): Handler { +function createSetHandler( + prop: K +): Handler { return (state, { payload }) => ({ ...state, [prop]: payload }); } -const setDisplays = createSetHandler('displays'); -const setStatus = createSetHandler('status'); +const setDisplays = createSetHandler('displays'); +const setStatus = createSetHandler('status'); function getStatsFromHistoryEntry( { @@ -108,8 +106,8 @@ const removeHistoryEntry: Handler = (state, { payload }) => ({ history: state.history.filter((e) => e.uuid !== payload), }); -const setUpdateStatus = createSetHandler('updateStatus'); -const setAnalysis = createSetHandler('analysis'); +const setUpdateStatus = createSetHandler('updateStatus'); +const setAnalysis = createSetHandler('analysis'); const addAnalysisResults: Handler = ( state: State, @@ -125,6 +123,8 @@ const addAnalysisResults: Handler = ( }; }; +const setStats = createSetHandler('stats'); + export const appReducer = createReducer({ [ActionTypes.SET_DISPLAYS]: setDisplays, [ActionTypes.SET_STATUS]: setStatus, @@ -136,4 +136,5 @@ export const appReducer = createReducer({ [ActionTypes.SET_ANALYSIS]: setAnalysis, [ActionTypes.CLEAR_ANALYSIS]: setAnalysis, [ActionTypes.ADD_ANALYSIS_RESULTS]: addAnalysisResults, + [ActionTypes.SET_STATS]: setStats, });