Files
Andreas Christou d7f7cd1e61 Schema: Initial Azure Monitor query schema (#62018)
* 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
2023-02-03 16:06:54 +00:00

41 lines
1.3 KiB
TypeScript

import { useState, useCallback, useMemo } from 'react';
import { AzureMonitorErrorish } from '../types';
import { messageFromElement } from './messageFromError';
type SourcedError = [string, AzureMonitorErrorish];
export default function useLastError() {
const [errors, setErrors] = useState<SourcedError[]>([]);
// Handles errors from any child components that request data to display their options
const addError = useCallback((errorSource: string, error: AzureMonitorErrorish | undefined) => {
setErrors((errors) => {
const errorsCopy = [...errors];
const index = errors.findIndex(([vSource]) => vSource === errorSource);
// If there's already an error, remove it. If we're setting a new error
// below, we'll move it to the front
if (index > -1) {
errorsCopy.splice(index, 1);
}
// And then add the new error to the top of the array. If error is defined, it was already
// removed above.
if (error) {
errorsCopy.unshift([errorSource, error]);
}
return errorsCopy;
});
}, []);
const errorMessage = useMemo(() => {
const recentError = errors[0];
return recentError && messageFromElement(recentError[1]);
}, [errors]);
return [errorMessage, addError] as const;
}