Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour committed Jan 14, 2025
1 parent c4fbd20 commit cd8408d
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions docs/src/components/VersionDropdown.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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}`
};
});

Expand All @@ -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) {
Expand All @@ -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(() => {
Expand Down

0 comments on commit cd8408d

Please sign in to comment.