-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fleet UI: Convert URLs in Policy resolution text to be clickable links (
- Loading branch information
1 parent
4e8696c
commit ccdd1a0
Showing
6 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Policy resolutions that include URLs are clickable in the UI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import ClickableUrls from "./ClickableUrls"; | ||
|
||
const TEXT_WITH_URLS = | ||
"Contact your IT administrator to ensure your Mac is receiving a profile that disables advertisement tracking. https://privacyinternational.org/guide-step/4335/macos-opt-out-targeted-ads or https://support.apple.com/en-us/HT202074"; | ||
const URL_1 = | ||
"https://privacyinternational.org/guide-step/4335/macos-opt-out-targeted-ads"; | ||
const URL_2 = "https://support.apple.com/en-us/HT202074"; | ||
|
||
describe("ClickableUrls - component", () => { | ||
it("renders text and icon", () => { | ||
render(<ClickableUrls text={TEXT_WITH_URLS} />); | ||
|
||
const link1 = screen.getByRole("link", { name: URL_1 }); | ||
const link2 = screen.getByRole("link", { name: URL_2 }); | ||
|
||
expect(link1).toHaveAttribute("href", URL_1); | ||
expect(link1).toHaveAttribute("target", "_blank"); | ||
expect(link2).toHaveAttribute("href", URL_2); | ||
expect(link2).toHaveAttribute("target", "_blank"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from "react"; | ||
import * as DOMPurify from "dompurify"; | ||
import classnames from "classnames"; | ||
|
||
interface IClickableUrls { | ||
text: string; | ||
className?: string; | ||
} | ||
|
||
const baseClass = "clickable-urls"; | ||
|
||
const urlReplacer = (match: string) => { | ||
const url = match.startsWith("http") ? match : `https://${match}`; | ||
return `<a href=${url} target="_blank" rel="noreferrer"> | ||
${match} | ||
</a>`; | ||
}; | ||
|
||
const ClickableUrls = ({ text, className }: IClickableUrls): JSX.Element => { | ||
const clickableUrlClasses = classnames(baseClass, className); | ||
|
||
// Regex to find case insensitive URLs and replace with link | ||
const replacedLinks = text.replaceAll( | ||
/(https?)?(:\/\/)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g, | ||
urlReplacer | ||
); | ||
const sanitizedResolutionContent = DOMPurify.sanitize(replacedLinks, { | ||
ADD_ATTR: ["target"], // Allows opening in a new tab | ||
}); | ||
|
||
const textWithLinks = ( | ||
<div | ||
className={clickableUrlClasses} | ||
dangerouslySetInnerHTML={{ __html: sanitizedResolutionContent }} | ||
/> | ||
); | ||
|
||
return textWithLinks; | ||
}; | ||
export default ClickableUrls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./ClickableUrls"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters