Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
fix(website): support missing algolia config
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Jun 4, 2020
1 parent b89aeb5 commit 4b30cdd
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,37 @@ function Hit({ hit, children }) {
return <Link to={hit.url}>{children}</Link>;
}

function SearchBar() {
const { siteConfig = {} } = useDocusaurusContext();
function DocSearch({
indexName,
appId = 'BH4D9OD16A',
apiKey,
searchParameters,
}) {
const history = useHistory();
const [isOpen, setIsOpen] = useState(false);

const {
indexName,
appId = 'BH4D9OD16A',
apiKey,
searchParameters,
} = siteConfig.themeConfig.algolia;

const importDocSearchModalIfNeeded = useCallback(
function importDocSearchModalIfNeeded() {
if (DocSearchModal) {
return Promise.resolve();
}

return Promise.all([
import('@docsearch/react/modal'),
import('@docsearch/react/style'),
]).then(([{ DocSearchModal: Modal }]) => {
DocSearchModal = Modal;
});
},
[]
);
const importDocSearchModalIfNeeded = useCallback(() => {
if (DocSearchModal) {
return Promise.resolve();
}

const onOpen = useCallback(
function onOpen() {
importDocSearchModalIfNeeded().then(() => {
setIsOpen(true);
});
},
[importDocSearchModalIfNeeded, setIsOpen]
);
return Promise.all([
import('@docsearch/react/modal'),
import('@docsearch/react/style'),
]).then(([{ DocSearchModal: Modal }]) => {
DocSearchModal = Modal;
});
}, []);

const onClose = useCallback(
function onClose() {
setIsOpen(false);
},
[setIsOpen]
);
const onOpen = useCallback(() => {
importDocSearchModalIfNeeded().then(() => {
setIsOpen(true);
});
}, [importDocSearchModalIfNeeded, setIsOpen]);

const onClose = useCallback(() => {
setIsOpen(false);
}, [setIsOpen]);

useDocSearchKeyboardEvents({ isOpen, onOpen, onClose });

Expand Down Expand Up @@ -115,4 +103,19 @@ function SearchBar() {
);
}

function SearchBar() {
const { siteConfig = {} } = useDocusaurusContext();

if (!siteConfig.themeConfig.algolia) {
// eslint-disable-next-line no-console
console.warn(`DocSearch requires an \`algolia\` field in your \`themeConfig\`.
See: https://v2.docusaurus.io/docs/search/#using-algolia-docsearch`);

return null;
}

return <DocSearch {...siteConfig.themeConfig.algolia} />;
}

export default SearchBar;

0 comments on commit 4b30cdd

Please sign in to comment.