|
| 1 | +import { JsonView as ReactJsonView } from 'react-json-view-lite' |
| 2 | +import 'react-json-view-lite/dist/index.css' |
| 3 | +import styles from './json-view-dialog.module.css' |
| 4 | +import { cn } from '../utils' |
| 5 | +import { Button } from './button' |
| 6 | +import { useCallback, useState } from 'react' |
| 7 | +import { asJson } from '@/utils/as-json' |
| 8 | +import { toast } from 'react-toastify' |
| 9 | +import { Dialog, DialogContent, DialogHeader } from '@/features/common/components/dialog' |
| 10 | +import { useResolvedTheme } from '@/features/settings/data/theme' |
| 11 | + |
| 12 | +type Props = { |
| 13 | + json: object |
| 14 | + expandJsonLevel?: (level: number) => boolean |
| 15 | +} |
| 16 | + |
| 17 | +export function OpenJsonViewDialogButton({ json, expandJsonLevel: exapandJsonLevel = defaultExpandLevel }: Props) { |
| 18 | + const [dialogOpen, setDialogOpen] = useState(false) |
| 19 | + |
| 20 | + const openJsonViewDialog = useCallback(() => { |
| 21 | + setDialogOpen(true) |
| 22 | + }, [setDialogOpen]) |
| 23 | + |
| 24 | + const styleDark: StyleProps = { |
| 25 | + container: styles['key-dark'], |
| 26 | + basicChildStyle: styles['basic-element-style'], |
| 27 | + collapseIcon: styles['collapse-icon'], |
| 28 | + expandIcon: styles['expand-icon'], |
| 29 | + collapsedContent: styles['collapsed-content'], |
| 30 | + label: styles['label'], |
| 31 | + clickableLabel: styles['clickable-label'], |
| 32 | + nullValue: '', |
| 33 | + undefinedValue: '', |
| 34 | + stringValue: styles['value-string-dark'], |
| 35 | + booleanValue: styles['value-boolean-dark'], |
| 36 | + numberValue: styles['value-number-dark'], |
| 37 | + otherValue: '', |
| 38 | + punctuation: styles['punctuation-dark'], |
| 39 | + noQuotesForStringValues: false, |
| 40 | + } |
| 41 | + const styleLight: StyleProps = { |
| 42 | + container: styles['key-light'], |
| 43 | + basicChildStyle: styles['basic-element-style'], |
| 44 | + collapseIcon: styles['collapse-icon'], |
| 45 | + expandIcon: styles['expand-icon'], |
| 46 | + collapsedContent: styles['collapsed-content'], |
| 47 | + label: styles['label'], |
| 48 | + clickableLabel: styles['clickable-label'], |
| 49 | + nullValue: '', |
| 50 | + undefinedValue: '', |
| 51 | + stringValue: styles['value-string-light'], |
| 52 | + booleanValue: styles['value-boolean-light'], |
| 53 | + numberValue: styles['value-number-light'], |
| 54 | + otherValue: '', |
| 55 | + punctuation: styles['punctuation-light'], |
| 56 | + noQuotesForStringValues: false, |
| 57 | + } |
| 58 | + |
| 59 | + const theme = useResolvedTheme() |
| 60 | + const currentStyle = theme === 'dark' ? styleDark : styleLight |
| 61 | + |
| 62 | + const copyJsonToClipboard = useCallback(() => { |
| 63 | + const jsonString = asJson(json) |
| 64 | + navigator.clipboard.writeText(jsonString) |
| 65 | + |
| 66 | + toast.success('JSON copied to clipboard') |
| 67 | + }, [json]) |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <Button variant="outline" onClick={openJsonViewDialog}> |
| 72 | + View JSON |
| 73 | + </Button> |
| 74 | + <Dialog open={dialogOpen} onOpenChange={setDialogOpen} modal={true}> |
| 75 | + <DialogContent className="bg-card"> |
| 76 | + <DialogHeader> |
| 77 | + <h4 className={cn('text-xl text-primary font-bold')}>JSON</h4> |
| 78 | + </DialogHeader> |
| 79 | + <div className={cn('border-solid border-2 border-border grid w-[900px] min-h-[200px] max-h-[500px] overflow-auto relative')}> |
| 80 | + <Button variant="default" className={cn('absolute top-2 right-2')} onClick={copyJsonToClipboard}> |
| 81 | + Copy |
| 82 | + </Button> |
| 83 | + <ReactJsonView data={json} shouldExpandNode={exapandJsonLevel} style={currentStyle} /> |
| 84 | + </div> |
| 85 | + </DialogContent> |
| 86 | + </Dialog> |
| 87 | + </> |
| 88 | + ) |
| 89 | +} |
| 90 | +// By default only render the top level because sometimes the object has too many children, which result in the UI thread being blocked on mount. |
| 91 | +const defaultExpandLevel = (level: number) => { |
| 92 | + return level < 1 |
| 93 | +} |
| 94 | + |
| 95 | +export interface StyleProps { |
| 96 | + container: string |
| 97 | + basicChildStyle: string |
| 98 | + label: string |
| 99 | + clickableLabel: string |
| 100 | + nullValue: string |
| 101 | + undefinedValue: string |
| 102 | + numberValue: string |
| 103 | + stringValue: string |
| 104 | + booleanValue: string |
| 105 | + otherValue: string |
| 106 | + punctuation: string |
| 107 | + expandIcon: string |
| 108 | + collapseIcon: string |
| 109 | + collapsedContent: string |
| 110 | + noQuotesForStringValues: boolean |
| 111 | +} |
0 commit comments