auto-component 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "auto-component",
3
+ "version": "0.0.1",
4
+ "main": "src/index.js",
5
+ "peerDependencies": {
6
+ "react": "^16.0.0",
7
+ "react-dom": "^16.0.0"
8
+ },
9
+ "files": [
10
+ "src"
11
+ ]
12
+ }
@@ -0,0 +1,93 @@
1
+ #content-creator {
2
+ position: relative;
3
+ width: 100%;
4
+ display: flex;
5
+ flex-direction: column;
6
+ align-items: center;
7
+ margin: 40px 0;
8
+ }
9
+ #content-creator div {
10
+ width: 100%;
11
+ display: flex;
12
+ flex-direction: row;
13
+ justify-content: center;
14
+ align-items: center;
15
+
16
+ }
17
+ #content-creator input {
18
+ width: 60%;
19
+ height: 40px;
20
+ border-radius: 5px;
21
+ color: #444;
22
+ font-size: 20px;
23
+ padding-left: 10px;
24
+ }
25
+ #content-creator button {
26
+ padding: 10px;
27
+ margin: 20px;
28
+ background: rgb(184, 230, 184);
29
+ color: #111;
30
+ border-radius: 5px;
31
+ box-shadow: 0px 0px 5px #fff;
32
+ }
33
+ #content-creator button:nth-child(3) {
34
+ padding: 10px 20px;
35
+ margin-left: 0px;
36
+ margin-right: 0px;
37
+ background: #db9d9d;
38
+ }
39
+ #content-creator button:hover {
40
+ background: green;
41
+ color: #eee;
42
+ }
43
+ #content-creator button:nth-child(3):hover {
44
+ background: #dc3c3c;
45
+ }
46
+ #content-creator div:nth-child(2) {
47
+ display: flex;
48
+ flex-direction: column;
49
+ }
50
+ .tab {
51
+ margin: 0 10%;
52
+ background: #444;
53
+ border-radius: 5px 5px 0 0;
54
+ cursor: pointer;
55
+ }
56
+ .tab:hover {
57
+ background: #666;
58
+ }
59
+ .selected {
60
+
61
+ background: #888;
62
+ color: #111;
63
+ }
64
+ #content-creator pre {
65
+ position: relative;
66
+ text-align: left;
67
+ max-width: 90%;
68
+ min-width: 90%;
69
+ max-height: 60vh;
70
+ background: #aaa;
71
+ color: #111;
72
+ padding: 40px 10px 40px 10px;
73
+ overflow: auto;
74
+ border: 2px solid black;
75
+ border-radius: 10px;
76
+ box-shadow: 0px 0px 5px #fff;
77
+ }
78
+ .copy-btn {
79
+ position: absolute;
80
+ top: 120px;
81
+ right: 7%;
82
+ max-width: 40px;
83
+ max-height: 30px;
84
+ cursor: pointer;
85
+ background: rgba(0, 0, 0, 0.2);
86
+ border-radius: 5px;
87
+ padding: 10px;
88
+ color: #fff;
89
+ }
90
+
91
+ .copy-btn:hover {
92
+ background: #666;
93
+ }
@@ -0,0 +1,188 @@
1
+ import { useState, useEffect } from 'react'
2
+ import beautify from 'js-beautify'
3
+ import response from '../../assets/response.js'
4
+ import './Componenter.css'
5
+
6
+ const Componenter = ({ exclusions }) => {
7
+
8
+ //**-----------**/
9
+ // ** State ** //
10
+ //**---------**/
11
+ const [currentHtml, setHtml] = useState('')
12
+ const [currentStyle, setStyles] = useState('')
13
+ const [ currentRequest, setRequest ] = useState('')
14
+
15
+ const [ responseData, setResponseData ] = useState(null)
16
+ const [ requestData, setRequestData ] = useState(null)
17
+
18
+ const [ activeTab, setActiveTab ] = useState('request')
19
+
20
+ //**-------------------------**/
21
+ // ** HTML/CSS Formatting ** //
22
+ //**-----------------------**/
23
+ // get the html/style for the current page and set state
24
+ const htmlContent = () => {
25
+ const body = document.querySelector('body')
26
+ const htmlContent = body ? body.innerHTML : ''
27
+ const cssStyles = document.documentElement.innerHTML
28
+
29
+ const cleanedHtml = cleanHtml(htmlContent, exclusions)
30
+ const cleanedStyles = cleanStyles(cssStyles)
31
+
32
+ setHtml(cleanedHtml)
33
+ setStyles(cleanedStyles)
34
+ }
35
+
36
+ // format html for display (breaks/indentation)
37
+ const formatHtml = (html) => {
38
+ return beautify.html(html, {
39
+ indent_size: 2,
40
+ wrap_line_length: 80,
41
+ max_preserve_newlines: 1,
42
+ })
43
+ }
44
+
45
+ // clean html of exclusions:
46
+ // if an element className includes 'exclude'
47
+ // (partial) the element wrapper remains, but contents removed
48
+ // OR, (full) the element and it's content are excluded from output
49
+
50
+ // partial exclusion
51
+ const cleanHtml = (html) => {
52
+ const regexExcludeClass = /(<[^>]*\sclass\s*=\s*['"]([^'"]*exclude[^'"]*)['"][^>]*>)[\s\S]*?(<\/[^>]*>)/g;
53
+ const cleanedHtml = html.replace(regexExcludeClass, '$1$3');
54
+ const scriptIndex = cleanedHtml.lastIndexOf('<script');
55
+
56
+ return formatHtml(cleanedHtml.substring(0, scriptIndex));
57
+ };
58
+
59
+ // full exclusion
60
+ const cleanHtmlFull = (html) => {
61
+ const regexExcludeClass = /<[^>]*\sclass\s*=\s*['"]([^'"]*exclude[^'"]*)['"][^>]*>[\s\S]*?<\/[^>]*>/g
62
+ const cleanedHtml = html.replace(regexExcludeClass, '')
63
+ const scriptIndex = cleanedHtml.lastIndexOf('<script')
64
+
65
+ return formatHtml(cleanedHtml.substring(0, scriptIndex))
66
+ }
67
+
68
+ // exclude non <style> data and remove comments
69
+ const cleanStyles = (css) => {
70
+ const styleRegex = /<style\b[^>]*>(.*?)<\/style>/gs
71
+ const matches = css.match(styleRegex)
72
+
73
+ if (matches) {
74
+ const cleanedMatches = matches.map(match => match.replace(/\/\*[\s\S]*?\*\//g, ''))
75
+ return cleanedMatches
76
+ }
77
+
78
+ return null
79
+ }
80
+
81
+
82
+ useEffect(() => {
83
+ htmlContent()
84
+ }, [])
85
+
86
+ //**---------------------------**/
87
+ // ** API Requests/Response ** //
88
+ //**-------------------------**/
89
+
90
+ // TEMP FUNCTION FOR TESTING
91
+ const sendRequest = (data) => {
92
+ return(response)
93
+ }
94
+
95
+ // make api request with updated state data
96
+ const handleRequest = async () => {
97
+ try {
98
+ const res = await sendRequest(requestData)
99
+ if (res) {
100
+ setResponseData(formatHtml(res.html))
101
+ setActiveTab('response')
102
+ }
103
+ } catch (err) {
104
+ console.log(err)
105
+ }
106
+ setIsLoading(false)
107
+ }
108
+
109
+ //**-----------------**/
110
+ // ** UI Handling ** //
111
+ //**---------------**/
112
+
113
+ const handleChange = (e) => {
114
+ e.preventDefault()
115
+ setRequest(e.target.value)
116
+ }
117
+
118
+ const handleGenerate = async (e) => {
119
+ await setRequestData({
120
+ request: currentRequest,
121
+ html: currentHtml
122
+ })
123
+ handleRequest()
124
+ }
125
+
126
+ const handleReset = () => {
127
+ setRequest('')
128
+ setResponseData('')
129
+ }
130
+
131
+ const handleRequestTab = () => {
132
+ setActiveTab('request')
133
+ }
134
+ const handleResponseTab = () => {
135
+ setActiveTab('response')
136
+ }
137
+
138
+ //**---------------**/
139
+ // ** Rendering ** //
140
+ //**-------------**/
141
+
142
+ const requestHTML = currentHtml ? (
143
+ `Review the details below for accuracy and privacy concerns.
144
+ If the contents of an element should be excluded, add the 'exclude' class to the element.
145
+ Click Generate to send the request and receive the auto component AI generated code.
146
+
147
+ User Request:\n `
148
+ + currentRequest
149
+ + "\n\nUser HTML:\n"
150
+ + currentHtml
151
+ ) : 'There was an error collecting your HTML. Ensure no top level elements are assigned the class "exclude"'
152
+
153
+ const responseHtml = responseData ? (
154
+ responseData
155
+ ) : 'No response has been generated'
156
+
157
+
158
+ return (
159
+ <>
160
+ {/* create the display window */}
161
+ <div id="content-creator">
162
+ <div>
163
+ <input type="text" value={currentRequest} onChange={handleChange} placeholder="Enter a request"></input>
164
+ <button onClick={handleGenerate}>Generate</button>
165
+ <button onClick={handleReset}>X</button>
166
+ </div>
167
+ <div>
168
+ <div>
169
+ <div className={`${activeTab === 'request' ? 'selected' : ''} tab`} onClick={handleRequestTab}>
170
+ Request
171
+ </div>
172
+ <div className={`${activeTab === 'response' ? 'selected' : ''} tab`} onClick={handleResponseTab}>
173
+ Response
174
+ </div>
175
+ </div>
176
+ <pre>
177
+ {activeTab === 'request' ? requestHTML : responseHtml}
178
+ </pre>
179
+ </div>
180
+ <div className="copy-btn">
181
+ copy
182
+ </div>
183
+ </div>
184
+ </>
185
+ )
186
+ }
187
+
188
+ export default Componenter