Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cookies to remember previously selected Cloud #287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 37 additions & 5 deletions src/components/cloudLinkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@
*/

import * as React from 'react';
import Cookies from 'js-cookie';

const initialCloud = {
name: 'New Relic Cloud',
baseUrl: 'https://work.withpixe.ai',
};
const availableClouds = [
{
name: 'New Relic Cloud',
baseUrl: 'https://work.withpixie.ai',
cloudAddr: 'withpixie.ai',
},
{
name: 'Cosmic Cloud',
baseUrl: 'https://work.getcosmic.ai',
cloudAddr: 'getcosmic.ai',
},
];

const initialCloud = availableClouds[0];

export const CloudLinkContext = React.createContext(
{
Expand All @@ -30,8 +41,29 @@ export const CloudLinkContext = React.createContext(
setSelectedCloud: (cloud) => { },
},
);
const getDefaultOrSelectedCloud = () => {
if (!Cookies.get('consent')) {
return initialCloud;
}
const selectedCloud = Cookies.get('selected-cloud');
if (selectedCloud) {
return JSON.parse(selectedCloud);
}
return initialCloud;
};
export default function CloudLinkProvider({ children }) {
const [selectedCloud, setSelectedCloud] = React.useState(initialCloud);
const isBrowser = typeof window !== 'undefined';
const [
selectedCloud,
setSelectedCloudState,
] = React.useState(isBrowser ? getDefaultOrSelectedCloud() : initialCloud);
const setSelectedCloud = (cloud) => {
setSelectedCloudState(cloud);
// If the user has not consented to cookies, do not set a default cloud.
if (Cookies.get('consent')) {
Cookies.set('selected-cloud', cloud);
}
};
return (
<CloudLinkContext.Provider
value={{
Expand Down