Skip to content

Commit

Permalink
feat: create utils for generating release notes urls with right filte…
Browse files Browse the repository at this point in the history
…rs (#1998)

* feat: calculate the release notes url based on product and productArea

* chore: release notes links to point to the new search page
  • Loading branch information
gabriele-ct authored Jun 12, 2024
1 parent 5c2fffd commit 3ddd920
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cold-owls-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-docs/gatsby-theme-docs': patch
---

Add util function to calculate the url for release notes page with pre initialized fields
1 change: 1 addition & 0 deletions packages/gatsby-theme-docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ export {
} from './src/modules/self-learning/components/if-user-logged';
export { default as LoginButton } from './src/modules/sso/components/login-button';
export * from './src/modules/ai-assistant';
export * from './src/utils/release-notes';
5 changes: 4 additions & 1 deletion packages/gatsby-theme-docs/src/layouts/internals/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isRightChapterRecursive,
} from './sidebar.utils';
import { useSiteData } from '../../hooks/use-site-data';
import { getReleaseNotesQueryStringBySiteTitle } from '../../utils/release-notes';

const ReleaseNotesIcon = createStyledIcon(Icons.ReleaseNotesSvgIcon);

Expand Down Expand Up @@ -534,7 +535,9 @@ const Sidebar = (props) => {
{shouldRenderLinkToReleaseNotes && (
<ReleaseNoteLinkContainer>
<SidebarLink
to="/releases"
to={`/docs/release-notes${getReleaseNotesQueryStringBySiteTitle(
props.siteTitle
)}`}
onClick={props.onLinkClick}
locationPath={
isReleasePage
Expand Down
51 changes: 51 additions & 0 deletions packages/gatsby-theme-docs/src/utils/release-notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export const MIN_DATERANGE = 0;
export const MAX_DATERANGE = 2537011284;

// TODO: this could be used to generate sitemap.xml urls query strings
const mapSiteTitleToFacetFilter = new Map([
['Merchant Center', { productArea: 'Merchant Center' }],
['HTTP API', { productArea: 'HTTP API' }],
['Checkout', { product: 'Checkout' }],
['Connect', { product: 'Connect' }],
['Frontend Development', { productArea: 'Frontend Development' }],
['Frontend Studio', { productArea: 'Frontend Studio' }],
['Import API', { productArea: 'Import API' }],
[
'Merchant Center Customizations',
{ productArea: 'Merchant Center Customizations' },
],
]);

export const buildReleaseNotesQueryString = (product, productArea) => {
let searchState = {
range: {
dateTimestamp: `${MIN_DATERANGE}:${MAX_DATERANGE}`,
},
};
if (product) {
searchState.refinementList = {
...searchState.refinementList,
product: [product],
};
}
if (productArea) {
searchState.refinementList = {
...searchState.refinementList,
product: [productArea],
};
}
return searchState.refinementList
? `?searchState=${encodeURIComponent(JSON.stringify(searchState))}`
: '';
};

export const getReleaseNotesQueryStringBySiteTitle = (siteTitle) => {
const productArea = mapSiteTitleToFacetFilter.get(siteTitle);
if (productArea) {
return buildReleaseNotesQueryString(
productArea.product,
productArea.productArea
);
}
return '';
};
55 changes: 55 additions & 0 deletions packages/gatsby-theme-docs/src/utils/release-notes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
buildReleaseNotesQueryString,
getReleaseNotesQueryStringBySiteTitle,
} from './release-notes';

describe('buildReleaseNotesQueryString', () => {
it('should return a query string with product and productArea', () => {
const product = 'Merchant Center';
const productArea = 'Frontend Development';

const queryString = buildReleaseNotesQueryString(product, productArea);

expect(queryString).toBe(
'?searchState=%7B%22range%22%3A%7B%22dateTimestamp%22%3A%220%3A2537011284%22%7D%2C%22refinementList%22%3A%7B%22product%22%3A%5B%22Frontend%20Development%22%5D%7D%7D'
);
});

it('should return a query string with only product', () => {
const product = 'Checkout';

const queryString = buildReleaseNotesQueryString(product);

expect(queryString).toBe(
'?searchState=%7B%22range%22%3A%7B%22dateTimestamp%22%3A%220%3A2537011284%22%7D%2C%22refinementList%22%3A%7B%22product%22%3A%5B%22Checkout%22%5D%7D%7D'
);
});

it('should return an empty query string if no arguments are provided', () => {
const queryString = buildReleaseNotesQueryString();

expect(queryString).toBe('');
});
});

describe('getReleaseNotesQueryStringBySiteTitle', () => {
it('should return a query string with productArea for "Merchant Center"', () => {
const siteTitle = 'Merchant Center';
const queryString = getReleaseNotesQueryStringBySiteTitle(siteTitle);
expect(queryString).toBe(
'?searchState=%7B%22range%22%3A%7B%22dateTimestamp%22%3A%220%3A2537011284%22%7D%2C%22refinementList%22%3A%7B%22product%22%3A%5B%22Merchant%20Center%22%5D%7D%7D'
);
});

it('should return an empty query string if siteTitle is not found', () => {
const siteTitle = 'Non-existent Title';
const queryString = getReleaseNotesQueryStringBySiteTitle(siteTitle);
expect(queryString).toBe('');
});
});

it('should return an empty query string if siteTitle is not found', () => {
const siteTitle = 'Non-existent Title';
const queryString = getReleaseNotesQueryStringBySiteTitle(siteTitle);
expect(queryString).toBe('');
});

0 comments on commit 3ddd920

Please sign in to comment.