1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-24 17:51:14 +02:00
unleash.unleash/frontend/src/component/insights/Insights.tsx
andreas-unleash 00d3490764
fix: Project select should not expand when selecting multiple projects (#6811)
Currently when you are selecting multiple projects, the autocomplete
expands indefinitely when focused.

This fixes that behaviour by limiting the project select to 1 visible
tag (even when focused) for Insights and 3 tags for Playground (same as
it was)

Closes
[1-2276](https://linear.app/unleash/issue/1-2276/limit-the-project-select-height-or-expand-its-length)



https://github.com/Unleash/unleash/assets/104830839/bf42a06e-8d30-49df-ac5b-a4a4f2685fa9

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2024-04-10 12:34:11 +03:00

80 lines
2.7 KiB
TypeScript

import { useState, type VFC } from 'react';
import { Box, styled } from '@mui/material';
import { ArrayParam, withDefault } from 'use-query-params';
import { usePersistentTableState } from 'hooks/usePersistentTableState';
import {
allOption,
ProjectSelect,
} from 'component/common/ProjectSelect/ProjectSelect';
import { useInsights } from 'hooks/api/getters/useInsights/useInsights';
import { InsightsHeader } from './components/InsightsHeader/InsightsHeader';
import { useInsightsData } from './hooks/useInsightsData';
import { InsightsCharts } from './InsightsCharts';
const StickyWrapper = styled(Box, {
shouldForwardProp: (prop) => prop !== 'scrolled',
})<{ scrolled?: boolean }>(({ theme, scrolled }) => ({
position: 'sticky',
top: 0,
zIndex: theme.zIndex.sticky,
padding: scrolled ? theme.spacing(2, 0) : theme.spacing(0, 0, 2),
background: theme.palette.background.application,
transition: 'padding 0.3s ease',
}));
export const Insights: VFC = () => {
const [scrolled, setScrolled] = useState(false);
const { insights, loading, error } = useInsights();
const stateConfig = {
projects: withDefault(ArrayParam, [allOption.id]),
};
const [state, setState] = usePersistentTableState('insights', stateConfig);
const setProjects = (projects: string[]) => {
setState({ projects });
};
const projects = state.projects
? (state.projects.filter(Boolean) as string[])
: [];
const insightsData = useInsightsData(insights, projects);
const handleScroll = () => {
if (!scrolled && window.scrollY > 0) {
setScrolled(true);
} else if (scrolled && window.scrollY === 0) {
setScrolled(false);
}
};
if (typeof window !== 'undefined') {
window.addEventListener('scroll', handleScroll);
}
return (
<>
<StickyWrapper scrolled={scrolled}>
<InsightsHeader
actions={
<ProjectSelect
selectedProjects={projects}
onChange={setProjects}
dataTestId={'DASHBOARD_PROJECT_SELECT'}
limitTags={1}
sx={{
flex: 1,
maxWidth: '360px',
width: '100%',
}}
/>
}
/>
</StickyWrapper>
<InsightsCharts
loading={loading}
projects={projects}
{...insightsData}
/>
</>
);
};