Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions frontend/app/api/classes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export async function GET(request: Request) {
}

const { searchParams } = new URL(request.url);
const classIdParam = searchParams.get("id")?.trim() || null;
const limitParam = searchParams.get("limit");
const offsetParam = searchParams.get("offset");
const departmentParamRaw = searchParams.get("department")?.trim() || null;
Expand All @@ -145,6 +146,34 @@ export async function GET(request: Request) {
const limit = paginated ? Math.min(Math.max(1, parseInt(limitParam, 10) || PAGE_SIZE), 1000) : PAGE_SIZE;
const offset = paginated ? Math.max(0, parseInt(offsetParam ?? "0", 10)) : 0;

if (classIdParam) {
const { data: course, error } = await supabase
.from("courses")
.select("id, title, department, course_number, term, year")
.eq("id", classIdParam)
.maybeSingle();

if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}

if (!course) {
return NextResponse.json({ error: "Course not found" }, { status: 404 });
}

const classes = buildClasses([course as CourseRow], new Map<string, number>());

return NextResponse.json(
{ classes, hasMore: false },
{
status: 200,
headers: {
"Cache-Control": "private, max-age=300, stale-while-revalidate=600",
},
}
);
}

if (paginated) {
let query = supabase
.from("courses")
Expand Down
92 changes: 48 additions & 44 deletions frontend/app/api/notes/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextResponse } from "next/server";
import { headers } from "next/headers";
import { createClient as createSupabaseClient } from "@supabase/supabase-js";
import { generateSignedUrl } from "@/lib/storage";
import { generateSignedUrls } from "@/lib/storage";
import { createClient } from "@/utils/supabaseServerClient";

type ResourceRow = {
Expand Down Expand Up @@ -102,7 +102,6 @@ export async function GET(req: Request) {
download_cost,
profiles ( display_name )
`,
{ count: "exact" },
)
.eq("status", "active"); // Only show active (approved) notes on dashboard

Expand All @@ -125,17 +124,19 @@ export async function GET(req: Request) {
query = query.eq("course_id", classId);
}

query = query.range(from, to);
// Over-fetch one row to compute hasMore without expensive exact counts.
query = query.range(from, to + 1);

const { data, error, count } = await query.returns<ResourceRow[]>();
const { data, error } = await query.returns<ResourceRow[]>();

if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}

const total = count ?? 0;
const hasMore = to + 1 < total;
const ids = data?.map((row) => row.id) ?? [];
const rows = data ?? [];
const hasMore = rows.length > pageSize;
const pageRows = hasMore ? rows.slice(0, pageSize) : rows;
const ids = pageRows.map((row) => row.id);

let voteStats: VoteStatRow[] = [];
if (ids.length > 0) {
Expand Down Expand Up @@ -183,49 +184,52 @@ export async function GET(req: Request) {
});
}

const normalized = data
? await Promise.all(
data.map(async (row) => {
const stats = voteMap.get(row.id) ?? { upvotes: 0, downvotes: 0, score: 0 };
const myVote = myVoteMap.get(row.id) ?? null;
const base = {
id: row.id,
title: row.title,
class_id: row.course_id,
created_at: row.created_at,
description: row.description ?? null,
storage_path: row.file_key,
profile_display_name: row.profiles?.display_name ?? null,
upvote_count: stats.upvotes,
downvote_count: stats.downvotes,
score: stats.score,
my_vote: myVote,
download_cost: row.download_cost ?? 0,
downloaded: downloadedIds.has(row.id),
};

const path = row.preview_key ?? row.file_key;
if (!path) {
return { ...base, previewUrl: null };
}

let previewUrl: string | null = null;
try {
previewUrl = await generateSignedUrl("resources", path);
} catch {
previewUrl = null;
}
return { ...base, previewUrl };
}),
)
: [];
const previewPaths = Array.from(
new Set(
pageRows
.map((row) => row.preview_key ?? row.file_key)
.filter((path): path is string => Boolean(path)),
),
);

let previewUrlMap = new Map<string, string>();
if (previewPaths.length > 0) {
try {
previewUrlMap = await generateSignedUrls("resources", previewPaths);
} catch {
previewUrlMap = new Map<string, string>();
}
}

const normalized = pageRows.map((row) => {
const stats = voteMap.get(row.id) ?? { upvotes: 0, downvotes: 0, score: 0 };
const myVote = myVoteMap.get(row.id) ?? null;
const path = row.preview_key ?? row.file_key;

return {
id: row.id,
title: row.title,
class_id: row.course_id,
created_at: row.created_at,
description: row.description ?? null,
storage_path: row.file_key,
profile_display_name: row.profiles?.display_name ?? null,
upvote_count: stats.upvotes,
downvote_count: stats.downvotes,
score: stats.score,
my_vote: myVote,
download_cost: row.download_cost ?? 0,
downloaded: downloadedIds.has(row.id),
previewUrl: path ? previewUrlMap.get(path) ?? null : null,
};
});

return NextResponse.json(
{
notes: normalized,
page,
pageSize,
total,
total: null,
hasMore,
},
{
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/auth/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function AuthPage() {
};

checkSession();
}, [router]);
}, [router, redirectTo]);

const content = checking ? (
<p className="auth-loading">Checking your session…</p>
Expand Down
Loading