Skip to content

Commit d1875ee

Browse files
committed
feat: add option to display private atoms
1 parent 236cd75 commit d1875ee

File tree

18 files changed

+160
-37
lines changed

18 files changed

+160
-37
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ type DevToolsProps = {
119119
// `raw` - parses the top level atom value but does not parse the values of atoms within atoms
120120
// `deep-nested` - Parses values of atoms within atoms. Linear performance curve. Bigger the object, the slower the performance
121121
atomValueParser?: 'raw' | 'deep-nested';
122+
// Private atoms are used internally in atoms like `atomWithStorage` or `atomWithLocation`, etc. to manage state.
123+
// Defaults to `false`
124+
shouldShowPrivateAtoms?: boolean;
122125
};
123126
};
124127
```

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"@storybook/testing-library": "^0.0.14-next.1",
104104
"@swc/core": "^1.3.25",
105105
"@swc/jest": "^0.2.24",
106+
"@tanstack/query-core": "^4.24.10",
106107
"@testing-library/jest-dom": "^5.16.5",
107108
"@testing-library/react": "^13.4.0",
108109
"@testing-library/user-event": "^14.4.3",
@@ -129,7 +130,8 @@
129130
"jest": "^29.3.1",
130131
"jest-environment-jsdom": "^29.3.1",
131132
"jest-watch-typeahead": "^2.2.1",
132-
"jotai": "^2.0.0",
133+
"jotai": "^2.0.2",
134+
"jotai-tanstack-query": "^0.5.0",
133135
"lint-staged": "^13.1.0",
134136
"prettier": "^2.8.1",
135137
"react": "^18.2.0",

src/DevTools/Extension/components/Shell/components/AtomViewer/atoms.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { atom } from 'jotai/vanilla';
22
import { atomWithDefault } from 'jotai/vanilla/utils';
33
import { AnyAtom, ValuesAtomTuple } from 'src/types';
4+
import { devToolsOptionsAtom } from '../../../../../atoms/devtools-options';
45
import { valuesAtom } from '../../../../../atoms/values-atom';
56
import { filterAtomsByString } from './utils/filter-atoms-by-string';
7+
import { filterPrivateAtoms } from './utils/filter-private-atoms';
68

79
type SelectedAtomAtomData = { atomKey: string; atom: AnyAtom };
810

@@ -14,10 +16,18 @@ export const selectedAtomAtom = atom<SelectedAtomAtomData | undefined>(
1416
const searchInputInternalValueAtom = atom('');
1517

1618
export const filteredValuesAtom = atomWithDefault<ValuesAtomTuple[]>((get) => {
17-
return filterAtomsByString(
19+
const filteredByString = filterAtomsByString(
1820
get(searchInputInternalValueAtom),
1921
get(valuesAtom),
2022
);
23+
24+
const { shouldShowPrivateAtoms } = get(devToolsOptionsAtom);
25+
if (!shouldShowPrivateAtoms) {
26+
const filteredByPrivate = filterPrivateAtoms(filteredByString);
27+
return filteredByPrivate;
28+
}
29+
30+
return filteredByString;
2131
});
2232

2333
export const searchInputAtom = atom(

src/DevTools/Extension/components/Shell/components/AtomViewer/components/AtomDetail/components/AtomDependentsList.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { Box, Code, List, Text } from '@mantine/core';
33
import { AnyAtom } from 'src/types';
4+
import { useDevToolsOptionsValue } from '../../../../../../../../atoms/devtools-options';
45
import { useAtomsSnapshots } from '../../../../../../../../hooks/useAtomsSnapshots';
56
import { parseDebugLabel } from '../../../../../../../../utils/parse-debug-label';
67

@@ -12,11 +13,23 @@ export const AtomDependentsList = ({
1213
atom,
1314
}: AtomDependentsListProps): JSX.Element => {
1415
const { dependents } = useAtomsSnapshots();
16+
const devtoolsOptions = useDevToolsOptionsValue();
1517

1618
const depsForAtom = React.useMemo(() => {
1719
const arr = Array.from(dependents.get(atom) || []);
18-
return arr.filter((a) => a.toString() !== atom.toString());
19-
}, [dependents, atom]);
20+
const filteredCurrentAtom = arr.filter(
21+
(a) => a.toString() !== atom.toString(),
22+
);
23+
24+
if (!devtoolsOptions.shouldShowPrivateAtoms) {
25+
const filteredPrivateAtoms = filteredCurrentAtom.filter(
26+
(a) => !a?.debugPrivate,
27+
);
28+
return filteredPrivateAtoms;
29+
}
30+
31+
return filteredCurrentAtom;
32+
}, [dependents, devtoolsOptions.shouldShowPrivateAtoms, atom]);
2033

2134
const listOfDependents = React.useMemo(
2235
() =>

src/DevTools/Extension/components/Shell/components/AtomViewer/components/AtomDetail/components/AtomMetaDetails.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import * as React from 'react';
2-
import { Box, Code, Text, Title } from '@mantine/core';
2+
import { Box, Code, MantineColor, Text, Title, Tooltip } from '@mantine/core';
33
import { AtomValueType } from '../../../../../../../../utils/get-type-of-atom-value';
44
import { parseDebugLabel } from '../../../../../../../../utils/parse-debug-label';
55

66
type AtomDetailItemProps = {
77
label: string;
88
value: string;
9+
color?: MantineColor;
910
};
1011

11-
const DisplayAtomDetailsItem = ({ label, value }: AtomDetailItemProps) => {
12+
const DisplayAtomDetailsItem = ({
13+
label,
14+
value,
15+
color,
16+
}: AtomDetailItemProps) => {
1217
return (
1318
<Box mb={10}>
1419
<Text
@@ -20,18 +25,25 @@ const DisplayAtomDetailsItem = ({ label, value }: AtomDetailItemProps) => {
2025
>
2126
{label}
2227
</Text>
23-
<Code data-testid={`display-detail-item-value-${value}`}>{value}</Code>
28+
<Code data-testid={`display-detail-item-value-${value}`} color={color}>
29+
{value}
30+
</Code>
2431
</Box>
2532
);
2633
};
2734

2835
type AtomMetaDetailsProps = {
29-
debugLabel?: string;
36+
debugLabel?: string | undefined;
3037
atomValueType: AtomValueType;
38+
isAtomPrivate?: boolean | undefined;
3139
};
3240

3341
export const AtomMetaDetails = React.memo(
34-
({ debugLabel, atomValueType }: AtomMetaDetailsProps): JSX.Element => {
42+
({
43+
debugLabel,
44+
atomValueType,
45+
isAtomPrivate,
46+
}: AtomMetaDetailsProps): JSX.Element => {
3547
return (
3648
<Box>
3749
<Title size="h3" mb={10}>
@@ -45,6 +57,9 @@ export const AtomMetaDetails = React.memo(
4557
value={parseDebugLabel(debugLabel)}
4658
/>
4759
<DisplayAtomDetailsItem label="Value type" value={atomValueType} />
60+
{isAtomPrivate && (
61+
<DisplayAtomDetailsItem label="Private" value={'Yes'} color={'red'} />
62+
)}
4863
</Box>
4964
);
5065
},

src/DevTools/Extension/components/Shell/components/AtomViewer/components/AtomDetail/components/AtomParseRawValue.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@ import React from 'react';
22
import { Box, Text } from '@mantine/core';
33
import { AnyAtomValue } from 'src/types';
44
import {
5-
AtomValueType,
5+
ErrorSymbol,
66
stringifyAtomValue,
77
} from '../../../../../../../../utils/';
8+
import { ErrorMessage } from '../../../../ErrorMessage';
89
import {
910
MemoizedValueRenderer,
1011
getPrismLanguageType,
1112
} from './MemoizedValueRenderer';
1213

1314
type AtomParseRawValueValueProps = {
1415
atomValue: AnyAtomValue;
15-
atomValueType: AtomValueType;
1616
};
1717

1818
export const AtomParseRawValueValue = ({
1919
atomValue,
20-
atomValueType,
2120
}: AtomParseRawValueValueProps): JSX.Element => {
2221
const prismLanguageType = getPrismLanguageType(atomValue);
22+
const parsedValue = stringifyAtomValue(atomValue);
2323

2424
return (
2525
<Box>
2626
<Text fw="bold" mb="sm">
2727
Raw value
2828
</Text>
2929
{/* TODO investigate if this could ever be the case given that the parent component is wrapped with suspense */}
30-
{atomValueType === 'promise' ? (
31-
<Text>No Preview available</Text>
30+
{parsedValue === ErrorSymbol ? (
31+
<ErrorMessage message="Failed to parse the value of the atom" />
3232
) : (
3333
<MemoizedValueRenderer
34-
value={stringifyAtomValue(atomValue)}
34+
value={parsedValue}
3535
prismLanguageType={prismLanguageType}
3636
/>
3737
)}

src/DevTools/Extension/components/Shell/components/AtomViewer/components/AtomDetail/components/DisplayAtomDetails.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ export const DisplayAtomDetails = ({ atom }: DisplayAtomDetailsProps) => {
2929
<AtomMetaDetails
3030
debugLabel={atom?.debugLabel}
3131
atomValueType={atomValueType}
32+
isAtomPrivate={atom?.debugPrivate}
3233
/>
3334

3435
{shouldDisplayRawValue && (
35-
<AtomParseRawValueValue
36-
atomValue={atomValue}
37-
atomValueType={atomValueType}
38-
/>
36+
<AtomParseRawValueValue atomValue={atomValue} />
3937
)}
4038

4139
{shouldDisplayDeepNestedValue && (

src/DevTools/Extension/components/Shell/components/AtomViewer/components/AtomList/components/AtomListItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useThemeMode } from '../../../../../../../../hooks/useThemeMode';
55
import { parseDebugLabel } from '../../../../../../../../utils/parse-debug-label';
66

77
type AtomListItemProps = {
8-
label?: string;
8+
label?: string | undefined;
99
onClick: (pos: number) => void;
1010
atomKey: string;
1111
pos: number;

src/DevTools/Extension/components/Shell/components/AtomViewer/utils/filter-atoms-by-string.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const filterAtomsByString = (
1010
return defaultAtoms;
1111
}
1212

13-
return defaultAtoms.filter((atomTuple) => {
14-
const parsedDebugLabel = parseDebugLabel(atomTuple[0].debugLabel);
13+
return defaultAtoms.filter(([atomTuple]) => {
14+
const parsedDebugLabel = parseDebugLabel(atomTuple?.debugLabel);
1515
const normalizedLabel = parsedDebugLabel.toLocaleLowerCase();
1616
return normalizedLabel.includes(normalizedStr);
1717
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ValuesAtomTuple } from '../../../../../../../types';
2+
3+
export const filterPrivateAtoms = (atoms: ValuesAtomTuple[]) => {
4+
return atoms.filter(([atom]) => !atom?.debugPrivate);
5+
};

0 commit comments

Comments
 (0)