diff --git a/src/components/PmaLink.tsx b/src/components/PmaLink.tsx index f03ca73..545c6c5 100644 --- a/src/components/PmaLink.tsx +++ b/src/components/PmaLink.tsx @@ -1,29 +1,15 @@ -import { usePhpMyAdminConnectionState } from "@/hooks/connection"; -import { useMemo } from "react"; +const { PHPMYADMIN_PORT = 8081 } = import.meta.env; type Props = { title?: string; }; export default function PmaLink(props: Props) { - const { url, state } = usePhpMyAdminConnectionState(); - - const classState = useMemo(() => { - switch (state) { - case "established": - return "status-success"; - case "failed": - return "status-error"; - case "checking": - return "status-primary"; - } - }, [state]); - - const statusClass = useMemo(() => `status ${classState}`, [classState]); + const host = window.location.hostname; + const url = `http://${host}:${PHPMYADMIN_PORT}`; return ( -
- {props.title ?? "phpMyAdmin"} + {props.title ?? "phpMyAdmin"} ({url})
); } diff --git a/src/hooks/connection.ts b/src/hooks/connection.ts deleted file mode 100644 index c26a550..0000000 --- a/src/hooks/connection.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { useLayoutEffect, useState } from "react"; - -const { WEB_PORT = 8080, PHPMYADMIN_PORT = 8081 } = import.meta.env; - -export async function checkConnection( - url: string, - timeout?: number, -): Promise { - const controller = new AbortController(); - if (timeout) { - setTimeout(() => controller.abort(), timeout); - } - - console.log(`Checking connection to ${url} ...`); - - try { - const response = await fetch(url, { - signal: controller.signal, - }); - - return response.status !== 404; - } catch (e) { - const error = e as Error; - - // check cors error - if (error.name === "TypeError") { - // return true; - } - - return false; - } -} - -export type ConnectionState = "established" | "failed" | "checking"; -export type ConnectionContextValue = { - url: string; - state: ConnectionState; -}; - -export function useConnectionState(url: string): ConnectionContextValue { - const [state, setState] = useState("checking"); - - useLayoutEffect(() => { - setState("checking"); - - checkConnection(url) - .then((isRunning) => { - setState(isRunning ? "established" : "failed"); - }) - .catch(() => setState("failed")); - }, [url]); - - return { - url, - state, - }; -} - -export function useDockerWebConnectionState(): ConnectionContextValue { - const host = window.location.hostname; - return useConnectionState(`http://${host}:${WEB_PORT}`); -} - -export function usePhpMyAdminConnectionState(): ConnectionContextValue { - const host = window.location.hostname; - return useConnectionState(`http://${host}:${PHPMYADMIN_PORT}`); -}