From b00fed8fa5788e4ce890878aef11976e324bb550 Mon Sep 17 00:00:00 2001 From: branberry Date: Tue, 17 Dec 2024 14:54:27 -0600 Subject: [PATCH] Refactor feature flags --- .../gatsby-source-snooty-prod/gatsby-node.js | 20 ++----------- plugins/utils/unified-toc.js | 28 +++++++++++-------- .../UnifiedSidenav/UnifiedSidenav.js | 6 ---- src/layouts/index.js | 5 ++-- src/utils/feature-flags.js | 5 ++++ 5 files changed, 27 insertions(+), 37 deletions(-) create mode 100644 src/utils/feature-flags.js diff --git a/plugins/gatsby-source-snooty-prod/gatsby-node.js b/plugins/gatsby-source-snooty-prod/gatsby-node.js index ca0451cff..1c3bbc24e 100644 --- a/plugins/gatsby-source-snooty-prod/gatsby-node.js +++ b/plugins/gatsby-source-snooty-prod/gatsby-node.js @@ -22,7 +22,7 @@ const { createOpenAPIChangelogNode } = require('../utils/openapi.js'); const { createProductNodes } = require('../utils/products.js'); const { createDocsetNodes } = require('../utils/docsets.js'); const { createBreadcrumbNodes } = require('../utils/breadcrumbs.js'); - +const { createTocNodes } = require('../utils/unified-toc.js'); const assets = new Map(); const projectComponents = new Set(); @@ -198,23 +198,7 @@ exports.sourceNodes = async ({ actions, createContentDigest, createNodeId, getNo await createBreadcrumbNodes({ db, createNode, createNodeId, createContentDigest }); // create TOC nodes - - try { - const tomlContents = (await fs.readFile(`${process.cwd()}/toc.toml`)).toString(); - const toc = load(tomlContents); - - createNode({ - tocTree: toc, - id: createNodeId('toc'), - internal: { - contentDigest: createContentDigest(toc), - type: 'TOC', - }, - parent: null, - }); - } catch (e) { - console.error('error occurred when reading the toc.toml', e); - } + await createTocNodes({ createNode, createNodeId, createContentDigest }); if (process.env['OFFLINE_DOCS'] !== 'true') { const umbrellaProduct = await db.realmInterface.getMetadata( diff --git a/plugins/utils/unified-toc.js b/plugins/utils/unified-toc.js index 3943a6f4c..226149dee 100644 --- a/plugins/utils/unified-toc.js +++ b/plugins/utils/unified-toc.js @@ -1,19 +1,25 @@ const { load } = require('js-toml'); +const fs = require('fs/promises'); const createTocNodes = async ({ createNode, createNodeId, createContentDigest }) => { // Get all MongoDB products for the sidenav - createNode({ - children: [], - id: createNodeId('toc'), - internal: { - contentDigest: createContentDigest(product), - type: 'Product', - }, - parent: null, - title: product.title, - url: product.baseUrl + product.slug, - }); + try { + const tomlContents = (await fs.readFile(`${process.cwd()}/toc.toml`)).toString(); + const toc = load(tomlContents); + + createNode({ + tocTree: toc, + id: createNodeId('toc'), + internal: { + contentDigest: createContentDigest(toc), + type: 'TOC', + }, + parent: null, + }); + } catch (e) { + console.error('error occurred when reading the toc.toml', e); + } }; module.exports = { diff --git a/src/components/UnifiedSidenav/UnifiedSidenav.js b/src/components/UnifiedSidenav/UnifiedSidenav.js index 179739519..f1e985c9c 100644 --- a/src/components/UnifiedSidenav/UnifiedSidenav.js +++ b/src/components/UnifiedSidenav/UnifiedSidenav.js @@ -11,12 +11,6 @@ import { sideNavItemTOCStyling } from '../Sidenav/styles/sideNavItem'; import { useUnifiedToc } from '../../hooks/use-unified-toc'; import { theme } from '../../theme/docsTheme'; -// Prevent content scrolling when the side nav is open on mobile and tablet screen sizes -const disableScroll = (shouldDisableScroll) => css` - body { - ${shouldDisableScroll && 'overflow: hidden;'} - } -`; const FormatTitle = styled.div` margin-left: var(--margin-left); scroll-margin-bottom: ${theme.size.xxlarge}; diff --git a/src/layouts/index.js b/src/layouts/index.js index 43f4dfd5f..19e0ce0e6 100644 --- a/src/layouts/index.js +++ b/src/layouts/index.js @@ -14,6 +14,7 @@ import useSnootyMetadata from '../utils/use-snooty-metadata'; import { useRemoteMetadata } from '../hooks/use-remote-metadata'; import { getAllLocaleCssStrings } from '../utils/locale'; import { UnifiedSidenav } from '../components/UnifiedSidenav/UnifiedSidenav'; +import { getFeatureFlags } from '../utils/feature-flags'; // TODO: Delete this as a part of the css cleanup // Currently used to preserve behavior and stop legacy css @@ -95,7 +96,7 @@ export const StyledContentContainer = styled('div')` const DefaultLayout = ({ children, data: { page }, pageContext: { slug, repoBranches, template } }) => { const { sidenav } = getTemplate(template); const { chapters, guides, slugToTitle, toctree, eol, project } = useSnootyMetadata(); - + const { isUnifiedToc } = getFeatureFlags(); const remoteMetadata = useRemoteMetadata(); const isInPresentationMode = usePresentationMode()?.toLocaleLowerCase() === 'true'; @@ -117,7 +118,7 @@ const DefaultLayout = ({ children, data: { page }, pageContext: { slug, repoBran > {!isInPresentationMode ?
:
} - {process.env.GATSBY_USE_UNIFIED_TOC ? ( + {isUnifiedToc ? ( ) : sidenav && !isInPresentationMode ? (