From c1c53851e24603a32c581300645dad0bff0b93e9 Mon Sep 17 00:00:00 2001 From: Cyrine Ben Romdhane <135605630+anicyne@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:30:46 +0200 Subject: [PATCH] Refactor: Removed duplicate logic --- src/pages/index.tsx | 7 ++----- src/shares/version.ts | 5 +++++ src/theme/Navbar/index.tsx | 7 ++----- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 2d7c54eec2..5e889ffec5 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -8,7 +8,7 @@ import type { FunctionComponent } from 'react'; import React from 'react'; import { KoliBriAbbr } from '../components/KoliBriAbbr'; import { useDocsPreferredVersion } from '@docusaurus/theme-common'; -import { Version } from '../../shares/version'; +import { determinateVersionId } from '../shares/version'; const HomepageHeader: FunctionComponent = () => (
@@ -26,10 +26,7 @@ const HomepageHeader: FunctionComponent = () => ( ); const HomepageButtons: FunctionComponent = () => { const docVersion = useDocsPreferredVersion(); - const version = - typeof docVersion?.preferredVersion?.name && parseFloat(docVersion?.preferredVersion?.name as Version) < 2 - ? 'v1' - : 'v2'; + const version = determinateVersionId(docVersion); return (
diff --git a/src/shares/version.ts b/src/shares/version.ts index e87339055d..343d362049 100644 --- a/src/shares/version.ts +++ b/src/shares/version.ts @@ -1,3 +1,8 @@ import type VERSIONS from '../../versions.json'; export type Version = (typeof VERSIONS)[number] & 'current'; + +export function determinateVersionId(docVersion) { + if (!docVersion || !docVersion.preferredVersion) return null; + return parseFloat(docVersion?.preferredVersion?.name as Version) < 2 ? 'v1' : 'v2'; +} diff --git a/src/theme/Navbar/index.tsx b/src/theme/Navbar/index.tsx index b99eb42fdf..6064143ef6 100644 --- a/src/theme/Navbar/index.tsx +++ b/src/theme/Navbar/index.tsx @@ -9,7 +9,7 @@ import { LanguageSwitch } from '@site/src/components/LanguageSwitch'; import Navbar from '@theme-original/Navbar'; import ThemeSelect from './ThemeSelect'; import { useDocsPreferredVersion } from '@docusaurus/theme-common'; -import { Version } from '../../shares/version'; +import { determinateVersionId } from '../../shares/version'; import { translate } from '@docusaurus/Translate'; export const NavbarWrapper: FunctionComponent = (props) => { @@ -28,10 +28,7 @@ export const NavbarWrapper: FunctionComponent = (props) => { // }, // }; const docVersion = useDocsPreferredVersion(); - const version = - typeof docVersion?.preferredVersion?.name && parseFloat(docVersion?.preferredVersion?.name as Version) < 2 - ? 'v1' - : 'v2'; + const version = determinateVersionId(docVersion); return (