From 7f092e45c98eb376f894605d71c5832712a00625 Mon Sep 17 00:00:00 2001 From: liam-tait Date: Tue, 16 Nov 2021 01:22:47 +1300 Subject: [PATCH 01/19] refactor(devtools): add types to Explorer Add types to Explorer component with as minimal functional changes as possible while still getting type safety 2742 --- src/devtools/Explorer.tsx | 79 +++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/src/devtools/Explorer.tsx b/src/devtools/Explorer.tsx index b409bc2ce0..c442e0e63e 100644 --- a/src/devtools/Explorer.tsx +++ b/src/devtools/Explorer.tsx @@ -1,5 +1,3 @@ -// @ts-nocheck - import React from 'react' import { styled } from './utils' @@ -17,7 +15,7 @@ export const Label = styled('span', { color: 'white', }) -export const Value = styled('span', (props, theme) => ({ +export const Value = styled('span', (_props, theme) => ({ color: theme.danger, })) @@ -32,7 +30,12 @@ export const Info = styled('span', { fontSize: '.7em', }) -export const Expander = ({ expanded, style = {}, ...rest }) => ( +type ExpanderProps = { + expanded: boolean + style?: React.CSSProperties +} + +export const Expander = ({ expanded, style = {} }: ExpanderProps) => ( ( ▶ ) +type Entry = { + label: string +} + +type RendererProps = { + handleEntry?: (entry: Entry) => JSX.Element + label?: string + value?: any + subEntries?: Entry[] + subEntryPages?: Entry[][] + type?: string + expanded?: boolean + toggle: (set?: boolean) => void + pageSize: number +} + +type Renderer = (props: RendererProps) => JSX.Element -const DefaultRenderer = ({ - handleEntry, +const DefaultRenderer: Renderer = ({ + handleEntry = () => null, label, value, // path, - subEntries, - subEntryPages, + subEntries = [], + subEntryPages = [], type, // depth, - expanded, + expanded = false, toggle, pageSize, }) => { - const [expandedPages, setExpandedPages] = React.useState([]) + const [expandedPages, setExpandedPages] = React.useState([]) return ( @@ -117,6 +137,12 @@ const DefaultRenderer = ({ ) } +type ExplorerProps = Partial & { + renderer?: Renderer + depth?: number + defaultExpanded?: true | Record +} + export default function Explorer({ value, defaultExpanded, @@ -124,20 +150,29 @@ export default function Explorer({ pageSize = 100, depth = 0, ...rest -}) { - const [expanded, setExpanded] = React.useState(defaultExpanded) +}: ExplorerProps) { + const [expanded, setExpanded] = React.useState(Boolean(defaultExpanded)) - const toggle = set => { - setExpanded(old => (typeof set !== 'undefined' ? set : !old)) + const toggle = (set?: boolean): void => { + setExpanded(old => (typeof set !== 'undefined' ? set : !Boolean(old))) } - const path = [] - - let type = typeof value + const path: string[] = [] + let type: + | 'string' + | 'number' + | 'bigint' + | 'boolean' + | 'symbol' + | 'undefined' + | 'object' + | 'function' + | 'array' + | 'Iterable' = typeof value let subEntries const subEntryPages = [] - const makeProperty = sub => { + const makeProperty = (sub: { label: string; value: unknown }) => { const newPath = path.concat(sub.label) const subDefaultExpanded = defaultExpanded === true @@ -155,7 +190,7 @@ export default function Explorer({ type = 'array' subEntries = value.map((d, i) => makeProperty({ - label: i, + label: i.toString(), value: d, }) ) @@ -167,7 +202,7 @@ export default function Explorer({ type = 'Iterable' subEntries = Array.from(value, (val, i) => makeProperty({ - label: i, + label: i.toString(), value: val, }) ) @@ -193,14 +228,12 @@ export default function Explorer({ return renderer({ handleEntry: entry => ( - + ), type, subEntries, subEntryPages, - depth, value, - path, expanded, toggle, pageSize, From 719c5d12ef61776c1c476dc2c50110baf50a65f9 Mon Sep 17 00:00:00 2001 From: liam-tait Date: Fri, 19 Nov 2021 19:31:59 +1300 Subject: [PATCH 02/19] remove unused set param from explorer toggle --- src/devtools/Explorer.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/devtools/Explorer.tsx b/src/devtools/Explorer.tsx index c442e0e63e..3695700ec7 100644 --- a/src/devtools/Explorer.tsx +++ b/src/devtools/Explorer.tsx @@ -59,7 +59,7 @@ type RendererProps = { subEntryPages?: Entry[][] type?: string expanded?: boolean - toggle: (set?: boolean) => void + toggle: () => void pageSize: number } @@ -153,9 +153,7 @@ export default function Explorer({ }: ExplorerProps) { const [expanded, setExpanded] = React.useState(Boolean(defaultExpanded)) - const toggle = (set?: boolean): void => { - setExpanded(old => (typeof set !== 'undefined' ? set : !Boolean(old))) - } + const toggle = () => setExpanded(old => !old) const path: string[] = [] let type: From bc9e61f1e97394824a965ca943e5a8ae2ea1149b Mon Sep 17 00:00:00 2001 From: liam-tait Date: Fri, 19 Nov 2021 19:38:24 +1300 Subject: [PATCH 03/19] Wrap Explorer toggle with useCallback --- src/devtools/Explorer.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/devtools/Explorer.tsx b/src/devtools/Explorer.tsx index 3695700ec7..2141672a74 100644 --- a/src/devtools/Explorer.tsx +++ b/src/devtools/Explorer.tsx @@ -153,7 +153,7 @@ export default function Explorer({ }: ExplorerProps) { const [expanded, setExpanded] = React.useState(Boolean(defaultExpanded)) - const toggle = () => setExpanded(old => !old) + const toggle = React.useCallback(() => setExpanded(old => !old), []) const path: string[] = [] let type: From 36cac2e782a6e9fb6b0ba6253be21f56d9a98c3f Mon Sep 17 00:00:00 2001 From: liam-tait Date: Fri, 19 Nov 2021 19:43:49 +1300 Subject: [PATCH 04/19] Rename Explorer toggle to toggleExpanded --- src/devtools/Explorer.tsx | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/devtools/Explorer.tsx b/src/devtools/Explorer.tsx index 2141672a74..32537b468a 100644 --- a/src/devtools/Explorer.tsx +++ b/src/devtools/Explorer.tsx @@ -59,7 +59,7 @@ type RendererProps = { subEntryPages?: Entry[][] type?: string expanded?: boolean - toggle: () => void + toggleExpanded: () => void pageSize: number } @@ -75,7 +75,7 @@ const DefaultRenderer: Renderer = ({ type, // depth, expanded = false, - toggle, + toggleExpanded, pageSize, }) => { const [expandedPages, setExpandedPages] = React.useState([]) @@ -84,7 +84,7 @@ const DefaultRenderer: Renderer = ({ {subEntryPages?.length ? ( <> -