Skip to content

Commit

Permalink
dropdown title sync
Browse files Browse the repository at this point in the history
Signed-off-by: Faeka Ansari <[email protected]>
  • Loading branch information
fykaa committed Jan 14, 2025
1 parent dfa6926 commit 91c4022
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions docs/src/components/VersionDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ 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 dropdown initialized");
console.log("Navbar Version dropdown initialized");

const [versions, setVersions] = useState([]);
const [loading, setLoading] = useState(true);
const [currentVersion, setCurrentVersion] = useState("");

useEffect(() => {
fetchVersions();
}, []);
const [currentVersion, setCurrentVersion] = useState('');

const fetchVersions = async () => {
const cachedData = localStorage.getItem(CACHE_KEY);
Expand All @@ -26,10 +23,10 @@ function VersionDropdown() {
}
}
try {
console.log("Before fetching versions")
console.log("Before fetching versions");
const response = await fetch(githubApiUrl);
const branches = await response.json();
console.log("fetched branches are: ", branches);
console.log("Fetched branches are: ", branches);

const releaseBranches = branches
.map(branch => branch.name)
Expand All @@ -42,7 +39,7 @@ function VersionDropdown() {
};
});

console.log("These are release branches before sorting and updating 0 element: ", releaseBranches)
console.log("These are release branches before sorting and updating 0 element: ", releaseBranches);
releaseBranches.sort((a, b) => {
if (a.version > b.version) {
return -1;
Expand All @@ -57,7 +54,7 @@ function VersionDropdown() {
version: 'Latest Version',
url: `https://docs.kargo.io`
};
console.log("These are release branches: ", releaseBranches)
console.log("These are release branches: ", releaseBranches);

localStorage.setItem(CACHE_KEY, JSON.stringify({
versions: releaseBranches,
Expand All @@ -72,6 +69,19 @@ 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;
};

useEffect(() => {
setCurrentVersion(getCurrentVersion());
fetchVersions();
}, []);

const handleVersionChange = (event) => {
const selectedVersion = versions.find(v => v.version === event.target.value);
if (selectedVersion) {
Expand All @@ -86,6 +96,7 @@ function VersionDropdown() {
<select
className="navbar__link"
onChange={handleVersionChange}
value={currentVersion}
style={{
backgroundColor: 'transparent',
border: 'none',
Expand Down

0 comments on commit 91c4022

Please sign in to comment.