import React, { useState, useEffect } from "react"; import { Button } from '../ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../ui/card"; import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "../ui/select"; import { Input } from "../ui/input"; import { Label } from "../ui/label"; import Loader from "../ui/loader"; import { useToast } from "../ui/toast"; import { Tabs, TabsList, TabsTrigger, TabsContent } from "../ui/tabs"; import { Textarea } from "../ui/textarea"; import { Switch } from "../ui/switch"; import { useIsLoggedIn } from '../../lib/isLoggedIn'; export default function NewCloudInstance() { const PUBLIC_UTHO_API_KEY = import.meta.env.PUBLIC_UTHO_API_KEY; const { isLoggedIn, loading, error, sessionData } = useIsLoggedIn(); const { showToast } = useToast(); const [isLoading, setIsLoading] = useState(false); const [isFetchingData, setIsFetchingData] = useState(true); const [deployError, setDeployError] = useState() const [deployStatus, setDeployStatus] = useState({}); const [copied, setCopied] = useState(false); const [plans, setPlans] = useState([ { "id": "10027", "type": "cloud", "slug": "dedicated-cpu", "disk": "80", "ram": "4096", "cpu": "2", "bandwidth": "1000", "dedicated_vcore": "1", "is_available": "YES", "price": 26.83, "price_currency": 26.83, "price_cur": "$26.8" }, { "id": "10028", "type": "cloud", "slug": "dedicated-cpu", "disk": "160", "ram": "8192", "cpu": "4", "bandwidth": "2000", "dedicated_vcore": "1", "is_available": "YES", "price": 53.72, "price_currency": 53.72, "price_cur": "$53.7" }, { "id": "10029", "type": "cloud", "slug": "dedicated-cpu", "disk": "480", "ram": "32768", "cpu": "8", "bandwidth": "3000", "dedicated_vcore": "1", "is_available": "YES", "price": 151.91, "price_currency": 151.91, "price_cur": "$151.9" }, { "id": "10030", "type": "cloud", "slug": "dedicated-cpu", "disk": "960", "ram": "65536", "cpu": "16", "bandwidth": "4000", "dedicated_vcore": "1", "is_available": "YES", "price": 292.19, "price_currency": 292.19, "price_cur": "$292.2" }, ]); // const [sshKeys, setSshKeys] = useState([]); const [showAddSshKey, setShowAddSshKey] = useState(false); const [vpcs, setVpcs] = useState(); const [images, setImages] = useState([ { "distro": null, "distribution": "Alma Linux", "version": "9.2 x86_64", "image": "almalinux-9.2-x86_64", "cost": 0 }, { "distro": null, "distribution": "Alma Linux", "version": "8.4 x86_64", "image": "AlmaLinux_8.4_x86_64", "cost": 0 }, { "distro": null, "distribution": "Centos", "version": "7.3 x86_64", "image": "centos-7.3-x86_64", "cost": 0 }, { "distro": null, "distribution": "Centos", "version": "8 x86_64", "image": "centos-8-x86_64", "cost": 0 }, { "distro": null, "distribution": "Centos", "version": "7.9 x86_64", "image": "centos-7.9-x86_64", "cost": 0 }, { "distro": null, "distribution": "Centos", "version": "Ameyo x86_64", "image": "AmeyoCentos7", "cost": 0 }, { "distro": null, "distribution": "Debian", "version": "9.4 x86_64", "image": "debian-9.4-x86_64", "cost": 0 }, { "distro": null, "distribution": "Debian", "version": "9.13 x86_64", "image": "debian-9.13-x86_64", "cost": 0 }, { "distro": null, "distribution": "Debian", "version": "10 x86_64", "image": "debian-10-x86_64", "cost": 0 }, { "distro": null, "distribution": "Debian", "version": "11 x86_64", "image": "debian-11-x86_64", "cost": 0 }, { "distro": null, "distribution": "Debian", "version": "12 x86_64", "image": "debian-12-x86_64", "cost": 0 }, { "distro": null, "distribution": "Fedora", "version": "34 x86_64", "image": "fedora-34-x86_64", "cost": 0 }, { "distro": null, "distribution": "Fedora", "version": "32 x86_64", "image": "fedora-32-x86_64", "cost": 0 }, { "distro": null, "distribution": "Fedora", "version": "33 x86_64", "image": "fedora-33-x86_64", "cost": 0 }, { "distro": null, "distribution": "FortiOS", "version": "7.6.2 ", "image": "fortios-762", "cost": 0 }, { "distro": null, "distribution": "Rocky Linux", "version": "8.7 x86_64", "image": "rocky-8.7-86_64", "cost": 0 }, { "distro": null, "distribution": "Rocky Linux", "version": "8.8 x86_64", "image": "rocky-8.8-x86_64", "cost": 0 }, { "distro": null, "distribution": "Rocky Linux", "version": "9.1 x86_64", "image": "rocky-9.1-x86_64", "cost": 0 }, { "distro": null, "distribution": "Rocky Linux", "version": "9.2 x86_64", "image": "rocky-9.2-x86_64", "cost": 0 }, { "distro": null, "distribution": "Ubuntu", "version": "20.04 x86_64", "image": "ubuntu-20.04-x86_64", "cost": 0 }, { "distro": null, "distribution": "Ubuntu", "version": "18.04 x86_64", "image": "ubuntu-18.10-x86_64", "cost": 0 }, { "distro": null, "distribution": "Ubuntu", "version": "22.04 x86_64", "image": "ubuntu-22.04-x86_64", "cost": 0 }, { "distro": null, "distribution": "Windows", "version": "Server 2012R2 x86_64", "image": "windows-server2012r2-x86_64", "cost": 1200 }, { "distro": null, "distribution": "Windows", "version": "Server 2016 x86_64", "image": "windows-2016", "cost": 1200 }, { "distro": null, "distribution": "Windows", "version": "Server 2019 x86_64", "image": "windows2019-10nov2020", "cost": 1200 }, { "distro": null, "distribution": "Windows", "version": "Server 2022 x86_64", "image": "windows-2022", "cost": 1200 } ]) const [formData, setFormData] = useState({ dcslug: '', planid: '', billingcycle: "hourly", auth: "option2", enable_publicip: '', subnetRequired: '', firewall: "23434645", cpumodel: "intel", enablebackup: '', root_password: '', support: "unmanaged", vpc: '', cloud: [ { hostname: '' } ], image: '', sshkeys: '' }); useEffect(() => { const fetchData = async () => { try { const vpcResponse = await fetch('https://api.utho.com/v2/vpc', { headers: { "Authorization": `Bearer ${PUBLIC_UTHO_API_KEY}` } }); const vpcsData = await vpcResponse.json(); setVpcs(vpcsData.vpc || []); } catch (error) { showToast({ title: "Error", description: "Failed to load configuration data", variant: "destructive" }); } finally { setIsFetchingData(false); } }; fetchData(); }, []); const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); try { const payload = { dcslug: formData.dcslug, planid: formData.planid, billingcycle: "hourly", auth: "option2", enable_publicip: formData.enable_publicip ? true : false, subnetRequired: false, firewall: "23434645", cpumodel: "intel", enablebackup: formData.enablebackup ? true : false, root_password: formData.rootPassword, support: "unmanaged", vpc: formData.dcslug === 'inmumbaizone2' ? '68c36d65-bda8-43cc-94b5-ae28bb2c3306' : formData.dcslug === 'inbangalore' ? 'c17032c9-3cfd-4028-8f2a-f3f5aa8c2976' : '', cloud: [ { hostname: formData.hostname } ], image: formData.image, sshkeys: formData.sshkeys }; // inbangalore = c17032c9-3cfd-4028-8f2a-f3f5aa8c2976 // inmumbai = 68c36d65-bda8-43cc-94b5-ae28bb2c3306 // Add authentication based on selected method if (formData.auth === "ssh_key") { payload.sshkeys = formData.sshkeys; } else { payload.password = formData.password; } console.log('payload', payload) const response = await fetch("https://host-api.cs1.hz.siliconpin.com/v1/vps/?query=deploy&source=chanel_1", { credentials: "include", method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }); const data = await response.json(); if (data.status === "success") { setDeployStatus({status : data.status, ip : data.ipv4}) // showToast({ // title: "Deployment Started", // description: `Server ${data.cloudid} is being deployed with IP ${data.ipv4}`, // variant: "success" // }); // Reset form after successful deployment setFormData(prev => ({ ...prev, hostname: "", planid: "" })); } else { throw new Error(data.message || "Failed to deploy instance"); } } catch (error) { setDeployError(error.message) } finally { setIsLoading(false); } }; const handleChange = (e) => { const { name, value, type, checked } = e.target; setFormData(prev => ({ ...prev, [name]: type === "checkbox" ? checked : value })); }; const handleAddSshKey = async () => { try { const response = await fetch('https://api.utho.com/v2/key/import', { method: "POST", headers: { "Authorization": `Bearer ${PUBLIC_UTHO_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ name: formData.newSshKeyName, sshkey: formData.newSshKeyValue }) }); const data = await response.json(); if (data.status === 'success') { // Refresh SSH keys list const sshResponse = await fetch("https://api.utho.com/v2/key", { headers: { "Authorization": `Bearer ${PUBLIC_UTHO_API_KEY}` } }); const sshData = await sshResponse.json(); setSshKeys(sshData.key || []); showToast({ title: "SSH Key Added", description: "Your SSH key has been successfully added", variant: "success" }); setShowAddSshKey(false); } } catch (error) { showToast({ title: "Error", description: "Failed to add SSH key", variant: "destructive" }); } }; const handleCopy = (text) => { navigator.clipboard.writeText(text); setCopied(true); setTimeout(() => setCopied(false), 1000); // revert after 1 second }; if (loading || (isLoggedIn && isFetchingData)) { return ; } if (deployError) return

Error: {deployError?.message}

; if (!isLoggedIn) { return

You are not Logged in. Click Here to login first then you can add this service.

; } if(deployStatus.status === 'success'){ return ( <> Create New Vertual Private Server (VPS) Provide the root password then you can access vps using the password.
later on you can setup ssh key

Your New VPS

) } return ( Create New Vertual Private Server (VPS) Provide the root password then you can access vps using the password.
later on you can setup ssh key
{/*

Authentication

setFormData({...formData, auth: value})} className="w-full"> SSH Key Password
{showAddSshKey && (