|
| 1 | +"use client"; |
| 2 | +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 3 | +import { io } from "socket.io-client"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { Input } from "@/components/ui/input"; |
| 6 | +import { Github } from "lucide-react"; |
| 7 | +import { Fira_Code } from "next/font/google"; |
| 8 | +import axios from "axios"; |
| 9 | + |
| 10 | +const socket = io("http://localhost:9002"); |
| 11 | + |
| 12 | +const firaCode = Fira_Code({ subsets: ["latin"] }); |
| 13 | + |
| 14 | +export default function Home() { |
| 15 | + const [repoURL, setURL] = useState<string>(""); |
| 16 | + |
| 17 | + const [logs, setLogs] = useState<string[]>([]); |
| 18 | + |
| 19 | + const [loading, setLoading] = useState(false); |
| 20 | + |
| 21 | + const [projectId, setProjectId] = useState<string | undefined>(); |
| 22 | + const [deployPreviewURL, setDeployPreviewURL] = useState< |
| 23 | + string | undefined |
| 24 | + >(); |
| 25 | + |
| 26 | + const logContainerRef = useRef<HTMLElement>(null); |
| 27 | + |
| 28 | + const isValidURL: [boolean, string | null] = useMemo(() => { |
| 29 | + if (!repoURL || repoURL.trim() === "") return [false, null]; |
| 30 | + const regex = new RegExp( |
| 31 | + /^(?:https?:\/\/)?(?:www\.)?github\.com\/([^\/]+)\/([^\/]+)(?:\/)?$/ |
| 32 | + ); |
| 33 | + return [regex.test(repoURL), "Enter valid Github Repository URL"]; |
| 34 | + }, [repoURL]); |
| 35 | + |
| 36 | + const handleClickDeploy = useCallback(async () => { |
| 37 | + setLoading(true); |
| 38 | + |
| 39 | + const { data } = await axios.post(`http://localhost:9000/project`, { |
| 40 | + gitURL: repoURL, |
| 41 | + slug: projectId, |
| 42 | + }); |
| 43 | + |
| 44 | + if (data && data.data) { |
| 45 | + const { projectSlug, url } = data.data; |
| 46 | + setProjectId(projectSlug); |
| 47 | + setDeployPreviewURL(url); |
| 48 | + |
| 49 | + console.log(`Subscribing to logs:${projectSlug}`); |
| 50 | + socket.emit("subscribe", `logs:${projectSlug}`); |
| 51 | + } |
| 52 | + }, [projectId, repoURL]); |
| 53 | + |
| 54 | + const handleSocketIncommingMessage = useCallback((message: string) => { |
| 55 | + console.log(`[Incomming Socket Message]:`, typeof message, message); |
| 56 | + const { log } = JSON.parse(message); |
| 57 | + setLogs((prev) => [...prev, log]); |
| 58 | + logContainerRef.current?.scrollIntoView({ behavior: "smooth" }); |
| 59 | + }, []); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + socket.on("message", handleSocketIncommingMessage); |
| 63 | + |
| 64 | + return () => { |
| 65 | + socket.off("message", handleSocketIncommingMessage); |
| 66 | + }; |
| 67 | + }, [handleSocketIncommingMessage]); |
| 68 | + |
| 69 | + return ( |
| 70 | + <main className="flex justify-center items-center h-[100vh]"> |
| 71 | + <div className="w-[600px]"> |
| 72 | + <span className="flex justify-start items-center gap-2"> |
| 73 | + <Github className="text-5xl" /> |
| 74 | + <Input |
| 75 | + disabled={loading} |
| 76 | + value={repoURL} |
| 77 | + onChange={(e) => setURL(e.target.value)} |
| 78 | + type="url" |
| 79 | + placeholder="Github URL" |
| 80 | + /> |
| 81 | + </span> |
| 82 | + <Button |
| 83 | + onClick={handleClickDeploy} |
| 84 | + disabled={!isValidURL[0] || loading} |
| 85 | + className="w-full mt-3" |
| 86 | + > |
| 87 | + {loading ? "In Progress" : "Deploy"} |
| 88 | + </Button> |
| 89 | + {deployPreviewURL && ( |
| 90 | + <div className="mt-2 bg-slate-900 py-4 px-2 rounded-lg"> |
| 91 | + <p> |
| 92 | + Preview URL{" "} |
| 93 | + <a |
| 94 | + target="_blank" |
| 95 | + className="text-sky-400 bg-sky-950 px-3 py-2 rounded-lg" |
| 96 | + href={deployPreviewURL} |
| 97 | + > |
| 98 | + {deployPreviewURL} |
| 99 | + </a> |
| 100 | + </p> |
| 101 | + </div> |
| 102 | + )} |
| 103 | + {logs.length > 0 && ( |
| 104 | + <div |
| 105 | + className={`${firaCode.className} text-sm text-green-500 logs-container mt-5 border-green-500 border-2 rounded-lg p-4 h-[300px] overflow-y-auto`} |
| 106 | + > |
| 107 | + <pre className="flex flex-col gap-1"> |
| 108 | + {logs.map((log, i) => ( |
| 109 | + <code |
| 110 | + ref={logs.length - 1 === i ? logContainerRef : undefined} |
| 111 | + key={i} |
| 112 | + >{`> ${log}`}</code> |
| 113 | + ))} |
| 114 | + </pre> |
| 115 | + </div> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + </main> |
| 119 | + ); |
| 120 | +} |
0 commit comments