Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions code/core/src/manager-api/modules/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,22 @@ export const init: ModuleFn = ({ store }) => {
},
// TODO: Move this to it's own "info" module later
getDocsUrl: ({ asset, subpath = asset, versioned, renderer, ref = 'ui' }) => {
const {
versions: { latest, current },
} = store.getState();
const { versions } = store.getState();
const latestVersion = versions.latest?.version;
const currentVersion = versions.current?.version;
const activeVersion =
(currentVersion?.startsWith('0.0.0') && latestVersion) || currentVersion;

let url = `https://storybook.js.org/${asset ? 'docs-assets' : 'docs'}/`;

if (asset && current?.version) {
url += `${semver.major(current.version)}.${semver.minor(current.version)}/`;
} else if (versioned && current?.version && latest?.version) {
const versionDiff = semver.diff(latest.version, current.version);
const isLatestDocs =
versionDiff === 'patch' ||
versionDiff === null ||
// assume latest version when current version is a 0.0.0 canary
semver.satisfies(current.version, '0.0.0', { includePrerelease: true });

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't actually match on a 0.0.0-canary.0. A startsWith('0.0.0') match is actually good enough for our needs here.

if (asset && activeVersion) {
url += `${semver.major(activeVersion)}.${semver.minor(activeVersion)}/`;
} else if (versioned && activeVersion && latestVersion) {
const versionDiff = semver.diff(latestVersion, activeVersion);
const isLatestDocs = versionDiff === 'patch' || versionDiff === null;

if (!isLatestDocs) {
url += `${semver.major(current.version)}.${semver.minor(current.version)}/`;
url += `${semver.major(activeVersion)}.${semver.minor(activeVersion)}/`;
}
}

Expand Down
28 changes: 28 additions & 0 deletions code/core/src/manager-api/tests/versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const majorDiff = {
current: { version: '6.2.1' },
latest: { version: '7.6.10' },
};
const canary = {
current: { version: '0.0.0-canary.0' },
latest: { version: '7.6.10' },
};
const newerPrerelease = {
current: { version: '8.0.0-beta' },
latest: { version: '7.6.10' },
Expand Down Expand Up @@ -186,6 +190,17 @@ describe('versions API', () => {
);
});

it('returns the latest versioned url when current is a canary', async () => {
const store = createMockStore();
const { init, api, state: initialState } = initVersions({ store });

await init();

setVersions(store, initialState, canary);

expect(api.getDocsUrl({ versioned: true })).toEqual('https://storybook.js.org/docs/?ref=ui');
});

it('returns the versioned url when current is a prerelease', async () => {
const store = createMockStore();
const { init, api, state: initialState } = initVersions({ store });
Expand Down Expand Up @@ -250,6 +265,19 @@ describe('versions API', () => {
'https://storybook.js.org/docs-assets/7.6/api/doc-block-controls.png?ref=ui'
);
});

it('returns the latest versioned url with assets path for canary version', async () => {
const store = createMockStore();
const { init, api, state: initialState } = initVersions({ store });

await init();

setVersions(store, initialState, canary);

expect(api.getDocsUrl({ asset: 'api/doc-block-controls.png' })).toEqual(
'https://storybook.js.org/docs-assets/7.6/api/doc-block-controls.png?ref=ui'
);
});
});

describe('versionUpdateAvailable', () => {
Expand Down