File size: 5,197 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { PayloadAction, Selector } from '@reduxjs/toolkit';
import { createSelector, createSlice } from '@reduxjs/toolkit';
import type { LogNamespace } from 'app/logging/logger';
import { zLogNamespace } from 'app/logging/logger';
import { EMPTY_ARRAY } from 'app/store/constants';
import type { PersistConfig, RootState } from 'app/store/store';
import { uniq } from 'lodash-es';

import type { Language, SystemState } from './types';

const initialSystemState: SystemState = {
  _version: 1,
  shouldConfirmOnDelete: true,
  shouldAntialiasProgressImage: false,
  shouldConfirmOnNewSession: true,
  language: 'en',
  shouldUseNSFWChecker: false,
  shouldUseWatermarker: false,
  shouldEnableInformationalPopovers: true,
  shouldEnableModelDescriptions: true,
  logIsEnabled: true,
  logLevel: 'debug',
  logNamespaces: [...zLogNamespace.options],
  shouldShowInvocationProgressDetail: false,
};

export const systemSlice = createSlice({
  name: 'system',
  initialState: initialSystemState,
  reducers: {
    setShouldConfirmOnDelete: (state, action: PayloadAction<boolean>) => {
      state.shouldConfirmOnDelete = action.payload;
    },
    logIsEnabledChanged: (state, action: PayloadAction<SystemState['logIsEnabled']>) => {
      state.logIsEnabled = action.payload;
    },
    logLevelChanged: (state, action: PayloadAction<SystemState['logLevel']>) => {
      state.logLevel = action.payload;
    },
    logNamespaceToggled: (state, action: PayloadAction<LogNamespace>) => {
      if (state.logNamespaces.includes(action.payload)) {
        state.logNamespaces = uniq(state.logNamespaces.filter((n) => n !== action.payload)).toSorted();
      } else {
        state.logNamespaces = uniq([...state.logNamespaces, action.payload]).toSorted();
      }
    },
    shouldAntialiasProgressImageChanged: (state, action: PayloadAction<boolean>) => {
      state.shouldAntialiasProgressImage = action.payload;
    },
    languageChanged: (state, action: PayloadAction<Language>) => {
      state.language = action.payload;
    },
    shouldUseNSFWCheckerChanged(state, action: PayloadAction<boolean>) {
      state.shouldUseNSFWChecker = action.payload;
    },
    shouldUseWatermarkerChanged(state, action: PayloadAction<boolean>) {
      state.shouldUseWatermarker = action.payload;
    },
    setShouldEnableInformationalPopovers(state, action: PayloadAction<boolean>) {
      state.shouldEnableInformationalPopovers = action.payload;
    },
    setShouldEnableModelDescriptions(state, action: PayloadAction<boolean>) {
      state.shouldEnableModelDescriptions = action.payload;
    },
    shouldConfirmOnNewSessionToggled(state) {
      state.shouldConfirmOnNewSession = !state.shouldConfirmOnNewSession;
    },
    setShouldShowInvocationProgressDetail(state, action: PayloadAction<boolean>) {
      state.shouldShowInvocationProgressDetail = action.payload;
    },
  },
});

export const {
  setShouldConfirmOnDelete,
  logIsEnabledChanged,
  logLevelChanged,
  logNamespaceToggled,
  shouldAntialiasProgressImageChanged,
  languageChanged,
  shouldUseNSFWCheckerChanged,
  shouldUseWatermarkerChanged,
  setShouldEnableInformationalPopovers,
  setShouldEnableModelDescriptions,
  shouldConfirmOnNewSessionToggled,
  setShouldShowInvocationProgressDetail,
} = systemSlice.actions;

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const migrateSystemState = (state: any): any => {
  if (!('_version' in state)) {
    state._version = 1;
  }
  return state;
};

export const systemPersistConfig: PersistConfig<SystemState> = {
  name: systemSlice.name,
  initialState: initialSystemState,
  migrate: migrateSystemState,
  persistDenylist: [],
};

export const selectSystemSlice = (state: RootState) => state.system;
const createSystemSelector = <T>(selector: Selector<SystemState, T>) => createSelector(selectSystemSlice, selector);

export const selectSystemLogLevel = createSystemSelector((system) => system.logLevel);
export const selectSystemLogNamespaces = createSystemSelector((system) =>
  system.logNamespaces.length > 0 ? system.logNamespaces : EMPTY_ARRAY
);
export const selectSystemLogIsEnabled = createSystemSelector((system) => system.logIsEnabled);
export const selectSystemShouldConfirmOnDelete = createSystemSelector((system) => system.shouldConfirmOnDelete);
export const selectSystemShouldUseNSFWChecker = createSystemSelector((system) => system.shouldUseNSFWChecker);
export const selectSystemShouldUseWatermarker = createSystemSelector((system) => system.shouldUseWatermarker);
export const selectSystemShouldAntialiasProgressImage = createSystemSelector(
  (system) => system.shouldAntialiasProgressImage
);
export const selectSystemShouldEnableInformationalPopovers = createSystemSelector(
  (system) => system.shouldEnableInformationalPopovers
);
export const selectSystemShouldEnableModelDescriptions = createSystemSelector(
  (system) => system.shouldEnableModelDescriptions
);
export const selectSystemShouldConfirmOnNewSession = createSystemSelector((system) => system.shouldConfirmOnNewSession);
export const selectSystemShouldShowInvocationProgressDetail = createSystemSelector(
  (system) => system.shouldShowInvocationProgressDetail
);