forked from daveschumaker/artbot-for-stable-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayRawData.tsx
142 lines (130 loc) · 3.69 KB
/
DisplayRawData.tsx
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
import clsx from 'clsx'
import { useState } from 'react'
import { toast } from 'react-toastify'
import ImageParamsForApi from '../models/ImageParamsForApi'
import CopyIcon from './icons/CopyIcon'
import EyeIcon from './icons/EyeIcon'
import { Button } from './UI/Button'
const DisplayRawData = ({ data }: { data: any }) => {
const [expandJson, setExpandJson] = useState(false)
const [display, setDisplay] = useState('apiParams')
const classes = [
'bg-slate-800',
'font-mono',
'text-white',
'text-sm',
'overflow-x-auto',
'p-2',
'w-full'
]
const cleanData = () => {
const obj = Object.assign({}, data)
if (obj.canvasData) {
obj.canvasData = '[true] (removed for logging error)'
} else {
delete obj.canvasData
}
if (obj.maskData) {
obj.maskData = '[true] (removed for logging error)'
} else {
delete obj.maskData
}
if (obj.base64String) {
delete obj.base64String
delete obj.thumbnail
}
if (obj.source_image) {
obj.source_image = '[true] (string removed for logging error)'
}
if (obj.source_mask) {
obj.source_mask = '[true] (string removed for logging error)'
}
return obj
}
const handleCopyData = () => {
const dataToCopy =
display === 'apiParams'
? JSON.stringify(
new ImageParamsForApi(data, { hasError: true }),
null,
2
)
: JSON.stringify(cleanData(), null, 2)
navigator?.clipboard?.writeText(dataToCopy).then(() => {
toast.success('Data copied', {
pauseOnFocusLoss: false,
position: 'top-center',
autoClose: 2500,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: false,
draggable: false,
progress: undefined,
theme: 'light'
})
})
}
return (
<div className="w-full flex flex-col gap-2">
<div className="mt-4">
<div
className="font-bold text-sm cursor-pointer text-[#14B8A6]"
onClick={() => {
if (expandJson) {
setExpandJson(false)
} else {
setExpandJson(true)
}
}}
>
[ {expandJson ? 'Hide' : 'View'} request parameters ]
</div>
</div>
{expandJson && (
<>
<div className={clsx(classes)}>
<pre>
{display === 'apiParams' && (
<>
{JSON.stringify(
new ImageParamsForApi(data, { hasError: true }),
null,
2
)}
</>
)}
{display === 'rawInput' && (
<>{JSON.stringify(cleanData(), null, 2)}</>
)}
</pre>
</div>
<div className="text-sm">
<strong>Tip:</strong> Request parameters are useful for potentially
debugging what went wrong during an image request.
</div>
<div className="flex flex-row gap-2">
<Button
title="View data"
onClick={() => {
if (display === 'apiParams') {
setDisplay('rawInput')
} else {
setDisplay('apiParams')
}
}}
>
<EyeIcon />
{display === 'apiParams' && <>View raw input</>}
{display === 'rawInput' && <>View request params</>}
</Button>
<Button title="Copy data" onClick={() => handleCopyData()}>
<CopyIcon />
Copy JSON
</Button>
</div>
</>
)}
</div>
)
}
export default DisplayRawData