Skip to content

Commit

Permalink
Upgrade utils files to typescript (apache#25089)
Browse files Browse the repository at this point in the history
* Migrate utils to typescript.

* Use PropsWithChildren for typing props.
  • Loading branch information
pierrejeambrun authored Jul 15, 2022
1 parent b4e9c67 commit ec3b6fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
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

0 comments on commit ec3b6fc

Please sign in to comment.