refactor: properly type docs version (#5284)

* Type docs version

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Move non-null assertions

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>

* Test again
This commit is contained in:
Joshua Chen 2021-08-05 16:52:35 +08:00 committed by GitHub
parent bc6c67720a
commit 0a668366c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 29 deletions

View file

@ -15,7 +15,7 @@ import React, {
import {useThemeConfig, DocsVersionPersistence} from '../useThemeConfig';
import {isDocsPluginEnabled} from '../docsUtils';
import {useAllDocsData} from '@theme/hooks/useDocs';
import {useAllDocsData, GlobalPluginData} from '@theme/hooks/useDocs';
import DocsPreferredVersionStorage from './DocsPreferredVersionStorage';
@ -54,7 +54,7 @@ function readStorageState({
}: {
pluginIds: string[];
versionPersistence: DocsVersionPersistence;
allDocsData: any; // TODO find a way to type it :(
allDocsData: Record<string, GlobalPluginData>;
}): DocsPreferredVersionState {
// The storage value we read might be stale,
// and belong to a version that does not exist in the site anymore
@ -68,7 +68,7 @@ function readStorageState({
);
const pluginData = allDocsData[pluginId];
const versionExists = pluginData.versions.some(
(version: any) => version.name === preferredVersionNameUnsafe,
(version) => version.name === preferredVersionNameUnsafe,
);
if (versionExists) {
return {preferredVersionName: preferredVersionNameUnsafe};

View file

@ -6,25 +6,24 @@
*/
import {useCallback} from 'react';
import {useDocsPreferredVersionContext} from './DocsPreferredVersionProvider';
import {useAllDocsData, useDocsData} from '@theme/hooks/useDocs';
import {useAllDocsData, useDocsData, GlobalVersion} from '@theme/hooks/useDocs';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/constants';
// TODO improve typing
import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants';
// Note, the preferredVersion attribute will always be null before mount
export function useDocsPreferredVersion(
pluginId: string | undefined = DEFAULT_PLUGIN_ID,
) {
): {
preferredVersion: GlobalVersion | null | undefined;
savePreferredVersionName: (versionName: string) => void;
} {
const docsData = useDocsData(pluginId);
const [state, api] = useDocsPreferredVersionContext();
const {preferredVersionName} = state[pluginId];
const preferredVersion = preferredVersionName
? docsData.versions.find(
(version: any) => version.name === preferredVersionName,
)
? docsData.versions.find((version) => version.name === preferredVersionName)
: null;
const savePreferredVersionName = useCallback(
@ -37,7 +36,10 @@ export function useDocsPreferredVersion(
return {preferredVersion, savePreferredVersionName} as const;
}
export function useDocsPreferredVersionByPluginId(): Record<string, any> {
export function useDocsPreferredVersionByPluginId(): Record<
string,
GlobalVersion | null | undefined
> {
const allDocsData = useAllDocsData();
const [state] = useDocsPreferredVersionContext();
@ -47,17 +49,14 @@ export function useDocsPreferredVersionByPluginId(): Record<string, any> {
return preferredVersionName
? docsData.versions.find(
(version: any) => version.name === preferredVersionName,
(version) => version.name === preferredVersionName,
)
: null;
}
const pluginIds = Object.keys(allDocsData);
const result: Record<
string,
any // TODO find a way to type this properly!
> = {};
const result: Record<string, GlobalVersion | null | undefined> = {};
pluginIds.forEach((pluginId) => {
result[pluginId] = getPluginIdPreferredVersion(pluginId);
});