Skip to content

Fix missing setTimeout cleanup in hero section collision effect #940

@MODSetter

Description

@MODSetter

Description

The hero section's collision useEffect creates two setTimeout calls that are never cleaned up. If the component unmounts or collision changes rapidly, these timers can fire on unmounted components or cause stale state updates.

Files to Update

  • surfsense_web/components/homepage/hero-section.tsx (lines 279–294)

What to Do

Store timeout IDs and clear them in the cleanup function:

// Before
useEffect(() => {
  if (collision.detected && collision.coordinates) {
    setTimeout(() => {
      setCollision({ detected: false, coordinates: null });
      setCycleCollisionDetected(false);
      if (beamRef.current) {
        beamRef.current.style.opacity = "1";
      }
    }, 2000);
    setTimeout(() => {
      setBeamKey((prevKey) => prevKey + 1);
    }, 2000);
  }
}, [collision]);

// After
useEffect(() => {
  if (!collision.detected || !collision.coordinates) return;

  const timer1 = setTimeout(() => {
    setCollision({ detected: false, coordinates: null });
    setCycleCollisionDetected(false);
    if (beamRef.current) {
      beamRef.current.style.opacity = "1";
    }
  }, 2000);

  const timer2 = setTimeout(() => {
    setBeamKey((prevKey) => prevKey + 1);
  }, 2000);

  return () => {
    clearTimeout(timer1);
    clearTimeout(timer2);
  };
}, [collision]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions