|
| 1 | +import { formatDistanceToNow } from "date-fns"; |
| 2 | +import { Plus, Rocket } from "lucide-react"; |
| 3 | +import Link from "next/link"; |
| 4 | +import { useMemo } from "react"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { api } from "@/utils/api"; |
| 7 | + |
| 8 | +type DeploymentStatus = "idle" | "running" | "done" | "error"; |
| 9 | + |
| 10 | +const statusDotClass: Record<string, string> = { |
| 11 | + done: "bg-muted-foreground/60", |
| 12 | + running: "bg-amber-500", |
| 13 | + error: "bg-red-500", |
| 14 | + idle: "bg-muted-foreground/30", |
| 15 | +}; |
| 16 | + |
| 17 | +function getServiceInfo(d: any) { |
| 18 | + const app = d.application; |
| 19 | + const comp = d.compose; |
| 20 | + if (app?.environment?.project && app.environment) { |
| 21 | + return { |
| 22 | + name: app.name as string, |
| 23 | + environment: app.environment.name as string, |
| 24 | + projectName: app.environment.project.name as string, |
| 25 | + href: `/dashboard/project/${app.environment.project.projectId}/environment/${app.environment.environmentId}/services/application/${app.applicationId}`, |
| 26 | + }; |
| 27 | + } |
| 28 | + if (comp?.environment?.project && comp.environment) { |
| 29 | + return { |
| 30 | + name: comp.name as string, |
| 31 | + environment: comp.environment.name as string, |
| 32 | + projectName: comp.environment.project.name as string, |
| 33 | + href: `/dashboard/project/${comp.environment.project.projectId}/environment/${comp.environment.environmentId}/services/compose/${comp.composeId}`, |
| 34 | + }; |
| 35 | + } |
| 36 | + return null; |
| 37 | +} |
| 38 | + |
| 39 | +function StatCard({ |
| 40 | + label, |
| 41 | + value, |
| 42 | + delta, |
| 43 | +}: { |
| 44 | + label: string; |
| 45 | + value: string; |
| 46 | + delta?: string; |
| 47 | +}) { |
| 48 | + return ( |
| 49 | + <div className="rounded-xl border bg-background p-5 min-h-[120px] flex flex-col justify-between"> |
| 50 | + <span className="text-xs uppercase tracking-wider text-muted-foreground"> |
| 51 | + {label} |
| 52 | + </span> |
| 53 | + <div className="flex flex-col gap-1"> |
| 54 | + <span className="text-3xl font-semibold tracking-tight">{value}</span> |
| 55 | + {delta && ( |
| 56 | + <span className="text-xs text-muted-foreground"> |
| 57 | + {delta} |
| 58 | + </span> |
| 59 | + )} |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +export const ShowHome = () => { |
| 66 | + const { data: auth } = api.user.get.useQuery(); |
| 67 | + const { data: projects } = api.project.all.useQuery(); |
| 68 | + const { data: servers } = api.server.all.useQuery(); |
| 69 | + const { data: permissions } = api.user.getPermissions.useQuery(); |
| 70 | + const canReadDeployments = !!permissions?.deployment.read; |
| 71 | + const { data: deployments } = api.deployment.allCentralized.useQuery( |
| 72 | + undefined, |
| 73 | + { |
| 74 | + enabled: canReadDeployments, |
| 75 | + refetchInterval: 10000, |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + const firstName = auth?.user?.firstName?.trim(); |
| 80 | + |
| 81 | + const { totals, serverCount } = useMemo(() => { |
| 82 | + let applications = 0; |
| 83 | + let compose = 0; |
| 84 | + let databases = 0; |
| 85 | + const dbKeys = [ |
| 86 | + "postgres", |
| 87 | + "mysql", |
| 88 | + "mariadb", |
| 89 | + "mongo", |
| 90 | + "redis", |
| 91 | + "libsql", |
| 92 | + ] as const; |
| 93 | + for (const p of projects ?? []) { |
| 94 | + for (const env of p.environments ?? []) { |
| 95 | + applications += env.applications?.length ?? 0; |
| 96 | + compose += env.compose?.length ?? 0; |
| 97 | + for (const key of dbKeys) { |
| 98 | + databases += (env as any)[key]?.length ?? 0; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return { |
| 103 | + totals: { |
| 104 | + projects: projects?.length ?? 0, |
| 105 | + applications, |
| 106 | + compose, |
| 107 | + databases, |
| 108 | + services: applications + compose + databases, |
| 109 | + }, |
| 110 | + serverCount: servers?.length ?? 0, |
| 111 | + }; |
| 112 | + }, [projects, servers]); |
| 113 | + |
| 114 | + const recentDeployments = useMemo(() => { |
| 115 | + if (!deployments) return []; |
| 116 | + return [...deployments] |
| 117 | + .sort( |
| 118 | + (a, b) => |
| 119 | + new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(), |
| 120 | + ) |
| 121 | + .slice(0, 10); |
| 122 | + }, [deployments]); |
| 123 | + |
| 124 | + return ( |
| 125 | + <div className="w-full flex flex-col gap-6"> |
| 126 | + <div className="flex flex-col gap-6 sm:flex-row sm:items-end sm:justify-between"> |
| 127 | + <div className="flex flex-col gap-1"> |
| 128 | + <h1 className="text-3xl font-semibold tracking-tight"> |
| 129 | + {firstName ? `Welcome back, ${firstName}` : "Welcome back"} |
| 130 | + </h1> |
| 131 | + <p className="text-sm text-muted-foreground"> |
| 132 | + {totals.services} services across {serverCount}{" "} |
| 133 | + {serverCount === 1 ? "server" : "servers"} · {totals.projects}{" "} |
| 134 | + {totals.projects === 1 ? "project" : "projects"} |
| 135 | + </p> |
| 136 | + </div> |
| 137 | + <Button asChild className="w-fit"> |
| 138 | + <Link href="/dashboard/projects"> |
| 139 | + <Plus className="size-4" /> |
| 140 | + New project |
| 141 | + </Link> |
| 142 | + </Button> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4"> |
| 146 | + <StatCard |
| 147 | + label="Deploys / 24h" |
| 148 | + value={String(recentDeployments.length)} |
| 149 | + delta="placeholder" |
| 150 | + /> |
| 151 | + <StatCard label="Avg build" value="—" delta="placeholder" /> |
| 152 | + <StatCard label="CPU" value="—" delta="placeholder" /> |
| 153 | + <StatCard label="Memory" value="—" delta="placeholder" /> |
| 154 | + </div> |
| 155 | + |
| 156 | + <div className="rounded-xl border bg-background"> |
| 157 | + <div className="flex items-center justify-between px-5 py-4 border-b"> |
| 158 | + <div className="flex items-center gap-2"> |
| 159 | + <Rocket className="size-4 text-muted-foreground" /> |
| 160 | + <h2 className="text-sm font-semibold">Recent deployments</h2> |
| 161 | + </div> |
| 162 | + {canReadDeployments && ( |
| 163 | + <Link |
| 164 | + href="/dashboard/deployments" |
| 165 | + className="text-xs text-muted-foreground hover:text-foreground transition-colors" |
| 166 | + > |
| 167 | + view all → |
| 168 | + </Link> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + {!canReadDeployments ? ( |
| 172 | + <div className="p-10 text-center text-sm text-muted-foreground"> |
| 173 | + You do not have permission to view deployments. |
| 174 | + </div> |
| 175 | + ) : recentDeployments.length === 0 ? ( |
| 176 | + <div className="p-10 text-center text-sm text-muted-foreground"> |
| 177 | + No deployments yet. |
| 178 | + </div> |
| 179 | + ) : ( |
| 180 | + <ul className="divide-y"> |
| 181 | + {recentDeployments.map((d) => { |
| 182 | + const info = getServiceInfo(d); |
| 183 | + if (!info) return null; |
| 184 | + const status = (d.status ?? "idle") as DeploymentStatus; |
| 185 | + return ( |
| 186 | + <li key={d.deploymentId}> |
| 187 | + <Link |
| 188 | + href={info.href} |
| 189 | + className="flex items-center gap-4 px-5 py-4 hover:bg-muted/40 transition-colors" |
| 190 | + > |
| 191 | + <span |
| 192 | + className={`size-2 rounded-full shrink-0 ${statusDotClass[status] ?? statusDotClass.idle}`} |
| 193 | + aria-hidden |
| 194 | + /> |
| 195 | + <div className="flex flex-col min-w-0 flex-1"> |
| 196 | + <span className="text-sm truncate"> |
| 197 | + {info.name} |
| 198 | + </span> |
| 199 | + <span className="text-xs text-muted-foreground truncate"> |
| 200 | + {info.projectName} · {info.environment} |
| 201 | + </span> |
| 202 | + </div> |
| 203 | + <span className="text-xs text-muted-foreground w-20 text-right hidden sm:inline"> |
| 204 | + {status} |
| 205 | + </span> |
| 206 | + <span className="text-xs text-muted-foreground w-24 text-right hidden md:inline"> |
| 207 | + {formatDistanceToNow(new Date(d.createdAt), { |
| 208 | + addSuffix: true, |
| 209 | + })} |
| 210 | + </span> |
| 211 | + <span className="text-xs text-muted-foreground hover:text-foreground transition-colors"> |
| 212 | + logs → |
| 213 | + </span> |
| 214 | + </Link> |
| 215 | + </li> |
| 216 | + ); |
| 217 | + })} |
| 218 | + </ul> |
| 219 | + )} |
| 220 | + </div> |
| 221 | + </div> |
| 222 | + ); |
| 223 | +}; |
0 commit comments