117 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-04-16 22:50:42 +01:00
import { Card } from "@/app/components/card";
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";
2024-04-16 22:50:42 +01:00
import { cn } from "@/common/utils";
import { CachedEndpointStatus, getMojangEndpointStatus, Status } from "mcutils-library";
import { Metadata, Viewport } 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";
import { Colors } from "@/common/colors";
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
*/
2024-04-19 21:19:14 +01:00
export const revalidate = 0;
2024-04-16 23:53:10 +01:00
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());
}
export async function generateViewport(): Promise<Viewport> {
const { endpoints } = await getMojangEndpointStatus();
let warning = false;
for (const endpoint of endpoints) {
if (endpoint.status != Status.ONLINE) {
warning = true;
break;
}
}
return {
themeColor: warning ? Colors.yellow : Colors.green,
};
}
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
let description = "Current Mojang API Status:\n";
for (const endpoint of endpoints) {
const { name, hostname, status } = endpoint;
2024-04-19 20:05:55 +01:00
description += `${name}: ${capitalizeFirstLetter(status.toLowerCase())}\n`;
}
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
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>
<Card classNameCard="py-0 pb-2" classNameContent="w-[23rem] md:w-[35rem] text-left">
<div className="flex flex-col divide-y gap-2">
{endpoints.length == 0 && <p>Unable to fetch endpoint statuses</p>}
{endpoints.length > 0 &&
endpoints.map((endpoint: CachedEndpointStatus) => {
const { name, hostname, status } = endpoint;
const url = `https://${hostname}`;
return (
<div key={name} className="flex flex-row justify-between pt-2">
<div className="flex flex-col leading-[1.5rem]">
<p className="font-semibold">{name}</p>
<Link
href={url}
className="text-sm text-primary hover:opacity-75 transition-all transform-gpu"
target="_blank"
>
<p>{url}</p>
</Link>
</div>
<div className={cn("flex items-center font-semibold", getColor(status))}>
<p>{formatStatus(status)}</p>
</div>
</div>
);
})}
2024-04-18 05:24:21 +01:00
</div>
</Card>
</div>
2024-04-16 22:50:42 +01:00
);
}