import React, { Component } from "react"; import {BsArrowLeftShort, BsSearch} from 'react-icons/bs'; import "../../css/dist/output.css" import { random_colour, random_emoji } from "../../helper/visual"; import { Message, Header, Modal, Button, Icon } from 'semantic-ui-react' export default class Navbar extends Component{ constructor(){ super() this.fetch_classes() this.temp_host = 0 } state = {open : true, menu : [], colour : [], text : "", name : "", emoji : [], mode : false, modal : false, error : false } async fetch_classes(){ try { setInterval( async () => { const menu = [] await fetch("http://localhost:2000/api/open/ports", { method: 'GET', mode : 'cors',}) .then(response => response.json()) .then(data => { for (var i = 0; i < data.length; i++){ menu.push(data[i]) } }) .catch(error => {console.log(error)}) var diff = menu.length - this.state.menu.length if(diff !== 0){ this.hanelTabs(menu, diff) } },1000); }catch(e){ console.log(e) } } append_gradio = () => { // var expression = /((http([s]){0,1}:\/\/){0,1}(localhost|127.0.0.1){1}(([:]){0,1}[\0-9]{4}){0,1}\/{0,1}){1}/g; // var regex = new RegExp(expression); // if(!this.state.text.match(regex) || (this.state.text === "http://localhost:3001/")){ // this.setState({open : this.state.open, // menu : this.state.menu, // text: this.state.text, // name : this.state.name, // colour : this.state.colour, // emoji : this.state.emoji, // error : true, // modal : this.state.modal }) // return // } // if((this.state.text.includes("localhost") || this.state.text.includes("127.0.0.1")) // && this.state.menu.some(e => e.host === this.state.text)){ // this.setState({open : this.state.open, // menu : this.state.menu, // text: this.state.text, // name : this.state.name, // colour : this.state.colour, // emoji : this.state.emoji, // error : true, // modal : this.state.modal }) // return // } fetch(this.state.text, {method : "GET", mode: 'no-cors'}).then((re) => { console.log(re) fetch("http://localhost:2000/api/append/port", {method: 'POST', mode : 'cors', headers : { 'Content-Type' : 'application/json' }, body: JSON.stringify({file : "", kwargs : {}, name : this.state.name === "" ?`temp_class_${this.temp_host++}` : `${this.state.name}`, port: 0 , host : this.state.text}) }).then(resp => { this.setState({open : this.state.open, menu : this.state.menu, text: "", name : "", colour : this.state.colour, emoji : this.state.emoji, error : false, modal : false }) }).catch(() => this.setState({open : this.state.open, menu : this.state.menu, text: this.state.text, colour : this.state.colour, emoji : this.state.emoji, error : true, modal : this.state.modal })) }) } handelModal = (bool) => { this.setState({open : this.state.open, menu : this.state.menu, text: this.state.text, name: this.state.name, colour : this.state.colour, emoji : this.state.emoji, error : !bool ? false : this.state.error , modal : bool}) } onDragStart = (event, nodeType, item, index) => { console.log(item) event.dataTransfer.setData('application/reactflow', nodeType); event.dataTransfer.setData('application/host', item.host) event.dataTransfer.setData('application/name', item.name) event.dataTransfer.setData('application/colour', this.state.colour[index]) event.dataTransfer.setData('application/item', JSON.stringify(item)) event.dataTransfer.effectAllowed = 'move'; }; onDragDrop = (e) => { e.preventDefault(); var item = JSON.parse(e.dataTransfer.getData('application/item')); fetch("http://localhost:2000/api/remove/port", {method : "POST", mode: 'cors', headers : { 'Content-Type' : 'application/json' }, body: JSON.stringify(item) }).then((re)=>{ }) } hanelTabs = (e, d) => { // if less then 0 we must remove colour's and emoji's // get index of the object // remove var c = [] var j = [] if(d < 0){ var a = this.state.menu.filter(item => !e.includes(item)) // get the items not in menu anymore c = this.state.colour j = this.state.emoji for(var k=0; k < d; k++){ c.splice(this.state.menu.indexOf(a[k]), 1) j.splice(this.state.menu.indexOf(a[k]), 1) } this.setState({open : this.state.open, menu : e, text: this.state.text, name: this.state.name, colour : c, emoji : j, error : this.state.error, modal : this.state.modal }) }else{ //append new colours for(var i =0; i < d; i++){ c.push(random_colour()); j.push(random_emoji()); } this.setState({open : this.state.open, menu : e, text: this.state.text, name: this.state.name, colour : this.state.colour.concat(c), emoji : this.state.emoji.concat(j), error : this.state.error, modal : this.state.modal }) } } appendTabs = () => { this.setState({open : this.state.open, menu : this.state.menu, text: this.state.text, name: this.state.name, colour : [...this.state.colour, random_colour] , emoji : [...this.state.emoji, random_emoji], error : this.state.error, modal : this.state.modal }) } handelNavbar = () => { this.setState({open : !this.state.open, menu : this.state.menu, text: this.state.text, name: this.state.name, colour : this.state.colour, emoji : this.state.emoji, error : this.state.error, modal : this.state.modal }) } updateText(e, type){ this.setState({open : this.state.open, menu : this.state.menu, text: type === "text" ? e.target.value : this.state.text, name: type === "name" ? e.target.value : this.state.name, colour : this.state.colour, emoji : this.state.emoji, error : this.state.error, modal : this.state.modal }) } subComponents(item, index){ if (this.state.colour.length === 0 || this.state.emoji.length === 0){ this.hanelTabs() } return(<>
  • this.onDragStart(event, 'custom', item, index)} className={` text-white text-sm flex text-center items-center cursor-pointer p-4 px-2 mt-4 ${ this.state.open ? `hover:animate-pulse ${this.state.colour[index] === null ? "" : this.state.colour[index]} ` : `hidden`} rounded-md mt-2`} draggable> {`${this.state.emoji[index] === null ? "" : this.state.emoji[index]} ${item.name}`}
  • ) } render(){ // compare the menu if the menu has changed change the state of the navbar // states to change // 1. tabs // 2. add colour return (

    Gradio Flow 🤗

    {this.handelModal(true)}}>
    🌐
    Append Shared Gradio Hosts
    This feature just allows you to append other hosted applications like gradio/streamlit to have it within the Gradio Flow interface, that may not be sending its hosting location to the api or have shared links to other people's gradio application.
    { this.state.error && 🚫 Connection to url

    🤔 Either the connection is forbidden, already exist within the menu or it is unreachable...

    }
      {this.state.menu.map((menu, index) => {return this.subComponents(menu, index)})}
    {e.preventDefault()}} onDrop={(e)=>{this.onDragDrop(e)}}>
    ) } } /** * * **/