2024-04-16 17:54:22 +01:00
|
|
|
"use client";
|
|
|
|
|
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";
|
2024-04-18 05:18:25 +01:00
|
|
|
import { ReactElement } from "react";
|
2024-04-16 19:12:26 +01:00
|
|
|
import { Button } from "../ui/button";
|
|
|
|
import { Input } from "../ui/input";
|
2024-04-18 05:18:25 +01:00
|
|
|
import { Label } from "../ui/label";
|
|
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../ui/select";
|
2024-04-16 17:54:22 +01:00
|
|
|
|
2024-04-18 05:18:25 +01:00
|
|
|
type LookupServerProps = {
|
|
|
|
currentPlatform: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function LookupServer({ currentPlatform }: LookupServerProps): ReactElement {
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup a server based on the platform
|
|
|
|
*
|
|
|
|
* @param platform the server platform
|
2024-04-18 05:18:25 +01:00
|
|
|
* @param query the query to lookup
|
2024-04-16 17:54:22 +01:00
|
|
|
*/
|
2024-04-18 05:18:25 +01:00
|
|
|
const lookupServer = async (platform: ServerPlatform, query: string) => {
|
|
|
|
if (!query || query.length === 0) {
|
2024-04-16 18:49:23 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-04-17 18:26:53 +01:00
|
|
|
|
|
|
|
try {
|
2024-04-18 05:18:25 +01:00
|
|
|
await getServer(platform, query);
|
2024-04-17 18:26:53 +01:00
|
|
|
} catch (err) {
|
|
|
|
toast({
|
|
|
|
title: "Error",
|
|
|
|
variant: "destructive",
|
|
|
|
description: (err as Error).message,
|
|
|
|
duration: 5000,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-18 05:18:25 +01:00
|
|
|
router.push(`/server/${platform}/${query}`);
|
2024-04-17 18:08:13 +01:00
|
|
|
};
|
|
|
|
|
2024-04-16 17:54:22 +01:00
|
|
|
return (
|
2024-04-17 18:26:53 +01:00
|
|
|
<form
|
2024-04-18 05:18:25 +01:00
|
|
|
className="flex flex-col gap-2 justify-center items-center mt-4"
|
|
|
|
action={(form: FormData) => {
|
|
|
|
lookupServer(form.get("platform") as ServerPlatform, form.get("query") as string);
|
|
|
|
}}
|
2024-04-17 18:26:53 +01:00
|
|
|
>
|
2024-04-16 21:50:08 +01:00
|
|
|
<div className="flex gap-2 justify-center">
|
2024-04-18 05:18:25 +01:00
|
|
|
<div className="flex flex-col gap-2 items-start">
|
|
|
|
<Label>Platform</Label>
|
|
|
|
<Select name="platform">
|
|
|
|
<SelectTrigger className="w-[180px]">
|
|
|
|
<SelectValue placeholder={currentPlatform} />
|
|
|
|
</SelectTrigger>
|
|
|
|
<SelectContent>
|
|
|
|
<SelectItem value={ServerPlatform.Java}>Java</SelectItem>
|
|
|
|
<SelectItem value={ServerPlatform.Bedrock}>Bedrock</SelectItem>
|
|
|
|
</SelectContent>
|
|
|
|
</Select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex flex-col gap-2 items-start">
|
|
|
|
<Label htmlFor="query">Hostname</Label>
|
|
|
|
<Input className="w-fit" type="search" name="query" placeholder="Query..." maxLength={128} />
|
|
|
|
</div>
|
2024-04-16 21:50:08 +01:00
|
|
|
</div>
|
2024-04-18 05:18:25 +01:00
|
|
|
|
|
|
|
<Button type="submit">Search</Button>
|
2024-04-16 19:35:07 +01:00
|
|
|
</form>
|
2024-04-16 17:54:22 +01:00
|
|
|
);
|
|
|
|
}
|