refactor: remove random sort

This commit is contained in:
trafficlunar 2026-04-17 00:54:13 +01:00
parent 8ebc480233
commit c72dab1962
5 changed files with 23 additions and 80 deletions

View file

@ -1,7 +1,6 @@
import { Prisma } from "@prisma/client";
import crypto from "crypto";
import seedrandom from "seedrandom";
import { searchSchema } from "@/lib/schemas";
import { auth } from "@/lib/auth";
@ -23,7 +22,7 @@ export default async function MiiList({ searchParams, userId, parentPage }: Prop
const parsed = searchSchema.safeParse(searchParams);
if (!parsed.success) return <h1>{parsed.error.issues[0].message}</h1>;
const { q: query, sort, tags, exclude, platform, gender, makeup, allowCopying, quarantined, page = 1, limit = 24, seed } = parsed.data;
const { q: query, sort, tags, exclude, platform, gender, makeup, allowCopying, quarantined, page = 1, limit = 24 } = parsed.data;
// My Likes page
let miiIdsLiked: number[] | undefined = undefined;
@ -110,61 +109,29 @@ export default async function MiiList({ searchParams, userId, parentPage }: Prop
let totalCount: number;
let miis: Prisma.MiiGetPayload<{ select: typeof select }>[];
if (sort === "random") {
// Get all IDs that match the where conditions
const matchingIds = await prisma.mii.findMany({
where,
select: { id: true },
});
// Sorting by likes, newest, or oldest
let orderBy: Prisma.MiiOrderByWithRelationInput[];
totalCount = matchingIds.length;
if (matchingIds.length === 0) return;
// Use seed for consistent random results
const randomSeed = seed || crypto.randomInt(0, 1_000_000_000);
const rng = seedrandom(randomSeed.toString());
// Randomize all IDs using the Durstenfeld algorithm
for (let i = matchingIds.length - 1; i > 0; i--) {
const j = Math.floor(rng() * (i + 1));
[matchingIds[i], matchingIds[j]] = [matchingIds[j], matchingIds[i]];
}
// Convert to number[] array
const selectedIds = matchingIds.slice(skip, skip + limit).map((i) => i.id);
miis = await prisma.mii.findMany({
where: {
id: { in: selectedIds },
},
select,
});
if (sort === "likes") {
orderBy = [{ likedBy: { _count: "desc" } }, { name: "asc" }];
} else if (sort === "oldest") {
orderBy = [{ createdAt: "asc" }, { name: "asc" }];
} else {
// Sorting by likes, newest, or oldest
let orderBy: Prisma.MiiOrderByWithRelationInput[];
if (sort === "likes") {
orderBy = [{ likedBy: { _count: "desc" } }, { name: "asc" }];
} else if (sort === "oldest") {
orderBy = [{ createdAt: "asc" }, { name: "asc" }];
} else {
// default to newest
orderBy = [{ createdAt: "desc" }, { name: "asc" }];
}
[totalCount, miis] = await Promise.all([
prisma.mii.count({ where: { ...where, userId } }),
prisma.mii.findMany({
where,
orderBy,
select,
skip,
take: limit,
}),
]);
// default to newest
orderBy = [{ createdAt: "desc" }, { name: "asc" }];
}
[totalCount, miis] = await Promise.all([
prisma.mii.count({ where: { ...where, userId } }),
prisma.mii.findMany({
where,
orderBy,
select,
skip,
take: limit,
}),
]);
const lastPage = Math.ceil(totalCount / limit);
return (

View file

@ -5,9 +5,9 @@ import { useTransition } from "react";
import { useSelect } from "downshift";
import { Icon } from "@iconify/react";
type Sort = "likes" | "newest" | "oldest" | "random";
type Sort = "likes" | "newest" | "oldest";
const items = ["likes", "newest", "oldest", "random"];
const items = ["likes", "newest", "oldest"];
export default function SortSelect() {
const router = useRouter();
@ -26,10 +26,6 @@ export default function SortSelect() {
params.set("page", "1");
params.set("sort", selectedItem);
if (selectedItem == "random") {
params.set("seed", Math.floor(Math.random() * 1_000_000_000).toString());
}
startTransition(() => {
router.push(`?${params.toString()}`, { scroll: false });
});