UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

76 lines (75 loc) 3.05 kB
import { jsx as _jsx } from "react/jsx-runtime"; /* * Copyright 2025 The Kubernetes Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Box from '@mui/material/Box'; import Grid from '@mui/material/Grid'; import React from 'react'; import { useDispatch } from 'react-redux'; import { useLocation } from 'react-router-dom'; import { setNamespaceFilter } from '../../redux/filterSlice'; import { useTypedSelector } from '../../redux/reducers/reducers'; import { NamespacesAutocomplete } from './NamespacesAutocomplete'; import SectionHeader from './SectionHeader'; /** * Get the filter value by name from the URL * * @param key - the name of the filter * @param location - the location object from react-router * @returns the filter value as an array of strings */ function getFilterValueByNameFromURL(key, location) { const searchParams = new URLSearchParams(location.search); const filterValue = searchParams.get(key); if (!filterValue) { return []; } return filterValue.split(' '); } export default function SectionFilterHeader(props) { const { noNamespaceFilter = false, actions: propsActions = [], preRenderFromFilterActions, ...headerProps } = props; const filter = useTypedSelector(state => state.filter); const dispatch = useDispatch(); const location = useLocation(); React.useEffect(() => { const namespace = getFilterValueByNameFromURL('namespace', location); if (namespace.length > 0) { const namespaceFromStore = [...filter.namespaces].sort(); if (namespace .slice() .sort() .every((value, index) => value !== namespaceFromStore[index])) { dispatch(setNamespaceFilter(namespace)); } } }, // eslint-disable-next-line react-hooks/exhaustive-deps []); let actions = []; if (preRenderFromFilterActions) { actions.push(...preRenderFromFilterActions); } if (!!propsActions) { actions = actions.concat(propsActions); } if (!noNamespaceFilter) { actions.push(_jsx(NamespacesAutocomplete, {})); } return (_jsx(React.Fragment, { children: _jsx(SectionHeader, { ...headerProps, actions: actions.length <= 1 ? actions : [ _jsx(Box, { children: _jsx(Grid, { container: true, spacing: 1, alignItems: "center", children: actions.map((action, i) => (_jsx(Grid, { item: true, children: action }, i))) }) }), ] }) })); }