Skip to content

Commit

Permalink
Refactor feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Dec 17, 2024
1 parent fee8250 commit b00fed8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
20 changes: 2 additions & 18 deletions plugins/gatsby-source-snooty-prod/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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(
Expand Down
28 changes: 17 additions & 11 deletions plugins/utils/unified-toc.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
6 changes: 0 additions & 6 deletions src/components/UnifiedSidenav/UnifiedSidenav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
5 changes: 3 additions & 2 deletions src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';
Expand All @@ -117,7 +118,7 @@ const DefaultLayout = ({ children, data: { page }, pageContext: { slug, repoBran
>
<GlobalGrid isInPresentationMode={isInPresentationMode}>
{!isInPresentationMode ? <Header eol={eol} template={template} /> : <div />}
{process.env.GATSBY_USE_UNIFIED_TOC ? (
{isUnifiedToc ? (
<UnifiedSidenav />
) : sidenav && !isInPresentationMode ? (
<Sidenav
Expand Down
5 changes: 5 additions & 0 deletions src/utils/feature-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function getFeatureFlags() {
return {
isUnifiedToc: Boolean(process.env.GATSBY_USE_UNIFIED_TOC),
};
}

0 comments on commit b00fed8

Please sign in to comment.