-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: audience mdx component (#1630)
- Loading branch information
1 parent
0227529
commit dc8d75d
Showing
4 changed files
with
39 additions
and
0 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,30 @@ | ||
import { PropsWithChildren, ReactNode } from "react"; | ||
import { useFernUser } from "../../../atoms"; | ||
|
||
export interface AudienceProps { | ||
/** | ||
* The audience to check against | ||
*/ | ||
audience?: string | string[]; | ||
|
||
/** | ||
* Invert the audience check | ||
*/ | ||
not?: boolean; | ||
} | ||
|
||
export function Audience({ not, audience, children }: PropsWithChildren<AudienceProps>): ReactNode { | ||
const user = useFernUser(); | ||
|
||
if (audience == null || (Array.isArray(audience) && audience.length === 0)) { | ||
return children; | ||
} | ||
|
||
const audienceArr = Array.isArray(audience) ? audience : [audience]; | ||
const userAudienceArr = | ||
user?.audience == null ? [] : Array.isArray(user.audience) ? user.audience : [user.audience]; | ||
|
||
const show = audienceArr.some((aud) => userAudienceArr.includes(aud)); | ||
|
||
return not ? !show && children : show && children; | ||
} |
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 * from "./Audience"; |
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