From f9c74774215f3d9ba43526ebdd55475a07b8a577 Mon Sep 17 00:00:00 2001 From: vferraro-scottlogic Date: Thu, 14 Dec 2023 11:18:20 +0000 Subject: [PATCH] SLVUU-117 add type guard for layoutJSON --- vuu-ui/packages/vuu-layout/src/utils/typeOf.ts | 4 +++- .../src/layout-management/useLayoutManager.tsx | 14 ++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/vuu-ui/packages/vuu-layout/src/utils/typeOf.ts b/vuu-ui/packages/vuu-layout/src/utils/typeOf.ts index 99bc4b3679..771af39f40 100644 --- a/vuu-ui/packages/vuu-layout/src/utils/typeOf.ts +++ b/vuu-ui/packages/vuu-layout/src/utils/typeOf.ts @@ -1,5 +1,5 @@ import { ReactElement } from 'react'; -import { LayoutModel, WithType } from '../layout-reducer'; +import { LayoutJSON, LayoutModel, WithType } from '../layout-reducer'; export function typeOf(element?: LayoutModel | WithType): string | undefined { if (element) { @@ -23,3 +23,5 @@ export function typeOf(element?: LayoutModel | WithType): string | undefined { } export const isTypeOf = (element: ReactElement, type: string) => typeOf(element) === type; + +export const isLayoutJSON = (layout: LayoutJSON): layout is LayoutJSON=> "type" in layout; diff --git a/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx b/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx index f2778f763d..a97eb65062 100644 --- a/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx +++ b/vuu-ui/packages/vuu-shell/src/layout-management/useLayoutManager.tsx @@ -15,6 +15,7 @@ import { RemoteLayoutPersistenceManager, resolveJSONPath, defaultApplicationJson, + isLayoutJSON, } from "@finos/vuu-layout"; import { NotificationLevel, useNotifications } from "@finos/vuu-popups"; import { LayoutMetadata, LayoutMetadataDto } from "./layoutTypes"; @@ -154,8 +155,12 @@ export const LayoutManagementProvider = ( const saveApplicationLayout = useCallback( (layout: LayoutJSON) => { - setApplicationLayout(layout, false); - getPersistenceManager().saveApplicationJSON(applicationJSONRef.current); + if (isLayoutJSON(layout)) { + setApplicationLayout(layout, false); + getPersistenceManager().saveApplicationJSON(applicationJSONRef.current); + } else { + console.error("Tried to save invalid application layout", layout); + } }, [setApplicationLayout] ); @@ -167,7 +172,7 @@ export const LayoutManagementProvider = ( "#main-tabs.ACTIVE_CHILD" ); - if (layoutToSave) { + if (layoutToSave && isLayoutJSON(layoutToSave)) { getPersistenceManager() .createLayout(metadata, ensureLayoutHasTitle(layoutToSave, metadata)) .then((metadata) => { @@ -187,10 +192,11 @@ export const LayoutManagementProvider = ( console.error("Error occurred while saving layout", error); }); } else { + console.error("Tried to save invalid layout", layoutToSave); notify({ type: NotificationLevel.Error, header: "Failed to Save Layout", - body: "Cannot save undefined layout", + body: "Cannot save invalid layout", }); } },