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

fix: update logic to conditionally show Get Started and Billing routes #3807

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: update logic to conditionally show Get Started and Billing routes
  • Loading branch information
YounixM committed Oct 26, 2023
commit 3080cba8296f24640c6fe970b1c79799c324869a
72 changes: 44 additions & 28 deletions frontend/src/container/SideNav/SideNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import ROUTES from 'constants/routes';
import useLicense, { LICENSE_PLAN_KEY } from 'hooks/useLicense';
import history from 'lib/history';
import { LifeBuoy } from 'lucide-react';
import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
Expand All @@ -33,6 +39,7 @@ import {

function SideNav(): JSX.Element {
const dispatch = useDispatch();
const [menuItems, setMenuItems] = useState(defaultMenuItems);
const [collapsed, setCollapsed] = useState<boolean>(
getLocalStorageKey(IS_SIDEBAR_COLLAPSED) === 'true',
);
Expand All @@ -44,36 +51,45 @@ function SideNav(): JSX.Element {
featureResponse,
} = useSelector<AppState, AppReducer>((state) => state.app);

const { data } = useLicense();
const { data, isFetching } = useLicense();

let secondaryMenuItems: MenuItem[] = [];

const isOnBasicPlan =
data?.payload?.licenses?.some(
(license) =>
license.isCurrent && license.planKey === LICENSE_PLAN_KEY.BASIC_PLAN,
) || data?.payload?.licenses === null;

const menuItems = useMemo(
() =>
defaultMenuItems.filter((item) => {
const isOnboardingEnabled =
featureResponse.data?.find(
(feature) => feature.name === FeatureKeys.ONBOARDING,
)?.active || false;

if (role !== 'ADMIN' || isOnBasicPlan) {
return item.key !== ROUTES.BILLING;
}

if (!isOnboardingEnabled || !isCloudUser()) {
return item.key !== ROUTES.GET_STARTED;
}

return true;
}),
[featureResponse.data, isOnBasicPlan, role],
);
useEffect((): void => {
const isOnboardingEnabled =
featureResponse.data?.find(
(feature) => feature.name === FeatureKeys.ONBOARDING,
)?.active || false;

if (!isOnboardingEnabled || !isCloudUser()) {
let items = [...menuItems];

items = items.filter((item) => item.key !== ROUTES.GET_STARTED);

setMenuItems(items);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [featureResponse.data]);

// using a separate useEffect as the license fetching call takes few milliseconds
useEffect(() => {
if (!isFetching) {
let items = [...menuItems];

const isOnBasicPlan =
data?.payload?.licenses?.some(
(license) =>
license.isCurrent && license.planKey === LICENSE_PLAN_KEY.BASIC_PLAN,
) || data?.payload?.licenses === null;

if (role !== 'ADMIN' || isOnBasicPlan) {
items = items.filter((item) => item.key !== ROUTES.BILLING);
}

setMenuItems(items);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data?.payload?.licenses, isFetching, role]);

const { pathname, search } = useLocation();

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/useLicense/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const LICENSE_PLAN_KEY = {
ENTERPRISE_PLAN: 'ENTERPRISE_PLAN',
BASIC_PLAN: 'BASIC_PLAN ',
BASIC_PLAN: 'BASIC_PLAN',
};

export const LICENSE_PLAN_STATUS = {
Expand Down