Skip to content

Commit

Permalink
Subscriber notices now work in more cases (#553)
Browse files Browse the repository at this point in the history
Subscriber notices used to work for basic links, but not in cases where links
wrapped components, such as the Cards we use frequently to organize content.
In addition to ensuring notices appear in more cases, this fixes the logic
for extracting the repo name to ensure it appears in the notice.
  • Loading branch information
ebeneliason authored Oct 12, 2022
1 parent 6c67700 commit d0af868
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/SubscribersOnlyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const SubscribersOnlyModal: React.FC<SubscribersOnlyModalProps> = ({
}

const gitHubRepoName = externalLink.match(
/https:\/\/github.com\/gruntwork-io\/(.*?)\/.*/
/https:\/\/github.com\/gruntwork-io\/([^/]*)/
)

const setDontWarnMe = (event) => {
Expand Down
16 changes: 11 additions & 5 deletions src/theme/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,18 @@ function Root({ children }) {

useEffect(() => {
const listener = (event) => {
// Sometimes our links wrap components, such as Cards. In these cases, the event
// target is often a child element of the <a> we're attempting to extract the
// href data from, and so we search for the closest parent <a>. In the event that
// an <a> is clicked directly, that <a> itself will be returned.
const targetLink = event.target.closest("a")

// Allow clicks on the external GitHub link FROM the modal notices to work normally
if (event.target.dataset.modalExempt) {
if (targetLink.dataset.modalExempt) {
return
}

if (isGruntworkCisRepo(event.target.href)) {
if (isGruntworkCisRepo(targetLink.href)) {
const dontWarn = window.localStorage.getItem(
DONT_SHOW_CIS_GITHUB_WARNING_KEY
)
Expand All @@ -123,12 +129,12 @@ function Root({ children }) {
}

event.preventDefault()
setCisNoticeLink(event.target.href)
setCisNoticeLink(targetLink.href)
setDisplayCisNotice(true)
return
}

if (isPrivateGruntworkRepo(event.target.href)) {
if (isPrivateGruntworkRepo(targetLink.href)) {
const dontWarn = window.localStorage.getItem(
DONT_SHOW_PRIVATE_GITHUB_WARNING_KEY
)
Expand All @@ -139,7 +145,7 @@ function Root({ children }) {
}

event.preventDefault()
setSubscriberNoticeLink(event.target.href)
setSubscriberNoticeLink(targetLink.href)
setDisplaySubscriberNotice(true)
return
}
Expand Down

0 comments on commit d0af868

Please sign in to comment.