Spaces:
Running
Running
fix: show real idea totals on browse list
Browse files- app/api/ideas/route.ts +5 -2
- components/HomeList.tsx +13 -2
app/api/ideas/route.ts
CHANGED
|
@@ -107,6 +107,7 @@ export async function GET(request: NextRequest) {
|
|
| 107 |
|
| 108 |
const where = cursorDate && !Number.isNaN(cursorDate.getTime()) ? { createdAt: { lt: cursorDate } } : undefined;
|
| 109 |
const isSearching = Boolean(normalizedQuery) && queryTokens.length > 0;
|
|
|
|
| 110 |
|
| 111 |
const ideas = await prisma.idea.findMany({
|
| 112 |
take: isSearching ? Math.max(limit * 10, 250) : limit + 1,
|
|
@@ -196,9 +197,11 @@ export async function GET(request: NextRequest) {
|
|
| 196 |
}
|
| 197 |
|
| 198 |
const items = filtered.slice(0, limit).map((entry) => entry.idea);
|
| 199 |
-
return NextResponse.json({ items, nextCursor: null });
|
| 200 |
}
|
| 201 |
|
|
|
|
|
|
|
| 202 |
const hasMore = mapped.length > limit;
|
| 203 |
const page = hasMore ? mapped.slice(0, limit) : mapped;
|
| 204 |
|
|
@@ -208,7 +211,7 @@ export async function GET(request: NextRequest) {
|
|
| 208 |
: [...page].sort((a, b) => scoreHot(b.upvotesCount, b.createdAt) - scoreHot(a.upvotesCount, a.createdAt));
|
| 209 |
|
| 210 |
const nextCursor = hasMore ? page[page.length - 1]?.createdAt.toISOString() ?? null : null;
|
| 211 |
-
return NextResponse.json({ items, nextCursor });
|
| 212 |
}
|
| 213 |
|
| 214 |
export async function POST(request: NextRequest) {
|
|
|
|
| 107 |
|
| 108 |
const where = cursorDate && !Number.isNaN(cursorDate.getTime()) ? { createdAt: { lt: cursorDate } } : undefined;
|
| 109 |
const isSearching = Boolean(normalizedQuery) && queryTokens.length > 0;
|
| 110 |
+
const totalIdeasPromise = isSearching ? Promise.resolve(null) : prisma.idea.count();
|
| 111 |
|
| 112 |
const ideas = await prisma.idea.findMany({
|
| 113 |
take: isSearching ? Math.max(limit * 10, 250) : limit + 1,
|
|
|
|
| 197 |
}
|
| 198 |
|
| 199 |
const items = filtered.slice(0, limit).map((entry) => entry.idea);
|
| 200 |
+
return NextResponse.json({ items, nextCursor: null, totalCount: filtered.length });
|
| 201 |
}
|
| 202 |
|
| 203 |
+
const totalCount = (await totalIdeasPromise) ?? mapped.length;
|
| 204 |
+
|
| 205 |
const hasMore = mapped.length > limit;
|
| 206 |
const page = hasMore ? mapped.slice(0, limit) : mapped;
|
| 207 |
|
|
|
|
| 211 |
: [...page].sort((a, b) => scoreHot(b.upvotesCount, b.createdAt) - scoreHot(a.upvotesCount, a.createdAt));
|
| 212 |
|
| 213 |
const nextCursor = hasMore ? page[page.length - 1]?.createdAt.toISOString() ?? null : null;
|
| 214 |
+
return NextResponse.json({ items, nextCursor, totalCount });
|
| 215 |
}
|
| 216 |
|
| 217 |
export async function POST(request: NextRequest) {
|
components/HomeList.tsx
CHANGED
|
@@ -20,6 +20,7 @@ export function HomeList() {
|
|
| 20 |
const [sort, setSort] = useState<"hot" | "new">("hot");
|
| 21 |
const [loading, setLoading] = useState(true);
|
| 22 |
const [items, setItems] = useState<IdeaCard[]>([]);
|
|
|
|
| 23 |
const [error, setError] = useState("");
|
| 24 |
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
|
| 25 |
const [queryInput, setQueryInput] = useState("");
|
|
@@ -52,11 +53,16 @@ export function HomeList() {
|
|
| 52 |
return data;
|
| 53 |
})
|
| 54 |
.then((data) => {
|
| 55 |
-
if (active)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
})
|
| 57 |
.catch((err) => {
|
| 58 |
if (active) {
|
| 59 |
setItems([]);
|
|
|
|
| 60 |
setError(err instanceof Error ? err.message : "Failed to load ideas");
|
| 61 |
}
|
| 62 |
})
|
|
@@ -70,6 +76,11 @@ export function HomeList() {
|
|
| 70 |
}, [sort, query]);
|
| 71 |
|
| 72 |
const empty = useMemo(() => !loading && !error && items.length === 0, [loading, error, items.length]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
return (
|
| 75 |
<div className="space-y-4">
|
|
@@ -106,7 +117,7 @@ export function HomeList() {
|
|
| 106 |
</button>
|
| 107 |
</div>
|
| 108 |
<p className="subtle text-sm">
|
| 109 |
-
{
|
| 110 |
{query ? ` matching "${query}"` : ""}
|
| 111 |
</p>
|
| 112 |
</div>
|
|
|
|
| 20 |
const [sort, setSort] = useState<"hot" | "new">("hot");
|
| 21 |
const [loading, setLoading] = useState(true);
|
| 22 |
const [items, setItems] = useState<IdeaCard[]>([]);
|
| 23 |
+
const [totalCount, setTotalCount] = useState<number | null>(null);
|
| 24 |
const [error, setError] = useState("");
|
| 25 |
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
|
| 26 |
const [queryInput, setQueryInput] = useState("");
|
|
|
|
| 53 |
return data;
|
| 54 |
})
|
| 55 |
.then((data) => {
|
| 56 |
+
if (active) {
|
| 57 |
+
const nextItems = data.items || [];
|
| 58 |
+
setItems(nextItems);
|
| 59 |
+
setTotalCount(typeof data.totalCount === "number" ? data.totalCount : nextItems.length);
|
| 60 |
+
}
|
| 61 |
})
|
| 62 |
.catch((err) => {
|
| 63 |
if (active) {
|
| 64 |
setItems([]);
|
| 65 |
+
setTotalCount(0);
|
| 66 |
setError(err instanceof Error ? err.message : "Failed to load ideas");
|
| 67 |
}
|
| 68 |
})
|
|
|
|
| 76 |
}, [sort, query]);
|
| 77 |
|
| 78 |
const empty = useMemo(() => !loading && !error && items.length === 0, [loading, error, items.length]);
|
| 79 |
+
const visibleCount = items.length;
|
| 80 |
+
const summaryLabel =
|
| 81 |
+
totalCount !== null && visibleCount < totalCount
|
| 82 |
+
? `Showing ${visibleCount} of ${totalCount} ideas`
|
| 83 |
+
: `${totalCount ?? visibleCount} ideas`;
|
| 84 |
|
| 85 |
return (
|
| 86 |
<div className="space-y-4">
|
|
|
|
| 117 |
</button>
|
| 118 |
</div>
|
| 119 |
<p className="subtle text-sm">
|
| 120 |
+
{summaryLabel}
|
| 121 |
{query ? ` matching "${query}"` : ""}
|
| 122 |
</p>
|
| 123 |
</div>
|