Skip to content

Commit

Permalink
fix: Safely parse announcement Flag (#3052)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg committed Nov 28, 2023
1 parent 7b48af0 commit 6994f6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
3 changes: 3 additions & 0 deletions frontend/common/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ const Utils = Object.assign({}, require('./base/_utils'), {
getFlagsmithHasFeature(key: string) {
return flagsmith.hasFeature(key)
},
getFlagsmithJSONValue(key: string, defaultValue: any) {
return flagsmith.getValue(key, { fallback: defaultValue, json: true })
},
getFlagsmithValue(key: string) {
return flagsmith.getValue(key)
},
Expand Down
48 changes: 23 additions & 25 deletions frontend/web/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,13 @@ const App = class extends Component {
if (document.location.href.includes('widget')) {
return <div>{this.props.children}</div>
}
const announcementValue = JSON.parse(
Utils.getFlagsmithValue('announcement'),
)
const announcementValue = Utils.getFlagsmithJSONValue('announcement', null)
const dismissed = flagsmith.getTrait('dismissed_announcement')
const showBanner =
announcementValue && (!dismissed || dismissed !== announcementValue.id)
announcementValue &&
(!dismissed || dismissed !== announcementValue.id) &&
Utils.getFlagsmithHasFeature('announcement') &&
this.state.showAnnouncement

return (
<Provider store={getStore()}>
Expand Down Expand Up @@ -489,27 +490,24 @@ const App = class extends Component {
id={AccountStore.getOrganisation()?.id}
/>
)}
{user &&
showBanner &&
Utils.getFlagsmithHasFeature('announcement') &&
this.state.showAnnouncement && (
<Row>
<InfoMessage
title={announcementValue.title}
infoMessageClass={'announcement'}
isClosable={announcementValue.isClosable}
close={() =>
this.closeAnnouncement(announcementValue.id)
}
buttonText={announcementValue.buttonText}
url={announcementValue.url}
>
<div>
<div>{announcementValue.description}</div>
</div>
</InfoMessage>
</Row>
)}
{user && showBanner && (
<Row>
<InfoMessage
title={announcementValue.title}
infoMessageClass={'announcement'}
isClosable={announcementValue.isClosable}
close={() =>
this.closeAnnouncement(announcementValue.id)
}
buttonText={announcementValue.buttonText}
url={announcementValue.url}
>
<div>
<div>{announcementValue.description}</div>
</div>
</InfoMessage>
</Row>
)}
{this.props.children}
</Fragment>
)}
Expand Down

1 comment on commit 6994f6b

@vercel
Copy link

@vercel vercel bot commented on 6994f6b Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.