Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Use new Algolia app for new structure #31178

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/src/modules/components/AppFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export function DeferredAppSearch() {
<React.Fragment>
{/* Suspense isn't supported for SSR yet */}
{mounted ? (
<React.Suspense fallback={null}>
<React.Suspense fallback={<Box sx={{ minWidth: { sm: 200 } }} />}>
<AppSearch />
</React.Suspense>
) : null}
) : (
<Box sx={{ minWidth: { sm: 200 } }} />
)}
</React.Fragment>
);
}
Expand Down
19 changes: 0 additions & 19 deletions docs/src/modules/components/AppNavDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,6 @@ ProductIdentifier.propTypes = {
versionSelector: PropTypes.element,
};

const AppSearch = React.lazy(() => import('docs/src/modules/components/AppSearch'));
export function DeferredAppSearch() {
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => {
setMounted(true);
}, []);

return (
<React.Fragment>
{/* Suspense isn't supported for SSR yet */}
{mounted ? (
<React.Suspense fallback={null}>
<AppSearch />
</React.Suspense>
) : null}
</React.Fragment>
);
}

function PersistScroll(props) {
const { slot, children, enabled } = props;
const rootRef = React.useRef();
Expand Down
21 changes: 12 additions & 9 deletions docs/src/modules/components/AppSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Link from 'docs/src/modules/components/Link';
import { useTranslate, useUserLanguage } from 'docs/src/modules/utils/i18n';
import useLazyCSS from 'docs/src/modules/utils/useLazyCSS';
import { useRouter } from 'next/router';
import replaceUrl from 'docs/src/modules/utils/replaceUrl';
import { isNewLocation } from 'docs/src/modules/utils/replaceUrl';

const SearchButton = styled('button')(({ theme }) => {
return {
Expand Down Expand Up @@ -192,6 +192,8 @@ export default function AppSearch() {
setIsOpen(true);
}, [setIsOpen]);
const router = useRouter();
const isNewDocStructure = isNewLocation(router.asPath);

const keyboardNavigator = {
navigate({ item }) {
const as = item.userLanguage !== 'en' ? `/${item.userLanguage}${item.as}` : item.as;
Expand Down Expand Up @@ -285,7 +287,12 @@ export default function AppSearch() {
createPortal(
<DocSearchModal
initialQuery={initialQuery}
apiKey="1d8534f83b9b0cfea8f16498d19fbcab"
appId={isNewDocStructure ? 'TZGZ85B9TB' : 'BH4D9OD16A'}
apiKey={
isNewDocStructure
? '8177dfb3e2be72b241ffb8c5abafa899'
: '1d8534f83b9b0cfea8f16498d19fbcab'
}
indexName="material-ui"
searchParameters={{
facetFilters: ['version:master', facetFilterLanguage],
Expand All @@ -299,20 +306,16 @@ export default function AppSearch() {
parseUrl.href = item.url;

let hash = parseUrl.hash;
let pathname = parseUrl.pathname;

if (['lvl2', 'lvl3'].includes(item.type)) {
// remove '#heading-' from `href` url so that the link targets <span class="anchor-link"> inside <h2> or <h3>
// this will make the title appear under the Header
hash = hash.replace('#heading-', '#');
}

// TODO: remove this logic once the migration to new structure is done.
// This logic covers us during the ~60 minutes that it takes Algolia to run a crawl and update its index.
// It also allows us to have a search bar that works in dev mode while the new structure is not pushed to production.
pathname = replaceUrl(pathname, router.asPath);

const { canonicalAs, canonicalPathname } = pathnameToLanguage(`${pathname}${hash}`);
const { canonicalAs, canonicalPathname } = pathnameToLanguage(
`${parseUrl.pathname}${hash}`,
);

return {
...item,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/utils/replaceUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import FEATURE_TOGGLE from 'docs/src/featureToggle';

function isNewLocation(url: string) {
export function isNewLocation(url: string) {
url = url.replace(/^\/[a-z]{2}\//, '/');
if (url === '/x' || url === '/x/') {
// skipped if it is the X marketing page
Expand Down