Skip to content

Commit

Permalink
fix: use module.exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mbazhlekova committed Sep 21, 2021
1 parent 97a943e commit 113e8a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion scripts/list-quickstart-urls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const getPackNr1Url = require('../src/utils/get-pack-nr1-url');
const { getPackNr1Url } = require('../src/utils/get-pack-nr1-url');
const quickstarts = require('../src/data/quickstarts.json');
const fs = require('fs');

Expand Down
25 changes: 16 additions & 9 deletions src/components/InstallButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/react';
import { Button, Link, Icon } from '@newrelic/gatsby-theme-newrelic';
import { NR1_LOGIN_URL } from '../data/constants';
import { quickstart } from '../types';

const {
import {
getPackNr1Url,
getGuidedInstallStackedNr1Url,
} = require('../utils/get-pack-nr1-url');
} from '../utils/get-pack-nr1-url';
import {
NR1_LOGIN_URL,
NR1_GUIDED_INSTALL_NERDLET,
NR1_PACK_DETAILS_NERDLET,
} from '../data/constants';
import { quickstart } from '../types';

/**
* @param {String} id
* @param {String} nerdletId
* @returns {String}
*/
const createInstallLink = (id, hasGuidedInstall) => {
const createInstallLink = (id, nerdletId, hasGuidedInstall) => {
const platformUrl = hasGuidedInstall
? getGuidedInstallStackedNr1Url()
: getPackNr1Url(id);
? getGuidedInstallStackedNr1Url(nerdletId)
: getPackNr1Url(id, nerdletId);
const url = new URL(
`?return_to=${encodeURIComponent(platformUrl)}`,
NR1_LOGIN_URL
Expand Down Expand Up @@ -47,10 +50,14 @@ const InstallButton = ({ quickstart, ...props }) => {
if (!hasInstallableComponent && !hasComponent(quickstart, 'documentation')) {
return null;
}

const nerdletId = hasGuidedInstall
? NR1_GUIDED_INSTALL_NERDLET
: NR1_PACK_DETAILS_NERDLET;
// If we have an install-able component, generate a URL. Otherwise, link to the
// first documentation supplied.
const url = hasInstallableComponent
? createInstallLink(quickstart.id, hasGuidedInstall)
? createInstallLink(quickstart.id, nerdletId, hasGuidedInstall)
: quickstart.documentation[0].url;

return (
Expand Down
38 changes: 14 additions & 24 deletions src/utils/get-pack-nr1-url.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
const {
NR1_PACK_DETAILS_NERDLET,
NR1_GUIDED_INSTALL_NERDLET,
NR1_EXPLORER_NERDLET,
} = require('../data/constants');

// FIXME: update this to production URL when deployed / launched
const NR1_BASE_URL = 'https://staging-one.newrelic.com';
const NR1_BASE_URL_LOCAL = 'https://dev-one.newrelic.com';
const NERDLET_PATH = `launcher/nr1-core.explorer/`;
const NR1_EXPLORER_NERDLET = 'nr1-core.listing';

/**
* @param {string} quickstartId The ID for an observability pack.
* @param {boolean} [debug] If set to true, this will add `packages=local`.
* @returns {string} The URL for the pack details within the platform.
*/
const getPackNr1Url = (quickstartId, debug = false) => {
const getPackNr1Url = (quickstartId, nerdletId, debug = false) => {
const pane = JSON.stringify({
nerdletId: NR1_PACK_DETAILS_NERDLET,
nerdletId,
quickstartId,
});

Expand All @@ -30,24 +25,19 @@ const getPackNr1Url = (quickstartId, debug = false) => {
const local = debug ? 'packages=local&' : '';
const NR1_URL = debug ? NR1_BASE_URL_LOCAL : NR1_BASE_URL;

const packDetailsUrl = new URL(
`${NERDLET_PATH}?${local}pane=${hash}`,
NR1_URL
);
const url = new URL(`${NERDLET_PATH}?${local}pane=${hash}`, NR1_URL);

return packDetailsUrl.href;
return url.href;
};

/**
* @param {boolean} [debug] If set to true, this will add `packages=local`.
* @returns {string} The URL for the pack details within the platform.
*/
const getGuidedInstallStackedNr1Url = (debug = false) => {
const getGuidedInstallStackedNr1Url = (nerdletId, debug = false) => {
const pane = JSON.stringify({ nerdletId: NR1_EXPLORER_NERDLET });
const card = JSON.stringify({ nerdletId: NR1_GUIDED_INSTALL_NERDLET });
const card = JSON.stringify({
nerdletId,
});

const paneHash =
window && window.btoa
const hash =
typeof window !== 'undefined' && window.btoa
? btoa(pane)
: Buffer.from(pane, 'utf-8').toString('base64');

Expand All @@ -59,12 +49,12 @@ const getGuidedInstallStackedNr1Url = (debug = false) => {
const local = debug ? 'packages=local&' : '';
const NR1_URL = debug ? NR1_BASE_URL_LOCAL : NR1_BASE_URL;

const guidedInstallUrl = new URL(
`${NERDLET_PATH}?${local}pane=${paneHash}&cards[0]=${cardHash}`,
const url = new URL(
`${NERDLET_PATH}?${local}pane=${hash}&cards[0]=${cardHash}`,
NR1_URL
);

return guidedInstallUrl.href;
return url.href;
};

module.exports = { getPackNr1Url, getGuidedInstallStackedNr1Url };

0 comments on commit 113e8a7

Please sign in to comment.