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

Add paths attribute to per workflow banner settings #3109

Merged
merged 13 commits into from
Sep 13, 2024
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from "react";
import { useLocation } from "react-router-dom";
import isEmpty from "lodash/isEmpty";

import { Alert } from "../Feedback";
import Grid from "../grid";
import { Link as LinkComponent } from "../link";
import type { AppBanners } from "../Types";
import { findPathMatchList } from "../utils";

interface LayoutWithNotificationsProps {
bannersData: AppBanners;
Expand All @@ -22,8 +24,17 @@ const LayoutWithNotifications = ({
const perWorkflowData = bannersData?.perWorkflow;
const multiWorkflowData = bannersData?.multiWorkflow;

const showAlertPerWorkflow =
const location = useLocation();

const pathMatches = findPathMatchList(location?.pathname, perWorkflowData[workflow]?.paths);

const hasPerWorkflowAlert =
workflow && perWorkflowData[workflow] && !perWorkflowData[workflow]?.dismissed;
const showAlertPerWorkflow = !isEmpty(perWorkflowData[workflow]?.paths)
? hasPerWorkflowAlert &&
(perWorkflowData[workflow]?.paths?.includes(location.pathname) || pathMatches)
: hasPerWorkflowAlert;

const showAlertMultiWorkflow =
showAlertPerWorkflow || perWorkflowData[workflow]?.dismissed
? false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const useCompareAppNotificationsData = (banners: AppBanners) => {
linkText: bannersPreferences?.perWorkflow?.[key].linkText,
link: bannersPreferences?.perWorkflow?.[key].link,
severity: bannersPreferences?.perWorkflow?.[key].severity,
paths: bannersPreferences?.perWorkflow?.[key].paths,
};

if (!isEqual(banners?.perWorkflow?.[key], perWorkflowPreferences)) {
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/core/src/Types/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Banner extends Pick<AlertProps, "title" | "severity"> {
dismissed: boolean;
linkText?: string;
link?: string;
paths?: string[];
}

export interface PerWorkflowBanner {
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// eslint-disable-next-line import/prefer-default-export
export { default as getDisplayName } from "./getDisplayName";
export { default as findPathMatchList } from "./pathMatching";
17 changes: 17 additions & 0 deletions frontend/packages/core/src/utils/pathMatching.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { matchPath } from "react-router";

const findPathMatchList = (locationPathname: string, pathstoMatch: string[]) => {
lucechal14 marked this conversation as resolved.
Show resolved Hide resolved
let pathFound = false;

pathstoMatch?.forEach((path: string) => {
lucechal14 marked this conversation as resolved.
Show resolved Hide resolved
const match = matchPath({ path }, locationPathname);

if (match) {
pathFound = true;
}
});

return pathFound;
};

export default findPathMatchList;
Loading