diff --git a/app/dash/Dashboard.tsx b/app/dash/Dashboard.tsx index 93e8b85..16499d7 100644 --- a/app/dash/Dashboard.tsx +++ b/app/dash/Dashboard.tsx @@ -1,12 +1,13 @@ "use client"; import Navbar from "@/components/Navbar"; import ErrorToast from "@/components/Error"; -import { Edit, Plus, Trash, Dice5, Copy, ScanSearch, ChevronRight} from "lucide-react"; +import { Edit, Plus, Trash, Dice5, ScanSearch, ChevronRight} from "lucide-react"; import { useEffect, useState, useMemo } from "react"; import axios from "axios"; import Fuse from "fuse.js"; import Footer from "@/components/Footer" import Cookies from "js-cookie"; +import RandomModal from "./components/RandomModal"; import { SortType, Server, Port } from "@/app/types"; import { compareIp } from "@/app/utils"; @@ -41,7 +42,7 @@ export default function Dashboard() { const [portPort, setPortPort] = useState(null); const [editItem, setEditItem] = useState(null); - const [randomPort, setRandomPort] = useState(null); + const [showRandomModal, setShowRandomModal] = useState(false); const [isScanning, setIsScanning] = useState(false); @@ -251,29 +252,6 @@ const usedPorts = useMemo(() => { return ports; }, [servers]); -const generateRandomPort = () => { - let port; - let attempts = 0; - - do { - port = Math.floor(Math.random() * (65535 - 1024) + 1024); - attempts++; - } while (usedPorts.has(port) && attempts < 1000); - - if (attempts >= 1000) { - handleError("Could not find free port after 1000 attempts"); - return; - } - - setRandomPort(port); - setShowRandomModal(true); -}; - - const copyToClipboard = () => { - if (randomPort !== null) { - navigator.clipboard.writeText(randomPort.toString()); - } -}; const sortedPorts = (ports: Port[]) => [...ports].sort((a, b) => a.port - b.port); @@ -377,48 +355,19 @@ const generateRandomPort = () => { - {showRandomModal && randomPort !== null && ( - -
-
-

Random Port Generator

-

Your allocated port number

-
- -
- - {randomPort} - -
- -
- - - -
-
-
+ {showRandomModal && ( + )} + + + + + + + + ); +}; + +export default RandomModal; diff --git a/app/utils.ts b/app/utils.ts index 896116d..3923357 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -1,3 +1,5 @@ +import { Server } from "@/app/types"; + export const compareIp = (a: string, b: string) => { const pa = a.split('.').map(n => parseInt(n, 10)); const pb = b.split('.').map(n => parseInt(n, 10)); @@ -6,4 +8,32 @@ export const compareIp = (a: string, b: string) => { if (diff !== 0) return diff; } return 0; - }; \ No newline at end of file + }; + +const generateRandomPort = (): number => { + return Math.floor(Math.random() * (65535 - 1024) + 1024); +}; + +export const findAvailablePort = (usedPorts: Set, maxRetries: number): number | null => { + let port; + let attempts = 0; + + do { + port = generateRandomPort(); + attempts++; + } while (usedPorts.has(port) && attempts < maxRetries); + + return attempts < maxRetries ? port : null; +}; + +export const generateRandomPortWithUsedPortsContext = (usedPorts: Set, maxRetries: number): number | null => { + return findAvailablePort(usedPorts, maxRetries); +}; + +export const generateRandomPortWithServerContext = (serverContext: Server | undefined, maxRetries: number): number | null => { + if (!serverContext) { + return null; + } + const serverPorts = new Set(serverContext.ports.map(e => e.port)); // Use a Set for faster lookup + return findAvailablePort(serverPorts, maxRetries); +}; \ No newline at end of file