2024-04-16 17:54:22 +01:00
|
|
|
"use client";
|
|
|
|
|
2024-04-17 18:08:13 +01:00
|
|
|
import { capitalizeFirstLetter } from "@/common/string-utils";
|
2024-04-17 18:26:53 +01:00
|
|
|
import { useToast } from "@/common/use-toast";
|
|
|
|
import { ServerPlatform, getServer } from "mcutils-library";
|
2024-04-16 17:54:22 +01:00
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
import { useState } from "react";
|
2024-04-16 19:12:26 +01:00
|
|
|
import { Button } from "../ui/button";
|
|
|
|
import { Input } from "../ui/input";
|
2024-04-17 18:08:13 +01:00
|
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
|
2024-04-16 17:54:22 +01:00
|
|
|
|
2024-04-16 21:18:08 +01:00
|
|
|
export function LookupServer(): JSX.Element {
|
2024-04-16 17:54:22 +01:00
|
|
|
const router = useRouter();
|
2024-04-17 18:26:53 +01:00
|
|
|
const { toast } = useToast();
|
2024-04-16 17:54:22 +01:00
|
|
|
const [hostname, setHostname] = useState("");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the hostname value
|
|
|
|
*
|
|
|
|
* @param event the input event
|
|
|
|
*/
|
|
|
|
const setHostnameValue = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setHostname(event.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup a server based on the platform
|
|
|
|
*
|
|
|
|
* @param platform the server platform
|
|
|
|
*/
|
2024-04-17 18:26:53 +01:00
|
|
|
const lookupServer = async (platform: ServerPlatform) => {
|
2024-04-16 18:49:23 +01:00
|
|
|
if (!hostname || hostname.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-17 18:26:53 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await getServer(platform, hostname);
|
|
|
|
} catch (err) {
|
|
|
|
toast({
|
|
|
|
title: "Error",
|
|
|
|
variant: "destructive",
|
|
|
|
description: (err as Error).message,
|
|
|
|
duration: 5000,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-16 17:54:22 +01:00
|
|
|
router.push(`/server/${platform}/${hostname}`);
|
|
|
|
};
|
|
|
|
|
2024-04-17 18:08:13 +01:00
|
|
|
const LookupButton = ({ platform }: { platform: ServerPlatform }): JSX.Element => {
|
|
|
|
const name = capitalizeFirstLetter(platform);
|
|
|
|
return (
|
|
|
|
<Tooltip>
|
|
|
|
<TooltipTrigger asChild>
|
2024-04-17 18:26:53 +01:00
|
|
|
<Button type="submit" onClick={() => lookupServer(platform)}>
|
|
|
|
{name}
|
|
|
|
</Button>
|
2024-04-17 18:08:13 +01:00
|
|
|
</TooltipTrigger>
|
|
|
|
<TooltipContent>
|
|
|
|
<p>Click to lookup the server as a {name} server</p>
|
|
|
|
</TooltipContent>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-04-16 17:54:22 +01:00
|
|
|
return (
|
2024-04-17 18:26:53 +01:00
|
|
|
<form
|
|
|
|
className="flex gap-2 justify-center items-center mt-2 flex-col xs:flex-row"
|
|
|
|
action=""
|
|
|
|
onSubmit={(event) => event.preventDefault()}
|
|
|
|
>
|
2024-04-16 21:50:08 +01:00
|
|
|
<Input className="w-fit" placeholder="Hostname" value={hostname} onChange={setHostnameValue} maxLength={128} />
|
|
|
|
<div className="flex gap-2 justify-center">
|
2024-04-17 18:08:13 +01:00
|
|
|
<LookupButton platform={ServerPlatform.Java} />
|
|
|
|
<LookupButton platform={ServerPlatform.Bedrock} />
|
2024-04-16 21:50:08 +01:00
|
|
|
</div>
|
2024-04-16 19:35:07 +01:00
|
|
|
</form>
|
2024-04-16 17:54:22 +01:00
|
|
|
);
|
|
|
|
}
|