mirror of https://github.com/grafana/grafana
Table panel: Improve cell inspector (#91862)
* Improve cell inspector * Update types * Prettier * Type checking fixespull/91799/head
parent
aaf33c7923
commit
f14fd5828a
@ -1,57 +1,84 @@ |
||||
import { isString } from 'lodash'; |
||||
import { useState } from 'react'; |
||||
|
||||
import { ClipboardButton } from '../ClipboardButton/ClipboardButton'; |
||||
import { Drawer } from '../Drawer/Drawer'; |
||||
import { Stack } from '../Layout/Stack/Stack'; |
||||
import { CodeEditor } from '../Monaco/CodeEditor'; |
||||
import { Tab, TabsBar } from '../Tabs'; |
||||
|
||||
export enum TableCellInspectorMode { |
||||
code = 'code', |
||||
text = 'text', |
||||
} |
||||
|
||||
interface TableCellInspectorProps { |
||||
value: any; |
||||
onDismiss: () => void; |
||||
mode: 'code' | 'text'; |
||||
mode: TableCellInspectorMode; |
||||
} |
||||
|
||||
export function TableCellInspector({ value, onDismiss, mode }: TableCellInspectorProps) { |
||||
let displayValue = value; |
||||
const [currentMode, setMode] = useState(mode); |
||||
|
||||
if (isString(value)) { |
||||
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'; |
||||
} catch {} |
||||
} |
||||
} else { |
||||
displayValue = JSON.stringify(value, null, ' '); |
||||
} |
||||
let text = displayValue; |
||||
|
||||
if (mode === 'code') { |
||||
text = JSON.stringify(value, null, ' '); |
||||
} |
||||
const tabs = [ |
||||
{ |
||||
label: 'Plain text', |
||||
value: 'text', |
||||
}, |
||||
{ |
||||
label: 'Code editor', |
||||
value: 'code', |
||||
}, |
||||
]; |
||||
|
||||
const changeTabs = () => { |
||||
setMode(currentMode === TableCellInspectorMode.text ? TableCellInspectorMode.code : TableCellInspectorMode.text); |
||||
}; |
||||
|
||||
const tabBar = ( |
||||
<TabsBar> |
||||
{tabs.map((t, index) => ( |
||||
<Tab key={`${t.value}-${index}`} label={t.label} active={t.value === currentMode} onChangeTab={changeTabs} /> |
||||
))} |
||||
</TabsBar> |
||||
); |
||||
|
||||
return ( |
||||
<Drawer onClose={onDismiss} title="Inspect value"> |
||||
{mode === 'code' ? ( |
||||
<CodeEditor |
||||
width="100%" |
||||
height={500} |
||||
language="json" |
||||
showLineNumbers={true} |
||||
showMiniMap={(text && text.length) > 100} |
||||
value={text} |
||||
readOnly={true} |
||||
/> |
||||
) : ( |
||||
<pre>{text}</pre> |
||||
)} |
||||
<ClipboardButton icon="copy" getText={() => text}> |
||||
Copy to Clipboard |
||||
</ClipboardButton> |
||||
<Drawer onClose={onDismiss} title="Inspect value" tabs={tabBar}> |
||||
<Stack direction="column" gap={2}> |
||||
<ClipboardButton icon="copy" getText={() => text} style={{ marginLeft: 'auto', width: '200px' }}> |
||||
Copy to Clipboard |
||||
</ClipboardButton> |
||||
{currentMode === 'code' ? ( |
||||
<CodeEditor |
||||
width="100%" |
||||
height={500} |
||||
language="json" |
||||
showLineNumbers={true} |
||||
showMiniMap={(text && text.length) > 100} |
||||
value={text} |
||||
readOnly={true} |
||||
wordWrap={true} |
||||
/> |
||||
) : ( |
||||
<pre>{text}</pre> |
||||
)} |
||||
</Stack> |
||||
</Drawer> |
||||
); |
||||
} |
||||
|
Loading…
Reference in new issue