diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index a0e7078831..e371da2217 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -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; + 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; +} diff --git a/packages/docusaurus-theme-classic/src/theme/DocItem/index.tsx b/packages/docusaurus-theme-classic/src/theme/DocItem/index.tsx index 08604df192..92474675c4 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocItem/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/DocItem/index.tsx @@ -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 diff --git a/packages/docusaurus-theme-classic/src/theme/DocVersionBanner/index.tsx b/packages/docusaurus-theme-classic/src/theme/DocVersionBanner/index.tsx index e80c8165d5..751bae67ff 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocVersionBanner/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/DocVersionBanner/index.tsx @@ -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); diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx index 239d306f8d..5c79e46e3c 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx @@ -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 diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionNavbarItem.tsx index 9d1afb54f8..880d0473cc 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionNavbarItem.tsx @@ -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, diff --git a/packages/docusaurus-theme-classic/src/theme/hooks/useContextualSearchFilters.ts b/packages/docusaurus-theme-classic/src/theme/hooks/useContextualSearchFilters.ts index e279875cce..38e27d2bc0 100644 --- a/packages/docusaurus-theme-classic/src/theme/hooks/useContextualSearchFilters.ts +++ b/packages/docusaurus-theme-classic/src/theme/hooks/useContextualSearchFilters.ts @@ -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; diff --git a/packages/docusaurus-theme-common/src/types.d.ts b/packages/docusaurus-theme-common/src/types.d.ts index d62d2918c4..0c774660fb 100644 --- a/packages/docusaurus-theme-common/src/types.d.ts +++ b/packages/docusaurus-theme-common/src/types.d.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -/* eslint-disable import/no-duplicates */ /* eslint-disable spaced-comment */ /// /// diff --git a/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx b/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx index 2daaac2747..2176c25e5f 100644 --- a/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx +++ b/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/DocsPreferredVersionProvider.tsx @@ -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; }): 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}; diff --git a/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts b/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts index d4d0ef4cd2..8d4cc24d39 100644 --- a/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts +++ b/packages/docusaurus-theme-common/src/utils/docsPreferredVersion/useDocsPreferredVersion.ts @@ -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 { +export function useDocsPreferredVersionByPluginId(): Record< + string, + GlobalVersion | null | undefined +> { const allDocsData = useAllDocsData(); const [state] = useDocsPreferredVersionContext(); @@ -47,17 +49,14 @@ export function useDocsPreferredVersionByPluginId(): Record { 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 = {}; pluginIds.forEach((pluginId) => { result[pluginId] = getPluginIdPreferredVersion(pluginId); });