t2-mapper/src/components/JoinServerButton.tsx

57 lines
1.6 KiB
TypeScript
Raw Normal View History

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 {
return `${ms.toLocaleString()} ms`;
2026-03-09 12:38:40 -07:00
}
export function JoinServerButton({
isActive,
2026-03-09 12:38:40 -07:00
onOpenServerBrowser,
}: {
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}
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();
}
}}
data-active={isActive}
2026-03-09 12:38:40 -07:00
>
<BsFillLightningChargeFill
className={`${styles.LiveIcon} ${isLive ? styles.Pulsing : ""}`}
/>
<>
<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>
</>
{/* {isLive && ping != null && (
<span className={styles.PingLabel}>{formatPing(ping)}</span>
)} */}
2026-03-09 12:38:40 -07:00
</button>
);
}