-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ajiang/mark-authed' into ajiang/jwt-audiences
- Loading branch information
Showing
8 changed files
with
134 additions
and
71 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
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,45 @@ | ||
import * as FernNavigation from "@fern-api/fdr-sdk/navigation"; | ||
|
||
interface WithPrunedSidebarOpts { | ||
node: FernNavigation.NavigationNode; | ||
isAuthenticated: boolean; | ||
isAuthenticatedPagesDiscoverable: boolean; | ||
} | ||
|
||
/** | ||
* Note: at the stage of calling this function, the audiences should already been evaluated. | ||
*/ | ||
export function pruneNavigationPredicate( | ||
n: FernNavigation.NavigationNode, | ||
{ node, isAuthenticated, isAuthenticatedPagesDiscoverable }: WithPrunedSidebarOpts, | ||
): boolean { | ||
// prune hidden nodes, unless it is the current node | ||
if (FernNavigation.hasMetadata(n) && n.hidden) { | ||
return n.id === node.id; | ||
} | ||
|
||
// prune authenticated pages (unless the isAuthenticatedPagesDiscoverable flag is turned on) | ||
if (FernNavigation.hasMetadata(n) && n.authed && isAuthenticated && !isAuthenticatedPagesDiscoverable) { | ||
return false; | ||
} | ||
|
||
// prune nodes that are not pages and have no children (avoid pruning links) | ||
if (!FernNavigation.isPage(n) && !FernNavigation.isLeaf(n)) { | ||
return FernNavigation.getChildren(n).length > 0; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export function withPrunedSidebar( | ||
sidebar: FernNavigation.SidebarRootNode | undefined, | ||
opts: WithPrunedSidebarOpts, | ||
): FernNavigation.SidebarRootNode | undefined { | ||
if (!sidebar) { | ||
return sidebar; | ||
} | ||
|
||
return FernNavigation.Pruner.from(sidebar) | ||
.keep((n) => pruneNavigationPredicate(n, opts)) | ||
.get(); | ||
} |
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