import { notFound } from "next/navigation";
import { api } from "@/server/api/client";
import { TRPCError } from "@trpc/server";
import DayView from "@/components/dashboard/days/DayView";
import LoadingSpinner from "@/components/ui/spinner";

export default async function DayPage({ params }: { params: { dateQuery: string }; }) {
    let day;
    const { dateQuery } = await params;
    const timezone = await api.users.getTimezone();

    try {
        day = await api.days.get({
            dateQuery: dateQuery,
            timezone: timezone,
        });
    } catch (e) {
        if (e instanceof TRPCError) {
            if (e.code == "NOT_FOUND") {
                notFound();
            }
        }
        throw e;
    }

    return (
        <>
            {
                day == undefined ?
                    <LoadingSpinner /> :
                    <DayView
                        day={day}
                    />
            }
        </>
    );
}
