Skip to content

Commit

Permalink
fix: handle atom containing undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunvegda committed Feb 10, 2023
1 parent a20a235 commit c086a75
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { Box, Text } from '@mantine/core';
import { AnyAtomValue } from 'src/types';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { Stack } from '@mantine/core';
import { useAtomValue } from 'jotai/react';
import { AnyAtom } from 'src/types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { memo } from 'react';
import * as React from 'react';
import { Prism, PrismProps } from '@mantine/prism';
import { AnyAtomValue } from 'src/types';
import { getTypeOfAtomValue } from '../../../../../../../../utils';

// List of types to render in JavaScript syntax
const javaScriptLanguageTypes = ['object', 'array', 'null'];
const javaScriptLanguageTypes = ['object', 'array', 'null', 'undefined'];

export const getPrismLanguageType = (
atomValue: AnyAtomValue,
Expand All @@ -23,7 +23,7 @@ type MemoizedValueRendererProps = {
prismLanguageType: ReturnType<typeof getPrismLanguageType>;
};

export const MemoizedValueRenderer = memo(
export const MemoizedValueRenderer = React.memo(
({ prismLanguageType, value }: MemoizedValueRendererProps): JSX.Element => {
return (
<Prism language={prismLanguageType} mb={10} copyLabel="Copy value">
Expand Down
7 changes: 3 additions & 4 deletions src/DevTools/utils/get-type-of-atom-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { AnyAtom, AnyAtomValue, WithInitialValue } from 'src/types';

const isValueAtom = (value: AnyAtomValue): value is AnyAtom => {
return (
typeof (value as AnyAtom).read === 'function' ||
typeof (value as Partial<AnyAtom>)?.read === 'function' ||
typeof (value as WritableAtom<any, any, any>)?.write === 'function' ||
!!(value as WithInitialValue).init ||
!!(value as AnyAtom).debugLabel ||
(value as AnyAtom).toString().includes('atom')
!!(value as WithInitialValue)?.init ||
!!(value as Partial<AnyAtom>)?.debugLabel
);
};

Expand Down
6 changes: 6 additions & 0 deletions src/DevTools/utils/stringify-atom-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import { getTypeOfAtomValue } from './get-type-of-atom-value';

export const stringifyAtomValue = (atomValue: AnyAtomValue): string => {
const type = getTypeOfAtomValue(atomValue);

if (type === 'undefined') {
return 'undefined';
}

if (type === 'bigint') {
return String(atomValue);
}

const { json } = serialize(atomValue);
return JSON.stringify(json, null, 2);
};
9 changes: 7 additions & 2 deletions src/stories/Default/Demos/Random.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ textAtom.debugLabel = 'textAtom';
const bigintAtom = atom(BigInt(Number.MAX_SAFE_INTEGER));
bigintAtom.debugLabel = 'bigintAtom';

const atomReturnsUndefined = atom(undefined);
atomReturnsUndefined.debugLabel = 'atomReturnsUndefined';

const nestedObjectAtom = atom((get) => {
return {
nestedObject: {
Expand All @@ -28,8 +31,6 @@ nestedObjectAtom.debugLabel = 'nestedObjectAtom';

const atomsInAtomsCountAtom = atom(atom(atom((get) => get(countAtom))));
atomsInAtomsCountAtom.debugLabel = 'atomsInAtomsCountAtom';
// const circularAtom = atom({ foo: atom({ bar: atom(1) }) });
// circularAtom.debugLabel = 'circularAtom';

export const Random = () => {
const [count, setCount] = useAtom(countAtom, demoStoreOptions);
Expand All @@ -38,6 +39,10 @@ export const Random = () => {
const nestedObject = useAtomValue(nestedObjectAtom, demoStoreOptions);
const _text = useAtomValue(textAtom, demoStoreOptions);
const _bigint = useAtomValue(bigintAtom, demoStoreOptions);
const _atomReturnsUndefined = useAtomValue(
atomReturnsUndefined,
demoStoreOptions,
);
// const circular = useAtomValue(circularAtom, demoStoreOptions);
// console.log(circular);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down

0 comments on commit c086a75

Please sign in to comment.