Skip to content

Commit

Permalink
DOP-4060 adds language selector to unified footer on docs' pages. (#928)
Browse files Browse the repository at this point in the history
Co-authored-by: Caesar Bell <[email protected]>
Co-authored-by: rayangler <[email protected]>
  • Loading branch information
3 people authored Oct 3, 2023
1 parent 3c82aa2 commit 66aba67
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 67 deletions.
44 changes: 41 additions & 3 deletions src/components/DocumentBody.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { UnifiedFooter } from '@mdb/consistent-nav';
import { usePresentationMode } from '../hooks/use-presentation-mode';
Expand All @@ -9,6 +9,7 @@ import { getMetaFromDirective } from '../utils/get-meta-from-directive';
import { getPlaintext } from '../utils/get-plaintext';
import { getTemplate } from '../utils/get-template';
import useSnootyMetadata from '../utils/use-snooty-metadata';
import { isBrowser } from '../utils/is-browser';
import Widgets from './Widgets';
import SEO from './SEO';
import FootnoteContext from './Footnote/footnote-context';
Expand Down Expand Up @@ -67,11 +68,37 @@ const getAnonymousFootnoteReferences = (index, numAnonRefs) => {
return index > numAnonRefs ? [] : [`id${index + 1}`];
};

const HIDE_UNIFIED_FOOTER_LOCALE = process.env['GATSBY_HIDE_UNIFIED_FOOTER_LOCALE'] === 'true';
const AVAILABLE_LANGUAGES = ['English', '简体中文'];

const DocumentBody = (props) => {
const {
location,
pageContext: { page, slug, template },
} = props;

useEffect(() => {
// A workaround to remove the other locale options.
if (!HIDE_UNIFIED_FOOTER_LOCALE) {
const footer = document.getElementById('footer-container');
const footerUlElement = footer?.querySelector('ul[role=listbox]');
if (footerUlElement) {
// For DOP-4060 we only want to support English and Simple Chinese (for now)
const availableOptions = Array.from(footerUlElement.childNodes).reduce((accumulator, child) => {
if (AVAILABLE_LANGUAGES.includes(child.textContent)) {
accumulator.push(child);
}
return accumulator;
}, []);

footerUlElement.innerHTML = null;
availableOptions.forEach((child) => {
footerUlElement.appendChild(child);
});
}
}
}, []);

const initialization = () => {
const pageNodes = getNestedValue(['children'], page) || [];
const footnotes = getFootnotes(pageNodes);
Expand All @@ -90,6 +117,17 @@ const DocumentBody = (props) => {

const isInPresentationMode = usePresentationMode()?.toLocaleLowerCase() === 'true';

const onSelectLocale = (locale) => {
const localeHrefMap = {
'zh-cn': 'https://mongodbcom-cdn.website.staging.corp.mongodb.com/zh-cn/docs-qa/',
'en-us': 'https://mongodbcom-cdn.website.staging.corp.mongodb.com/docs-qa/',
};

if (isBrowser) {
window.location.href = localeHrefMap[locale];
}
};

return (
<>
<Widgets
Expand All @@ -109,8 +147,8 @@ const DocumentBody = (props) => {
</FootnoteContext.Provider>
</Widgets>
{!isInPresentationMode && (
<div data-testid="consistent-footer">
<UnifiedFooter hideLocale={true} />
<div data-testid="consistent-footer" id="footer-container">
<UnifiedFooter hideLocale={HIDE_UNIFIED_FOOTER_LOCALE} onSelectLocale={onSelectLocale} />
</div>
)}
</>
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Presentation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ jest.mock(`../../src/utils/use-snooty-metadata`, () => {
});

describe('DocumentBody', () => {
beforeAll(() => {
jest.spyOn(document, 'querySelector');
});
it('renders the necessary elements', () => {
mockLocation(null);
render(<DocumentBody location={window.location} pageContext={mockPageContext} />);
Expand All @@ -18,6 +21,12 @@ describe('DocumentBody', () => {
expect(footer).toBeVisible();
expect(footer).toMatchSnapshot();

if (!process.env.GATSBY_HIDE_UNIFIED_FOOTER_LOCALE) {
const languageSelector = screen.getByTestId('options');
expect(languageSelector).toBeInTheDocument();
expect(languageSelector.querySelectorAll('li')).toHaveLength(2);
}

const feedbackWidget = screen.getByText('Share Feedback');
expect(feedbackWidget).toBeVisible();
expect(feedbackWidget).toMatchSnapshot();
Expand Down
Loading

0 comments on commit 66aba67

Please sign in to comment.