github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/components/TopAppBar/TopAppBar.tsx (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  import { Textfield } from '../textfield';
    17  import React, { useCallback } from 'react';
    18  import { SideBar } from '../SideBar/SideBar';
    19  import { Button } from '../button';
    20  import { ShowBarWhite } from '../../../images';
    21  import { useSearchParams } from 'react-router-dom';
    22  import { Dropdown } from '../dropdown/dropdown';
    23  import { Checkbox } from '../dropdown/checkbox';
    24  import classNames from 'classnames';
    25  import {
    26      UpdateSidebar,
    27      useAllWarnings,
    28      useKuberpultVersion,
    29      useShownWarnings,
    30      useSidebarShown,
    31  } from '../../utils/store';
    32  import { Warning } from '../../../api/api';
    33  import { hideWithoutWarnings, KuberpultGitHubLink, setHideWithoutWarnings } from '../../utils/Links';
    34  
    35  export type TopAppBarProps = {
    36      showAppFilter: boolean;
    37      showTeamFilter: boolean;
    38      showWarningFilter: boolean;
    39  };
    40  
    41  export const TopAppBar: React.FC<TopAppBarProps> = (props) => {
    42      const sideBar = useSidebarShown();
    43      const [params, setParams] = useSearchParams();
    44  
    45      const toggleSideBar = useCallback(() => UpdateSidebar.set({ shown: !sideBar }), [sideBar]);
    46      const appNameParam = params.get('application') || '';
    47      const teamsParam = (params.get('teams') || '').split(',').filter((val) => val !== '');
    48  
    49      const version = useKuberpultVersion() || '2.6.0';
    50  
    51      const hideWithoutWarningsValue = hideWithoutWarnings(params);
    52      const allWarnings: Warning[] = useAllWarnings();
    53      const shownWarnings: Warning[] = useShownWarnings(teamsParam, appNameParam);
    54  
    55      const onWarningsFilterClick = useCallback((): void => {
    56          setHideWithoutWarnings(params, !hideWithoutWarningsValue);
    57          setParams(params);
    58      }, [hideWithoutWarningsValue, params, setParams]);
    59  
    60      const renderedWarnings =
    61          allWarnings.length === 0 ? (
    62              ''
    63          ) : (
    64              <div className="service-lane__warning">
    65                  {shownWarnings.length} warnings shown ({allWarnings.length} total).
    66              </div>
    67          );
    68  
    69      const [searchParams, setSearchParams] = useSearchParams(
    70          appNameParam === '' ? undefined : { application: `${appNameParam}` }
    71      );
    72      const onChangeApplication = useCallback(
    73          (event: any) => {
    74              if (event.target.value !== '') searchParams.set('application', event.target.value);
    75              else searchParams.delete('application');
    76              setSearchParams(searchParams);
    77          },
    78          [searchParams, setSearchParams]
    79      );
    80  
    81      const renderedAppFilter =
    82          props.showAppFilter === true ? (
    83              <div className="mdc-top-app-bar__section top-app-bar--wide-filter">
    84                  <Textfield
    85                      className={'top-app-bar-search-field'}
    86                      placeholder={'Application Name'}
    87                      value={appNameParam}
    88                      leadingIcon={'search'}
    89                      onChange={onChangeApplication}
    90                  />
    91              </div>
    92          ) : (
    93              <div className="mdc-top-app-bar__section top-app-bar--wide-filter"></div>
    94          );
    95      const renderedTeamsFilter =
    96          props.showTeamFilter === true ? (
    97              <div className="mdc-top-app-bar__section top-app-bar--narrow-filter">
    98                  <Dropdown className={'top-app-bar-search-field'} placeholder={'Teams'} leadingIcon={'search'} />
    99              </div>
   100          ) : (
   101              <div className="mdc-top-app-bar__section top-app-bar--narrow-filter"></div>
   102          );
   103      const renderedWarningsFilter =
   104          props.showWarningFilter === true ? (
   105              <div className="mdc-top-app-bar__section top-app-bar--narrow-filter">
   106                  <Checkbox
   107                      enabled={hideWithoutWarningsValue}
   108                      onClick={onWarningsFilterClick}
   109                      id="warningFilter"
   110                      label="hide apps without warnings"
   111                      classes=""
   112                  />
   113              </div>
   114          ) : (
   115              <div className="mdc-top-app-bar__section top-app-bar--narrow-filter"></div>
   116          );
   117  
   118      return (
   119          <div className="mdc-top-app-bar">
   120              <div className="mdc-top-app-bar__row">
   121                  <div className="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
   122                      <span className="mdc-top-app-bar__title">
   123                          Kuberpult <KuberpultGitHubLink version={version} />
   124                      </span>
   125                  </div>
   126                  {renderedAppFilter}
   127                  {renderedTeamsFilter}
   128                  {renderedWarningsFilter}
   129                  {renderedWarnings}
   130                  <div className="mdc-top-app-bar__section mdc-top-app-bar__section--align-end">
   131                      <strong className="sub-headline1">Planned Actions</strong>
   132                      <Button
   133                          className="mdc-show-button mdc-button--unelevated"
   134                          icon={<ShowBarWhite />}
   135                          onClick={toggleSideBar}
   136                      />
   137                      <SideBar
   138                          className={classNames(`mdc-drawer-sidebar mdc-drawer-sidebar-container`, {
   139                              'mdc-drawer-sidebar--displayed': sideBar,
   140                              'mdc-drawer-sidebar--hidden': !sideBar,
   141                          })}
   142                          toggleSidebar={toggleSideBar}
   143                      />
   144                  </div>
   145              </div>
   146          </div>
   147      );
   148  };