mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 21:32:20 +08:00

* Initial schema - Add types based off of current frontend * Rename and field-level comments * Update report and regenerate files * Rename frontend Azure folder - Doing this for consistency and to ensure code-generation works - Update betterer results due to file renames * Remove default and add back enum vals that I deleted * Set workspace prop as optional * Replace template variable types * Connect frontend query types - Keep properties optional for now to avoid major changes - Rename AzureMetricResource - Correctly use ResultFormat * Add TSVeneer decorator * Update schema * Update type * Update CODEOWNERS * Fix gen-cue issue * Fix backend test * Fix e2e test * Update code coverage * Remove references to old Azure Monitor path * Review * Regen files
25 lines
785 B
TypeScript
25 lines
785 B
TypeScript
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
export function useAsyncState<T>(asyncFn: () => Promise<T>, setError: Function, dependencies: unknown[]) {
|
|
// Use the lazy initial state functionality of useState to assign a random ID to the API call
|
|
// to track where errors come from. See useLastError.
|
|
const [errorSource] = useState(() => Math.random());
|
|
const [value, setValue] = useState<T>();
|
|
|
|
const finalValue = useMemo(() => value ?? [], [value]);
|
|
|
|
useEffect(() => {
|
|
asyncFn()
|
|
.then((results) => {
|
|
setValue(results);
|
|
setError(errorSource, undefined);
|
|
})
|
|
.catch((err) => {
|
|
setError(errorSource, err);
|
|
});
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, dependencies);
|
|
|
|
return finalValue;
|
|
}
|