-
-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: event timeline persistent state (#8240)
https://linear.app/unleash/issue/2-2700/persist-timeline-state-in-local-storage Implements persistent state management for the event timeline using local storage. I believe this improves UX by persisting both the timeline toggle (visibility) state and applied filters across page refreshes. Includes some scouting/refactoring and some workarounds to prevent the timeline from animating on page load (in most cases).
- Loading branch information
Showing
5 changed files
with
146 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
frontend/src/component/events/EventTimeline/useEventTimeline.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useLocalStorageState } from 'hooks/useLocalStorageState'; | ||
import type { IEnvironment } from 'interfaces/environments'; | ||
|
||
export type TimeSpanOption = { | ||
key: string; | ||
label: string; | ||
value: Duration; | ||
markers: string[]; | ||
}; | ||
|
||
export const timeSpanOptions: TimeSpanOption[] = [ | ||
{ | ||
key: '30m', | ||
label: 'last 30 min', | ||
value: { minutes: 30 }, | ||
markers: ['30 min ago'], | ||
}, | ||
{ | ||
key: '1h', | ||
label: 'last hour', | ||
value: { hours: 1 }, | ||
markers: ['1 hour ago', '30 min ago'], | ||
}, | ||
{ | ||
key: '3h', | ||
label: 'last 3 hours', | ||
value: { hours: 3 }, | ||
markers: ['3 hours ago', '2 hours ago', '1 hour ago'], | ||
}, | ||
{ | ||
key: '12h', | ||
label: 'last 12 hours', | ||
value: { hours: 12 }, | ||
markers: ['12 hours ago', '9 hours ago', '6 hours ago', '3 hours ago'], | ||
}, | ||
{ | ||
key: '24h', | ||
label: 'last 24 hours', | ||
value: { hours: 24 }, | ||
markers: [ | ||
'24 hours ago', | ||
'18 hours ago', | ||
'12 hours ago', | ||
'6 hours ago', | ||
], | ||
}, | ||
{ | ||
key: '48h', | ||
label: 'last 48 hours', | ||
value: { hours: 48 }, | ||
markers: [ | ||
'48 hours ago', | ||
'36 hours ago', | ||
'24 hours ago', | ||
'12 hours ago', | ||
], | ||
}, | ||
]; | ||
|
||
type EventTimelineState = { | ||
open: boolean; | ||
timeSpan: TimeSpanOption; | ||
environment?: IEnvironment; | ||
}; | ||
|
||
const defaultState: EventTimelineState = { | ||
open: true, | ||
timeSpan: timeSpanOptions[0], | ||
}; | ||
|
||
export const useEventTimeline = () => { | ||
const [state, setState] = useLocalStorageState<EventTimelineState>( | ||
'event-timeline:v1', | ||
defaultState, | ||
); | ||
|
||
const setField = <K extends keyof EventTimelineState>( | ||
key: K, | ||
value: EventTimelineState[K], | ||
) => { | ||
setState((prevState) => ({ ...prevState, [key]: value })); | ||
}; | ||
|
||
return { | ||
...state, | ||
setOpen: (open: boolean) => setField('open', open), | ||
setTimeSpan: (timeSpan: TimeSpanOption) => | ||
setField('timeSpan', timeSpan), | ||
setEnvironment: (environment: IEnvironment) => | ||
setField('environment', environment), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
frontend/src/component/layout/MainLayout/MainLayoutEventTimeline.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Box } from '@mui/material'; | ||
import { EventTimeline } from 'component/events/EventTimeline/EventTimeline'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
interface IMainLayoutEventTimelineProps { | ||
open: boolean; | ||
} | ||
|
||
export const MainLayoutEventTimeline = ({ | ||
open, | ||
}: IMainLayoutEventTimelineProps) => { | ||
const [isInitialLoad, setIsInitialLoad] = useState(true); | ||
|
||
useEffect(() => { | ||
setIsInitialLoad(false); | ||
}, []); | ||
|
||
return ( | ||
<Box | ||
sx={{ | ||
overflow: 'hidden', | ||
transition: isInitialLoad | ||
? 'none' | ||
: 'max-height 0.3s ease-in-out', | ||
maxHeight: open ? '105px' : '0', | ||
}} | ||
> | ||
<Box | ||
sx={(theme) => ({ | ||
padding: theme.spacing(2), | ||
backgroundColor: theme.palette.background.paper, | ||
})} | ||
> | ||
<EventTimeline /> | ||
</Box> | ||
</Box> | ||
); | ||
}; |