-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduces usePii hook and withPii HOC to pass vendor-specific …
…masking HTML attributes
- Loading branch information
1 parent
c878cce
commit e5e6bcc
Showing
10 changed files
with
430 additions
and
199 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { forwardRef, useContext, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { AppContext, usePii, withPii } from '@edx/frontend-platform/react'; | ||
|
||
// Example via `usePii` (hook) | ||
function UsernameWithPii({ children, ...rest }) { | ||
const piiProps = usePii('username', rest); | ||
return <span {...piiProps}>{children}</span>; | ||
} | ||
UsernameWithPii.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
// Example via `withPii` (HOC) | ||
const Username = forwardRef(({ children, ...rest }, ref) => ( | ||
<span ref={ref} {...rest}>{children}</span> | ||
)); | ||
Username.displayName = 'Username'; | ||
Username.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
const UsernameWithPii2 = withPii('username')(Username); | ||
|
||
export default function PiiPage() { | ||
const { authenticatedUser, config } = useContext(AppContext); | ||
const usernameRef = useRef(null); | ||
const { piiAttributeMappings } = config; | ||
return ( | ||
<div> | ||
<h1>Privacy options to obfuscate PII</h1> | ||
|
||
<h2>Current PII configuration</h2> | ||
{piiAttributeMappings ? ( | ||
<pre> | ||
{JSON.stringify(piiAttributeMappings, null, 2)} | ||
</pre> | ||
) : ( | ||
<p> | ||
No PII-related configuration found. Consider updating this application's <code>env.config.js</code> to | ||
configure any vendor-specific PII-related attributes/values. | ||
</p> | ||
)} | ||
|
||
<h2>Examples</h2> | ||
<p> | ||
The following examples below demonstrate using <code>usePii</code> and <code>withPii</code> to | ||
obfuscate PII (e.g., username) based on the application's configuration. Inspect the DOM | ||
elements for the rendered usernames below to observe the configured PII attributes/values. | ||
</p> | ||
|
||
{/* Example 1 */} | ||
<h3><code>usePii</code> (hook)</h3> | ||
<p> | ||
<i>Username:</i>{' '} | ||
<UsernameWithPii> | ||
{authenticatedUser.username} | ||
</UsernameWithPii> | ||
</p> | ||
|
||
{/* Example 2 */} | ||
<h3><code>withPii</code> (HOC)</h3> | ||
<p> | ||
<i>Username:</i>{' '} | ||
<UsernameWithPii2 ref={usernameRef}> | ||
{authenticatedUser.username} | ||
</UsernameWithPii2> | ||
</p> | ||
</div> | ||
); | ||
} |
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,49 @@ | ||
import { useContext, forwardRef, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { AppContext, usePii, withPii } from '@edx/frontend-platform/react'; | ||
|
||
// Example via `usePii` (hook) | ||
function UsernameWithPii({ children, ...rest }) { | ||
const piiProps = usePii('username', rest); | ||
return <span {...piiProps}>{children}</span>; | ||
} | ||
UsernameWithPii.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
// Example via `withPii` (HOC) | ||
const Username = forwardRef(({ children, ...rest }, ref) => ( | ||
<span ref={ref} {...rest}>{children}</span> | ||
)); | ||
Username.displayName = 'Username'; | ||
Username.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
const UsernameWithPii2 = withPii('username')(Username); | ||
|
||
export default function PrivacyOptionsExample() { | ||
const { authenticatedUser } = useContext(AppContext); | ||
const usernameRef = useRef(null); | ||
return ( | ||
<section> | ||
<h2>Privacy options for logging and analytics</h2> | ||
|
||
{/* Example 1 */} | ||
<h3><code>usePii</code> (hook)</h3> | ||
<p> | ||
<UsernameWithPii> | ||
{authenticatedUser.username} | ||
</UsernameWithPii> | ||
</p> | ||
|
||
{/* Example 2 */} | ||
<h3><code>withPii</code> (HOC)</h3> | ||
<p> | ||
<UsernameWithPii2 piiSelector="username" ref={usernameRef}> | ||
{authenticatedUser.username} | ||
</UsernameWithPii2> | ||
</p> | ||
</section> | ||
); | ||
} |
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
Oops, something went wrong.