New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#RI-2752 - Auto-refresh #633
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
81 changes: 81 additions & 0 deletions
81
redisinsight/ui/src/pages/browser/components/auto-refresh/AutoRefresh.spec.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,81 @@ | ||
import React from 'react' | ||
import { instance, mock } from 'ts-mockito' | ||
import { fireEvent, screen, render } from 'uiSrc/utils/test-utils' | ||
import AutoRefresh, { DEFAULT_REFRESH_RATE, Props } from './AutoRefresh' | ||
|
||
const mockedProps = mock<Props>() | ||
|
||
const INLINE_ITEM_EDITOR = 'inline-item-editor' | ||
|
||
describe('AutoRefresh', () => { | ||
it('should render', () => { | ||
expect(render(<AutoRefresh {...instance(mockedProps)} />)).toBeTruthy() | ||
}) | ||
|
||
it('prop "displayText = true" should show Refresh text', () => { | ||
const { queryByTestId } = render(<AutoRefresh {...instance(mockedProps)} displayText />) | ||
|
||
expect(queryByTestId('refresh-message-label')).toBeInTheDocument() | ||
}) | ||
|
||
it('prop "displayText = false" should hide Refresh text', () => { | ||
const { queryByTestId } = render(<AutoRefresh {...instance(mockedProps)} displayText={false} />) | ||
|
||
expect(queryByTestId('refresh-message-label')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('should call onRefresh', () => { | ||
const onRefresh = jest.fn() | ||
render(<AutoRefresh {...instance(mockedProps)} onRefresh={onRefresh} testid="refresh-key-btn" />) | ||
|
||
fireEvent.click(screen.getByTestId('refresh-key-btn')) | ||
expect(onRefresh).toBeCalled() | ||
}) | ||
|
||
it('refresh text should contain "Last refresh" time with disabled auto-refresh', async () => { | ||
render(<AutoRefresh {...instance(mockedProps)} displayText />) | ||
|
||
expect(screen.getByTestId('refresh-message-label')).toHaveTextContent(/Last refresh:/i) | ||
expect(screen.getByTestId('refresh-message')).toHaveTextContent('now') | ||
}) | ||
|
||
it('refresh text should contain "Auto-refresh" time with enabled auto-refresh', async () => { | ||
render(<AutoRefresh {...instance(mockedProps)} displayText />) | ||
|
||
fireEvent.click(screen.getByTestId('auto-refresh-config-btn')) | ||
fireEvent.click(screen.getByTestId('auto-refresh-switch')) | ||
|
||
expect(screen.getByTestId('refresh-message-label')).toHaveTextContent(/Auto refresh:/i) | ||
expect(screen.getByTestId('refresh-message')).toHaveTextContent(DEFAULT_REFRESH_RATE) | ||
}) | ||
|
||
describe('AutoRefresh Config', () => { | ||
it('Auto refresh config should render', () => { | ||
const { queryByTestId } = render(<AutoRefresh {...instance(mockedProps)} />) | ||
|
||
fireEvent.click(screen.getByTestId('auto-refresh-config-btn')) | ||
expect(queryByTestId('auto-refresh-switch')).toBeInTheDocument() | ||
}) | ||
|
||
it('should call onRefresh after enable auto-refresh and set 1 sec', async () => { | ||
const onRefresh = jest.fn() | ||
render(<AutoRefresh {...instance(mockedProps)} onRefresh={onRefresh} />) | ||
|
||
fireEvent.click(screen.getByTestId('auto-refresh-config-btn')) | ||
fireEvent.click(screen.getByTestId('auto-refresh-switch')) | ||
fireEvent.click(screen.getByTestId('refresh-rate')) | ||
|
||
fireEvent.change(screen.getByTestId(INLINE_ITEM_EDITOR), { target: { value: '1' } }) | ||
expect(screen.getByTestId(INLINE_ITEM_EDITOR)).toHaveValue('1') | ||
|
||
screen.getByTestId(/apply-btn/).click() | ||
|
||
await new Promise((r) => setTimeout(r, 1100)) | ||
expect(onRefresh).toBeCalledTimes(1) | ||
await new Promise((r) => setTimeout(r, 1100)) | ||
expect(onRefresh).toBeCalledTimes(2) | ||
await new Promise((r) => setTimeout(r, 1100)) | ||
expect(onRefresh).toBeCalledTimes(3) | ||
}) | ||
}) | ||
}) |
239 changes: 239 additions & 0 deletions
239
redisinsight/ui/src/pages/browser/components/auto-refresh/AutoRefresh.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,239 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { EuiButtonIcon, EuiPopover, EuiSwitch, EuiTextColor, EuiToolTip } from '@elastic/eui' | ||
import cx from 'classnames' | ||
|
||
import { MIN_REFRESH_RATE, Nullable, truncateNumberToFirstUnit, validateRefreshRateNumber } from 'uiSrc/utils' | ||
import InlineItemEditor from 'uiSrc/components/inline-item-editor' | ||
import { localStorageService } from 'uiSrc/services' | ||
import { BrowserStorageItem } from 'uiSrc/constants' | ||
|
||
import styles from './styles.module.scss' | ||
|
||
export interface Props { | ||
postfix: string | ||
loading: boolean | ||
displayText: boolean | ||
lastRefreshTime: Nullable<number> | ||
testid?: string | ||
containerClassName?: string | ||
turnOffAutoRefresh?: boolean | ||
onRefresh: (enableAutoRefresh: boolean, refreshRate: string) => void | ||
} | ||
|
||
const NOW = 'now' | ||
export const DEFAULT_REFRESH_RATE = '5.0' | ||
const MINUTE = 60 | ||
const DURATION_FIRST_REFRESH_TIME = 5 | ||
const TIMEOUT_TO_UPDATE_REFRESH_TIME = 1_000 * MINUTE // once a minute | ||
|
||
const AutoRefresh = ({ | ||
postfix, | ||
loading, | ||
displayText, | ||
lastRefreshTime, | ||
containerClassName = '', | ||
testid = '', | ||
turnOffAutoRefresh, | ||
onRefresh, | ||
}: Props) => { | ||
let intervalText: NodeJS.Timeout | ||
let timeoutRefresh: NodeJS.Timeout | ||
|
||
const [refreshMessage, setRefreshMessage] = useState(NOW) | ||
const [isPopoverOpen, setIsPopoverOpen] = useState(false) | ||
const [refreshRate, setRefreshRate] = useState<string>('') | ||
const [refreshRateMessage, setRefreshRateMessage] = useState<string>('') | ||
const [enableAutoRefresh, setEnableAutoRefresh] = useState(false) | ||
const [editingRate, setEditingRate] = useState(false) | ||
|
||
const onButtonClick = () => setIsPopoverOpen((isPopoverOpen) => !isPopoverOpen) | ||
const closePopover = () => { | ||
setEnableAutoRefresh(enableAutoRefresh) | ||
setIsPopoverOpen(false) | ||
} | ||
|
||
useEffect(() => { | ||
const refreshRateStorage = localStorageService.get(BrowserStorageItem.autoRefreshRate + postfix) | ||
|| DEFAULT_REFRESH_RATE | ||
|
||
setRefreshRate(refreshRateStorage) | ||
}, [postfix]) | ||
|
||
useEffect(() => { | ||
if (turnOffAutoRefresh && enableAutoRefresh) { | ||
setEnableAutoRefresh(false) | ||
clearInterval(timeoutRefresh) | ||
} | ||
}, [turnOffAutoRefresh]) | ||
|
||
// update refresh label text | ||
useEffect(() => { | ||
const delta = getLastRefreshDelta(lastRefreshTime) | ||
updateLastRefresh() | ||
|
||
intervalText = setInterval(() => { | ||
if (document.hidden) return | ||
|
||
updateLastRefresh() | ||
}, delta < DURATION_FIRST_REFRESH_TIME ? DURATION_FIRST_REFRESH_TIME : TIMEOUT_TO_UPDATE_REFRESH_TIME) | ||
return () => clearInterval(intervalText) | ||
}, [lastRefreshTime]) | ||
|
||
// refresh interval | ||
useEffect(() => { | ||
updateLastRefresh() | ||
|
||
if (enableAutoRefresh && !loading) { | ||
timeoutRefresh = setInterval(() => { | ||
if (document.hidden) return | ||
|
||
handleRefresh() | ||
}, +refreshRate * 1_000) | ||
} else { | ||
clearInterval(timeoutRefresh) | ||
} | ||
|
||
if (enableAutoRefresh) { | ||
updateAutoRefreshText(refreshRate) | ||
} | ||
|
||
return () => clearInterval(timeoutRefresh) | ||
}, [enableAutoRefresh, refreshRate, loading, lastRefreshTime]) | ||
|
||
const getLastRefreshDelta = (time:Nullable<number>) => (Date.now() - (time || 0)) / 1_000 | ||
|
||
const updateLastRefresh = () => { | ||
const delta = getLastRefreshDelta(lastRefreshTime) | ||
let text = '' | ||
|
||
if (delta > MINUTE) { | ||
text = truncateNumberToFirstUnit((Date.now() - (lastRefreshTime || 0)) / 1_000) | ||
} | ||
if (delta < MINUTE) { | ||
text = '< 1 min' | ||
} | ||
if (delta < DURATION_FIRST_REFRESH_TIME) { | ||
text = NOW | ||
} | ||
|
||
lastRefreshTime && setRefreshMessage(text) | ||
} | ||
|
||
const updateAutoRefreshText = (refreshRate: string) => { | ||
enableAutoRefresh && setRefreshRateMessage( | ||
// more than 1 minute | ||
+refreshRate > MINUTE ? `${Math.floor(+refreshRate / MINUTE)} min` : `${refreshRate} s` | ||
) | ||
} | ||
|
||
const handleApplyAutoRefreshRate = (value: string) => { | ||
setRefreshRate(+value > MIN_REFRESH_RATE ? value : `${MIN_REFRESH_RATE}`) | ||
setEditingRate(false) | ||
localStorageService.set(BrowserStorageItem.autoRefreshRate + postfix, value) | ||
} | ||
|
||
const handleDeclineAutoRefreshRate = () => { | ||
setEditingRate(false) | ||
} | ||
|
||
const handleRefresh = () => { | ||
onRefresh(enableAutoRefresh, refreshRate) | ||
} | ||
|
||
const onChangeEnableAutoRefresh = (value: boolean) => { | ||
setEnableAutoRefresh(value) | ||
} | ||
|
||
return ( | ||
<div className={cx(styles.container, containerClassName, { [styles.enable]: enableAutoRefresh })}> | ||
<EuiTextColor className={styles.summary}> | ||
{displayText && ( | ||
<span data-testid="refresh-message-label">{`${enableAutoRefresh ? 'Auto refresh:' : 'Last refresh:'}`}</span> | ||
)} | ||
<span className={styles.time} data-testid="refresh-message"> | ||
{` ${enableAutoRefresh ? refreshRateMessage : refreshMessage}`} | ||
</span> | ||
</EuiTextColor> | ||
|
||
<EuiToolTip | ||
title="Last Refresh" | ||
className={styles.tooltip} | ||
position="top" | ||
content={refreshMessage} | ||
> | ||
<EuiButtonIcon | ||
iconType="refresh" | ||
disabled={loading} | ||
onClick={handleRefresh} | ||
onMouseEnter={updateLastRefresh} | ||
className={cx(styles.btn, { [styles.rolling]: enableAutoRefresh })} | ||
aria-labelledby={testid?.replaceAll?.('-', ' ') || 'Refresh button'} | ||
data-testid={testid || 'refresh-btn'} | ||
/> | ||
</EuiToolTip> | ||
|
||
<EuiPopover | ||
ownFocus={false} | ||
anchorPosition="downRight" | ||
isOpen={isPopoverOpen} | ||
anchorClassName={styles.anchorWrapper} | ||
panelClassName={cx('popover-without-top-tail', styles.popoverWrapper)} | ||
closePopover={closePopover} | ||
button={( | ||
<EuiButtonIcon | ||
iconType="arrowDown" | ||
color="subdued" | ||
aria-label="Auto-refresh config popover" | ||
className={cx(styles.anchorBtn, { [styles.anchorBtnOpen]: isPopoverOpen })} | ||
onClick={onButtonClick} | ||
data-testid="auto-refresh-config-btn" | ||
/> | ||
)} | ||
> | ||
<div className={styles.switch}> | ||
<EuiSwitch | ||
compressed | ||
label="Auto Refresh" | ||
checked={enableAutoRefresh} | ||
onChange={(e) => onChangeEnableAutoRefresh(e.target.checked)} | ||
className={styles.switchOption} | ||
data-testid="auto-refresh-switch" | ||
/> | ||
</div> | ||
<div className={styles.inputContainer}> | ||
<div className={styles.inputLabel}>Refresh rate:</div> | ||
{!editingRate && ( | ||
<EuiTextColor | ||
color="subdued" | ||
style={{ cursor: 'pointer' }} | ||
onClick={() => setEditingRate(true)} | ||
data-testid="refresh-rate" | ||
> | ||
{`${refreshRate} s`} | ||
</EuiTextColor> | ||
)} | ||
{editingRate && ( | ||
<> | ||
<div className={styles.input} data-testid="auto-refresh-rate-input"> | ||
<InlineItemEditor | ||
initialValue={refreshRate} | ||
fieldName="refreshRate" | ||
placeholder={DEFAULT_REFRESH_RATE} | ||
isLoading={loading} | ||
validation={validateRefreshRateNumber} | ||
onDecline={() => handleDeclineAutoRefreshRate()} | ||
onApply={(value) => handleApplyAutoRefreshRate(value)} | ||
/> | ||
</div> | ||
<EuiTextColor color="subdued">{' s'}</EuiTextColor> | ||
</> | ||
)} | ||
</div> | ||
|
||
</EuiPopover> | ||
|
||
</div> | ||
) | ||
} | ||
|
||
export default AutoRefresh |
5 changes: 5 additions & 0 deletions
5
redisinsight/ui/src/pages/browser/components/auto-refresh/index.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,5 @@ | ||
import AutoRefresh from './AutoRefresh' | ||
|
||
export * from './AutoRefresh' | ||
|
||
export default AutoRefresh |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe move this function (getTextByRefreshTime or smth) to utils?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved