-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAdvancedEditor.tsx
More file actions
146 lines (130 loc) · 4.8 KB
/
AdvancedEditor.tsx
File metadata and controls
146 lines (130 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { useState, useEffect } from 'react'
import MapIcon from '../../assets/map-folded.svg'
import ChartIcon from '../../assets/icon-chart-bar.svg'
import MarkupIncludeIcon from '../../assets/icon-code.svg'
import { FilterFunction, JsonEditor, UpdateFunction } from 'json-edit-react'
import './advanced-editor-styles.css'
import _ from 'lodash'
import Tooltip from '../ui/Tooltip'
import EmbedEditor from './EmbedEditor'
const UNDEFINED_SENTINELS = new Set(['__undefined__', '__\u200bundefined__'])
const sanitizeConfigForAdvancedEditor = <T,>(input: T): T => {
if (input === undefined || input === null) return input
// Normalize values so copy/export from JsonEditor stays valid JSON.
const serialized = JSON.stringify(input, (_key, value) => {
if (typeof value === 'string' && UNDEFINED_SENTINELS.has(value)) return undefined
return value
})
if (serialized === undefined) return input
try {
return JSON.parse(serialized)
} catch {
return input
}
}
export const AdvancedEditor = ({
loadConfig,
config,
convertStateToConfig,
stripConfig = config => config,
onExpandCollapse = () => {}
}) => {
const [advancedToggle, _setAdvancedToggle] = useState(false)
const [configTextboxValue, setConfigTextbox] = useState<Record<string, any>>({})
const setAdvancedToggle = val => {
_setAdvancedToggle(val)
onExpandCollapse()
}
const collapseFields: FilterFunction = input => {
if (['datasets', 'data', 'originalFormattedData', 'formattedData'].includes(String(input.key))) return true
return false
}
const onUpdate: UpdateFunction = val => {
setConfigTextbox(sanitizeConfigForAdvancedEditor(val.newData))
}
const getParsedConfig = () => {
let parsedConfig = stripConfig(config)
if (config.type !== 'dashboard') {
parsedConfig = convertStateToConfig()
}
return sanitizeConfigForAdvancedEditor(parsedConfig)
}
useEffect(() => {
// Only process config when advanced editor is open to improve performance
if (advancedToggle) {
setConfigTextbox(getParsedConfig())
}
}, [config, advancedToggle])
// Initialize config when advanced editor is first opened
const handleToggleOpen = () => {
if (!advancedToggle) {
// Process config only when opening for the first time
setConfigTextbox(getParsedConfig())
}
setAdvancedToggle(!advancedToggle)
}
const typeLookup = {
chart: ['Charts', 'https://www.cdc.gov/cove/index.html', <ChartIcon />],
dashboard: ['Dashboard', 'https://www.cdc.gov/cove/index.html', <ChartIcon />],
map: ['Maps', 'https://www.cdc.gov/cove/index.html', <MapIcon />],
'markup-include': ['Markup Include', 'https://www.cdc.gov/cove/index.html', <MarkupIncludeIcon />],
'data-bite': ['Data Bite', 'https://www.cdc.gov/cove/index.html', <ChartIcon />],
'waffle-chart': ['Waffle Chart', 'https://www.cdc.gov/cove/index.html', <ChartIcon />]
}
if (!config.type) return <></>
return (
<>
<a
href={typeLookup[config.type][1]}
target='_blank'
rel='noopener noreferrer'
className='guidance-link'
style={{ cursor: 'pointer !important' }}
>
{typeLookup[config.type][2]}
<div>
<span className='heading-3'>Get Help with {typeLookup[config.type][0]}</span>
<p>Examples and documentation</p>
</div>
</a>
<div className='advanced'>
<span className='advanced-toggle-link' onClick={handleToggleOpen}>
<span>{advancedToggle ? `— ` : `+ `}</span>Advanced Options
</span>
{advancedToggle && (
<React.Fragment>
<section className='error-box py-2 px-3 my-2'>
<div>
<strong className='pt-1'>Warning</strong>
<p>This can cause serious errors in your visualization.</p>
</div>
</section>
<p className='pb-2'>
This tool displays the actual <acronym title='JavaScript Object Notation'>JSON</acronym> configuration
that is generated by this editor and allows you to edit properties directly and apply them.
</p>
<JsonEditor
className='advanced-json-editor'
data={configTextboxValue}
onUpdate={onUpdate}
rootName=''
collapse={collapseFields}
/>
<button
className='btn btn-success m-2 p-2'
onClick={() => {
loadConfig(sanitizeConfigForAdvancedEditor(configTextboxValue))
setAdvancedToggle(!advancedToggle)
}}
>
Apply Configuration Changes
</button>
</React.Fragment>
)}
</div>
{/* Share with Partners Section */}
<EmbedEditor config={config} />
</>
)
}
export default AdvancedEditor