Skip to content

Commit

Permalink
RSTUDIO-539: Better visualization for mixed and dictionaries (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik authored Jun 12, 2024
1 parent 4a7f295 commit e4ca01b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## vNext (TBD)

### Enhancements

* Improved data visualization for dictionaries and mixed types. ([#1663](https://github.com/realm/realm-studio/pull/1663/files))

### Fixed

* None

### Internals

* None

## 15.1.1 (2024-06-07)


Expand Down
7 changes: 3 additions & 4 deletions src/ui/RealmBrowser/Content/Table/types/DictionaryCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import React from 'react';
import { Badge } from 'reactstrap';
import Realm from 'realm';
import { asSafeJsonString } from '../../../../../utils/json';
import { prettifiedInspect } from '../../../../../utils/json';

// TODO: Get declaration from Realm
type Dictionary<T = unknown> = { [key: string]: T };
Expand All @@ -33,9 +33,8 @@ const displayValue = (
if (!dictionary) {
return 'null';
} else {
return asSafeJsonString(dictionary, {
cleanupRefs: true,
maxLength: VALUE_STRING_LENGTH_LIMIT,
return prettifiedInspect(dictionary, {
maxStringLength: VALUE_STRING_LENGTH_LIMIT,
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ui/RealmBrowser/JsonViewerDialog/JsonViewerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const JsonViewerDialog = ({
}: IJSonViewerDialogProps) => (
<Modal isOpen={visible} toggle={onCancel}>
<Form>
<ModalHeader>JSON Viewer</ModalHeader>
<ModalHeader>Viewer</ModalHeader>
{json.includes('"$refId":') && (
<CardBody style={{ borderBottom: '1px solid #dee2e6' }}>
<CardText>
Expand Down
2 changes: 1 addition & 1 deletion src/ui/RealmBrowser/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import util from 'util';
import util from 'node:util';

export const displayObject = (
object: Realm.Object | null,
Expand Down
33 changes: 27 additions & 6 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { display as displayDataCell } from '../ui/RealmBrowser/Content/Table/types/DataCell';
import { stringify } from 'flatted';

import { InspectOptions, inspect } from 'util';
// TODO: Investigate better solution.
const $REF_MATCHER =
/\s*\"\$ref[Id]*\" *: *(\"(.*?)\"(,|\s|)|\s*\{(.*?)\}(,|\s|))/g;
Expand All @@ -16,6 +15,29 @@ type SafeJsonOptions = {
shallow?: boolean;
};

export const prettifiedInspect = (
object: unknown,
options?: InspectOptions,
) => {
// If it is possible to serialize the object to a simpler structure with toJSON, do it.
const simplifiedObject =
object &&
typeof object === 'object' &&
'toJSON' in object &&
typeof object.toJSON === 'function'
? object.toJSON()
: object;

return inspect(simplifiedObject, {
compact: false,
// TODO: Can potentially support higher depth if one can hide symbols properly.
depth: 0,
breakLength: 80,
showHidden: false,
...options,
});
};

export const asSafeJsonString = (
value: unknown,
options: SafeJsonOptions = {},
Expand All @@ -39,7 +61,7 @@ export const asSafeJsonString = (
);
} else {
try {
json = stringify(value);
json = prettifiedInspect(value);
} catch (err) {
json = err instanceof Error ? err.message : String(err);
}
Expand Down Expand Up @@ -127,9 +149,8 @@ export const getCellStringRepresentation = (
}

if (canUseJsonViewer(property, value)) {
return asSafeJsonString(value, {
cleanupRefs: true,
maxLength: VALUE_STRING_LENGTH_LIMIT,
return prettifiedInspect(value, {
maxStringLength: VALUE_STRING_LENGTH_LIMIT,
});
}

Expand Down

0 comments on commit e4ca01b

Please sign in to comment.