2026-03-09 12:38:40 -07:00
|
|
|
|
import { BsFillLightningChargeFill } from "react-icons/bs";
|
2026-03-09 23:19:14 -07:00
|
|
|
|
import { useLiveSelector, selectPing } from "../state/liveConnectionStore";
|
2026-03-09 12:38:40 -07:00
|
|
|
|
import styles from "./JoinServerButton.module.css";
|
|
|
|
|
|
|
|
|
|
|
|
function formatPing(ms: number): string {
|
2026-03-12 16:25:04 -07:00
|
|
|
|
return `${ms.toLocaleString()} ms`;
|
2026-03-09 12:38:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function JoinServerButton({
|
2026-03-12 16:25:04 -07:00
|
|
|
|
isActive,
|
2026-03-09 12:38:40 -07:00
|
|
|
|
onOpenServerBrowser,
|
|
|
|
|
|
}: {
|
2026-03-12 16:25:04 -07:00
|
|
|
|
isActive: boolean;
|
2026-03-09 12:38:40 -07:00
|
|
|
|
onOpenServerBrowser: () => void;
|
|
|
|
|
|
}) {
|
2026-03-09 23:19:14 -07:00
|
|
|
|
const gameStatus = useLiveSelector((s) => s.gameStatus);
|
|
|
|
|
|
const serverName = useLiveSelector((s) => s.serverName);
|
|
|
|
|
|
const ping = useLiveSelector(selectPing);
|
|
|
|
|
|
const disconnectServer = useLiveSelector((s) => s.disconnectServer);
|
2026-03-09 12:38:40 -07:00
|
|
|
|
|
2026-03-09 23:19:14 -07:00
|
|
|
|
const isLive = gameStatus === "connected";
|
2026-03-09 12:38:40 -07:00
|
|
|
|
const isConnecting =
|
2026-03-09 23:19:14 -07:00
|
|
|
|
gameStatus === "connecting" ||
|
|
|
|
|
|
gameStatus === "challenging" ||
|
|
|
|
|
|
gameStatus === "authenticating";
|
2026-03-09 12:38:40 -07:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className={styles.Root}
|
2026-03-12 16:25:04 -07:00
|
|
|
|
aria-label={isLive ? "Connected – click to disconnect" : "Join server"}
|
|
|
|
|
|
title={isLive ? "Connected – click to disconnect" : "Join server"}
|
2026-03-09 12:38:40 -07:00
|
|
|
|
onClick={() => {
|
|
|
|
|
|
if (isLive) {
|
2026-03-09 23:19:14 -07:00
|
|
|
|
disconnectServer();
|
2026-03-09 12:38:40 -07:00
|
|
|
|
} else {
|
|
|
|
|
|
onOpenServerBrowser();
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
2026-03-12 16:25:04 -07:00
|
|
|
|
data-active={isActive}
|
2026-03-09 12:38:40 -07:00
|
|
|
|
>
|
|
|
|
|
|
<BsFillLightningChargeFill
|
|
|
|
|
|
className={`${styles.LiveIcon} ${isLive ? styles.Pulsing : ""}`}
|
|
|
|
|
|
/>
|
2026-03-12 16:25:04 -07:00
|
|
|
|
<>
|
|
|
|
|
|
<span className={styles.TextLabel}>Live</span>
|
|
|
|
|
|
<span className={styles.ButtonHint}>
|
|
|
|
|
|
{ping != null ? formatPing(ping) : "Join a game"}
|
2026-03-09 12:38:40 -07:00
|
|
|
|
</span>
|
2026-03-12 16:25:04 -07:00
|
|
|
|
</>
|
|
|
|
|
|
{/* {isLive && ping != null && (
|
|
|
|
|
|
<span className={styles.PingLabel}>{formatPing(ping)}</span>
|
|
|
|
|
|
)} */}
|
2026-03-09 12:38:40 -07:00
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|