aboutsummaryrefslogtreecommitdiff
path: root/jb-ui/src/models/grlStatsSlice.ts
blob: 004d321fe6832ca2dcf9edbd5825ffea460ef969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { StatsSnapshot } from "@/api_fsb";
import { RootState } from "@/store";
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit";

const initialState: StatsSnapshot[] = [];

const grlStatsSlice = createSlice({
  name: "grlStats",
  initialState,
  reducers: {
    addStatsData(state, action: PayloadAction<StatsSnapshot>) {
      state.push(action.payload);
    },
  },
});

export const { addStatsData } = grlStatsSlice.actions;
export default grlStatsSlice.reducer;

export const selectRecentStats = createSelector(
  [(state: RootState) => state.stats],
  (stats): StatsSnapshot | null => {
    const lastStat = stats[stats.length - 1];
    return lastStat ?? null;
  },
);

export const activeUsers = createSelector(
  [selectRecentStats],
  (recentStats): number | null => {
    return recentStats?.active_users_last_24h ?? null;
  },
);

export const activeSurveys = createSelector(
  [selectRecentStats],
  (recentStats): number | null => {
    return recentStats?.live_task_count?.total ?? null;
  },
);

export const maxPayout = createSelector(
  [selectRecentStats],
  (recentStats): number | null => {
    return recentStats?.live_tasks_max_payout?.value ?? null;
  },
);