Repository: facebook/docusaurus
File: packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx (TypeScript)

Code before:

type ConfiguredVersion = {
  version: GlobalVersion;
  configuration?: VersionConfiguration;
};

function configureVersions(
  versions: GlobalVersion[],
  staticVersions?: PropVersions,
): ConfiguredVersion[] {
  if (staticVersions) {
    // The versions are configured

    // Collect all the versions we have
    const versionMap = new Map<string, GlobalVersion>(
      versions.map((version) => [version.name, version]),
    );

    // Add secondary version identifiers to make the configuration process
    // more natural to a user
    for (const version of versions) {
      // 'version.name' has special conventions for current and next versions,
      // that's why we use 'version.label' as a secondary version identifier
      // that can be referenced in configuration
      const label = version.label;
      if (!versionMap.has(label)) versionMap.set(label, version);
    }

    // Keep only versions specified in configuration, reorder them accordingly
    const configuredVersions: ConfiguredVersion[] = [];
    for (const configuration of getVersionConfigurations(staticVersions)) {
      const version = versionMap.get(configuration.name);
      if (!version) {
        // A version configuration references a non-existing version, ignore it
        continue;
      }
      configuredVersions.push({version, configuration});
    }
    return configuredVersions;
  } else {
    // The versions are not configured
    return versions.map((version): ConfiguredVersion => {
      return {version};
    });
  }
}

function getVersionMainDoc(version: GlobalVersion): GlobalDoc {
  return version.docs.find((doc) => doc.id === version.mainDocId)!;
}


Review comment (suggestion):
Not super fan of this logic.
We don't do this kind of thing anywhere else in the codebase so I'd prefer not introduce this here

Code after:
      config?: PropVersionItem,
    ): VersionItem => {
      const version = versionMap.get(name);
      if (!version) {
        throw new Error(`No docs version exist for name '${name}', please verify your 'docsVersionDropdown' navbar item versions config.
Available version names:\n- ${versions.map((v) => `${v.name}`).join('\n- ')}`);
      }
      return {version, label: config?.label ?? version.label};
    };

    if (Array.isArray(configs)) {
      return configs.map((name) => toVersionItem(name, undefined));
    } else {
      return Object.entries(configs).map(([name, config]) =>
        toVersionItem(name, config),
      );
    }
  } else {
    return versions.map((version) => ({version, label: version.label}));
  }
}

function useVersionItems({
  docsPluginId,
  configs,
}: {
  docsPluginId: Props['docsPluginId'];
  configs: Props['versions'];
}): VersionItem[] {
  const versions = useVersions(docsPluginId);
  return getVersionItems(versions, configs);
}

function getVersionMainDoc(version: GlobalVersion): GlobalDoc {
  return version.docs.find((doc) => doc.id === version.mainDocId)!;
}

function getVersionTargetDoc(
  version: GlobalVersion,
  activeDocContext: ActiveDocContext,
): GlobalDoc {
  // We try to link to the same doc, in another version
  // When not possible, fallback to the "main doc" of the version
  return (
    activeDocContext.alternateDocVersions[version.name] ??
    getVersionMainDoc(version)
  );
}

// The version item to use for the "dropdown button"
function useDisplayedVersionItem({