Skip to content

Commit 743e1f4

Browse files
committed
Another attempt at updating tracking status
1. Added `useSignalEffect`: Tracks when computed signals (totalTrackersPillText, totalTrackersBlocked, cookiePopUpBlocked) change. 2. Used a ref to track previous values: prevValuesRef stores the last values to avoid unnecessary re-renders. 3. State update on change: When computed values change, update state via setRenderKey to trigger a re-render. 4. Conditional updates: Only update state when values actually change, reducing unnecessary re-renders. How it works: • useSignalEffect runs whenever the computed signals change. • It compares current values with previous values stored in the ref. • If values changed, it updates the ref and calls setRenderKey to force a re-render. • The re-render causes TickPill to display the updated values.
1 parent b202ed4 commit 743e1f4

File tree

1 file changed

+39
-2
lines changed
  • special-pages/pages/new-tab/app/activity/components

1 file changed

+39
-2
lines changed

special-pages/pages/new-tab/app/activity/components/Activity.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styles from './Activity.module.css';
33
// @todo legacyProtections: `stylesLegacy` can be removed once all platforms
44
// are ready for the new Protections Report
55
import stylesLegacy from './ActivityLegacy.module.css';
6-
import { useContext, useEffect, useRef } from 'preact/hooks';
6+
import { useContext, useEffect, useRef, useState } from 'preact/hooks';
77
import { memo } from 'preact/compat';
88
import { ActivityContext, ActivityServiceContext } from '../ActivityProvider.js';
99
import { useTypedTranslationWith } from '../../types.js';
@@ -14,7 +14,7 @@ import { Trans } from '../../../../../shared/components/TranslationsProvider.js'
1414
import { ActivityItem, ActivityItemLegacy } from './ActivityItem.js';
1515
import { ActivityBurningSignalContext, BurnProvider } from '../../burning/BurnProvider.js';
1616
import { useEnv } from '../../../../../shared/components/EnvironmentProvider.js';
17-
import { useComputed } from '@preact/signals';
17+
import { useComputed, useSignalEffect } from '@preact/signals';
1818
import { ActivityItemAnimationWrapper } from './ActivityItemAnimationWrapper.js';
1919
import { useDocumentVisibility } from '../../../../../shared/components/DocumentVisibility.js';
2020
import { HistoryItems, HistoryItemsLegacy } from './HistoryItems.js';
@@ -254,6 +254,7 @@ function TrackerStatus({ id, trackersFound }) {
254254
const { activity } = useContext(NormalizedDataContext);
255255

256256
// Use computed to reactively track trackingStatus changes
257+
// Access activity.value directly to ensure Preact Signals tracks it properly
257258
// This ensures the component updates when trackingStatus is populated via activity_onDataUpdate
258259
// or activity_onDataPatch messages
259260
const trackingStatus = useComputed(() => {
@@ -277,6 +278,42 @@ function TrackerStatus({ id, trackersFound }) {
277278
});
278279
const cookiePopUpBlocked = useComputed(() => activity.value.cookiePopUpBlocked?.[id]);
279280

281+
// Use a ref to track previous values and state to force re-renders
282+
// This ensures TickPill updates when trackingStatus data arrives
283+
// Initialize ref with current values to avoid false positives on first render
284+
const prevValuesRef = useRef({
285+
pillText: totalTrackersPillText.value,
286+
blocked: totalTrackersBlocked.value,
287+
cookieBlocked: !!cookiePopUpBlocked.value,
288+
});
289+
const [, setRenderKey] = useState(0);
290+
291+
// Track computed signal changes and update state only when values actually change
292+
// This ensures the component re-renders when trackingStatus data arrives
293+
useSignalEffect(() => {
294+
// Access computed values to ensure Preact Signals tracks them
295+
const currentPillText = totalTrackersPillText.value;
296+
const currentBlocked = totalTrackersBlocked.value;
297+
const currentCookieBlocked = !!cookiePopUpBlocked.value;
298+
299+
const prev = prevValuesRef.current;
300+
301+
// Only update state if values have actually changed
302+
if (
303+
prev.pillText !== currentPillText ||
304+
prev.blocked !== currentBlocked ||
305+
prev.cookieBlocked !== currentCookieBlocked
306+
) {
307+
prevValuesRef.current = {
308+
pillText: currentPillText,
309+
blocked: currentBlocked,
310+
cookieBlocked: currentCookieBlocked,
311+
};
312+
// Update state to force re-render
313+
setRenderKey((prev) => prev + 1);
314+
}
315+
});
316+
280317
return (
281318
<div class={styles.companiesIconRow} data-testid="TrackerStatus">
282319
<div class={styles.companiesText}>

0 commit comments

Comments
 (0)