Skip to content

Commit

Permalink
better download and mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed May 16, 2024
1 parent 19b22ae commit 8bc88ac
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ui/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function App () {
<div>
<input type='file' className='mb-2 w-full file-input' onChange={handleFileChange} />
</div>
{!!fields && (<ButtonDownloadJSON tree={fields} typeMap={typeMap} nameMap={typeMap} />)}
{!!fields && (<ButtonDownloadProto tree={fields} typeMap={typeMap} nameMap={typeMap} />)}
{!!fields && (<ButtonDownloadJSON tree={fields} typeMap={typeMap} nameMap={nameMap} />)}
{!!fields && (<ButtonDownloadProto tree={fields} typeMap={typeMap} nameMap={nameMap} />)}
</div>

{!!fields && (
Expand Down
7 changes: 6 additions & 1 deletion ui/src/ButtonDownloadJSON.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export default function ButtonDownloadJSON ({ tree, prefix = 'f', typeMap, nameMap, children = 'Download JSON', className = 'btn btn-primary', filename = 'download.json' }) {
const handleClick = e => {
e.target.setAttribute('href', `data:application/json;base64,${btoa(unescape(encodeURIComponent(JSON.stringify(tree.toJS(undefined, prefix, nameMap, typeMap), null, 2))))}`)
const d = tree.toJS(undefined, prefix, nameMap, typeMap)
console.log({ tree, typeMap, nameMap, json: d })
const u = URL.createObjectURL(new Blob([JSON.stringify(d, null, 2)], { type: 'application/json' }))
e.target.setAttribute('href', u)
setTimeout(() => URL.revokeObjectURL(u), 0)
}

return <a className={className} onClick={handleClick} href='#proto' download={filename}>{children}</a>
}
8 changes: 6 additions & 2 deletions ui/src/ButtonDownloadProto.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export default function ButtonDownloadProto ({ tree, prefix = 'f', typeMap, nameMap, messageName = 'MessageRoot', children = 'Download Proto', className = 'btn btn-primary', filename = 'download.proto' }) {
export default function ButtonDownloadProto ({ tree, prefix = 'f', typeMap, nameMap, messageName = 'MessageRoot', children = 'Download Proto', className = 'btn btn-secondary', filename = 'download.proto' }) {
const handleClick = e => {
e.target.setAttribute('href', `data:text/plain;base64,${btoa(unescape(encodeURIComponent(tree.toProto(undefined, prefix, nameMap, typeMap, messageName))))}`)
const d = tree.toProto(undefined, prefix, nameMap, typeMap, messageName)
const u = URL.createObjectURL(new Blob([d], { type: 'text/plain' }))
e.target.setAttribute('href', u)
setTimeout(() => URL.revokeObjectURL(u), 0)
}

return <a className={className} onClick={handleClick} href='#proto' download={filename}>{children}</a>
}
4 changes: 1 addition & 3 deletions ui/src/ProtoDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ badgeColors[wireTypes.I64] = 'accent'
badgeColors[wireTypes.I32] = 'secondary'
badgeColors[wireTypes.SGROUP] = 'primary'


const hex = (b) => [...b].map((c) => c.toString(16).padStart(2, '0')).join(' ')

export default function ProtoDisplay ({ open = false, tree, typeMap = {}, nameMap = {}, className }) {
const [o, setO] = useState(open)
if (tree) {
tree.renderType = 'sub'
}
return tree
? (
<details open={o} className={className}>
<details className={className} open={open}>
<summary>
<div className={`badge badge-${badgeColors[tree.type]} gap-2`}>{tree.name || 'root (0)'}</div>
<p className='text-gray-500 italic'>{parseLabels[tree.renderType]}</p>
Expand Down

0 comments on commit 8bc88ac

Please sign in to comment.