linter config, icons, fix stats
Some checks failed
Lint / Lint and Check (push) Failing after 47s

This commit is contained in:
2025-07-15 19:34:28 +02:00
parent d69d9fc129
commit 5d9eb9217e
6 changed files with 330 additions and 266 deletions

View File

@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { db, habits, users, habitLogs } from '@/lib/db';
import { getTokenCookie } from '@/lib/auth/cookies';
import { eq, and, desc, sql } from 'drizzle-orm';
import { eq, and, desc } from 'drizzle-orm';
async function getUserFromToken() {
const token = await getTokenCookie();
@@ -18,48 +18,58 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// Get all non-archived habits with their latest log and statistics
const userHabits = await db
.select({
id: habits.id,
name: habits.name,
type: habits.type,
targetFrequency: habits.targetFrequency,
color: habits.color,
icon: habits.icon,
createdAt: habits.createdAt,
// Get the latest log time
lastLoggedAt: sql<Date>`(
SELECT MAX(logged_at)
FROM ${habitLogs}
WHERE habit_id = ${habits.id}
)`.as('lastLoggedAt'),
// Get total logs count
totalLogs: sql<number>`(
SELECT COUNT(*)
FROM ${habitLogs}
WHERE habit_id = ${habits.id}
)`.as('totalLogs'),
// Get logs in last 7 days
logsLastWeek: sql<number>`(
SELECT COUNT(*)
FROM ${habitLogs}
WHERE habit_id = ${habits.id}
AND logged_at >= NOW() - INTERVAL '7 days'
)`.as('logsLastWeek'),
// Get logs in last 30 days
logsLastMonth: sql<number>`(
SELECT COUNT(*)
FROM ${habitLogs}
WHERE habit_id = ${habits.id}
AND logged_at >= NOW() - INTERVAL '30 days'
)`.as('logsLastMonth'),
})
// Get current timestamp for date calculations
const now = new Date();
const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
// First get all habits
const userHabitsBase = await db
.select()
.from(habits)
.where(and(eq(habits.userId, user.id), eq(habits.isArchived, false)))
.orderBy(desc(habits.createdAt));
return NextResponse.json({ habits: userHabits });
// Then get aggregated log data for each habit
const habitsWithStats = await Promise.all(
userHabitsBase.map(async (habit) => {
// Get all logs for this habit
const logs = await db
.select({
loggedAt: habitLogs.loggedAt,
})
.from(habitLogs)
.where(eq(habitLogs.habitId, habit.id));
// Calculate statistics
const totalLogs = logs.length;
const logsLastWeek = logs.filter((log) => log.loggedAt >= sevenDaysAgo).length;
const logsLastMonth = logs.filter((log) => log.loggedAt >= thirtyDaysAgo).length;
const lastLoggedAt =
logs.length > 0
? logs.reduce(
(latest, log) => (log.loggedAt > latest ? log.loggedAt : latest),
logs[0].loggedAt,
)
: null;
return {
id: habit.id,
name: habit.name,
type: habit.type,
targetFrequency: habit.targetFrequency,
color: habit.color,
icon: habit.icon,
createdAt: habit.createdAt,
lastLoggedAt,
totalLogs,
logsLastWeek,
logsLastMonth,
};
}),
);
return NextResponse.json({ habits: habitsWithStats });
} catch (error) {
console.error('Get habits error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });

View File

@@ -14,8 +14,10 @@ import {
Target,
Copy,
Check,
Trophy,
HeartCrack,
} from 'lucide-react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
@@ -150,9 +152,9 @@ export default function Dashboard() {
const getHabitIcon = (type: string) => {
switch (type) {
case 'positive':
return <TrendingUp className="h-5 w-5 text-emerald-500" />;
return <Trophy className="h-5 w-5 text-emerald-500" />;
case 'negative':
return <TrendingDown className="h-5 w-5 text-red-500" />;
return <HeartCrack className="h-5 w-5 text-red-500" />;
default:
return <Activity className="h-5 w-5 text-zinc-500" />;
}