Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade utils files to typescript #25089

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion airflow/www/static/js/api/useGridData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const useGridData = () => {
// only refetch if the refresh switch is on
refetchInterval: isRefreshOn && (autoRefreshInterval || 1) * 1000,
keepPreviousData: true,
onError: (error) => {
onError: (error: Error) => {
stopRefresh();
errorToast({
title: 'Auto-refresh Error',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import React from 'react';
import React, { PropsWithChildren } from 'react';
import { ChakraProvider, Table, Tbody } from '@chakra-ui/react';
import { QueryClient, QueryClientProvider } from 'react-query';
import { MemoryRouter } from 'react-router-dom';
Expand All @@ -26,7 +26,7 @@ import { ContainerRefProvider } from '../context/containerRef';
import { TimezoneProvider } from '../context/timezone';
import { AutoRefreshProvider } from '../context/autorefresh';

export const Wrapper = ({ children }) => {
export const Wrapper = ({ children }: PropsWithChildren) => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
Expand All @@ -52,13 +52,13 @@ export const Wrapper = ({ children }) => {
);
};

export const ChakraWrapper = ({ children }) => (
export const ChakraWrapper = ({ children }: PropsWithChildren) => (
<ChakraProvider>
{children}
</ChakraProvider>
);

export const TableWrapper = ({ children }) => (
export const TableWrapper = ({ children }: PropsWithChildren) => (
<Wrapper>
<Table>
<Tbody>
Expand All @@ -68,7 +68,7 @@ export const TableWrapper = ({ children }) => (
</Wrapper>
);

export const RouterWrapper = ({ children }) => (
export const RouterWrapper = ({ children }: PropsWithChildren) => (
<MemoryRouter>
{children}
</MemoryRouter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,35 @@
*/

import { useToast } from '@chakra-ui/react';
import type { ReactNode } from 'react';

export const getErrorDescription = (error, fallbackMessage) => {
if (error && error.response && error.response.data) {
return error.response.data;
interface ErrorObj {
response: {
data: string,
}
if (error instanceof Error) return error.message;
}

type ErrorType = Error | string | ErrorObj | null;

export const getErrorDescription = (error?: ErrorType, fallbackMessage?: string) => {
if (typeof error === 'string') return error;
if (error instanceof Error) return error.message;
if (error?.response?.data) {
return error.response.data;
}
return fallbackMessage || 'Something went wrong.';
};

const getErrorTitle = (error) => (error.message || 'Error');
const getErrorTitle = (error: Error) => (error.message || 'Error');

const useErrorToast = () => {
const toast = useToast();
// Add an error prop and handle it as a description
return ({ error, ...rest }) => {
return ({ error, title, ...rest }: { error: Error, title?: ReactNode }) => {
toast({
...rest,
status: 'error',
title: getErrorTitle(error),
title: title || getErrorTitle(error),
description: getErrorDescription(error).slice(0, 500),
});
};
Expand Down