From c3f7d60d851706fe968691f84d9ec6ad32f0eea8 Mon Sep 17 00:00:00 2001 From: Beef Date: Sun, 26 Feb 2023 02:20:01 +0000 Subject: [PATCH] [UI] Implement and use ErrorBoundary --- src/def.d.ts | 6 +- src/ui/components/ErrorBoundary.tsx | 61 +++++++ src/ui/components/Summary.tsx | 2 +- src/ui/components/index.ts | 3 +- .../settings/components/SettingsSection.tsx | 58 +++---- src/ui/settings/index.tsx | 4 +- src/ui/settings/pages/AssetBrowser.tsx | 37 +++-- src/ui/settings/pages/Developer.tsx | 152 +++++++++--------- src/ui/settings/pages/General.tsx | 107 ++++++------ src/ui/settings/pages/Plugins.tsx | 55 ++++--- 10 files changed, 279 insertions(+), 206 deletions(-) create mode 100644 src/ui/components/ErrorBoundary.tsx diff --git a/src/def.d.ts b/src/def.d.ts index 6013689..180c15e 100644 --- a/src/def.d.ts +++ b/src/def.d.ts @@ -7,7 +7,6 @@ import _moment from "moment"; type MetroModules = { [id: number]: any }; // Component types -// TODO: Make these not be here? interface SummaryProps { label: string; icon?: string; @@ -15,6 +14,10 @@ interface SummaryProps { children: JSX.Element | JSX.Element[]; } +interface ErrorBoundaryProps { + children: JSX.Element | JSX.Element[], +} + // Helper types for API functions type PropIntellisense

= Record & Record; type PropsFinder = (...props: T[]) => PropIntellisense; @@ -331,6 +334,7 @@ interface VendettaObject { Search: _React.ComponentType; // Vendetta Summary: (props: SummaryProps) => JSX.Element; + ErrorBoundary: (props: ErrorBoundaryProps) => JSX.Element; } toasts: { showToast: (content: string, asset: number) => void; diff --git a/src/ui/components/ErrorBoundary.tsx b/src/ui/components/ErrorBoundary.tsx new file mode 100644 index 0000000..ef9c811 --- /dev/null +++ b/src/ui/components/ErrorBoundary.tsx @@ -0,0 +1,61 @@ +import { ErrorBoundaryProps } from "@types"; +import { React, ReactNative as RN, stylesheet, constants } from "@metro/common"; +import { findByProps } from "@metro/filters"; +import { Forms } from "@ui/components"; +import { semanticColors } from "@ui/color"; + +interface ErrorBoundaryState { + hasErr: boolean; + errText?: string; +} + +const Button = findByProps("Looks", "Colors", "Sizes") as any; + +const styles = stylesheet.createThemedStyleSheet({ + view: { + flex: 1, + flexDirection: "column", + margin: 10, + }, + title: { + fontSize: 20, + textAlign: "center", + marginBottom: 5, + }, + codeblock: { + fontFamily: constants.Fonts.CODE_SEMIBOLD, + includeFontPadding: false, + fontSize: 12, + backgroundColor: semanticColors.BACKGROUND_SECONDARY, + padding: 5, + borderRadius: 5, + marginBottom: 5, + } +}); + +export default class ErrorBoundary extends React.Component { + constructor(props: ErrorBoundaryProps) { + super(props); + this.state = { hasErr: false }; + } + + static getDerivedStateFromError = (error: Error) => ({ hasErr: true, errText: error.message }); + + render() { + if (!this.state.hasErr) return this.props.children; + + return ( + + Uh oh. + {this.state.errText} +