-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add optional refresh cooldown for non-admin users #7714
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
Open
seungoh-lee
wants to merge
5
commits into
getredash:master
Choose a base branch
from
seungoh-lee:feature/refresh-cooldown
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7e0fc2
feat : refresh cooldwon
seungoh-lee 7446b4e
test: cover non-admin refresh cooldown; default to disabled
seungoh-lee 23923f5
perf(refresh-cooldown): scope cooldown countdown to a leaf component
seungoh-lee 99d9e88
Merge branch 'master' into feature/refresh-cooldown
seungoh-lee 049d839
Merge branch 'master' into feature/refresh-cooldown
seungoh-lee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,75 @@ | ||
| import { useState, useEffect, useRef, useCallback } from "react"; | ||
| import { currentUser, clientConfig } from "@/services/auth"; | ||
| import notification from "@/services/notification"; | ||
|
|
||
| function getCooldownSeconds() { | ||
| return currentUser.isAdmin ? 0 : clientConfig.nonAdminRefreshCooldown || 0; | ||
| } | ||
|
|
||
| function warnCooldown(seconds) { | ||
| notification.warning(`Refresh is on cooldown. Please wait ${seconds} seconds.`, null, { duration: 3 }); | ||
| } | ||
|
|
||
| // Remaining cooldown in seconds for a given retrieval time. Pure — no React state — so the | ||
| // execution/refresh path can gate on click without subscribing to a per-second timer. | ||
| export function getRefreshCooldownRemaining(retrievedAt) { | ||
| const cooldownSeconds = getCooldownSeconds(); | ||
| if (cooldownSeconds <= 0 || !retrievedAt) return 0; | ||
| const retrievedTime = new Date(retrievedAt).getTime(); | ||
| if (!retrievedTime) return 0; | ||
| const elapsed = Math.floor((Date.now() - retrievedTime) / 1000); | ||
| return Math.max(0, cooldownSeconds - elapsed); | ||
| } | ||
|
|
||
| // Shows the cooldown notification when active. Returns true if on cooldown (caller should abort). | ||
| export function notifyRefreshCooldown(retrievedAt) { | ||
| const remaining = getRefreshCooldownRemaining(retrievedAt); | ||
| if (remaining > 0) { | ||
| warnCooldown(remaining); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // Ticking countdown for display. Mount inside a small leaf component (see RefreshCooldownLabel) | ||
| // so the per-second state update only re-renders that label, not the whole page. | ||
| export default function useRefreshCooldown(retrievedAt) { | ||
| const [remainingTime, setRemainingTime] = useState(() => getRefreshCooldownRemaining(retrievedAt)); | ||
| const timerRef = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| if (timerRef.current) clearInterval(timerRef.current); | ||
|
|
||
| const remaining = getRefreshCooldownRemaining(retrievedAt); | ||
| setRemainingTime(remaining); | ||
|
|
||
| if (remaining > 0) { | ||
| timerRef.current = setInterval(() => { | ||
| const r = getRefreshCooldownRemaining(retrievedAt); | ||
| setRemainingTime(r); | ||
| if (r <= 0) { | ||
| clearInterval(timerRef.current); | ||
| timerRef.current = null; | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| return () => { | ||
| if (timerRef.current) clearInterval(timerRef.current); | ||
| }; | ||
| }, [retrievedAt]); | ||
|
|
||
| const isCooldownActive = remainingTime > 0; | ||
|
|
||
| const notifyCooldown = useCallback(() => { | ||
| if (isCooldownActive) { | ||
| warnCooldown(remainingTime); | ||
| } | ||
| }, [isCooldownActive, remainingTime]); | ||
|
|
||
| return { | ||
| isCooldownActive, | ||
| remainingTime, | ||
| notifyCooldown, | ||
| }; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
21 changes: 21 additions & 0 deletions
21
client/app/pages/queries/components/RefreshCooldownLabel.jsx
This file contains hidden or 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,21 @@ | ||
| import React from "react"; | ||
| import PropTypes from "prop-types"; | ||
| import useRefreshCooldown from "@/lib/hooks/useRefreshCooldown"; | ||
|
|
||
| // Button label that appends a live `(Ns)` countdown while a refresh cooldown is active. | ||
| // It owns the per-second countdown state, so during cooldown only this label re-renders — | ||
| // the surrounding page (editor, visualizations) stays off the 1Hz render path. | ||
| export default function RefreshCooldownLabel({ retrievedAt, label, children }) { | ||
| const { isCooldownActive, remainingTime } = useRefreshCooldown(retrievedAt); | ||
| return <React.Fragment>{isCooldownActive ? `${label} (${remainingTime}s)` : children}</React.Fragment>; | ||
| } | ||
|
|
||
| RefreshCooldownLabel.propTypes = { | ||
| retrievedAt: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
| label: PropTypes.string.isRequired, | ||
| children: PropTypes.node.isRequired, | ||
| }; | ||
|
|
||
| RefreshCooldownLabel.defaultProps = { | ||
| retrievedAt: null, | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.