Fix UI crash during setup (#4527)

This commit is contained in:
WithoutPants 2024-02-06 11:48:26 +11:00 committed by GitHub
parent 892d74c98b
commit 330581283a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 2 deletions

View file

@ -5,7 +5,7 @@ import { FormattedMessage, useIntl } from "react-intl";
import { Icon } from "../Shared/Icon";
import { StringListInput } from "../Shared/StringListInput";
import { PatchComponent } from "src/pluginApi";
import { useSettings } from "./context";
import { useSettings, useSettingsOptional } from "./context";
interface ISetting {
id?: string;
@ -37,7 +37,8 @@ export const Setting: React.FC<PropsWithChildren<ISetting>> = PatchComponent(
advanced,
} = props;
const { advancedMode } = useSettings();
// these components can be used in the setup wizard, where advanced mode is not available
const { advancedMode } = useSettingsOptional();
const intl = useIntl();

View file

@ -51,6 +51,35 @@ export interface ISettingsContextState {
refetch: () => void;
}
function noop() {}
const emptyState: ISettingsContextState = {
loading: false,
error: undefined,
general: {},
interface: {},
defaults: {},
scraping: {},
dlna: {},
ui: {},
plugins: {},
advancedMode: false,
apiKey: "",
saveGeneral: noop,
saveInterface: noop,
saveDefaults: noop,
saveScraping: noop,
saveDLNA: noop,
saveUI: noop,
savePluginSettings: noop,
setAdvancedMode: noop,
refetch: noop,
};
export const SettingStateContext =
React.createContext<ISettingsContextState | null>(null);
@ -64,6 +93,16 @@ export const useSettings = () => {
return context;
};
export function useSettingsOptional(): ISettingsContextState {
const context = React.useContext(SettingStateContext);
if (context === null) {
return emptyState;
}
return context;
}
export const SettingsContext: React.FC = ({ children }) => {
const Toast = useToast();