diff --git a/packages/threat-composer-app/src/containers/ThreatStatementEditor/index.tsx b/packages/threat-composer-app/src/containers/ThreatStatementEditor/index.tsx index 99613b58..75ce5148 100644 --- a/packages/threat-composer-app/src/containers/ThreatStatementEditor/index.tsx +++ b/packages/threat-composer-app/src/containers/ThreatStatementEditor/index.tsx @@ -21,10 +21,10 @@ import { useThreatPacksContext, DEFAULT_NEW_ENTITY_ID, ThreatPack, + getNewThreatStatement, } from '@aws/threat-composer'; import { useEffect, useState } from 'react'; import { useLocation, useParams } from 'react-router-dom'; -import { v4 as uuidV4 } from 'uuid'; import isMemoryRouterUsed from '../../utils/isMemoryRouterUsed'; const ThreatStatementEditor = () => { @@ -62,10 +62,7 @@ const ThreatStatementEditor = () => { const { threatPacks } = useThreatPacksContext(); const [editingStatement] = useState(() => { - let statement: TemplateThreatStatement = { - id: uuidV4(), - numericId: -1, - }; + let statement: TemplateThreatStatement = getNewThreatStatement(); if (threatId === DEFAULT_NEW_ENTITY_ID && idToCopy) { // Create new threat from copying an existing threat @@ -74,8 +71,7 @@ const ThreatStatementEditor = () => { const { id: _id, displayOrder, tags, metadata, ...rest } = copiedStatement; statement = { ...rest, - id: uuidV4(), - numericId: -1, + ...statement, }; } } else if (threatId === DEFAULT_NEW_ENTITY_ID && threatPackId && threatPackThreatId) { diff --git a/packages/threat-composer/src/components/threats/ThreatStatementEditor/index.tsx b/packages/threat-composer/src/components/threats/ThreatStatementEditor/index.tsx index bedb7075..0c60b46b 100644 --- a/packages/threat-composer/src/components/threats/ThreatStatementEditor/index.tsx +++ b/packages/threat-composer/src/components/threats/ThreatStatementEditor/index.tsx @@ -36,6 +36,7 @@ import threatFieldData from '../../../data/threatFieldData'; import threatStatementExamples from '../../../data/threatStatementExamples.json'; import threatStatementFormat from '../../../data/threatStatementFormat'; import useEditMetadata from '../../../hooks/useEditMetadata'; +import getNewThreatStatement from '../../../utils/getNewThreatStatement'; import getRecommendedEditor from '../../../utils/getRecommandedEditor'; import renderThreatStatement from '../../../utils/renderThreatStatement'; import scrollToTop from '../../../utils/scrollToTop'; @@ -152,7 +153,7 @@ const ThreatStatementEditorInner: FC<{ editingStatement: TemplateThreatStatement }, [editingStatement, composerMode]); const handleStartOver = useCallback(() => { - addStatement(); + setEditingStatement(getNewThreatStatement()); setEditor(undefined); fullExamplesRef.current?.collapse(); }, [addStatement]); diff --git a/packages/threat-composer/src/contexts/ThreatsContext/hooks/useThreats/index.ts b/packages/threat-composer/src/contexts/ThreatsContext/hooks/useThreats/index.ts index 611d881d..aaa0876c 100644 --- a/packages/threat-composer/src/contexts/ThreatsContext/hooks/useThreats/index.ts +++ b/packages/threat-composer/src/contexts/ThreatsContext/hooks/useThreats/index.ts @@ -14,8 +14,8 @@ limitations under the License. ******************************************************************************************************************** */ import { useCallback, useEffect, useState } from 'react'; -import { v4 as uuidV4 } from 'uuid'; import { ComposerMode, TemplateThreatStatement } from '../../../../customTypes'; +import getNewThreatStatement from '../../../../utils/getNewThreatStatement'; import { View } from '../../types'; const useThreats = ( @@ -29,24 +29,21 @@ const useThreats = ( const handleAddStatement = useCallback((idToCopy?: string) => { if (composerMode !== 'Full') { // If Full mode, the value will be set via the route in the app. + const newDefaultStatement = getNewThreatStatement(); + if (idToCopy) { const copiedStatement = statementList.find(st => st.id === idToCopy); if (copiedStatement) { const { id: _id, displayOrder, tags, metadata, ...rest } = copiedStatement; const newStatement = { ...rest, - id: uuidV4(), - numericId: -1, + ...newDefaultStatement, }; setEditingStatement(newStatement); } } else { - const newStatement: TemplateThreatStatement = { - id: uuidV4(), - numericId: -1, - }; - setEditingStatement(newStatement); + setEditingStatement(newDefaultStatement); } } }, [statementList, setEditingStatement]); diff --git a/packages/threat-composer/src/utils/getNewThreatStatement/index.tsx b/packages/threat-composer/src/utils/getNewThreatStatement/index.tsx new file mode 100644 index 00000000..a6bf3f5c --- /dev/null +++ b/packages/threat-composer/src/utils/getNewThreatStatement/index.tsx @@ -0,0 +1,26 @@ +/** ******************************************************************************************************************* + 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 { v4 as uuidV4 } from 'uuid'; +import { TemplateThreatStatement } from '../../customTypes'; + +const getNewThreatStatement = (): TemplateThreatStatement => { + return { + id: uuidV4(), + numericId: -1, + }; +}; + +export default getNewThreatStatement; \ No newline at end of file diff --git a/packages/threat-composer/src/utils/getThreatFromThreatPacksThreat/index.ts b/packages/threat-composer/src/utils/getThreatFromThreatPacksThreat/index.ts index dc0510c8..db9b2a2c 100644 --- a/packages/threat-composer/src/utils/getThreatFromThreatPacksThreat/index.ts +++ b/packages/threat-composer/src/utils/getThreatFromThreatPacksThreat/index.ts @@ -13,15 +13,14 @@ See the License for the specific language governing permissions and limitations under the License. ******************************************************************************************************************** */ -import { v4 as uuidv4 } from 'uuid'; import { METADATA_KEY_SOURCE, METADATA_KEY_SOURCE_THREAT_PACK, METADATA_KEY_SOURCE_THREAT_PACK_THREAT, METADATA_SOURCE_THREAT_PACK } from '../../configs'; import { TemplateThreatStatement } from '../../customTypes'; +import getNewThreatStatement from '../getNewThreatStatement'; const getThreatFromThreactPackThreat = (threatPackId: string, t: TemplateThreatStatement) => { return { ...t, - numericId: -1, - id: uuidv4(), + ...getNewThreatStatement(), tags: t.tags, metadata: [ ...(t.metadata || []), diff --git a/packages/threat-composer/src/utils/index.ts b/packages/threat-composer/src/utils/index.ts index 143a18db..6caf2d43 100644 --- a/packages/threat-composer/src/utils/index.ts +++ b/packages/threat-composer/src/utils/index.ts @@ -13,4 +13,5 @@ See the License for the specific language governing permissions and limitations under the License. ******************************************************************************************************************** */ -export { default as getThreatFromThreatPacksThreat } from './getThreatFromThreatPacksThreat'; \ No newline at end of file +export { default as getThreatFromThreatPacksThreat } from './getThreatFromThreatPacksThreat'; +export { default as getNewThreatStatement } from './getNewThreatStatement'; \ No newline at end of file