From cd8408d4730f4215c5cefb2ad2707f16f42e7562 Mon Sep 17 00:00:00 2001 From: Kent Rancourt Date: Tue, 14 Jan 2025 10:48:33 -0500 Subject: [PATCH] minor cleanup Signed-off-by: Kent Rancourt --- docs/src/components/VersionDropdown.js | 70 +++++++++++++++----------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/docs/src/components/VersionDropdown.js b/docs/src/components/VersionDropdown.js index 5e1e5f67e..ebf4984dd 100644 --- a/docs/src/components/VersionDropdown.js +++ b/docs/src/components/VersionDropdown.js @@ -1,28 +1,28 @@ import React, { useState, useEffect } from 'react'; import './VersionDropdown.css'; -const CACHE_KEY = 'kargo-docs-versions'; -const CACHE_DURATION = 1000 * 60 * 30; // 30 minutes -const githubApiUrl = 'https://api.github.com/repos/akuity/kargo/branches?protected=true'; - function VersionDropdown() { console.log("Navbar Version dropdown initialized"); + const githubApiUrl = 'https://api.github.com/repos/akuity/kargo/branches?protected=true'; + const protocol = 'https://'; + const domain = 'docs.kargo.io'; + const latestVersionLabel = 'Latest Version'; + const latestVersionUrl = `${protocol}${domain}`; + const edgeVersionLabel = 'Edge Version (main)'; + const edgeVersionUrl = `${protocol}main.${domain}`; + const unrecognizedVersionLabel = 'Unrecognized Version'; + const [versions, setVersions] = useState([]); const [loading, setLoading] = useState(true); const [currentVersion, setCurrentVersion] = useState(''); + // Change the implementation of the currentUrl function to aid in testing + const currentUrl = () => new URL(window.location.href); + + const versionLabel = (major, minor) => `v${major}.${minor}`; + const fetchVersions = async () => { - const cachedData = localStorage.getItem(CACHE_KEY); - if (cachedData) { - const { versions, timestamp } = JSON.parse(cachedData); - if (Date.now() - timestamp < CACHE_DURATION) { - console.log("Using cached versions:", versions); - setVersions(versions); - setLoading(false); - return; - } - } try { console.log("Before fetching versions"); const response = await fetch(githubApiUrl); @@ -35,8 +35,8 @@ function VersionDropdown() { .map(name => { const [major, minor] = name.replace('release-', '').split('.'); return { - version: `v${major}.${minor}`, - url: `https://${name.replace('.', '-')}.docs.kargo.io` + version: versionLabel(major, minor), + url: `${protocol}${name.replace('.', '-')}.${domain}` }; }); @@ -52,16 +52,24 @@ function VersionDropdown() { }); // Overwrite the first element with the latest version releaseBranches[0] = { - version: 'Latest Version', - url: `https://docs.kargo.io` + version: latestVersionLabel, + url: latestVersionUrl }; + // Put the "edge" version at the end of the list + releaseBranches.push({ + version: edgeVersionLabel, + url: edgeVersionUrl, + }) + const currentVersion = getCurrentVersion(); + // If the current version is not recognized, add it to the end of the list + if (currentVersion === unrecognizedVersionLabel) { + const url = currentUrl(); + releaseBranches.push({ + version: unrecognizedVersionLabel, + url: `${url.protocol}//${url.hostname}${url.port ? `:${url.port}` : ''}` + }); + } console.log("These are release branches: ", releaseBranches); - - localStorage.setItem(CACHE_KEY, JSON.stringify({ - versions: releaseBranches, - timestamp: Date.now() - })); - setVersions(releaseBranches); setLoading(false); } catch (error) { @@ -71,11 +79,15 @@ function VersionDropdown() { }; const getCurrentVersion = () => { - const url = window.location.href; - const match = url.match(/https:\/\/release-(\d+)-(\d+)\.docs\.kargo\.io/); - const currentVersion = match ? `v${match[1]}.${match[2]}` : 'Latest Version'; - console.log("Current version extracted:", currentVersion); - return currentVersion; + const url = currentUrl(); + if (url.hostname === new URL(latestVersionUrl).hostname && !url.port) { + return 'Latest Version'; + } + if (url.hostname === new URL(edgeVersionUrl).hostname && !url.port) { + return 'Edge Version (main)'; + } + const match = url.hostname.match(/^release-(\d+)-(\d+)\.docs\.kargo\.io$/); + return match && !url.port ? versionLabel(match[1], match[2]) : unrecognizedVersionLabel; }; useEffect(() => {