mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-15 17:50:48 +02:00
refactor: move useDashboardState into own file
This commit is contained in:
parent
e2354dcceb
commit
3a295d7344
@ -18,10 +18,6 @@ import { WelcomeDialog } from './WelcomeDialog';
|
||||
import { useLocalStorageState } from 'hooks/useLocalStorageState';
|
||||
import { usePersonalDashboard } from 'hooks/api/getters/usePersonalDashboard/usePersonalDashboard';
|
||||
import { getFeatureTypeIcons } from 'utils/getFeatureTypeIcons';
|
||||
import type {
|
||||
PersonalDashboardSchemaFlagsItem,
|
||||
PersonalDashboardSchemaProjectsItem,
|
||||
} from '../../openapi';
|
||||
import { usePersonalDashboardProjectDetails } from 'hooks/api/getters/usePersonalDashboard/usePersonalDashboardProjectDetails';
|
||||
import useLoading from '../../hooks/useLoading';
|
||||
import { MyProjects } from './MyProjects';
|
||||
@ -38,6 +34,7 @@ import ExpandMore from '@mui/icons-material/ExpandMore';
|
||||
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
|
||||
import useSplashApi from 'hooks/api/actions/useSplashApi/useSplashApi';
|
||||
import { useAuthSplash } from 'hooks/api/getters/useAuth/useAuthSplash';
|
||||
import { useDashboardState } from './useDashboardState';
|
||||
|
||||
export const StyledCardTitle = styled('div')<{ lines?: number }>(
|
||||
({ theme, lines = 2 }) => ({
|
||||
@ -109,96 +106,6 @@ const FlagListItem: FC<{
|
||||
);
|
||||
};
|
||||
|
||||
// todo: move into own file
|
||||
const useDashboardState = (
|
||||
projects: PersonalDashboardSchemaProjectsItem[],
|
||||
flags: PersonalDashboardSchemaFlagsItem[],
|
||||
) => {
|
||||
type State = {
|
||||
activeProject: string | undefined;
|
||||
activeFlag: PersonalDashboardSchemaFlagsItem | undefined;
|
||||
expandProjects: boolean;
|
||||
expandFlags: boolean;
|
||||
};
|
||||
|
||||
const defaultState: State = {
|
||||
activeProject: undefined,
|
||||
activeFlag: undefined,
|
||||
expandProjects: true,
|
||||
expandFlags: true,
|
||||
};
|
||||
|
||||
const [state, setState] = useLocalStorageState<State>(
|
||||
'personal-dashboard:v1',
|
||||
defaultState,
|
||||
);
|
||||
|
||||
const updateState = (newState: Partial<State>) =>
|
||||
setState({ ...defaultState, ...state, ...newState });
|
||||
|
||||
useEffect(() => {
|
||||
const updates: Partial<State> = {};
|
||||
const setDefaultFlag =
|
||||
flags.length &&
|
||||
(!state.activeFlag ||
|
||||
!flags.some((flag) => flag.name === state.activeFlag?.name));
|
||||
|
||||
if (setDefaultFlag) {
|
||||
updates.activeFlag = flags[0];
|
||||
}
|
||||
|
||||
const setDefaultProject =
|
||||
projects.length &&
|
||||
(!state.activeProject ||
|
||||
!projects.some(
|
||||
(project) => project.id === state.activeProject,
|
||||
));
|
||||
|
||||
if (setDefaultProject) {
|
||||
updates.activeProject = projects[0].id;
|
||||
}
|
||||
|
||||
if (Object.keys(updates).length) {
|
||||
updateState(updates);
|
||||
}
|
||||
}, [
|
||||
JSON.stringify(projects, null, 2),
|
||||
JSON.stringify(flags, null, 2),
|
||||
JSON.stringify(state, null, 2),
|
||||
]);
|
||||
|
||||
const { activeFlag, activeProject } = state;
|
||||
|
||||
const setActiveFlag = (flag: PersonalDashboardSchemaFlagsItem) => {
|
||||
updateState({
|
||||
activeFlag: flag,
|
||||
});
|
||||
};
|
||||
|
||||
const setActiveProject = (projectId: string) => {
|
||||
updateState({
|
||||
activeProject: projectId,
|
||||
});
|
||||
};
|
||||
|
||||
const toggleSectionState = (section: 'flags' | 'projects') => {
|
||||
const property = section === 'flags' ? 'expandFlags' : 'expandProjects';
|
||||
updateState({
|
||||
[property]: !(state[property] ?? true),
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
activeFlag,
|
||||
setActiveFlag,
|
||||
activeProject,
|
||||
setActiveProject,
|
||||
expandFlags: state.expandFlags ?? true,
|
||||
expandProjects: state.expandProjects ?? true,
|
||||
toggleSectionState,
|
||||
};
|
||||
};
|
||||
|
||||
const WelcomeSection = styled('div')(({ theme }) => ({
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
|
@ -0,0 +1,95 @@
|
||||
import { useLocalStorageState } from 'hooks/useLocalStorageState';
|
||||
import type {
|
||||
PersonalDashboardSchemaFlagsItem,
|
||||
PersonalDashboardSchemaProjectsItem,
|
||||
} from 'openapi';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export const useDashboardState = (
|
||||
projects: PersonalDashboardSchemaProjectsItem[],
|
||||
flags: PersonalDashboardSchemaFlagsItem[],
|
||||
) => {
|
||||
type State = {
|
||||
activeProject: string | undefined;
|
||||
activeFlag: PersonalDashboardSchemaFlagsItem | undefined;
|
||||
expandProjects: boolean;
|
||||
expandFlags: boolean;
|
||||
};
|
||||
|
||||
const defaultState: State = {
|
||||
activeProject: undefined,
|
||||
activeFlag: undefined,
|
||||
expandProjects: true,
|
||||
expandFlags: true,
|
||||
};
|
||||
|
||||
const [state, setState] = useLocalStorageState<State>(
|
||||
'personal-dashboard:v1',
|
||||
defaultState,
|
||||
);
|
||||
|
||||
const updateState = (newState: Partial<State>) =>
|
||||
setState({ ...defaultState, ...state, ...newState });
|
||||
|
||||
useEffect(() => {
|
||||
const updates: Partial<State> = {};
|
||||
const setDefaultFlag =
|
||||
flags.length &&
|
||||
(!state.activeFlag ||
|
||||
!flags.some((flag) => flag.name === state.activeFlag?.name));
|
||||
|
||||
if (setDefaultFlag) {
|
||||
updates.activeFlag = flags[0];
|
||||
}
|
||||
|
||||
const setDefaultProject =
|
||||
projects.length &&
|
||||
(!state.activeProject ||
|
||||
!projects.some(
|
||||
(project) => project.id === state.activeProject,
|
||||
));
|
||||
|
||||
if (setDefaultProject) {
|
||||
updates.activeProject = projects[0].id;
|
||||
}
|
||||
|
||||
if (Object.keys(updates).length) {
|
||||
updateState(updates);
|
||||
}
|
||||
}, [
|
||||
JSON.stringify(projects, null, 2),
|
||||
JSON.stringify(flags, null, 2),
|
||||
JSON.stringify(state, null, 2),
|
||||
]);
|
||||
|
||||
const { activeFlag, activeProject } = state;
|
||||
|
||||
const setActiveFlag = (flag: PersonalDashboardSchemaFlagsItem) => {
|
||||
updateState({
|
||||
activeFlag: flag,
|
||||
});
|
||||
};
|
||||
|
||||
const setActiveProject = (projectId: string) => {
|
||||
updateState({
|
||||
activeProject: projectId,
|
||||
});
|
||||
};
|
||||
|
||||
const toggleSectionState = (section: 'flags' | 'projects') => {
|
||||
const property = section === 'flags' ? 'expandFlags' : 'expandProjects';
|
||||
updateState({
|
||||
[property]: !(state[property] ?? true),
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
activeFlag,
|
||||
setActiveFlag,
|
||||
activeProject,
|
||||
setActiveProject,
|
||||
expandFlags: state.expandFlags ?? true,
|
||||
expandProjects: state.expandProjects ?? true,
|
||||
toggleSectionState,
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user