2024-03-18 13:58:05 +01:00
|
|
|
import { useState, type VFC } from 'react';
|
2024-03-04 12:56:17 +01:00
|
|
|
import { Box, styled } from '@mui/material';
|
|
|
|
import { ArrayParam, withDefault } from 'use-query-params';
|
|
|
|
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
2024-03-01 13:28:57 +01:00
|
|
|
import {
|
|
|
|
allOption,
|
2024-03-04 12:56:17 +01:00
|
|
|
ProjectSelect,
|
|
|
|
} from 'component/common/ProjectSelect/ProjectSelect';
|
2024-03-21 15:39:03 +01:00
|
|
|
import { useInsights } from 'hooks/api/getters/useInsights/useInsights';
|
|
|
|
import { InsightsHeader } from './components/InsightsHeader/InsightsHeader';
|
|
|
|
import { useInsightsData } from './hooks/useInsightsData';
|
|
|
|
import { InsightsCharts } from './InsightsCharts';
|
2024-03-20 08:24:56 +01:00
|
|
|
|
2024-03-20 16:11:50 +01:00
|
|
|
const StickyWrapper = styled(Box, {
|
|
|
|
shouldForwardProp: (prop) => prop !== 'scrolled',
|
|
|
|
})<{ scrolled?: boolean }>(({ theme, scrolled }) => ({
|
|
|
|
position: 'sticky',
|
|
|
|
top: 0,
|
2024-04-09 09:29:23 +02:00
|
|
|
zIndex: theme.zIndex.sticky,
|
2024-03-20 16:11:50 +01:00
|
|
|
padding: scrolled ? theme.spacing(2, 0) : theme.spacing(0, 0, 2),
|
|
|
|
background: theme.palette.background.application,
|
|
|
|
transition: 'padding 0.3s ease',
|
|
|
|
}));
|
2024-03-14 10:33:30 +01:00
|
|
|
|
2024-03-21 15:39:03 +01:00
|
|
|
export const Insights: VFC = () => {
|
2024-03-14 10:33:30 +01:00
|
|
|
const [scrolled, setScrolled] = useState(false);
|
2024-03-21 15:39:03 +01:00
|
|
|
const { insights, loading, error } = useInsights();
|
2024-03-04 12:56:17 +01:00
|
|
|
const stateConfig = {
|
|
|
|
projects: withDefault(ArrayParam, [allOption.id]),
|
|
|
|
};
|
2024-03-06 12:19:27 +01:00
|
|
|
const [state, setState] = usePersistentTableState('insights', stateConfig);
|
2024-03-04 12:56:17 +01:00
|
|
|
const setProjects = (projects: string[]) => {
|
|
|
|
setState({ projects });
|
|
|
|
};
|
|
|
|
const projects = state.projects
|
|
|
|
? (state.projects.filter(Boolean) as string[])
|
|
|
|
: [];
|
2024-03-15 09:37:02 +01:00
|
|
|
|
2024-03-21 15:39:03 +01:00
|
|
|
const insightsData = useInsightsData(insights, projects);
|
2024-01-30 10:07:16 +01:00
|
|
|
|
2024-03-14 10:33:30 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-01-22 11:07:38 +01:00
|
|
|
return (
|
|
|
|
<>
|
2024-03-14 10:33:30 +01:00
|
|
|
<StickyWrapper scrolled={scrolled}>
|
2024-03-21 15:39:03 +01:00
|
|
|
<InsightsHeader
|
2024-03-04 12:56:17 +01:00
|
|
|
actions={
|
|
|
|
<ProjectSelect
|
|
|
|
selectedProjects={projects}
|
|
|
|
onChange={setProjects}
|
|
|
|
dataTestId={'DASHBOARD_PROJECT_SELECT'}
|
2024-04-09 11:24:50 +02:00
|
|
|
sx={{
|
|
|
|
flex: 1,
|
|
|
|
maxWidth: '360px',
|
|
|
|
width: '100%',
|
|
|
|
}}
|
2024-03-04 12:56:17 +01:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2024-03-14 10:33:30 +01:00
|
|
|
</StickyWrapper>
|
2024-03-21 15:39:03 +01:00
|
|
|
<InsightsCharts
|
|
|
|
loading={loading}
|
|
|
|
projects={projects}
|
|
|
|
{...insightsData}
|
|
|
|
/>
|
2024-01-22 11:07:38 +01:00
|
|
|
</>
|
|
|
|
);
|
2024-01-18 12:32:25 +01:00
|
|
|
};
|