106 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-04-16 22:50:42 +01:00
import { Card } from "@/app/components/card";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table";
2024-04-16 22:53:57 +01:00
import { generateEmbed } from "@/common/embed";
2024-04-16 22:55:45 +01:00
import { capitalizeFirstLetter } from "@/common/string-utils";
import { formatTime } from "@/common/time-utils";
2024-04-16 22:50:42 +01:00
import { cn } from "@/common/utils";
2024-04-17 19:18:32 +01:00
import { getMojangEndpointStatus } from "mcutils-library";
2024-04-16 22:53:57 +01:00
import { Metadata } from "next";
2024-04-16 22:50:42 +01:00
import Link from "next/link";
2024-04-18 01:21:38 +01:00
import { ReactElement } from "react";
2024-04-16 22:50:42 +01:00
2024-04-16 23:53:10 +01:00
/**
* Force the page to be dynamic, so it will be regenerated on every request
*/
export const dynamic = "force-dynamic";
2024-04-16 22:50:42 +01:00
/**
* Gets the color of the status
*
* @param status the status of the endpoint
* @returns the color of the status
*/
function getColor(status: any): string {
switch (status) {
case "ONLINE":
return "text-green-500";
case "DEGRADED":
return "text-yellow-500";
case "OFFLINE":
return "text-red-500";
default:
return "text-gray-500";
}
}
2024-04-16 22:55:45 +01:00
/**
* Formats the status
*
* @param status the status of the endpoint
* @returns the formatted status
*/
function formatStatus(status: any): string {
return capitalizeFirstLetter(status.toLowerCase());
}
2024-04-16 22:53:57 +01:00
export async function generateMetadata(): Promise<Metadata> {
2024-04-17 19:18:32 +01:00
const { endpoints } = await getMojangEndpointStatus();
2024-04-16 22:53:57 +01:00
2024-04-17 17:36:29 +01:00
let description = Object.entries(endpoints)
2024-04-16 22:53:57 +01:00
.map(([url, status]) => {
2024-04-17 17:36:29 +01:00
return `${url}: ${formatStatus(status)}`;
2024-04-16 22:53:57 +01:00
})
.join("\n");
2024-04-17 17:36:29 +01:00
description += `\n\nEmbed Cache: ${formatTime(new Date())}`;
2024-04-16 22:53:57 +01:00
return generateEmbed({
title: "Mojang Status",
description: description,
});
}
2024-04-18 01:21:38 +01:00
export default async function Page(): Promise<ReactElement> {
2024-04-17 19:18:32 +01:00
const { endpoints } = await getMojangEndpointStatus();
2024-04-16 22:50:42 +01:00
const endpointsSize = Object.entries(endpoints).length;
return (
2024-04-18 05:24:21 +01:00
<div>
<div className="text-center mb-4">
<h1 className="text-xl">Mojang Status</h1>
<p>The current status of Mojang Services</p>
</div>
2024-04-19 16:55:00 +01:00
<Card classNameContent="w-full xs:w-fit text-left">
2024-04-18 05:24:21 +01:00
<div>
{endpointsSize === 0 && <p>Unable to fetch endpoint statuses</p>}
{endpointsSize > 0 && (
<Table className="md:w-[500px] text-start">
<TableHeader>
<TableRow>
<TableHead className="pl-1 h-8">Service</TableHead>
2024-04-18 08:51:52 +01:00
<TableHead className="pl-1 h-8">Status</TableHead>
2024-04-18 05:24:21 +01:00
</TableRow>
</TableHeader>
<TableBody>
{Object.entries(endpoints).map(([url, status]) => {
return (
<TableRow key={url}>
<TableCell className="p-[0.3rem]">
<Link className="hover:text-primary transition-all" href={url} target="_blank">
{url}
</Link>
</TableCell>
2024-04-18 08:51:52 +01:00
<TableCell className={cn(getColor(status), "p-[0.3rem] text-left")}>
2024-04-18 05:24:21 +01:00
{formatStatus(status)}
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
)}
</div>
</Card>
</div>
2024-04-16 22:50:42 +01:00
);
}