mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-03 19:32:35 +02:00
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:
parent
bc6c67720a
commit
0a668366c2
9 changed files with 66 additions and 29 deletions
|
@ -137,3 +137,32 @@ declare module '@theme/Seo' {
|
|||
const Seo: (props: Props) => JSX.Element;
|
||||
export default Seo;
|
||||
}
|
||||
|
||||
declare module '@theme/hooks/useDocs' {
|
||||
type GlobalPluginData = import('./types').GlobalPluginData;
|
||||
type GlobalVersion = import('./types').GlobalVersion;
|
||||
type ActivePlugin = import('./client/docsClientUtils').ActivePlugin;
|
||||
type ActiveDocContext = import('./client/docsClientUtils').ActiveDocContext;
|
||||
type DocVersionSuggestions = import('./client/docsClientUtils').DocVersionSuggestions;
|
||||
|
||||
export type {GlobalPluginData, GlobalVersion};
|
||||
export const useAllDocsData: () => Record<string, GlobalPluginData>;
|
||||
export const useDocsData: (pluginId?: string) => GlobalPluginData;
|
||||
export const useActivePlugin: (
|
||||
options: GetActivePluginOptions = {},
|
||||
) => ActivePlugin | undefined;
|
||||
export const useActivePluginAndVersion: (
|
||||
options: GetActivePluginOptions = {},
|
||||
) =>
|
||||
| {activePlugin: ActivePlugin; activeVersion: GlobalVersion | undefined}
|
||||
| undefined;
|
||||
export const useVersions: (pluginId?: string) => GlobalVersion[];
|
||||
export const useLatestVersion: (pluginId?: string) => GlobalVersion;
|
||||
export const useActiveVersion: (
|
||||
pluginId?: string,
|
||||
) => GlobalVersion | undefined;
|
||||
export const useActiveDocContext: (pluginId?: string) => ActiveDocContext;
|
||||
export const useDocVersionSuggestions: (
|
||||
pluginId?: string,
|
||||
) => DocVersionSuggestions;
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ function DocItem(props: Props): JSX.Element {
|
|||
lastUpdatedBy,
|
||||
} = metadata;
|
||||
|
||||
const {pluginId} = useActivePlugin({failfast: true});
|
||||
const {pluginId} = useActivePlugin({failfast: true})!;
|
||||
const versions = useVersions(pluginId);
|
||||
|
||||
// If site is not versioned or only one version is included
|
||||
|
|
|
@ -9,7 +9,11 @@ import React, {ComponentType} from 'react';
|
|||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
import Link from '@docusaurus/Link';
|
||||
import Translate from '@docusaurus/Translate';
|
||||
import {useActivePlugin, useDocVersionSuggestions} from '@theme/hooks/useDocs';
|
||||
import {
|
||||
useActivePlugin,
|
||||
useDocVersionSuggestions,
|
||||
GlobalVersion,
|
||||
} from '@theme/hooks/useDocs';
|
||||
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
|
||||
|
||||
import type {Props} from '@theme/DocVersionBanner';
|
||||
|
@ -109,10 +113,10 @@ function DocVersionBannerEnabled({versionMetadata}: Props): JSX.Element {
|
|||
const {
|
||||
siteConfig: {title: siteTitle},
|
||||
} = useDocusaurusContext();
|
||||
const {pluginId} = useActivePlugin({failfast: true});
|
||||
const {pluginId} = useActivePlugin({failfast: true})!;
|
||||
|
||||
const getVersionMainDoc = (version) =>
|
||||
version.docs.find((doc) => doc.id === version.mainDocId);
|
||||
const getVersionMainDoc = (version: GlobalVersion) =>
|
||||
version.docs.find((doc) => doc.id === version.mainDocId)!;
|
||||
|
||||
const {savePreferredVersionName} = useDocsPreferredVersion(pluginId);
|
||||
|
||||
|
|
|
@ -40,8 +40,10 @@ export default function DocNavbarItem({
|
|||
const latestVersion = useLatestVersion(docsPluginId);
|
||||
|
||||
// Versions used to look for the doc to link to, ordered + no duplicate
|
||||
const versions: GlobalDataVersion[] = uniq(
|
||||
[activeVersion, preferredVersion, latestVersion].filter(Boolean),
|
||||
const versions = uniq(
|
||||
[activeVersion, preferredVersion, latestVersion].filter(
|
||||
Boolean,
|
||||
) as GlobalDataVersion[],
|
||||
);
|
||||
const doc = getDocInVersions(versions, docId);
|
||||
const activeDocInfimaClassName = props.mobile
|
||||
|
|
|
@ -7,12 +7,16 @@
|
|||
|
||||
import React from 'react';
|
||||
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
|
||||
import {useActiveVersion, useLatestVersion} from '@theme/hooks/useDocs';
|
||||
import {
|
||||
useActiveVersion,
|
||||
useLatestVersion,
|
||||
GlobalVersion,
|
||||
} from '@theme/hooks/useDocs';
|
||||
import type {Props} from '@theme/NavbarItem/DocsVersionNavbarItem';
|
||||
import {useDocsPreferredVersion} from '@docusaurus/theme-common';
|
||||
|
||||
const getVersionMainDoc = (version) =>
|
||||
version.docs.find((doc) => doc.id === version.mainDocId);
|
||||
const getVersionMainDoc = (version: GlobalVersion) =>
|
||||
version.docs.find((doc) => doc.id === version.mainDocId)!;
|
||||
|
||||
export default function DocsVersionNavbarItem({
|
||||
label: staticLabel,
|
||||
|
|
|
@ -33,7 +33,7 @@ export default function useContextualSearchFilters(): ContextualSearchFilters {
|
|||
|
||||
const preferredVersion = docsPreferredVersionByPluginId[pluginId];
|
||||
|
||||
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast);
|
||||
const latestVersion = allDocsData[pluginId].versions.find((v) => v.isLast)!;
|
||||
|
||||
const version = activeVersion ?? preferredVersion ?? latestVersion;
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/* eslint-disable import/no-duplicates */
|
||||
/* eslint-disable spaced-comment */
|
||||
/// <reference types="@docusaurus/module-type-aliases" />
|
||||
/// <reference types="@docusaurus/plugin-content-blog" />
|
||||
|
|
|
@ -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};
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue