Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/components/QueryResultTable/Cell/Cell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {Popup} from '@gravity-ui/uikit';
import {ClipboardButton, Popup} from '@gravity-ui/uikit';

import {b} from '../QueryResultTable';

Expand All @@ -9,11 +9,25 @@ interface CellProps {
value: string;
}

//helper function to try to format a string as JSON, if it fails, return the original string
function tryFormatJson(value: string): {formatted: string; isJson: boolean} {
try {
const parsed = JSON.parse(value);
if (typeof parsed !== 'object' || parsed === null) {
return {formatted: value, isJson: false};
}
return {formatted: JSON.stringify(parsed, null, 2), isJson: true};
} catch {
return {formatted: value, isJson: false};
}
}

export const Cell = React.memo(function Cell(props: CellProps) {
const {className, value} = props;

const [open, setOpen] = React.useState(false);
const anchorRef = React.useRef<HTMLSpanElement | null>(null);
const {formatted, isJson} = React.useMemo(() => tryFormatJson(value), [value]);

const handleToggle = React.useCallback(() => {
setOpen((prevOpen) => !prevOpen);
Expand All @@ -32,7 +46,10 @@ export const Cell = React.memo(function Cell(props: CellProps) {
anchorRef={anchorRef}
onOutsideClick={handleClose}
>
<div className={b('cell-popup')}>{value}</div>
<div className={b('cell-popup')}>
{isJson ? <pre className={b('cell-popup-json')}>{formatted}</pre> : formatted}
<ClipboardButton text={formatted} size="s" className={b('cell-popup-copy')} />
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Copy button copies pretty-printed form, not original value

The PR description says "copy the cell value," but text={formatted} copies the indented multi-line JSON (with extra whitespace) rather than the original compact string from the query result. Users pasting into code or a query editor will get the reformatted version instead of the raw value. Use text={value} to copy the original.

Suggested change
<ClipboardButton text={formatted} size="s" className={b('cell-popup-copy')} />
<ClipboardButton text={value} size="s" className={b('cell-popup-copy')} />
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/components/QueryResultTable/Cell/Cell.tsx
Line: 48

Comment:
**Copy button copies pretty-printed form, not original value**

The PR description says "copy the cell value," but `text={formatted}` copies the indented multi-line JSON (with extra whitespace) rather than the original compact string from the query result. Users pasting into code or a query editor will get the reformatted version instead of the raw value. Use `text={value}` to copy the original.

```suggestion
                    <ClipboardButton text={value} size="s" className={b('cell-popup-copy')} />
```

How can I resolve this? If you propose a fix, please make it concise.

</div>
</Popup>
<span ref={anchorRef} className={b('cell', className)} onClick={handleToggle}>
{value}
Expand Down
17 changes: 17 additions & 0 deletions src/components/QueryResultTable/QueryResultTable.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
word-break: break-word;
}

&__cell-popup-json {
overflow-x: auto;

max-height: 300px;
margin: 0;

font-family: monospace;
font-size: 12px;
white-space: pre;
}

&__cell-popup-copy {
display: flex;

margin-top: 8px;
}

&__message {
padding: 15px 10px;
}
Expand Down
Loading