Skip to content

Commit

Permalink
Fix UI crash during setup (stashapp#4527)
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants authored and halkeye committed Sep 1, 2024
1 parent 50aac07 commit c32db48
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ui/v2.5/src/components/Settings/Inputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
39 changes: 39 additions & 0 deletions ui/v2.5/src/components/Settings/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();

Expand Down

0 comments on commit c32db48

Please sign in to comment.