Skip to content

Commit 1d2be8c

Browse files
committed
Refactor background check logic to compute time remaining on demand
1 parent 1cfc76a commit 1d2be8c

2 files changed

Lines changed: 37 additions & 32 deletions

File tree

frontend/src/App.tsx

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,8 @@ const WailBrewApp = () => {
153153

154154
// Background update checking state
155155
const [isBackgroundCheckRunning, setIsBackgroundCheckRunning] = useState<boolean>(false);
156-
const [timeUntilNextCheck, setTimeUntilNextCheck] = useState<number>(0);
157156
const lastKnownOutdatedCount = useRef<number>(0);
158157
const backgroundCheckInterval = useRef<ReturnType<typeof setInterval> | null>(null);
159-
const timeUpdateInterval = useRef<ReturnType<typeof setInterval> | null>(null);
160158
const nextCheckTime = useRef<number>(Date.now() + 15 * 60 * 1000); // 15 minutes from now
161159

162160
// Track update event listeners for cleanup (prevents duplicate listeners bug)
@@ -392,9 +390,6 @@ const WailBrewApp = () => {
392390
if (backgroundCheckInterval.current) {
393391
clearInterval(backgroundCheckInterval.current);
394392
}
395-
if (timeUpdateInterval.current) {
396-
clearInterval(timeUpdateInterval.current);
397-
}
398393
if (loadingTimerInterval.current) {
399394
clearInterval(loadingTimerInterval.current);
400395
}
@@ -558,13 +553,6 @@ const WailBrewApp = () => {
558553

559554
// Start background update checking
560555
const startBackgroundUpdateCheck = () => {
561-
// Update time until next check every second
562-
timeUpdateInterval.current = setInterval(() => {
563-
const now = Date.now();
564-
const timeRemaining = Math.max(0, nextCheckTime.current - now);
565-
setTimeUntilNextCheck(Math.floor(timeRemaining / 1000)); // Convert to seconds
566-
}, 1000);
567-
568556
// Perform initial check after a short delay
569557
setTimeout(() => {
570558
performBackgroundUpdateCheck();
@@ -576,17 +564,10 @@ const WailBrewApp = () => {
576564
}, 15 * 60 * 1000); // 15 minutes
577565
};
578566

579-
// Format time until next check
580-
const formatTimeUntilNextCheck = (seconds: number): string => {
581-
if (seconds <= 0) return t('backgroundCheck.checkingNow');
582-
583-
const minutes = Math.floor(seconds / 60);
584-
const remainingSeconds = seconds % 60;
585-
586-
if (minutes > 0) {
587-
return t('backgroundCheck.nextCheckIn', { minutes, seconds: remainingSeconds });
588-
}
589-
return t('backgroundCheck.nextCheckInSeconds', { seconds: remainingSeconds });
567+
// Get seconds until next background check (computed on demand, no re-renders)
568+
const getSecondsUntilNextCheck = (): number => {
569+
const timeRemaining = Math.max(0, nextCheckTime.current - Date.now());
570+
return Math.floor(timeRemaining / 1000);
590571
};
591572

592573
const checkAppUpdatesOnStartup = async () => {
@@ -2016,8 +1997,7 @@ const WailBrewApp = () => {
20161997
sidebarWidth={sidebarWidth}
20171998
sidebarRef={sidebarRef}
20181999
isBackgroundCheckRunning={isBackgroundCheckRunning}
2019-
timeUntilNextCheck={timeUntilNextCheck}
2020-
formatTimeUntilNextCheck={formatTimeUntilNextCheck}
2000+
getSecondsUntilNextCheck={getSecondsUntilNextCheck}
20212001
/>
20222002
<div
20232003
className="sidebar-resize-handle"

frontend/src/components/Sidebar.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ interface SidebarProps {
2121
sidebarWidth?: number;
2222
sidebarRef?: React.RefObject<HTMLElement | null>;
2323
isBackgroundCheckRunning?: boolean;
24-
timeUntilNextCheck?: number;
25-
formatTimeUntilNextCheck?: (seconds: number) => string;
24+
getSecondsUntilNextCheck?: () => number;
2625
}
2726

2827
const Sidebar: React.FC<SidebarProps> = ({
@@ -39,24 +38,50 @@ const Sidebar: React.FC<SidebarProps> = ({
3938
sidebarWidth,
4039
sidebarRef,
4140
isBackgroundCheckRunning = false,
42-
timeUntilNextCheck = 0,
43-
formatTimeUntilNextCheck,
41+
getSecondsUntilNextCheck,
4442
}) => {
4543
const { t, i18n } = useTranslation();
4644
const currentLanguage = mapToSupportedLanguage(i18n.resolvedLanguage ?? i18n.language);
4745
const [showTooltip, setShowTooltip] = useState(false);
4846
const [tooltipPosition, setTooltipPosition] = useState({ top: 0, left: 0 });
47+
const [tooltipText, setTooltipText] = useState("");
4948
const iconRef = useRef<HTMLDivElement>(null);
5049

51-
// Update tooltip position when showing
50+
// Format seconds into a readable countdown string
51+
const formatCountdown = (seconds: number): string => {
52+
if (seconds <= 0) return t('backgroundCheck.checkingNow');
53+
const minutes = Math.floor(seconds / 60);
54+
const remainingSeconds = seconds % 60;
55+
if (minutes > 0) {
56+
return t('backgroundCheck.nextCheckIn', { minutes, seconds: remainingSeconds });
57+
}
58+
return t('backgroundCheck.nextCheckInSeconds', { seconds: remainingSeconds });
59+
};
60+
61+
// Update tooltip position and start countdown only while tooltip is visible
5262
useEffect(() => {
5363
if (showTooltip && iconRef.current) {
5464
const rect = iconRef.current.getBoundingClientRect();
5565
setTooltipPosition({
5666
top: rect.bottom + 8,
5767
left: rect.left + rect.width / 2,
5868
});
69+
70+
// Compute initial value immediately
71+
if (getSecondsUntilNextCheck) {
72+
setTooltipText(formatCountdown(getSecondsUntilNextCheck()));
73+
}
74+
75+
// Update every second only while tooltip is visible
76+
const interval = setInterval(() => {
77+
if (getSecondsUntilNextCheck) {
78+
setTooltipText(formatCountdown(getSecondsUntilNextCheck()));
79+
}
80+
}, 1000);
81+
82+
return () => clearInterval(interval);
5983
}
84+
// eslint-disable-next-line react-hooks/exhaustive-deps
6085
}, [showTooltip]);
6186

6287
// Detect if user is on Mac
@@ -130,7 +155,7 @@ const Sidebar: React.FC<SidebarProps> = ({
130155
}}
131156
/>
132157
)}
133-
{showTooltip && formatTimeUntilNextCheck && ReactDOM.createPortal(
158+
{showTooltip && getSecondsUntilNextCheck && ReactDOM.createPortal(
134159
<div
135160
className="background-check-tooltip"
136161
style={{
@@ -141,7 +166,7 @@ const Sidebar: React.FC<SidebarProps> = ({
141166
zIndex: 99999,
142167
}}
143168
>
144-
{formatTimeUntilNextCheck(timeUntilNextCheck)}
169+
{tooltipText}
145170
</div>,
146171
document.body
147172
)}

0 commit comments

Comments
 (0)