From 9c501cb7717e8670f0cd4004b08570e013aebc3b Mon Sep 17 00:00:00 2001 From: Jessie Wei Date: Wed, 24 Jan 2024 08:34:48 +1100 Subject: [PATCH] chore: Support applyTheme from window API (#73) --- .../generic/WindowExporter/index.tsx | 21 --------------- .../components/ThreatModelView/index.tsx | 14 +++++----- .../src/contexts/GlobalSetupContext/index.tsx | 26 +++++++++++++++++++ .../src/customTypes/dataExchange.ts | 2 ++ .../threat-composer/src/customTypes/index.ts | 3 ++- .../src/customTypes/windowAPI.ts | 22 ++++++++++++++++ 6 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 packages/threat-composer/src/customTypes/windowAPI.ts diff --git a/packages/threat-composer/src/components/generic/WindowExporter/index.tsx b/packages/threat-composer/src/components/generic/WindowExporter/index.tsx index 3dfdcfab..6cc2d848 100644 --- a/packages/threat-composer/src/components/generic/WindowExporter/index.tsx +++ b/packages/threat-composer/src/components/generic/WindowExporter/index.tsx @@ -15,29 +15,8 @@ ******************************************************************************************************************** */ import { useCallback, FC, PropsWithChildren, useEffect } from 'react'; import { useWorkspacesContext } from '../../../contexts'; -import { ThreatComposerNamespace } from '../../../customTypes/dataExchange'; import useExportImport from '../../../hooks/useExportImport'; import useRemoveData from '../../../hooks/useRemoveData'; -import EventController from '../../../utils/EventController'; - -declare global { - interface Window { - threatcomposer: ThreatComposerNamespace; - } -} - -const stringifyWorkspaceData = (data: any) => { - return JSON.stringify(data, null, 2); -}; - -const eventController = new EventController(); - -window.threatcomposer = { - stringifyWorkspaceData, - addEventListener: (eventName, eventHandler) => - eventController.addEventListener(eventName, eventHandler), - dispatchEvent: (event) => eventController.dispatchEvent(event), -}; /** * Export threat-composer functionalities via window object. diff --git a/packages/threat-composer/src/components/report/ThreatModel/components/ThreatModelView/index.tsx b/packages/threat-composer/src/components/report/ThreatModel/components/ThreatModelView/index.tsx index 27f74882..0832cc5d 100644 --- a/packages/threat-composer/src/components/report/ThreatModel/components/ThreatModelView/index.tsx +++ b/packages/threat-composer/src/components/report/ThreatModel/components/ThreatModelView/index.tsx @@ -108,22 +108,22 @@ const ThreatModelView: FC = ({ const getNextStepButtons = useCallback(() => { const buttons: ReactNode[] = []; if (!hasContentDetails?.applicationInfo) { - buttons.push(); + buttons.push(); } if (!hasContentDetails?.architecture) { - buttons.push(); + buttons.push(); } if (!hasContentDetails?.dataflow) { - buttons.push(); + buttons.push(); } if (!hasContentDetails?.assumptions) { - buttons.push(); + buttons.push(); } if (!hasContentDetails?.threats) { - buttons.push(); + buttons.push(); } - if (!hasContentDetails?.threats) { - buttons.push(); + if (!hasContentDetails?.mitigations) { + buttons.push(); } const len = buttons.length; return buttons.flatMap((b, index) => index === len - 1 ? {b} : [b, or]); diff --git a/packages/threat-composer/src/contexts/GlobalSetupContext/index.tsx b/packages/threat-composer/src/contexts/GlobalSetupContext/index.tsx index 6757bb9c..87119d7a 100644 --- a/packages/threat-composer/src/contexts/GlobalSetupContext/index.tsx +++ b/packages/threat-composer/src/contexts/GlobalSetupContext/index.tsx @@ -14,12 +14,14 @@ limitations under the License. ******************************************************************************************************************** */ /** @jsxImportSource @emotion/react */ +import { applyMode as applyCloudscapeMode, Mode, applyDensity as applyCloudscapeDensity, Density } from '@cloudscape-design/global-styles'; import { FC, PropsWithChildren, useState, useEffect } from 'react'; import useLocalStorageState from 'use-local-storage-state'; import { GlobalSetupContext, useGlobalSetupContext } from './context'; import InfoModal from '../../components/global/InfoModal'; import { LOCAL_STORAGE_KEY_NEW_VISIT_FLAG } from '../../configs/localStorageKeys'; import { ComposerMode, DataExchangeFormat } from '../../customTypes'; +import EventController from '../../utils/EventController'; import '@cloudscape-design/global-styles/index.css'; @@ -32,6 +34,30 @@ export interface GlobalSetupContextProviderProps { onDefineWorkload?: () => void; } +const stringifyWorkspaceData = (data: any) => { + return JSON.stringify(data, null, 2); +}; + +const applyDensity = (density?: string) => { + applyCloudscapeDensity(density === 'compact' ? Density.Compact : Density.Comfortable); +}; + +const applyTheme = (theme?: string) => { + applyCloudscapeMode(theme === 'dark' ? Mode.Dark : Mode.Light); +}; + +const eventController = new EventController(); + +window.threatcomposer = { + stringifyWorkspaceData, + addEventListener: (eventName, eventHandler) => + eventController.addEventListener(eventName, eventHandler), + dispatchEvent: (event) => eventController.dispatchEvent(event), + applyDensity, + applyTheme, +}; + + const GlobalSetupContextProvider: FC> = ({ children, composerMode = 'Full', diff --git a/packages/threat-composer/src/customTypes/dataExchange.ts b/packages/threat-composer/src/customTypes/dataExchange.ts index 814b5bfe..c49f5128 100644 --- a/packages/threat-composer/src/customTypes/dataExchange.ts +++ b/packages/threat-composer/src/customTypes/dataExchange.ts @@ -75,4 +75,6 @@ export interface ThreatComposerNamespace { renameWorkspace?: (id: string, newWorkspaceName: string) => Promise; dispatchEvent: (event: CustomEvent) => void; addEventListener: (eventName: string, eventHandler: EventHandler) => void; + applyDensity: (density?: string) => void; + applyTheme: (theme?: string) => void; } \ No newline at end of file diff --git a/packages/threat-composer/src/customTypes/index.ts b/packages/threat-composer/src/customTypes/index.ts index 881cd48b..b8dc8486 100644 --- a/packages/threat-composer/src/customTypes/index.ts +++ b/packages/threat-composer/src/customTypes/index.ts @@ -26,4 +26,5 @@ export * from './dataflow'; export * from './dataExchange'; export * from './events'; export * from './components'; -export * from './referencePacks'; \ No newline at end of file +export * from './referencePacks'; +export * from './windowAPI'; \ No newline at end of file diff --git a/packages/threat-composer/src/customTypes/windowAPI.ts b/packages/threat-composer/src/customTypes/windowAPI.ts new file mode 100644 index 00000000..e1339db3 --- /dev/null +++ b/packages/threat-composer/src/customTypes/windowAPI.ts @@ -0,0 +1,22 @@ +/** ******************************************************************************************************************* + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"). + You may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ******************************************************************************************************************** */ +import { ThreatComposerNamespace } from './dataExchange'; + +declare global { + interface Window { + threatcomposer: ThreatComposerNamespace; + } +}