Skip to content

Commit 0008bb0

Browse files
author
Brian Vaughn
committed
Inspectable complex hook values
1 parent 47a5fd5 commit 0008bb0

File tree

4 files changed

+84
-24
lines changed

4 files changed

+84
-24
lines changed

shells/dev/app/InspectableElements/CustomHooks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,37 @@ import React, {
1111
useState,
1212
} from 'react';
1313

14+
const object = {
15+
string: 'abc',
16+
number: 123,
17+
boolean: true,
18+
null: null,
19+
undefined: undefined,
20+
array: ['a', 'b', 'c'],
21+
object: { foo: 1, bar: 2, baz: 3 },
22+
};
23+
1424
function useNestedInnerHook() {
1525
return useState(123);
1626
}
1727
function useNestedOuterHook() {
1828
return useNestedInnerHook();
1929
}
2030

31+
function useCustomObject() {
32+
useDebugValue(object);
33+
return useState(123);
34+
}
35+
2136
function FunctionWithHooks(props: any, ref: React$Ref<any>) {
2237
const [count, updateCount] = useState(0);
38+
const [_, __] = useState(object);
2339

2440
// Custom hook with a custom debug label
2541
const debouncedCount = useDebounce(count, 1000);
2642

43+
useCustomObject();
44+
2745
const onClick = useCallback(
2846
function onClick() {
2947
updateCount(count + 1);

src/devtools/views/HooksTree.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import React from 'react';
44
import { getMetaValueLabel } from './utils';
5+
import { KeyValue } from './InspectedElementTree';
56
import styles from './HooksTree.css';
67

78
import type { HooksNode, HooksTree } from 'src/backend/types';
@@ -34,31 +35,67 @@ function HooksNodeView({ hooksNode }: { hooksNode: HooksNode }) {
3435

3536
const isCustomHook = subHooks.length > 0;
3637

37-
// Format data for display to mimic the props/state/context for now.
3838
const type = typeof value;
39+
3940
let displayValue;
40-
if (isCustomHook && value === undefined) {
41-
displayValue = null;
42-
} else if (
43-
type === 'number' ||
44-
type === 'string' ||
45-
type === 'boolean' ||
46-
value == null
47-
) {
41+
let isComplexDisplayValue = false;
42+
43+
// Format data for display to mimic the props/state/context for now.
44+
if (type === 'number' || type === 'string' || type === 'boolean') {
4845
displayValue = value;
49-
} else {
50-
displayValue = getMetaValueLabel(value);
46+
} else if (value === null) {
47+
displayValue = 'null';
48+
} else if (value === undefined) {
49+
displayValue = null;
50+
} else if (Array.isArray(value)) {
51+
isComplexDisplayValue = true;
52+
displayValue = 'Array';
53+
} else if (type === 'object') {
54+
isComplexDisplayValue = true;
55+
displayValue = 'Object';
5156
}
5257

53-
return (
54-
<div className={styles.HooksNode}>
55-
<div className={styles.NameValueRow}>
56-
<span className={styles.Name}>{name}: </span> {/* $FlowFixMe */}
57-
<span className={styles.Value}>{displayValue}</span>
58-
</div>
59-
<InnerHooksTreeView hooksTree={subHooks} />
60-
</div>
61-
);
58+
if (isCustomHook) {
59+
if (isComplexDisplayValue) {
60+
return (
61+
<div className={styles.HooksNode}>
62+
<div className={styles.NameValueRow}>
63+
<span className={styles.Name}>{name}: </span>
64+
</div>
65+
<KeyValue depth={1} name="DebugValue" value={value} />
66+
<InnerHooksTreeView hooksTree={subHooks} />
67+
</div>
68+
);
69+
} else {
70+
return (
71+
<div className={styles.HooksNode}>
72+
<div className={styles.NameValueRow}>
73+
<span className={styles.Name}>{name}: </span> {/* $FlowFixMe */}
74+
<span className={styles.Value}>{displayValue}</span>
75+
</div>
76+
<InnerHooksTreeView hooksTree={subHooks} />
77+
</div>
78+
);
79+
}
80+
} else {
81+
if (isComplexDisplayValue) {
82+
return (
83+
<div className={styles.HooksNode}>
84+
<KeyValue depth={0} name={name} value={value} />
85+
</div>
86+
);
87+
} else {
88+
return (
89+
<div className={styles.HooksNode}>
90+
<div className={styles.NameValueRow}>
91+
<span className={styles.Name}>{name}: </span>
92+
{/* $FlowFixMe */}
93+
<span className={styles.Value}>{displayValue}</span>
94+
</div>
95+
</div>
96+
);
97+
}
98+
}
6299
}
63100

64101
// $FlowFixMe

src/devtools/views/InspectedElementTree.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type KeyValueProps = {|
4747
value: any,
4848
|};
4949

50-
function KeyValue({ depth, name, value }: KeyValueProps) {
50+
export function KeyValue({ depth, name, value }: KeyValueProps) {
5151
const dataType = typeof value;
5252
const isSimpleType =
5353
dataType === 'number' ||
@@ -64,6 +64,10 @@ function KeyValue({ depth, name, value }: KeyValueProps) {
6464
displayValue = `"${value}"`;
6565
} else if (dataType === 'boolean') {
6666
displayValue = value ? 'true' : 'false';
67+
} else if (value === null) {
68+
displayValue = 'null';
69+
} else if (value === undefined) {
70+
displayValue = 'undefined';
6771
}
6872

6973
children = (
@@ -99,6 +103,7 @@ function KeyValue({ depth, name, value }: KeyValueProps) {
99103
</div>
100104
);
101105
} else {
106+
// $FlowFixMe
102107
children = Object.entries(value).map(([name, value]) => (
103108
<KeyValue key={name} depth={depth + 1} name={name} value={value} />
104109
));

src/devtools/views/SettingsContext.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ function updateThemeVariables(theme: Theme): void {
132132
updateStyleHelper(theme, 'color-dim');
133133
updateStyleHelper(theme, 'color-dimmer');
134134
updateStyleHelper(theme, 'color-dimmest');
135-
updateStyleHelper(theme, 'color-search-match');
136-
updateStyleHelper(theme, 'color-search-match-current');
137-
updateStyleHelper(theme, 'color-text-color');
138135
updateStyleHelper(theme, 'color-jsx-arrow-brackets');
139136
updateStyleHelper(theme, 'color-jsx-arrow-brackets-inverted');
140137
updateStyleHelper(theme, 'color-tree-node-selected');
141138
updateStyleHelper(theme, 'color-tree-node-hover');
139+
updateStyleHelper(theme, 'color-search-match');
140+
updateStyleHelper(theme, 'color-search-match-current');
141+
updateStyleHelper(theme, 'color-text-color');
142142
}
143143

144144
export { SettingsContext, SettingsContextController };

0 commit comments

Comments
 (0)