import { AddApplication } from "@/components/dashboard/project/add-application"; import { AddCompose } from "@/components/dashboard/project/add-compose"; import { AddDatabase } from "@/components/dashboard/project/add-database"; import { AddTemplate } from "@/components/dashboard/project/add-template"; import { MariadbIcon, MongodbIcon, MysqlIcon, PostgresqlIcon, RedisIcon, } from "@/components/icons/data-tools-icons"; import { DashboardLayout } from "@/components/layouts/dashboard-layout"; import { ProjectLayout } from "@/components/layouts/project-layout"; import { DateTooltip } from "@/components/shared/date-tooltip"; import { StatusTooltip } from "@/components/shared/status-tooltip"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, } from "@/components/ui/breadcrumb"; import { Button } from "@/components/ui/button"; import { Card, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { DropdownMenu, DropdownMenuContent, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { appRouter } from "@/server/api/root"; import type { findProjectById } from "@/server/api/services/project"; import { validateRequest } from "@/server/auth/auth"; import { api } from "@/utils/api"; import { createServerSideHelpers } from "@trpc/react-query/server"; import { CircuitBoard, FolderInput, GlobeIcon, PlusIcon } from "lucide-react"; import type { GetServerSidePropsContext, InferGetServerSidePropsType, } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import React, { type ReactElement } from "react"; import superjson from "superjson"; export type Services = { name: string; type: | "mariadb" | "application" | "postgres" | "mysql" | "mongo" | "redis" | "compose"; description?: string | null; id: string; createdAt: string; status?: "idle" | "running" | "done" | "error"; }; type Project = Awaited>; export const extractServices = (data: Project | undefined) => { const applications: Services[] = data?.applications.map((item) => ({ name: item.name, type: "application", id: item.applicationId, createdAt: item.createdAt, status: item.applicationStatus, description: item.description, })) || []; const mariadb: Services[] = data?.mariadb.map((item) => ({ name: item.name, type: "mariadb", id: item.mariadbId, createdAt: item.createdAt, status: item.applicationStatus, description: item.description, })) || []; const postgres: Services[] = data?.postgres.map((item) => ({ name: item.name, type: "postgres", id: item.postgresId, createdAt: item.createdAt, status: item.applicationStatus, description: item.description, })) || []; const mongo: Services[] = data?.mongo.map((item) => ({ name: item.name, type: "mongo", id: item.mongoId, createdAt: item.createdAt, status: item.applicationStatus, description: item.description, })) || []; const redis: Services[] = data?.redis.map((item) => ({ name: item.name, type: "redis", id: item.redisId, createdAt: item.createdAt, status: item.applicationStatus, description: item.description, })) || []; const mysql: Services[] = data?.mysql.map((item) => ({ name: item.name, type: "mysql", id: item.mysqlId, createdAt: item.createdAt, status: item.applicationStatus, description: item.description, })) || []; const compose: Services[] = data?.compose.map((item) => ({ name: item.name, type: "compose", id: item.composeId, createdAt: item.createdAt, status: item.composeStatus, description: item.description, })) || []; applications.push( ...mysql, ...redis, ...mongo, ...postgres, ...mariadb, ...compose, ); applications.sort((a, b) => { return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); }); return applications; }; const Project = ( props: InferGetServerSidePropsType, ) => { const { projectId } = props; const { data: auth } = api.auth.get.useQuery(); const { data: user } = api.user.byAuthId.useQuery( { authId: auth?.id || "", }, { enabled: !!auth?.id && auth?.rol === "user", }, ); const { data } = api.project.one.useQuery({ projectId }); const router = useRouter(); const emptyServices = data?.mariadb?.length === 0 && data?.mongo?.length === 0 && data?.mysql?.length === 0 && data?.postgres?.length === 0 && data?.redis?.length === 0 && data?.applications?.length === 0 && data?.compose?.length === 0; const applications = extractServices(data); return (
Projects {data?.name}

{data?.name}

{data?.description}

{(auth?.rol === "admin" || user?.canCreateServices) && ( Actions )}
{emptyServices ? (
No services added yet. Click on Create Service.
) : (
{applications?.map((service) => ( { router.push( `/dashboard/project/${projectId}/services/${service.type}/${service.id}`, ); }} className="group relative cursor-pointer bg-transparent transition-colors hover:bg-card h-fit" >
{service.name} {service.description && ( {service.description} )}
{service.type === "postgres" && ( )} {service.type === "redis" && ( )} {service.type === "mariadb" && ( )} {service.type === "mongo" && ( )} {service.type === "mysql" && ( )} {service.type === "application" && ( )} {service.type === "compose" && ( )}
Created
))}
)}
); }; export default Project; Project.getLayout = (page: ReactElement) => { return {page}; }; export async function getServerSideProps( ctx: GetServerSidePropsContext<{ projectId: string }>, ) { const { params } = ctx; const { req, res } = ctx; const { user, session } = await validateRequest(req, res); if (!user) { return { redirect: { permanent: true, destination: "/", }, }; } // Fetch data from external API const helpers = createServerSideHelpers({ router: appRouter, ctx: { req: req as any, res: res as any, db: null as any, session: session, user: user, }, transformer: superjson, }); // Valid project, if not return to initial homepage.... if (typeof params?.projectId === "string") { try { await helpers.project.one.fetch({ projectId: params?.projectId, }); return { props: { trpcState: helpers.dehydrate(), projectId: params?.projectId, }, }; } catch (error) { return { redirect: { permanent: false, destination: "/", }, }; } } return { redirect: { permanent: false, destination: "/", }, }; }