MCPcopy Create free account
hub / github.com/dailydotdev/apps / useBlinkLinkedRow

Function useBlinkLinkedRow

packages/shared/src/features/boost/BoostManager.tsx:475–545  ·  view source on GitHub ↗
(campaignId?: string)

Source from the content-addressed store, hash-verified

473 * view when it is off screen, then blinked. Returns the id while it blinks.
474 */
475const useBlinkLinkedRow = (campaignId?: string): string | undefined => {
476 const [blinkingId, setBlinkingId] = useState<string>();
477 const doneIdRef = useRef<string>();
478 const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
479 const frameRef = useRef<number>();
480
481 // Re-renders don't cancel a pending blink, only leaving the page does
482 useEffect(
483 () => () => {
484 clearTimeout(timeoutRef.current);
485 cancelAnimationFrame(frameRef.current ?? 0);
486 },
487 [],
488 );
489
490 // Runs after every render until the row is on the page
491 useEffect(() => {
492 if (!campaignId || doneIdRef.current === campaignId) {
493 return;
494 }
495
496 const row = document.querySelector(`[data-campaign-id="${campaignId}"]`);
497 // Not laid out yet, like while dev styles load
498 if (!row?.getClientRects().length) {
499 return;
500 }
501
502 doneIdRef.current = campaignId;
503 const blink = () => {
504 cancelAnimationFrame(frameRef.current ?? 0);
505 setBlinkingId(campaignId);
506 timeoutRef.current = setTimeout(() => setBlinkingId(undefined), 1200);
507 };
508 const { top, bottom } = row.getBoundingClientRect();
509
510 if (top >= 0 && bottom <= window.innerHeight) {
511 blink();
512 return;
513 }
514
515 const reduceMotion = window.matchMedia(
516 '(prefers-reduced-motion: reduce)',
517 ).matches;
518 row.scrollIntoView({
519 block: 'center',
520 behavior: reduceMotion ? 'auto' : 'smooth',
521 });
522
523 // Blink once the page has moved and held still for a few frames (longer
524 // if it never moves, as smooth scrolling can start late), or after 1.5s
525 let lastY = window.scrollY;
526 let stillFrames = 0;
527 let hasMoved = false;
528 const waitForStill = () => {
529 const isStill = window.scrollY === lastY;
530 hasMoved = hasMoved || !isStill;
531 stillFrames = isStill ? stillFrames + 1 : 0;
532 lastY = window.scrollY;

Callers 1

BoostsListFunction · 0.85

Calls 1

blinkFunction · 0.85

Tested by

no test coverage detected