From 3530d600fdecf7ddbc6caa610e47311af2c48d52 Mon Sep 17 00:00:00 2001 From: Jack Kavanagh Date: Thu, 14 Mar 2024 09:15:53 +0100 Subject: [PATCH] harden lint rule about array indexes (#6758) * harden rule about array indexes * simplify error boundary --- .eslintrc.js | 3 +- .../src/ui/components/error-boundary.tsx | 58 +++++-------------- .../src/ui/components/modals/error-modal.tsx | 4 +- 3 files changed, 18 insertions(+), 47 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 04f5d0efe5..1fd386fc05 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,7 +5,6 @@ const { TYPESCRIPT_CONVERSION, TYPESCRIPT_EXTENSION, UNKNOWN, - WARN, } = require('eslint-config-helpers'); /** @type { import('eslint').Linter.Config } */ @@ -119,7 +118,7 @@ module.exports = { 'react/jsx-closing-bracket-location': [ERROR, 'line-aligned'], 'react/prefer-stateless-function': ERROR, 'react/jsx-key': [ERROR, { 'checkFragmentShorthand': true }], - 'react/no-array-index-key': WARN(UNKNOWN), + 'react/no-array-index-key': ERROR, 'react/self-closing-comp': ERROR, 'react-hooks/exhaustive-deps': [ERROR, { diff --git a/packages/insomnia/src/ui/components/error-boundary.tsx b/packages/insomnia/src/ui/components/error-boundary.tsx index 36843949db..447faf08e1 100644 --- a/packages/insomnia/src/ui/components/error-boundary.tsx +++ b/packages/insomnia/src/ui/components/error-boundary.tsx @@ -6,9 +6,6 @@ interface Props { children: ReactNode; errorClassName?: string; showAlert?: boolean; - // Avoid using invalidation with showAlert, otherwise an alert will be shown with every attempted re-render - invalidationKey?: string; - renderError?: (error: Error) => ReactNode; } interface State { @@ -24,21 +21,6 @@ class SingleErrorBoundary extends PureComponent { info: null, }; - // eslint-disable-next-line camelcase - UNSAFE_componentWillReceiveProps(nextProps: Props) { - const { error, info } = this.state; - const invalidationKeyChanged = nextProps.invalidationKey !== this.props.invalidationKey; - const isErrored = error !== null || info !== null; - const shouldResetError = invalidationKeyChanged && isErrored; - - if (shouldResetError) { - this.setState({ - error: null, - info: null, - }); - } - } - componentDidCatch( error: Error, info: { @@ -61,7 +43,6 @@ class SingleErrorBoundary extends PureComponent { showError({ error, title: 'Application Error', - // @ts-expect-error -- TSCONVERSION message: (

Failed to render {componentName}. Please report the error to our Github Issues @@ -75,35 +56,26 @@ class SingleErrorBoundary extends PureComponent { } render() { - const { error, info } = this.state; - const { errorClassName, children, renderError } = this.props; - - if (error && info) { - return renderError ? ( - renderError(error) - ) : ( -

Render Failure: {error.message}
+ if (this.state.error) { + return ( +
Render Failure: {this.state.error.message}
); } - return children; + return this.props.children; } } -export class ErrorBoundary extends PureComponent { - render() { - const { children, ...extraProps } = this.props; +export const ErrorBoundary = (props: Props) => { + const { children, ...extraProps } = props; - if (!children) { - return null; - } - - // Unwrap multiple children into single children for better error isolation - const childArray = Array.isArray(children) ? children : [children]; - return childArray.map((child, i) => ( - - {child} - - )); + if (!children) { + return null; } -} + + return ( + + {children} + + ); +}; diff --git a/packages/insomnia/src/ui/components/modals/error-modal.tsx b/packages/insomnia/src/ui/components/modals/error-modal.tsx index 0c2d4b3353..dd742744fa 100644 --- a/packages/insomnia/src/ui/components/modals/error-modal.tsx +++ b/packages/insomnia/src/ui/components/modals/error-modal.tsx @@ -1,4 +1,4 @@ -import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react'; +import React, { forwardRef, ReactNode, useImperativeHandle, useRef, useState } from 'react'; import { Modal, type ModalHandle, ModalProps } from '../base/modal'; import { ModalBody } from '../base/modal-body'; @@ -9,7 +9,7 @@ export interface ErrorModalOptions { title?: string; error?: Error | null; addCancel?: boolean; - message?: string; + message?: string | ReactNode; } export interface ErrorModalHandle { show: (options: ErrorModalOptions) => void;