import type { Route } from "./+types/home"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Plus, Kanban, LogOut } from "lucide-react"; import { Link, Form, redirect } from "react-router"; import { db } from "@lib/db"; import { projects as projectsTable, tasks, users } from "@lib/db/schema"; import { count, eq } from "drizzle-orm"; import Layout from "@/components/layout"; import { getCurrentUser } from "@lib/auth-utils"; import { getUserProjects } from "@lib/auth"; export function meta({}: Route.MetaArgs) { return [{ title: "Projects - FramSpor" }]; } export async function loader({ request }: { request: Request }) { // Check if there are any users const existingUsers = await db.select().from(users).limit(1); // If users exist, redirect to login if (existingUsers.length === 0) { return redirect("/setup"); } const user = await getCurrentUser(request); if (!user) { return redirect("/login"); } // Fetch user's accessible projects const projects = await getUserProjects(user.id); // For each project, count the number of tasks const projectsWithTaskCount = await Promise.all( projects.map(async (project) => { const taskCountResult = await db .select({ count: count() }) .from(tasks) .where(eq(tasks.projectId, project.id)); return { ...project, taskCount: taskCountResult[0]?.count || 0 }; }) ); return { projects: projectsWithTaskCount, user }; } export default function Home({ loaderData }: Route.ComponentProps) { const { projects, user } = loaderData as { projects: any[]; user: any }; return ( {/* Header */}

Projects

Welcome, {user.username}! You have {projects.length} project {projects.length === 1 ? "" : "s"}. {user.isAdmin && (Admin)}

{/* Projects Grid */}
{projects.map((project: any) => (
{project.name} {project.description}
{project.taskCount} tasks
))}
{/* Empty State */} {projects.length === 0 && (

No projects yet

Create your first project to get started with task management

)}
); }