Skip to content

Commit 108d50a

Browse files
committed
Rewrite configuration reducer in RTK
1 parent 962e341 commit 108d50a

File tree

8 files changed

+118
-140
lines changed

8 files changed

+118
-140
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports = {
7474
'reducers/browser.ts',
7575
'reducers/client.ts',
7676
'reducers/code.ts',
77+
'reducers/configuration.ts',
7778
'reducers/crates.ts',
7879
'reducers/featureFlags.ts',
7980
'reducers/globalConfiguration.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ node_modules
2525
!reducers/browser.ts
2626
!reducers/client.ts
2727
!reducers/code.ts
28+
!reducers/configuration.ts
2829
!reducers/crates.ts
2930
!reducers/featureFlags.ts
3031
!reducers/globalConfiguration.ts

ui/frontend/AdvancedOptionsMenu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback } from 'react';
22
import { useSelector, useDispatch } from 'react-redux';
33

4-
import * as actions from './actions';
4+
import * as config from './reducers/configuration';
55
import { Either as EitherConfig, Select as SelectConfig } from './ConfigElement';
66
import MenuGroup from './MenuGroup';
77
import { State } from './reducers';
@@ -16,8 +16,8 @@ const AdvancedOptionsMenu: React.FC = () => {
1616

1717
const dispatch = useDispatch();
1818

19-
const changeEdition = useCallback((e: Edition) => dispatch(actions.changeEdition(e)), [dispatch]);
20-
const changeBacktrace = useCallback((b: Backtrace) => dispatch(actions.changeBacktrace(b)), [dispatch]);
19+
const changeEdition = useCallback((e: Edition) => dispatch(config.changeEdition(e)), [dispatch]);
20+
const changeBacktrace = useCallback((b: Backtrace) => dispatch(config.changeBacktrace(b)), [dispatch]);
2121

2222
const channel = useSelector((state: State) => state.configuration.channel);
2323
const switchText = (channel !== Channel.Nightly) ? ' (will select nightly Rust)' : '';

ui/frontend/ChannelMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useSelector, useDispatch } from 'react-redux';
44
import MenuGroup from './MenuGroup';
55
import SelectOne from './SelectOne';
66

7-
import * as actions from './actions';
7+
import * as config from './reducers/configuration';
88
import * as selectors from './selectors';
99
import State from './state';
1010
import { Channel } from './types';
@@ -25,7 +25,7 @@ const ChannelMenu: React.FC<ChannelMenuProps> = props => {
2525

2626
const dispatch = useDispatch();
2727
const changeChannel = useCallback((channel: Channel) => {
28-
dispatch(actions.changeChannel(channel));
28+
dispatch(config.changeChannel(channel));
2929
props.close();
3030
}, [dispatch, props]);
3131

ui/frontend/ConfigMenu.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useDispatch, useSelector } from 'react-redux';
66
import { Either as EitherConfig, Select as SelectConfig } from './ConfigElement';
77
import MenuGroup from './MenuGroup';
88

9-
import * as actions from './actions';
9+
import * as config from './reducers/configuration';
1010
import State from './state';
1111
import {
1212
AssemblyFlavor,
@@ -33,19 +33,19 @@ const ConfigMenu: React.FC = () => {
3333
const processAssembly = useSelector((state: State) => state.configuration.processAssembly);
3434

3535
const dispatch = useDispatch();
36-
const changeAceTheme = useCallback((t: string) => dispatch(actions.changeAceTheme(t)), [dispatch]);
37-
const changeMonacoTheme = useCallback((t: string) => dispatch(actions.changeMonacoTheme(t)), [dispatch]);
38-
const changeKeybinding = useCallback((k: string) => dispatch(actions.changeKeybinding(k)), [dispatch]);
39-
const changeOrientation = useCallback((o: Orientation) => dispatch(actions.changeOrientation(o)), [dispatch]);
40-
const changeEditorStyle = useCallback((e: Editor) => dispatch(actions.changeEditor(e)), [dispatch]);
36+
const changeAceTheme = useCallback((t: string) => dispatch(config.changeAceTheme(t)), [dispatch]);
37+
const changeMonacoTheme = useCallback((t: string) => dispatch(config.changeMonacoTheme(t)), [dispatch]);
38+
const changeKeybinding = useCallback((k: string) => dispatch(config.changeKeybinding(k)), [dispatch]);
39+
const changeOrientation = useCallback((o: Orientation) => dispatch(config.changeOrientation(o)), [dispatch]);
40+
const changeEditorStyle = useCallback((e: Editor) => dispatch(config.changeEditor(e)), [dispatch]);
4141
const changeAssemblyFlavor =
42-
useCallback((a: AssemblyFlavor) => dispatch(actions.changeAssemblyFlavor(a)), [dispatch]);
42+
useCallback((a: AssemblyFlavor) => dispatch(config.changeAssemblyFlavor(a)), [dispatch]);
4343
const changePairCharacters =
44-
useCallback((p: PairCharacters) => dispatch(actions.changePairCharacters(p)), [dispatch]);
44+
useCallback((p: PairCharacters) => dispatch(config.changePairCharacters(p)), [dispatch]);
4545
const changeProcessAssembly =
46-
useCallback((p: ProcessAssembly) => dispatch(actions.changeProcessAssembly(p)), [dispatch]);
46+
useCallback((p: ProcessAssembly) => dispatch(config.changeProcessAssembly(p)), [dispatch]);
4747
const changeDemangleAssembly =
48-
useCallback((d: DemangleAssembly) => dispatch(actions.changeDemangleAssembly(d)), [dispatch]);
48+
useCallback((d: DemangleAssembly) => dispatch(config.changeDemangleAssembly(d)), [dispatch]);
4949

5050
return (
5151
<Fragment>

ui/frontend/ModeMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useSelector, useDispatch } from 'react-redux';
44
import MenuGroup from './MenuGroup';
55
import SelectOne from './SelectOne';
66

7-
import * as actions from './actions';
7+
import * as config from './reducers/configuration';
88
import State from './state';
99
import { Mode } from './types';
1010

@@ -16,7 +16,7 @@ const ModeMenu: React.FC<ModeMenuProps> = props => {
1616
const mode = useSelector((state: State) => state.configuration.mode);
1717
const dispatch = useDispatch();
1818
const changeMode = useCallback((mode: Mode) => {
19-
dispatch(actions.changeMode(mode));
19+
dispatch(config.changeMode(mode));
2020
props.close();
2121
}, [dispatch, props]
2222
);

ui/frontend/actions.ts

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ import {
77
} from './selectors';
88
import State from './state';
99
import {
10-
AssemblyFlavor,
1110
Backtrace,
1211
Channel,
13-
DemangleAssembly,
1412
Edition,
15-
Editor,
1613
Mode,
17-
Orientation,
18-
PairCharacters,
1914
PrimaryAction,
2015
PrimaryActionAuto,
2116
PrimaryActionCore,
22-
ProcessAssembly,
2317
} from './types';
2418

2519
import { performCommonExecute, wsExecuteRequest } from './reducers/output/execute';
@@ -31,6 +25,13 @@ import { performCompileToMirOnly } from './reducers/output/mir';
3125
import { performCompileToWasmOnly } from './reducers/output/wasm';
3226
import { navigateToHelp, navigateToIndex } from './reducers/page';
3327
import { addCrateType, editCode } from './reducers/code';
28+
import {
29+
changeBacktrace,
30+
changeChannel,
31+
changeEditionRaw,
32+
changeMode,
33+
changePrimaryAction,
34+
} from './reducers/configuration';
3435

3536
export type ThunkAction<T = void> = ReduxThunkAction<T, State, {}, Action>;
3637
export type SimpleThunkAction<T = void> = ReduxThunkAction<T, State, {}, AnyAction>;
@@ -41,73 +42,10 @@ const createAction = <T extends string, P extends {}>(type: T, props?: P) => (
4142

4243
export enum ActionType {
4344
InitializeApplication = 'INITIALIZE_APPLICATION',
44-
ChangeEditor = 'CHANGE_EDITOR',
45-
ChangeKeybinding = 'CHANGE_KEYBINDING',
46-
ChangeAceTheme = 'CHANGE_ACE_THEME',
47-
ChangeMonacoTheme = 'CHANGE_MONACO_THEME',
48-
ChangePairCharacters = 'CHANGE_PAIR_CHARACTERS',
49-
ChangeOrientation = 'CHANGE_ORIENTATION',
50-
ChangeAssemblyFlavor = 'CHANGE_ASSEMBLY_FLAVOR',
51-
ChangePrimaryAction = 'CHANGE_PRIMARY_ACTION',
52-
ChangeChannel = 'CHANGE_CHANNEL',
53-
ChangeDemangleAssembly = 'CHANGE_DEMANGLE_ASSEMBLY',
54-
ChangeProcessAssembly = 'CHANGE_PROCESS_ASSEMBLY',
55-
ChangeMode = 'CHANGE_MODE',
56-
ChangeEdition = 'CHANGE_EDITION',
57-
ChangeBacktrace = 'CHANGE_BACKTRACE',
5845
}
5946

6047
export const initializeApplication = () => createAction(ActionType.InitializeApplication);
6148

62-
export const changeEditor = (editor: Editor) =>
63-
createAction(ActionType.ChangeEditor, { editor });
64-
65-
export const changeKeybinding = (keybinding: string) =>
66-
createAction(ActionType.ChangeKeybinding, { keybinding });
67-
68-
export const changeAceTheme = (theme: string) =>
69-
createAction(ActionType.ChangeAceTheme, { theme });
70-
71-
export const changeMonacoTheme = (theme: string) =>
72-
createAction(ActionType.ChangeMonacoTheme, { theme });
73-
74-
export const changePairCharacters = (pairCharacters: PairCharacters) =>
75-
createAction(ActionType.ChangePairCharacters, { pairCharacters });
76-
77-
export const changeOrientation = (orientation: Orientation) =>
78-
createAction(ActionType.ChangeOrientation, { orientation });
79-
80-
export const changeAssemblyFlavor = (assemblyFlavor: AssemblyFlavor) =>
81-
createAction(ActionType.ChangeAssemblyFlavor, { assemblyFlavor });
82-
83-
export const changeDemangleAssembly = (demangleAssembly: DemangleAssembly) =>
84-
createAction(ActionType.ChangeDemangleAssembly, { demangleAssembly });
85-
86-
export const changeProcessAssembly = (processAssembly: ProcessAssembly) =>
87-
createAction(ActionType.ChangeProcessAssembly, { processAssembly });
88-
89-
const changePrimaryAction = (primaryAction: PrimaryAction) =>
90-
createAction(ActionType.ChangePrimaryAction, { primaryAction });
91-
92-
export const changeChannel = (channel: Channel) =>
93-
createAction(ActionType.ChangeChannel, { channel });
94-
95-
export const changeMode = (mode: Mode) =>
96-
createAction(ActionType.ChangeMode, { mode });
97-
98-
const changeEditionRaw = (edition: Edition) =>
99-
createAction(ActionType.ChangeEdition, { edition });
100-
101-
export const changeEdition = (edition: Edition): ThunkAction => dispatch => {
102-
if (edition === Edition.Rust2024) {
103-
dispatch(changeChannel(Channel.Nightly));
104-
}
105-
106-
dispatch(changeEditionRaw(edition));
107-
}
108-
109-
export const changeBacktrace = (backtrace: Backtrace) =>
110-
createAction(ActionType.ChangeBacktrace, { backtrace });
11149

11250
export const reExecuteWithBacktrace = (): ThunkAction => dispatch => {
11351
dispatch(changeBacktrace(Backtrace.Enabled));
@@ -274,20 +212,11 @@ export function showExample(code: string): ThunkAction {
274212

275213
export type Action =
276214
| ReturnType<typeof initializeApplication>
277-
| ReturnType<typeof changePairCharacters>
278-
| ReturnType<typeof changeAssemblyFlavor>
279215
| ReturnType<typeof changeBacktrace>
280216
| ReturnType<typeof changeChannel>
281-
| ReturnType<typeof changeDemangleAssembly>
282217
| ReturnType<typeof changeEditionRaw>
283-
| ReturnType<typeof changeEditor>
284-
| ReturnType<typeof changeKeybinding>
285218
| ReturnType<typeof changeMode>
286-
| ReturnType<typeof changeOrientation>
287219
| ReturnType<typeof changePrimaryAction>
288-
| ReturnType<typeof changeProcessAssembly>
289-
| ReturnType<typeof changeAceTheme>
290-
| ReturnType<typeof changeMonacoTheme>
291220
| ReturnType<typeof editCode>
292221
| ReturnType<typeof addCrateType>
293222
| ReturnType<typeof navigateToIndex>

ui/frontend/reducers/configuration.ts

Lines changed: 92 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Action, ActionType } from '../actions';
1+
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
2+
3+
import { SimpleThunkAction } from '../actions';
24
import {
35
AssemblyFlavor,
46
Backtrace,
@@ -35,7 +37,7 @@ export interface State {
3537
backtrace: Backtrace;
3638
}
3739

38-
const DEFAULT: State = {
40+
const initialState: State = {
3941
editor: Editor.Ace,
4042
ace: {
4143
keybinding: 'ace',
@@ -56,48 +58,93 @@ const DEFAULT: State = {
5658
backtrace: Backtrace.Disabled,
5759
};
5860

59-
export default function configuration(state = DEFAULT, action: Action): State {
60-
switch (action.type) {
61-
case ActionType.ChangeEditor:
62-
return { ...state, editor: action.editor };
63-
case ActionType.ChangeKeybinding: {
64-
const { ace } = state;
61+
const slice = createSlice({
62+
name: 'configuration',
63+
initialState,
64+
reducers: {
65+
changeAceTheme: (state, action: PayloadAction<string>) => {
66+
state.ace.theme = action.payload;
67+
},
6568

66-
return { ...state, ace: { ...ace, keybinding: action.keybinding } };
67-
}
68-
case ActionType.ChangeAceTheme: {
69-
const { ace } = state;
70-
return { ...state, ace: { ...ace, theme: action.theme } };
71-
}
72-
case ActionType.ChangePairCharacters: {
73-
const { ace } = state;
74-
return { ...state, ace: { ...ace, pairCharacters: action.pairCharacters } };
75-
}
76-
case ActionType.ChangeMonacoTheme: {
77-
const { monaco } = state;
78-
return { ...state, monaco: { ...monaco, theme: action.theme } };
79-
}
80-
case ActionType.ChangeOrientation:
81-
return { ...state, orientation: action.orientation };
82-
case ActionType.ChangeAssemblyFlavor:
83-
return { ...state, assemblyFlavor: action.assemblyFlavor };
84-
case ActionType.ChangeDemangleAssembly:
85-
return { ...state, demangleAssembly: action.demangleAssembly };
86-
case ActionType.ChangeProcessAssembly:
87-
return { ...state, processAssembly: action.processAssembly };
88-
case ActionType.ChangePrimaryAction:
89-
return { ...state, primaryAction: action.primaryAction };
90-
case ActionType.ChangeChannel: {
91-
return { ...state, channel: action.channel };
92-
}
93-
case ActionType.ChangeMode:
94-
return { ...state, mode: action.mode };
95-
case ActionType.ChangeEdition: {
96-
return { ...state, edition: action.edition };
69+
changeAssemblyFlavor: (state, action: PayloadAction<AssemblyFlavor>) => {
70+
state.assemblyFlavor = action.payload;
71+
},
72+
73+
changeBacktrace: (state, action: PayloadAction<Backtrace>) => {
74+
state.backtrace = action.payload;
75+
},
76+
77+
changeChannel: (state, action: PayloadAction<Channel>) => {
78+
state.channel = action.payload;
79+
},
80+
81+
changeDemangleAssembly: (state, action: PayloadAction<DemangleAssembly>) => {
82+
state.demangleAssembly = action.payload;
83+
},
84+
85+
changeEditionRaw: (state, action: PayloadAction<Edition>) => {
86+
state.edition = action.payload;
87+
},
88+
89+
changeEditor: (state, action: PayloadAction<Editor>) => {
90+
state.editor = action.payload;
91+
},
92+
93+
changeKeybinding: (state, action: PayloadAction<string>) => {
94+
state.ace.keybinding = action.payload;
95+
},
96+
97+
changeMode: (state, action: PayloadAction<Mode>) => {
98+
state.mode = action.payload;
99+
},
100+
101+
changeMonacoTheme: (state, action: PayloadAction<string>) => {
102+
state.monaco.theme = action.payload;
103+
},
104+
105+
changeOrientation: (state, action: PayloadAction<Orientation>) => {
106+
state.orientation = action.payload;
107+
},
108+
109+
changePairCharacters: (state, action: PayloadAction<PairCharacters>) => {
110+
state.ace.pairCharacters = action.payload;
111+
},
112+
113+
changePrimaryAction: (state, action: PayloadAction<PrimaryAction>) => {
114+
state.primaryAction = action.payload;
115+
},
116+
117+
changeProcessAssembly: (state, action: PayloadAction<ProcessAssembly>) => {
118+
state.processAssembly = action.payload;
119+
},
120+
},
121+
});
122+
123+
export const {
124+
changeAceTheme,
125+
changeAssemblyFlavor,
126+
changeBacktrace,
127+
changeChannel,
128+
changeDemangleAssembly,
129+
changeEditionRaw,
130+
changeEditor,
131+
changeKeybinding,
132+
changeMode,
133+
changeMonacoTheme,
134+
changeOrientation,
135+
changePairCharacters,
136+
changePrimaryAction,
137+
changeProcessAssembly,
138+
} = slice.actions;
139+
140+
export const changeEdition =
141+
(edition: Edition): SimpleThunkAction =>
142+
(dispatch) => {
143+
if (edition === Edition.Rust2024) {
144+
dispatch(changeChannel(Channel.Nightly));
97145
}
98-
case ActionType.ChangeBacktrace:
99-
return { ...state, backtrace: action.backtrace };
100-
default:
101-
return state;
102-
}
103-
}
146+
147+
dispatch(changeEditionRaw(edition));
148+
};
149+
150+
export default slice.reducer;

0 commit comments

Comments
 (0)