Skip to content

Commit

Permalink
feat(client-electron): add ability clear stats
Browse files Browse the repository at this point in the history
  • Loading branch information
marcincichocki authored Oct 22, 2023
1 parent 6ae3bfb commit e598e04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/electron/common/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Analysis,
AnalysisResult,
AppSettings,
AppStats,
HistoryEntry,
UpdateStatus,
WorkerStatus,
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {}
}
25 changes: 25 additions & 0 deletions src/electron/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import {
Action,
ActionTypes,
AnalysisInput,
AppStats,
BreachProtocolCommands,
BreachProtocolKeyBinds,
COMMANDS,
defaultOptions,
DropZoneFileValidationErrors,
KEY_BINDS,
PackageDetails,
SetStatsAction,
UpdateSettingsAction,
WorkerStatus,
} from '../common';
Expand Down Expand Up @@ -125,6 +127,12 @@ export class Main {
this.restoreDefaultSettings();
},
},
{
label: 'Clear stats',
click: () => {
this.clearStats();
},
},
]);

private trayMenu: MenuItemConstructorOptions[] = [
Expand Down Expand Up @@ -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);
}
}
19 changes: 10 additions & 9 deletions src/electron/main/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any, S = State> = (state: S, action: Action<T>) => State;

Expand All @@ -28,12 +24,14 @@ export function createReducer<S = State>(
};
}

function createSetHandler<T = any>(prop: keyof State): Handler<T, State> {
function createSetHandler<K extends keyof State>(
prop: K
): Handler<State[K], State> {
return (state, { payload }) => ({ ...state, [prop]: payload });
}

const setDisplays = createSetHandler<ScreenshotDisplayOutput[]>('displays');
const setStatus = createSetHandler<WorkerStatus>('status');
const setDisplays = createSetHandler('displays');
const setStatus = createSetHandler('status');

function getStatsFromHistoryEntry(
{
Expand Down Expand Up @@ -108,8 +106,8 @@ const removeHistoryEntry: Handler<string> = (state, { payload }) => ({
history: state.history.filter((e) => e.uuid !== payload),
});

const setUpdateStatus = createSetHandler<UpdateStatus>('updateStatus');
const setAnalysis = createSetHandler<Analysis>('analysis');
const setUpdateStatus = createSetHandler('updateStatus');
const setAnalysis = createSetHandler('analysis');

const addAnalysisResults: Handler<AnalysisResult> = (
state: State,
Expand All @@ -125,6 +123,8 @@ const addAnalysisResults: Handler<AnalysisResult> = (
};
};

const setStats = createSetHandler('stats');

export const appReducer = createReducer<State>({
[ActionTypes.SET_DISPLAYS]: setDisplays,
[ActionTypes.SET_STATUS]: setStatus,
Expand All @@ -136,4 +136,5 @@ export const appReducer = createReducer<State>({
[ActionTypes.SET_ANALYSIS]: setAnalysis,
[ActionTypes.CLEAR_ANALYSIS]: setAnalysis,
[ActionTypes.ADD_ANALYSIS_RESULTS]: addAnalysisResults,
[ActionTypes.SET_STATS]: setStats,
});

0 comments on commit e598e04

Please sign in to comment.