Implement a reusable usePolling hook to replace ad-hoc intervals
Description
src/app/stats/page.tsx re-implements a setInterval polling loop with manual cancelled flags and clearInterval cleanup, and any future auto-refresh page (events, admin) would copy the same boilerplate. The repo already has a clean fetch state machine in src/lib/useApi.ts but nothing for repeated polling. This issue extracts a tested usePolling hook.
Requirements and context
- Repository scope: Agentpay-Org/Agentpay-frontend only.
- Create
src/lib/usePolling.ts exporting a hook that fetches on an interval, supports pause/resume, exposes loading | error | ok state plus a lastUpdated timestamp, and cancels on unmount.
- Refactor
src/app/stats/page.tsx to consume it, preserving the existing 5s cadence and observable behaviour.
- Reuse
apiGet under the hood; do not introduce a new fetch path.
- Keep cancellation semantics consistent with
useApi (no state updates after unmount).
Suggested execution
- Fork the repo and create a branch
git checkout -b refactor/lib-use-polling-hook
- Implement changes
- Write code in: create
src/lib/usePolling.ts; refactor src/app/stats/page.tsx.
- Write comprehensive tests in: create
src/lib/__tests__/usePolling.test.tsx — fake timers, assert repeated fetches, pause stops them, error surfaces, and unmount clears the interval.
- Add documentation: add a JSDoc usage example in
usePolling.ts.
- Validate no act() warnings and deterministic teardown.
- Test and commit
Test and commit
- Run
npm run lint, npm run typecheck, npm test, and npm run build.
- Cover edge cases: pause before first tick, error then recovery, and rapid unmount.
- Include the
npm test output and coverage for the hook.
Example commit message
refactor(lib): add usePolling hook and adopt it in the stats page
Guidelines
- Minimum 95 percent test coverage for the hook and changed page.
- Clear, reviewer-focused documentation.
- Timeframe: 96 hours.
Community & contribution rewards
- 💬 Join the AgentPay community on Discord for questions, reviews, and faster merges: https://discord.gg/eXvRKkgcv
- ⭐ This is a GrantFox OSS / Official Campaign task and may be rewarded. When your PR is merged you'll be prompted to rate the project — if this issue and the maintainers helped you ship, we'd be grateful for a 5-star rating. Clear questions in Discord and tidy, well-tested PRs are the fastest path to a merge and a reward.
Implement a reusable usePolling hook to replace ad-hoc intervals
Description
src/app/stats/page.tsxre-implements asetIntervalpolling loop with manualcancelledflags andclearIntervalcleanup, and any future auto-refresh page (events, admin) would copy the same boilerplate. The repo already has a clean fetch state machine insrc/lib/useApi.tsbut nothing for repeated polling. This issue extracts a testedusePollinghook.Requirements and context
src/lib/usePolling.tsexporting a hook that fetches on an interval, supports pause/resume, exposesloading | error | okstate plus alastUpdatedtimestamp, and cancels on unmount.src/app/stats/page.tsxto consume it, preserving the existing 5s cadence and observable behaviour.apiGetunder the hood; do not introduce a new fetch path.useApi(no state updates after unmount).Suggested execution
git checkout -b refactor/lib-use-polling-hooksrc/lib/usePolling.ts; refactorsrc/app/stats/page.tsx.src/lib/__tests__/usePolling.test.tsx— fake timers, assert repeated fetches, pause stops them, error surfaces, and unmount clears the interval.usePolling.ts.Test and commit
npm run lint,npm run typecheck,npm test, andnpm run build.npm testoutput and coverage for the hook.Example commit message
refactor(lib): add usePolling hook and adopt it in the stats pageGuidelines
Community & contribution rewards