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

refactor(v2): use new way to get versions for search page #3604

Merged
merged 2 commits into from
Oct 22, 2020

Conversation

lex111
Copy link
Contributor

@lex111 lex111 commented Oct 17, 2020

Motivation

Using useDocs hook instead of deprecated useVersioning, also after that (as bonus) we can search by latest (current) version.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

Preview.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@lex111 lex111 added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Oct 17, 2020
@lex111 lex111 requested a review from slorber as a code owner October 17, 2020 19:27
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 17, 2020
@@ -33,9 +35,10 @@ function Search() {
themeConfig: {algolia: {appId = 'BH4D9OD16A', apiKey, indexName} = {}},
} = {},
} = useDocusaurusContext();
const {versions} = useDocsData(DEFAULT_PLUGIN_ID);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about that.

Copy link
Collaborator

@slorber slorber Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost good, but could fail for some users if they use docs plugin with a custom id.

If user use twice the docs plugin (like ios + android having different versioning schemes), we should rather:

  • retrieve all versions, of the 2 plugin instances (useAllDocsData() is a map pluginId->pluginInstanceData)
  • show one select per plugin instance

For unversioned docs instances (like community in our site) it's probably not worth it to display the select, but it is useful to show 1 dropdown for iOS and one dropdown for Android, as they don't necessarily have the same versions.

Also, the facet filtering in the query should be adapted to match this multi-instance docs setup that use a different "tag" for ios v1 and android v1, as they are separate docs.

I think this line:

.addFacetRefinement('version', searchVersion)

Should become something like:

.addFacetRefinement('docusaurus_tag',' 'docs-community-current')

All this may seem a bit complicated for little ROI.
I'm not sure it's really worth letting the user do a query such as "docs from iOS v1 OR docs from android v1.1".

Unless you feel like solving this problem, what I suggest temporarily is to assume there's a single version.

Unfortunately, we can't guarantee the user won't provide a pluginId, even if there is a single docs instance, so reading the default plugin id data is not safe by default.

Something like that would be safer:

function useSearchVersions() {
  const allDocsData = useAllDocsData();
  const pluginIds = Object.keys(allDocsData).length;
  if (pluginIds.length === 0) {
    return [];
  } else {
    const pluginData =
      allDocsData[DEFAULT_PLUGIN_ID] ?? allDocsData[pluginIds[0]];
    return pluginData.versions;
  }
}

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 17, 2020

Deploy preview for docusaurus-2 ready!

Built with commit b8bc278

https://deploy-preview-3604--docusaurus-2.netlify.app

@slorber
Copy link
Collaborator

slorber commented Oct 21, 2020

Hi @lex111

I updated your PR with a better docs multi-instance support, and rewired the Algolia facetting to the new tag attribute.

For docs single-instance, and sites with only 1 versioned doc instance, it should have no impact at all.

For sites with 2 versioned docs instances (quite rare), it will show 2 select menus, with the respective versions for each instance.

For our site, it does not make sense to show a dropdown for the community docs instance, as it's unversioned, but you can force display it if needed by removing the .filter(([, docsData]) => docsData.versions.length > 1); line

image

Probably not perfect UX yet, and there are other things we could improve (like the ability to only search in blog or in a specific doc plugin instance) but it should not change anything for 99% of users so I think it's good enough.

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to explain a bit the implementation details (could even be helpful for future self 🤪 )

return Object.entries(allDocsData).reduce((acc, [pluginId, pluginData]) => {
return {...acc, [pluginId]: pluginData.versions[0].name};
}, {});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of just a version string, we now need a version string per plugin instance, so it's now a map

docsSearchVersionsHelpers.allDocsData,
)
// Do not show a version select for unversioned docs plugin instances
.filter(([, docsData]) => docsData.versions.length > 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensure our "community" docs instance do not lead to a select menu being displayed, as there will never be more than 1 value in this select it's useless

`docs-${pluginId}-${searchVersion}`,
);
},
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query is like: default OR docs-alpha66 OR docs-community-current, ie we search in pages, blogs, and the selected docs versions .

It's an improvement compared to previous search page as we can now search in pages and blog, which was not possible by using the previous facet filter logic (only searching in docs)

see:

)}>
{versionedPluginEntries.map(([pluginId, docsData]) => {
const labelPrefix =
versionedPluginEntries.length > 1 ? `${pluginId}: ` : '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "label prefix" is not perfect, unfortunately, I'm not sure we have a "label" for the docs version instance, so it's not easy to create a great UX.

I think multi-instance versioning is rare enough so that we can figure this out later, but if you have ideas...

@lex111
Copy link
Contributor Author

lex111 commented Oct 21, 2020

@slorber awwwesome! Thank you very much for this!

@slorber
Copy link
Collaborator

slorber commented Oct 22, 2020

great you like this :) let's merge then

@slorber slorber merged commit 0ec5d53 into master Oct 22, 2020
@lex111 lex111 deleted the lex111/search-page-new-versions branch October 26, 2020 22:06
@lex111 lex111 added this to the v2.0.0-alpha.67 milestone Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Algolia search results page: handle docs multi-instance + contextual search
4 participants