-
Notifications
You must be signed in to change notification settings - Fork 20
feat: pretty-print JSON and add copy button in cell popup #3801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; | ||||||
|
|
||||||
|
|
@@ -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); | ||||||
|
|
@@ -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')} /> | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The PR description says "copy the cell value," but
Suggested change
Prompt To Fix With AIThis 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} | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.