mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import type { LoaderFunctionArgs } from "@remix-run/node";
|
|
import { artsByUserId } from "~/features/art/queries/artsByUserId.server";
|
|
import { getUserId } from "~/features/auth/core/user.server";
|
|
import { countUnvalidatedArt } from "~/features/img-upload";
|
|
import * as UserRepository from "~/features/user-page/UserRepository.server";
|
|
import { notFoundIfFalsy } from "~/utils/remix.server";
|
|
import { userParamsSchema } from "../user-page-schemas";
|
|
|
|
export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
|
const loggedInUser = await getUserId(request);
|
|
|
|
const { identifier } = userParamsSchema.parse(params);
|
|
const user = notFoundIfFalsy(
|
|
await UserRepository.identifierToUserId(identifier),
|
|
);
|
|
|
|
const arts = artsByUserId(user.id);
|
|
|
|
const tagCounts = arts.reduce(
|
|
(acc, art) => {
|
|
if (!art.tags) return acc;
|
|
|
|
for (const tag of art.tags) {
|
|
acc[tag] = (acc[tag] ?? 0) + 1;
|
|
}
|
|
return acc;
|
|
},
|
|
{} as Record<string, number>,
|
|
);
|
|
|
|
const tagCountsSortedArr = Object.entries(tagCounts).sort(
|
|
(a, b) => b[1] - a[1],
|
|
);
|
|
|
|
return {
|
|
arts,
|
|
tagCounts: tagCountsSortedArr.length > 0 ? tagCountsSortedArr : null,
|
|
unvalidatedArtCount:
|
|
user.id === loggedInUser?.id ? countUnvalidatedArt(user.id) : 0,
|
|
};
|
|
};
|