Skip to content

Commit f83d124

Browse files
authored
Get rid of useModule in Appearence settings (#5143)
* Get rid of use modules * fix eslint
1 parent ffeef0f commit f83d124

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

Diff for: app/components-react/shared/inputs/inputs.ts

+32
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ export function useTextInput<
329329
}
330330

331331
/**
332+
* @deprecated use bindFormState instead
332333
* 2-way binding util for inputs
333334
*
334335
* @example
@@ -355,6 +356,37 @@ export function createBinding<TState extends object, TExtraProps extends object
355356
return createFormBinding(stateGetter, stateSetter, extraPropsGenerator).proxy;
356357
}
357358

359+
type TFormBindings<TState, TExtraProps = {}> = {
360+
[K in keyof TState]: {
361+
name: K;
362+
value: TState[K];
363+
onChange: (newVal: TState[K]) => unknown;
364+
};
365+
} &
366+
TExtraProps;
367+
368+
export function bindFormState<TFormState, TExtraProps = {}>(
369+
getFormState: () => TFormState,
370+
updateFormState: (statePatch: Partial<TFormState>) => unknown,
371+
extraProps?: TExtraProps,
372+
) {
373+
const formState = getFormState();
374+
const result = {} as any;
375+
for (const fieldName in formState) {
376+
result[fieldName] = {
377+
name: fieldName,
378+
value: formState[fieldName],
379+
onChange: (value: any) => {
380+
updateFormState({ [fieldName]: value } as Partial<TFormState>);
381+
},
382+
};
383+
}
384+
385+
extraProps ?? Object.assign(result, extraProps);
386+
387+
return result as TFormBindings<TFormState, TExtraProps>;
388+
}
389+
358390
function createValidationRules(type: TInputType, inputProps: IInputCommonProps<unknown>) {
359391
const rules = inputProps.rules ? [...inputProps.rules] : [];
360392
if (inputProps.required) {

Diff for: app/components-react/windows/settings/Appearance.tsx

+5-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { Row, Col, Select } from 'antd';
55
import { CheckboxInput, ListInput, SliderInput, SwitchInput } from '../../shared/inputs';
66
import { getDefined } from '../../../util/properties-type-guards';
77
import { ObsSettingsSection } from './ObsSettings';
8-
import * as remote from '@electron/remote';
9-
import { injectFormBinding, useModule } from 'slap';
108
import { ENavName, EMenuItemKey, IAppMenuItem, menuTitles } from 'services/side-nav';
119
import { useVuex } from 'components-react/hooks';
1210
import styles from './Appearance.m.less';
@@ -16,6 +14,7 @@ import Scrollable from 'components-react/shared/Scrollable';
1614
import UltraIcon from 'components-react/shared/UltraIcon';
1715
import { CustomizationState } from 'services/customization';
1816
import { useRealmObject } from 'components-react/hooks/realm';
17+
import { bindFormState } from 'components-react/shared/inputs';
1918

2019
const { Option } = Select;
2120

@@ -33,17 +32,10 @@ export function AppearanceSettings() {
3332
// Hooks up reactivity for Customization state
3433
useRealmObject(CustomizationService.state);
3534

36-
const { bind } = useModule(() => {
37-
function getSettings() {
38-
return CustomizationService.state.toObject() as CustomizationState;
39-
}
40-
41-
function setSettings(newSettings: CustomizationState) {
42-
CustomizationService.actions.setSettings(newSettings as any);
43-
}
44-
45-
return { bind: injectFormBinding(getSettings, setSettings) };
46-
});
35+
const bind = bindFormState(
36+
() => CustomizationService.state.toObject() as CustomizationState,
37+
(newSettings: CustomizationState) => CustomizationService.setSettings(newSettings as any),
38+
);
4739

4840
const {
4941
compactView,

0 commit comments

Comments
 (0)