Table: Cell inspector auto-detecting JSON (#81152)

* set inspect mode to json if no errors parsing json
This commit is contained in:
Galen Kistler 2024-01-24 12:08:18 -06:00 committed by GitHub
parent a138ce668d
commit a81d3b1d22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,9 +14,18 @@ interface TableCellInspectModalProps {
export function TableCellInspectModal({ value, onDismiss, mode }: TableCellInspectModalProps) {
let displayValue = value;
if (isString(value)) {
try {
value = JSON.parse(value);
} catch {} // ignore errors
const trimmedValue = value.trim();
// Exclude numeric strings like '123' from being displayed in code/JSON mode
if (trimmedValue[0] === '{' || trimmedValue[0] === '[' || mode === 'code') {
try {
value = JSON.parse(value);
mode = 'code';
} catch {
mode = 'text';
} // ignore errors
} else {
mode = 'text';
}
} else {
displayValue = JSON.stringify(value, null, ' ');
}