chore(ui): delete unused usePageVisible hook
Added as a reusable primitive during Plan 03 Task 9, but the intended consumer (NotificationBell live-region refresh) was removed during code review, leaving the hook unused. Delete it — YAGNI; reintroduce when a real consumer shows up. Verified upstream impact (gitnexus): 0 callers, LOW risk. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,30 +0,0 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest';
|
|
||||||
import { renderHook, act } from '@testing-library/react';
|
|
||||||
import { usePageVisible } from './usePageVisible';
|
|
||||||
|
|
||||||
describe('usePageVisible', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
Object.defineProperty(document, 'visibilityState', {
|
|
||||||
value: 'visible',
|
|
||||||
configurable: true,
|
|
||||||
writable: true,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns true when visible, false when hidden', () => {
|
|
||||||
const { result } = renderHook(() => usePageVisible());
|
|
||||||
expect(result.current).toBe(true);
|
|
||||||
|
|
||||||
act(() => {
|
|
||||||
Object.defineProperty(document, 'visibilityState', { value: 'hidden', configurable: true });
|
|
||||||
document.dispatchEvent(new Event('visibilitychange'));
|
|
||||||
});
|
|
||||||
expect(result.current).toBe(false);
|
|
||||||
|
|
||||||
act(() => {
|
|
||||||
Object.defineProperty(document, 'visibilityState', { value: 'visible', configurable: true });
|
|
||||||
document.dispatchEvent(new Event('visibilitychange'));
|
|
||||||
});
|
|
||||||
expect(result.current).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tracks Page Visibility API state for the current document.
|
|
||||||
*
|
|
||||||
* Returns `true` when the tab is visible, `false` when hidden. Useful for
|
|
||||||
* pausing work (polling, animations, expensive DOM effects) while the tab
|
|
||||||
* is backgrounded. SSR-safe: defaults to `true` when `document` is undefined.
|
|
||||||
*/
|
|
||||||
export function usePageVisible(): boolean {
|
|
||||||
const [visible, setVisible] = useState(() =>
|
|
||||||
typeof document === 'undefined' ? true : document.visibilityState === 'visible',
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const onChange = () => setVisible(document.visibilityState === 'visible');
|
|
||||||
document.addEventListener('visibilitychange', onChange);
|
|
||||||
return () => document.removeEventListener('visibilitychange', onChange);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return visible;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user