Skip to content

Commit

Permalink
Merge pull request #616 from ExpressLRS/expert-mode
Browse files Browse the repository at this point in the history
Expert mode
  • Loading branch information
jurgelenas authored Jun 15, 2024
2 parents 4b22ae8 + 37b51ea commit 771e7de
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 64 deletions.
5 changes: 3 additions & 2 deletions src/i18n/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@
"Settings": "Settings",
"LanguageSelector": "Language selector",
"CurrentLanguage": "Current language",
"DeveloperMode": "Developer Mode",
"DeveloperOptions": "Developer Options"
"ExpertMode": "Expert Mode",
"ApplicationOptions": "Application Options",
"ExpertModeDescription": "Enable Expert Mode to unlock advanced features including additional firmware sources, the ability to build firmware with manual user-defined parameters, and access to serial monitor screen for detailed diagnostics."
},
"SupportView": {
"Support": "Support",
Expand Down
3 changes: 2 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ ipcMain.on(

ipcMain.handle(
IpcRequest.SaveFile,
async (_, arg: SaveFileRequestBody): Promise<SaveFileResponseBody> => {
async (_, args: [SaveFileRequestBody]): Promise<SaveFileResponseBody> => {
const arg = args[0];
const result = await dialog.showSaveDialog({
title: 'Save File',
defaultPath: arg.defaultPath,
Expand Down
6 changes: 3 additions & 3 deletions src/ui/components/DeviceOptionsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '../../gql/generated/types';
import ShowAlerts from '../ShowAlerts';
import Loader from '../Loader';
import useDeveloperMode from '../../hooks/useDeveloperMode';
import useAppState from '../../hooks/useAppState';

const styles: Record<string, SxProps<Theme>> = {
categoryTitle: {
Expand Down Expand Up @@ -243,11 +243,11 @@ const DeviceOptionsForm: FunctionComponent<DeviceOptionsFormProps> = (
});
};

const { isDeveloperModeEnabled } = useDeveloperMode();
const { isExpertModeEnabled } = useAppState();

return (
<>
{isDeveloperModeEnabled && (
{isExpertModeEnabled && (
<FormControl component="fieldset" sx={styles.userDefinesMode}>
<RadioGroup
row
Expand Down
12 changes: 6 additions & 6 deletions src/ui/components/FirmwareVersionForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { ChooseFolderResponseBody, IpcRequest } from '../../../ipc';
import ApplicationStorage from '../../storage';
import GitRepository from '../../models/GitRepository';
import useDeveloperMode from '../../hooks/useDeveloperMode';
import useAppState from '../../hooks/useAppState';

const styles: Record<string, SxProps<Theme>> = {
tabs: {
Expand Down Expand Up @@ -405,7 +405,7 @@ const FirmwareVersionForm: FunctionComponent<FirmwareVersionCardProps> = (
const showBetaFpvAlert =
localPath?.toLocaleLowerCase()?.indexOf('betafpv') > -1;

const { isDeveloperModeEnabled } = useDeveloperMode();
const { isExpertModeEnabled } = useAppState();

return (
<>
Expand All @@ -419,25 +419,25 @@ const FirmwareVersionForm: FunctionComponent<FirmwareVersionCardProps> = (
label={t('FirmwareVersionForm.OfficialReleases')}
value={FirmwareSource.GitTag}
/>
{isDeveloperModeEnabled && (
{isExpertModeEnabled && (
<Tab
label={t('FirmwareVersionForm.GitBranch')}
value={FirmwareSource.GitBranch}
/>
)}
{isDeveloperModeEnabled && (
{isExpertModeEnabled && (
<Tab
label={t('FirmwareVersionForm.GitCommit')}
value={FirmwareSource.GitCommit}
/>
)}
{isDeveloperModeEnabled && (
{isExpertModeEnabled && (
<Tab
label={t('FirmwareVersionForm.Local')}
value={FirmwareSource.Local}
/>
)}
{isDeveloperModeEnabled && (
{isExpertModeEnabled && (
<Tab
label={t('FirmwareVersionForm.GitPullRequest')}
value={FirmwareSource.GitPullRequest}
Expand Down
6 changes: 2 additions & 4 deletions src/ui/components/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { SxProps, Theme } from '@mui/system';
import { useTranslation } from 'react-i18next';
import useAppState from '../../hooks/useAppState';
import AppStatus from '../../models/enum/AppStatus';
import useDeveloperMode from '../../hooks/useDeveloperMode';

const drawerWidth = 215;

Expand All @@ -43,7 +42,6 @@ const styles: Record<string, SxProps<Theme>> = {

const Sidebar: FunctionComponent = () => {
const location = useLocation();
const { isDeveloperModeEnabled } = useDeveloperMode();
const configuratorActive =
matchPath(location.pathname, '/configurator') !== null;
const backpackActive = matchPath(location.pathname, '/backpack') !== null;
Expand All @@ -53,7 +51,7 @@ const Sidebar: FunctionComponent = () => {
matchPath(location.pathname, '/serial-monitor') !== null;
const settingsActive = matchPath(location.pathname, '/settings') !== null;
const supportActive = matchPath(location.pathname, '/support') !== null;
const { appStatus } = useAppState();
const { appStatus, isExpertModeEnabled } = useAppState();

const navigationEnabled = appStatus !== AppStatus.Busy;

Expand Down Expand Up @@ -103,7 +101,7 @@ const Sidebar: FunctionComponent = () => {
<ListItemText primary={t('Sidebar.Logs')} />
</ListItemButton>

{isDeveloperModeEnabled && (
{isExpertModeEnabled && (
<ListItemButton
component={Link}
to="/serial-monitor"
Expand Down
24 changes: 20 additions & 4 deletions src/ui/context/AppStateProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React, { FunctionComponent, useMemo, useState } from 'react';
import AppState from '../models/AppState';
import AppStatus from '../models/enum/AppStatus';
import ApplicationStorage from '../storage';

export const AppStateContext = React.createContext<{
appState: AppState;
setAppState: (appState: AppState) => void;
}>({
appState: { appStatus: AppStatus.Interactive },
appState: {
appStatus: AppStatus.Interactive,
isExpertModeEnabled: false,
},
setAppState: () => {},
});

Expand All @@ -17,10 +21,22 @@ interface AppStateProviderContextProps {
const AppStateProvider: FunctionComponent<AppStateProviderContextProps> = ({
children,
}) => {
const [appState, setAppState] = useState<AppState>({
appStatus: AppStatus.Interactive,
const [appState, setAppState] = useState<AppState>(() => {
const storage = new ApplicationStorage();
return {
appStatus: AppStatus.Interactive,
isExpertModeEnabled: storage.getExpertModeEnabled() ?? false,
};
});
const value = useMemo(() => ({ appState, setAppState }), [appState]);
const preSetAppState = (state: AppState) => {
const storage = new ApplicationStorage();
storage.setExpertModeEnabled(state.isExpertModeEnabled);
setAppState(state);
};
const value = useMemo(
() => ({ appState, setAppState: preSetAppState }),
[appState]
);
return (
<AppStateContext.Provider value={value}>
{children}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/hooks/useAppState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export default function useAppState() {
setAppState({ ...appState, appStatus: mode });
};

return { ...appState, setAppStatus };
return { ...appState, appState, setAppStatus, setAppState };
}
28 changes: 0 additions & 28 deletions src/ui/hooks/useDeveloperMode.tsx

This file was deleted.

1 change: 1 addition & 0 deletions src/ui/models/AppState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import AppStatus from './enum/AppStatus';

export default interface AppState {
appStatus: AppStatus;
isExpertModeEnabled: boolean;
}
16 changes: 8 additions & 8 deletions src/ui/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export interface IApplicationStorage {

getShowSensitiveFieldData(field: string): Promise<boolean | null>;

setDeveloperModeEnabled(value: boolean): Promise<void>;
setExpertModeEnabled(value: boolean): void;

getDeveloperModeEnabled(): Promise<boolean | null>;
getExpertModeEnabled(): boolean | null;
}

const DEVICE_OPTIONS_BY_TARGET_KEYSPACE = 'device_options';
Expand All @@ -72,7 +72,7 @@ const WIFI_SSID_KEY = 'wifi_ssid';
const WIFI_PASSWORD_KEY = 'wifi_password';
const REGULATORY_DOMAIN_900_KEY = 'regulatory_domain_900';
const REGULATORY_DOMAIN_2400_KEY = 'regulatory_domain_2400';
const DEVELOPER_MODE = 'developer_mode';
const EXPERT_MODE = 'expert_mode';

export default class ApplicationStorage implements IApplicationStorage {
async saveDeviceOptions(
Expand Down Expand Up @@ -223,18 +223,18 @@ export default class ApplicationStorage implements IApplicationStorage {
return null;
}

async setDeveloperModeEnabled(value: boolean): Promise<void> {
localStorage.setItem(DEVELOPER_MODE, JSON.stringify(value));
setExpertModeEnabled(value: boolean): void {
localStorage.setItem(EXPERT_MODE, JSON.stringify(value));
}

async getDeveloperModeEnabled(): Promise<boolean | null> {
const value = localStorage.getItem(DEVELOPER_MODE);
getExpertModeEnabled(): boolean | null {
const value = localStorage.getItem(EXPERT_MODE);

if (value) {
try {
return JSON.parse(value);
} catch (e) {
console.error(`failed to parse state for ${DEVELOPER_MODE}`, e);
console.error(`failed to parse state for ${EXPERT_MODE}`, e);
return null;
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/ui/views/SettingsView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import CardTitle from '../../components/CardTitle';
import MainLayout from '../../layouts/MainLayout';
import Omnibox, { Option } from '../../components/Omnibox';
import locales from '../../../i18n/locales.json';
import useDeveloperMode from '../../hooks/useDeveloperMode';
import useAppState from '../../hooks/useAppState';

const SettingsView: FunctionComponent = () => {
const { t, i18n } = useTranslation();
Expand Down Expand Up @@ -47,8 +47,13 @@ const SettingsView: FunctionComponent = () => {
};
}

const { isDeveloperModeEnabled, setDeveloperMode } = useDeveloperMode();

const { appState, setAppState } = useAppState();
const setExpertMode = () => {
setAppState({
...appState,
isExpertModeEnabled: !appState.isExpertModeEnabled,
});
};
return (
<MainLayout>
<Card>
Expand All @@ -69,20 +74,21 @@ const SettingsView: FunctionComponent = () => {
<Divider />
<CardTitle
icon={<DeveloperModeIcon />}
title={t('SettingsView.DeveloperOptions')}
title={t('SettingsView.ApplicationOptions')}
/>
<CardContent style={{ paddingLeft: 26, marginTop: -18 }}>
<FormControlLabel
control={
<Checkbox
checked={isDeveloperModeEnabled}
checked={appState.isExpertModeEnabled}
onChange={() => {
setDeveloperMode();
setExpertMode();
}}
/>
}
label={t('SettingsView.DeveloperMode')}
label={<>{t('SettingsView.ExpertMode')}</>}
/>
<p>{t('SettingsView.ExpertModeDescription')}</p>
</CardContent>
</Card>
</MainLayout>
Expand Down

0 comments on commit 771e7de

Please sign in to comment.