Skip to content

Commit

Permalink
add warning when layout defined is not in the list
Browse files Browse the repository at this point in the history
  • Loading branch information
aneuwald-ctw committed Dec 18, 2024
1 parent 3b19772 commit 9dc671c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/suite-base/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ export function App(props: AppProps): React.JSX.Element {
providers.unshift(...extraProviders);
}

// The toast and logs provider comes first so they are available to all downstream providers
providers.unshift(<StudioToastProvider />);
providers.unshift(<StudioLogsSettingsProvider />);

// Problems provider also must come before other, dependent contexts.
providers.unshift(<ProblemsContextProvider />);
providers.unshift(<CurrentLayoutProvider loaders={layoutLoaders} />);
Expand All @@ -121,6 +117,10 @@ export function App(props: AppProps): React.JSX.Element {
const layoutStorage = useMemo(() => new IdbLayoutStorage(), []);
providers.unshift(<LayoutStorageContext.Provider value={layoutStorage} />);

// The toast and logs provider comes first so they are available to all downstream providers
providers.unshift(<StudioToastProvider />);
providers.unshift(<StudioLogsSettingsProvider />);

const MaybeLaunchPreference = enableLaunchPreferenceScreen === true ? LaunchPreference : Fragment;

useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/suite-base/src/i18n/en/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
export const general = {
foxglove: "Foxglove",
learnMore: "Learn more",
noDefaultLayoutParameter:
"The layout '{{layoutName}}' specified in the app parameters does not exist.",
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import { act, renderHook } from "@testing-library/react";
import { SnackbarProvider } from "notistack";
import { SnackbarProvider, useSnackbar } from "notistack";
import { useEffect } from "react";

import { Condvar } from "@lichtblick/den/async";
Expand All @@ -28,6 +29,14 @@ import AppParametersProvider from "@lichtblick/suite-base/providers/AppParameter
import CurrentLayoutProvider from "@lichtblick/suite-base/providers/CurrentLayoutProvider";
import { MAX_SUPPORTED_LAYOUT_VERSION } from "@lichtblick/suite-base/providers/CurrentLayoutProvider/constants";
import { ILayoutManager } from "@lichtblick/suite-base/services/ILayoutManager";
import BasicBuilder from "@lichtblick/suite-base/testing/builders/BasicBuilder";

jest.mock("notistack", () => ({
...jest.requireActual("notistack"),
useSnackbar: jest.fn().mockReturnValue({
enqueueSnackbar: jest.fn(),
}),
}));

const TEST_LAYOUT: LayoutData = {
layout: "ExamplePanel!1",
Expand Down Expand Up @@ -316,4 +325,25 @@ describe("CurrentLayoutProvider", () => {
expect(selectedLayout).toBeDefined();
expect(selectedLayout).toBe("layout2");
});

it("should show a message to the user if the defaultLayout from app parameter is not found", async () => {
const mockAppParameters = { defaultLayout: BasicBuilder.string() };

const { result } = renderTest({
mockLayoutManager,
mockUserProfile,
mockAppParameters,
});

await act(async () => {
await result.current.childMounted;
});

const { enqueueSnackbar } = useSnackbar();

expect(enqueueSnackbar).toHaveBeenCalledWith(
`The layout '${mockAppParameters.defaultLayout}' specified in the app parameters does not exist.`,
{ variant: "warning" },
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as _ from "lodash-es";
import { useSnackbar } from "notistack";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { getNodeAtPath } from "react-mosaic-component";
import { useAsync, useAsyncFn, useMountedState } from "react-use";
import shallowequal from "shallowequal";
Expand Down Expand Up @@ -71,6 +72,8 @@ export default function CurrentLayoutProvider({
const analytics = useAnalytics();
const isMounted = useMountedState();

const { t } = useTranslation("general");

const appParameters = useAppParameters();

const [mosaicId] = useState(() => uuidv4());
Expand Down Expand Up @@ -285,13 +288,20 @@ export default function CurrentLayoutProvider({

const layouts = await layoutManager.getLayouts();

// // Check if there's a layout specified by app parameter
// Check if there's a layout specified by app parameter
const defaultLayoutFromParameters = layouts.find((l) => l.name === appParameters.defaultLayout);
if (defaultLayoutFromParameters) {
await setSelectedLayoutId(defaultLayoutFromParameters.id, { saveToProfile: false });
return;
}

// It there is a defaultLayout setted but didnt found a layout, show a error to the user
if (appParameters.defaultLayout) {
enqueueSnackbar(t("noDefaultLayoutParameter", { layoutName: appParameters.defaultLayout }), {
variant: "warning",
});
}

// Retreive the selected layout id from the user's profile. If there's no layout specified
// or we can't load it then save and select a default layout
const layout = currentLayoutId ? await layoutManager.getLayout(currentLayoutId) : undefined;
Expand Down

0 comments on commit 9dc671c

Please sign in to comment.