Skip to content

refactor: extract shared useAsync hook#534

Open
gambtho wants to merge 3 commits intomainfrom
thgamble/use-async-hook
Open

refactor: extract shared useAsync hook#534
gambtho wants to merge 3 commits intomainfrom
thgamble/use-async-hook

Conversation

@gambtho
Copy link
Copy Markdown
Collaborator

@gambtho gambtho commented Mar 26, 2026

Summary

Closes #533

  • Adds a generic useAsync<T> hook (src/hooks/useAsync.ts) that encapsulates the loading/error/data useState pattern with built-in request-ID-based cancellation, used by 12+ hooks across the codebase
  • Migrates useChartData and usePipelineRuns as proof-of-concept consumers, reducing boilerplate by ~90 lines
  • Full test coverage (7 tests) for initial state, success, error, stale request cancellation, args passthrough, reset, and unmount cleanup

Details

The hook provides:

  • data, loading, error state management
  • execute(...args) for manual triggering
  • reset() to clear state
  • Automatic cancellation of stale requests via request ID counter
  • Unmount safety via mounted ref

Test plan

  • npm run tsc passes (pre-existing guidepup module errors only)
  • npm test -- --run passes (pre-existing failures only)
  • npx vitest run src/hooks/useAsync.test.ts — 7/7 tests pass
  • Scaling tab renders correctly (useChartData migration)
  • Deployments tab renders pipeline runs (usePipelineRuns migration)

Copilot AI review requested due to automatic review settings March 26, 2026 03:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a shared useAsync<T> React hook for standardizing async data/loading/error state handling with stale-request cancellation, and migrates a couple of existing hooks to validate the approach.

Changes:

  • Added generic useAsync hook with request-id-based stale request cancellation and reset().
  • Added dedicated unit tests for useAsync (success, error, cancellation, args passthrough, reset, unmount behavior).
  • Refactored useChartData and usePipelineRuns to use useAsync and remove bespoke cancellation/boilerplate.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
plugins/aks-desktop/src/hooks/useAsync.ts New shared async state hook with cancellation + reset.
plugins/aks-desktop/src/hooks/useAsync.test.ts Unit tests validating useAsync behavior.
plugins/aks-desktop/src/components/Scaling/hooks/useChartData.ts Migrates chart data fetching/caching hook to useAsync.
plugins/aks-desktop/src/components/Deployments/hooks/usePipelineRuns.ts Migrates GitHub pipeline runs hook to useAsync.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gambtho gambtho force-pushed the thgamble/use-async-hook branch from 4b91cbb to 3f2841c Compare March 26, 2026 03:26
Copilot AI review requested due to automatic review settings March 26, 2026 03:44
@gambtho gambtho force-pushed the thgamble/use-async-hook branch from 3f2841c to f40e9bb Compare March 26, 2026 03:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gambtho gambtho force-pushed the thgamble/use-async-hook branch from f40e9bb to aab9ed4 Compare March 26, 2026 05:49
gambtho added 2 commits March 26, 2026 01:52
Provides a reusable hook that handles loading, error, and data state for
async operations with race-condition protection and unmount safety.
Supports both immediate (run on mount) and deferred (manual execute) modes
with type-safe overloads.
Replaces hand-rolled loading/error/staleness logic with useAsync,
removing ~60 lines of boilerplate while preserving caching and
Prometheus query behavior.
@gambtho gambtho force-pushed the thgamble/use-async-hook branch from aab9ed4 to 4d0ccda Compare March 26, 2026 05:52
Copilot AI review requested due to automatic review settings March 26, 2026 06:01
@gambtho gambtho force-pushed the thgamble/use-async-hook branch from 4d0ccda to 1965ca9 Compare March 26, 2026 06:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Replaces manual signal-based cancellation and state management with
useAsync, simplifying the hook while preserving partial-failure handling
and sorted/truncated results.
@gambtho gambtho force-pushed the thgamble/use-async-hook branch from 1965ca9 to 1391ffb Compare March 26, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: extract shared useAsync hook for loading/error/data state management

2 participants