Skip to content

Commit

Permalink
fix: Fix an issue where the StartOver button in Threat Editor was not…
Browse files Browse the repository at this point in the history
… working (#113)
  • Loading branch information
jessieweiyi authored Jun 3, 2024
1 parent 4146c57 commit a1c9f35
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -152,7 +153,7 @@ const ThreatStatementEditorInner: FC<{ editingStatement: TemplateThreatStatement
}, [editingStatement, composerMode]);

const handleStartOver = useCallback(() => {
addStatement();
setEditingStatement(getNewThreatStatement());
setEditor(undefined);
fullExamplesRef.current?.collapse();
}, [addStatement]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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]);
Expand Down
26 changes: 26 additions & 0 deletions packages/threat-composer/src/utils/getNewThreatStatement/index.tsx
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []),
Expand Down
3 changes: 2 additions & 1 deletion packages/threat-composer/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************************************************** */
export { default as getThreatFromThreatPacksThreat } from './getThreatFromThreatPacksThreat';
export { default as getThreatFromThreatPacksThreat } from './getThreatFromThreatPacksThreat';
export { default as getNewThreatStatement } from './getNewThreatStatement';

0 comments on commit a1c9f35

Please sign in to comment.