Spaces:
Configuration error
Configuration error
zack
commited on
Commit
·
c2465c6
1
Parent(s):
030e997
chore: remove src/components/Navagation/navbar.js 🧹
Browse files
frontend/src/components/Navagation/navbar.js
DELETED
@@ -1,242 +0,0 @@
|
|
1 |
-
import React, { Component } from "react";
|
2 |
-
import { Icon } from 'semantic-ui-react'
|
3 |
-
import Import from '../Modal/importer'
|
4 |
-
import { random_colour, random_emoji } from "../../helper/visual";
|
5 |
-
|
6 |
-
import "../../css/dist/output.css"
|
7 |
-
|
8 |
-
import {BsArrowLeftShort} from 'react-icons/bs';
|
9 |
-
import {ReactComponent as ReactLogo} from '../../images/logo.svg'
|
10 |
-
|
11 |
-
export default class Navbar extends Component{
|
12 |
-
constructor(props){
|
13 |
-
super(props)
|
14 |
-
this.temp_host = 0
|
15 |
-
this.deleteNode = props.onDelete
|
16 |
-
this.state = {
|
17 |
-
open : true,
|
18 |
-
menu : [],
|
19 |
-
colour : props.colour || [],
|
20 |
-
text : "",
|
21 |
-
name : "",
|
22 |
-
emoji : props.emoji || [],
|
23 |
-
mode : false,
|
24 |
-
modal : false,
|
25 |
-
error : false
|
26 |
-
}
|
27 |
-
|
28 |
-
}
|
29 |
-
|
30 |
-
componentDidMount(){
|
31 |
-
this.fetch_classes()
|
32 |
-
|
33 |
-
}
|
34 |
-
|
35 |
-
/**
|
36 |
-
* Asynchronously call the Flask api server every second to check if there exist a gradio application info
|
37 |
-
* @return null
|
38 |
-
*/
|
39 |
-
fetch_classes = async () => {
|
40 |
-
try {
|
41 |
-
setInterval( async () => {
|
42 |
-
await fetch("http://localhost:2000/api/open/ports", { method: 'GET', mode : 'cors',})
|
43 |
-
.then(response => response.json())
|
44 |
-
.then(data => {
|
45 |
-
this.handelTabs(this.state.menu,data, data)
|
46 |
-
this.setState({menu : data})
|
47 |
-
})
|
48 |
-
.catch(error => {console.log(error)})
|
49 |
-
|
50 |
-
},1000);
|
51 |
-
}catch(e){
|
52 |
-
console.log(e)
|
53 |
-
}
|
54 |
-
}
|
55 |
-
|
56 |
-
/**
|
57 |
-
* Append new node from the user
|
58 |
-
*/
|
59 |
-
appendStreamNode = async (type) => {
|
60 |
-
const pattern = {
|
61 |
-
local : /^https?:\/\/(localhost)*(:[0-9]+)?(\/)?$/,
|
62 |
-
share : /^https?:\/\/*([0-9]{5})*(-gradio)*(.app)?(\/)?$/,
|
63 |
-
hugginFace : /^https?:\/\/*(hf.space)\/*(embed)\/*([a-zA-Z0-9+_-]+)\/*([a-zA-Z0-9+_-]+)\/*([+])?(\/)?$/
|
64 |
-
}
|
65 |
-
|
66 |
-
if (this.state.name.length > 20 ||
|
67 |
-
this.state.text === ""||
|
68 |
-
this.state.menu.findIndex(element => {return element.name.toLowerCase() === this.state.name.toLowerCase() || element.host.includes(this.state.text) }) !== -1 ||
|
69 |
-
this.state.text.includes(" ") ||
|
70 |
-
(!pattern.local.test(this.state.text) &&
|
71 |
-
!pattern.share.test(this.state.text) &&
|
72 |
-
!pattern.hugginFace.test(this.state.text))){
|
73 |
-
|
74 |
-
this.setState({
|
75 |
-
'text': '',
|
76 |
-
'name': '',
|
77 |
-
'error': true})
|
78 |
-
return
|
79 |
-
}
|
80 |
-
|
81 |
-
fetch(this.state.text, {method : "GET", mode: 'no-cors'}).then((re) => {
|
82 |
-
fetch("http://localhost:2000/api/append/port", {method: 'POST', mode : 'cors', headers : { 'Content-Type' : 'application/json' }, body: JSON.stringify({file : "", kwargs : { type : type }, name : this.state.name === "" ?`temp_class_${this.temp_host++}` : `${this.state.name}`, port: 0 , host : this.state.text}) }).then(resp => {
|
83 |
-
this.setState({'text': "",'name' : "",'error' : false,'modal' : false })
|
84 |
-
|
85 |
-
}).catch(() => this.setState({'text': '', 'name' : '', 'error' : true, }))
|
86 |
-
}).catch((err)=> this.setState({'text': '','name' : '', 'error' : true,}))
|
87 |
-
}
|
88 |
-
|
89 |
-
/**
|
90 |
-
* error check the user input
|
91 |
-
* @param {*} bool boolean of the current state of the modal
|
92 |
-
*/
|
93 |
-
handelModal = (bool) => {
|
94 |
-
this.setState({'error' : !bool ? false : this.state.error ,
|
95 |
-
'modal' : bool})
|
96 |
-
}
|
97 |
-
|
98 |
-
/**
|
99 |
-
* when dragged get all the information needed
|
100 |
-
* @param {*} event
|
101 |
-
* @param {*} nodeType string 'custom' node type
|
102 |
-
* @param {*} item object information returned from the api
|
103 |
-
* @param {*} index current index
|
104 |
-
*/
|
105 |
-
onDragStart = (event, nodeType, item, index) => {
|
106 |
-
event.dataTransfer.setData('application/reactflow', nodeType);
|
107 |
-
event.dataTransfer.setData('application/style', JSON.stringify({colour : this.state.colour[index], emoji : this.state.emoji[index] }))
|
108 |
-
event.dataTransfer.setData('application/item', JSON.stringify(item))
|
109 |
-
event.dataTransfer.effectAllowed = 'move';
|
110 |
-
};
|
111 |
-
|
112 |
-
/**
|
113 |
-
* droped event that occurs when the user drops the Tab within the tash div.
|
114 |
-
* The function just deletes all nodes within React-Flow enviorment related,
|
115 |
-
* and remove it from the api.
|
116 |
-
* @param {*} e drop event
|
117 |
-
*/
|
118 |
-
onDragDrop = (e) => {
|
119 |
-
e.preventDefault();
|
120 |
-
var item = JSON.parse(e.dataTransfer.getData('application/item'));
|
121 |
-
fetch("http://localhost:2000/api/remove/port", {method : "POST", mode: 'cors', headers : { 'Content-Type' : 'application/json' }, body: JSON.stringify(item) }).then((re)=>{
|
122 |
-
this.deleteNode(item.name)
|
123 |
-
})
|
124 |
-
|
125 |
-
}
|
126 |
-
|
127 |
-
/**
|
128 |
-
* update the tabs within the navbar
|
129 |
-
* @param {*} e current menu
|
130 |
-
* @param {*} d integer variable of the diffence between the current menu and new menu updated ment
|
131 |
-
*/
|
132 |
-
handelTabs = async (e, d) => {
|
133 |
-
// if less then 0 we must remove colour's and emoji's
|
134 |
-
// get index of the object
|
135 |
-
// remove
|
136 |
-
var c = []
|
137 |
-
var j = []
|
138 |
-
if (d.length - e.length === 0) return
|
139 |
-
else if(d.length - e.length < 0){
|
140 |
-
var a = this.state.menu.filter(item => e.includes(item)) // get the items not in menu anymore
|
141 |
-
c = this.state.colour
|
142 |
-
j = this.state.emoji
|
143 |
-
for(var k=0; k < d.length; k++){
|
144 |
-
c.splice(this.state.menu.indexOf(a[k]), 1)
|
145 |
-
j.splice(this.state.menu.indexOf(a[k]), 1)
|
146 |
-
}
|
147 |
-
this.setState({'colour' : c, 'emoji' : j})
|
148 |
-
}else{
|
149 |
-
//append new colours
|
150 |
-
for(var i =0; i < d.length; i++){
|
151 |
-
c.push(random_colour(i === 0 ? null : c[i-1]));
|
152 |
-
j.push(random_emoji(i === 0 ? null : c[i-1]));
|
153 |
-
|
154 |
-
}
|
155 |
-
const colour = [...this.state.colour]
|
156 |
-
const emoji = [...this.state.emoji]
|
157 |
-
this.setState({'colour' : [...colour, ...c], 'emoji' : [...emoji, ...j],})
|
158 |
-
}
|
159 |
-
}
|
160 |
-
|
161 |
-
handelError = (boolean) => {
|
162 |
-
this.setState({'error' : boolean})
|
163 |
-
}
|
164 |
-
|
165 |
-
/**
|
166 |
-
* handel navagation open and close function
|
167 |
-
*/
|
168 |
-
handelNavbar = () => {
|
169 |
-
this.setState({'open' : !this.state.open})
|
170 |
-
}
|
171 |
-
|
172 |
-
/**
|
173 |
-
*
|
174 |
-
* @param {*} e : event type to get the target value of the current input
|
175 |
-
* @param {*} type : text | name string that set the changed value of the input to the current value
|
176 |
-
*/
|
177 |
-
updateText = (e, type) => {
|
178 |
-
this.setState({[`${type}`] : e.target.value })
|
179 |
-
}
|
180 |
-
|
181 |
-
/**
|
182 |
-
*
|
183 |
-
* @param {*} item : object infomation from the flask api
|
184 |
-
* @param {*} index : current index with in the list
|
185 |
-
* @returns div component that contians infomation of gradio
|
186 |
-
*/
|
187 |
-
subComponents(item, index){
|
188 |
-
|
189 |
-
return(<>
|
190 |
-
<li key={`${index}-li`} onDragStart={(event) => this.onDragStart(event, 'custom', item, index)}
|
191 |
-
className={` text-white text-md flex flex-col text-center items-center cursor-grab shadow-lg
|
192 |
-
p-5 px-2 mt-4 rounded-md ${ this.state.open ? `hover:animate-pulse ${this.state.colour[index] === null ? "" : this.state.colour[index]} ` : `hidden`} break-all -z-20`} draggable>
|
193 |
-
|
194 |
-
<div key={`${index}-div`} className=" absolute -mt-2 text-4xl opacity-60 z-10 ">{`${this.state.emoji[index] === null ? "" : this.state.emoji[index]}`}</div>
|
195 |
-
<h4 key={`${index}-h4`} className={` max-w-full font-sans text-blue-50 leading-tight font-bold text-xl flex-1 z-20 ${this.state.open ? "" : "hidden"}`} style={{"textShadow" : "0px 1px 2px rgba(0, 0, 0, 0.25)"}} >{`${item.name}`} </h4>
|
196 |
-
|
197 |
-
</li >
|
198 |
-
|
199 |
-
</>)
|
200 |
-
}
|
201 |
-
|
202 |
-
|
203 |
-
render(){
|
204 |
-
|
205 |
-
return (<div>
|
206 |
-
|
207 |
-
<div className={`z-10 flex-1 float-left bg-white dark:bg-stone-900 h-screen p-5 pt-8 ${this.state.open ? "lg:w-72 md:64 sm:w-60" : "w-10"} duration-300 absolute shadow-2xl border-black border-r-[1px] dark:border-white dark:text-white`} >
|
208 |
-
|
209 |
-
<BsArrowLeftShort onClick={this.handelNavbar} className={` bg-white text-Retro-darl-blue text-3xl rounded-full absolute -right-3 top-9 border border-black cursor-pointer ${!this.state.open && 'rotate-180'} dark:border-white duration-300 dark:text-white dark:bg-stone-900 `}/>
|
210 |
-
|
211 |
-
<div className="inline-flex w-full">
|
212 |
-
<h1 className={`font-sans font-bold text-lg ${this.state.open ? "" : "hidden"} duration-500 ml-auto mr-auto`}> <ReactLogo className="w-9 h-9 ml-auto mr-auto"/> Gradio Flow </h1>
|
213 |
-
</div>
|
214 |
-
|
215 |
-
<div className={`rounded-md text-center ${this.state.open ? "" : "px-0"} py-3`} onClick={() => {this.handelModal(true)}}>
|
216 |
-
<div className={` text-center bg-transparent w-full h-10 border border-slate-300 hover:border-Retro-purple hover:animate-pulse border-dashed rounded-md py-2 pl-5 ${this.state.open ? "pr-3" : "hidden"} shadow-sm sm:text-sm`}>
|
217 |
-
<Icon className=" block mr-auto ml-auto" name="plus"/>
|
218 |
-
</div>
|
219 |
-
</div>
|
220 |
-
<Import open={this.state.modal}
|
221 |
-
quitHandeler={this.handelModal}
|
222 |
-
textHandler={this.updateText}
|
223 |
-
appendHandler={this.appendStreamNode}
|
224 |
-
handelError={this.handelError}
|
225 |
-
catch={this.state.error}/>
|
226 |
-
|
227 |
-
<div className=" relative z-10 h-auto overflow-auto pt-4">
|
228 |
-
<ul className="pt-2">
|
229 |
-
{this.state.menu.map((menu, index) => {return this.subComponents(menu, index)})}
|
230 |
-
</ul>
|
231 |
-
</div>
|
232 |
-
|
233 |
-
<div className={`${this.state.open ? "" : "hidden"} absolute bottom-0 left-0 w-full text-center p-5`} onDragOver={(e)=> {e.preventDefault()}} onDrop={(e)=>{this.onDragDrop(e)}}>
|
234 |
-
<div className={` text-center bg-transparent w-full h-10 border border-red-600 border-dashed rounded-md py-2 pl-5 p-4 ${this.state.open ? "pr-3" : "hidden"} shadow-sm sm:text-sm`}>
|
235 |
-
<Icon name='trash alternate' />
|
236 |
-
</div>
|
237 |
-
</div>
|
238 |
-
</div>
|
239 |
-
|
240 |
-
</div>)
|
241 |
-
}
|
242 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|