Skip to content

Commit

Permalink
fix(Storybook): change logic for version selector
Browse files Browse the repository at this point in the history
  • Loading branch information
hamikhambardzumyan committed Feb 19, 2024
1 parent 1d3aa2c commit 6abd758
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions .storybook/components/SidebarLabelWrapper/SidebarLabelWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const cmpStages = {
};

const { versions } = libVersions;
const [lastVersion] = [...versions].reverse();

const getVersionDropdown = (currentVersion) =>
`<div class="versions">
Expand Down Expand Up @@ -44,16 +45,19 @@ const SidebarLabelWrapper = ({ item }) => {
if (renderOnce || !document.querySelector('#versions__select')) {
renderOnce = false;
const sidebar = document.querySelector('.css-194spiq');
const currentVersionRegex = /v\d\.\d\.\d/;
const currentVersion =
window.location.href.match(currentVersionRegex) && window.location.href.match(currentVersionRegex)[0];
const currentVersionRegex = /v\d+\.\d+\.\d+/;
const [versionInURL] = window.location.href.match(currentVersionRegex) || [];
const currentVersion = versionInURL || lastVersion;
const versions__select = getVersionDropdown(currentVersion);
sidebar.insertAdjacentHTML('beforeend', versions__select);
const select = document.querySelector('#versions__select');

select.addEventListener('change', () => {
if (currentVersion) {
window.location.href = window.location.href.replace(currentVersion, select.value);
}
const modifiedURL = versionInURL
? window.location.href.replace(currentVersion, select.value)
: window.location.href.replace(/(\.com\/)(\?path=)/, `$1${currentVersion}$2`);

window.location.href = modifiedURL;
});
}
}, []);
Expand Down
2 changes: 1 addition & 1 deletion .storybook/lib-versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "versions": ["v2.9.0", "v2.8.0", "v2.7.0", "v2.6.0", "v2.5.0", "v2.4.0", "v2.3.0", "v2.2.0", "v2.1.0"] }
{ "versions": ["v2.11.1", "v2.11.2", "v2.12.1", "v2.12.2", "v2.12.3"] }

0 comments on commit 6abd758

Please sign in to comment.