2024-09-10 21:24:40 +02:00
|
|
|
import { json, LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
|
2024-09-24 20:56:10 +02:00
|
|
|
import { Link, redirect, useLoaderData, useRouteLoaderData } from "@remix-run/react";
|
2024-09-17 22:12:12 +02:00
|
|
|
import { UserWithMembers } from "~/lib/api/user";
|
2024-09-05 22:29:12 +02:00
|
|
|
import serverRequest from "~/lib/request.server";
|
2024-09-24 20:56:10 +02:00
|
|
|
import { loader as rootLoader } from "~/root";
|
2024-09-26 02:15:54 +02:00
|
|
|
import { Alert, Button, Pagination } from "react-bootstrap";
|
2024-09-24 20:56:10 +02:00
|
|
|
import { Trans, useTranslation } from "react-i18next";
|
2024-09-25 19:48:28 +02:00
|
|
|
import { PersonPlusFill } from "react-bootstrap-icons";
|
|
|
|
import MemberCard from "~/routes/$username/MemberCard";
|
2024-09-26 02:15:54 +02:00
|
|
|
import { ReactNode } from "react";
|
2024-09-29 20:32:54 +02:00
|
|
|
import BaseProfile from "~/components/profile/BaseProfile";
|
2024-09-05 22:29:12 +02:00
|
|
|
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => {
|
|
|
|
const { user } = data!;
|
|
|
|
|
2024-09-25 15:14:48 +02:00
|
|
|
return [{ title: `@${user.username} • pronouns.cc` }];
|
2024-09-05 22:29:12 +02:00
|
|
|
};
|
|
|
|
|
2024-09-17 22:12:12 +02:00
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
|
|
const url = new URL(request.url);
|
2024-09-25 16:43:53 +02:00
|
|
|
let memberPage = parseInt(url.searchParams.get("page") ?? "0", 10);
|
2024-09-17 22:12:12 +02:00
|
|
|
|
2024-09-05 22:29:12 +02:00
|
|
|
let username = params.username!;
|
|
|
|
if (!username.startsWith("@")) throw redirect(`/@${username}`);
|
|
|
|
username = username.substring("@".length);
|
|
|
|
|
2024-09-17 22:12:12 +02:00
|
|
|
const user = await serverRequest<UserWithMembers>("GET", `/users/${username}`);
|
2024-09-25 16:43:53 +02:00
|
|
|
const pageCount = Math.ceil(user.members.length / 20);
|
2024-09-17 22:12:12 +02:00
|
|
|
let members = user.members.slice(memberPage * 20, (memberPage + 1) * 20);
|
2024-09-25 16:43:53 +02:00
|
|
|
if (members.length === 0) {
|
|
|
|
members = user.members.slice(0, 20);
|
|
|
|
memberPage = 0;
|
|
|
|
}
|
2024-09-05 22:29:12 +02:00
|
|
|
|
2024-09-25 16:43:53 +02:00
|
|
|
return json({ user, members, currentPage: memberPage, pageCount });
|
2024-09-05 22:29:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function UserPage() {
|
2024-09-24 20:56:10 +02:00
|
|
|
const { t } = useTranslation();
|
2024-09-26 02:15:54 +02:00
|
|
|
const { user, members, currentPage, pageCount } = useLoaderData<typeof loader>();
|
2024-09-24 20:56:10 +02:00
|
|
|
const { meUser } = useRouteLoaderData<typeof rootLoader>("root") || { meUser: undefined };
|
|
|
|
|
2024-09-25 19:48:28 +02:00
|
|
|
const isMeUser = meUser && meUser.id === user.id;
|
2024-09-05 22:29:12 +02:00
|
|
|
|
2024-09-26 02:15:54 +02:00
|
|
|
const paginationItems: ReactNode[] = [];
|
|
|
|
for (let i = 0; i < pageCount; i++) {
|
|
|
|
paginationItems.push(
|
|
|
|
<Pagination.Item key={i} as={Link} to={`/@${user.username}?page=${i}`}>
|
|
|
|
{i + 1}
|
|
|
|
</Pagination.Item>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pagination = (
|
|
|
|
<Pagination className="justify-content-center">
|
|
|
|
<Pagination.Prev
|
|
|
|
as={Link}
|
2024-09-29 20:37:30 +02:00
|
|
|
// @ts-expect-error using as=Link causes an error here, even though it runs completely fine
|
2024-09-26 02:15:54 +02:00
|
|
|
to={`/@${user.username}?page=${currentPage - 1}`}
|
|
|
|
disabled={currentPage === 0}
|
|
|
|
/>
|
|
|
|
{paginationItems}
|
|
|
|
<Pagination.Next
|
|
|
|
as={Link}
|
2024-09-29 20:37:30 +02:00
|
|
|
// @ts-expect-error using as=Link causes an error here, even though it runs completely fine
|
2024-09-26 02:15:54 +02:00
|
|
|
to={`/@${user.username}?page=${currentPage + 1}`}
|
|
|
|
disabled={currentPage === pageCount - 1}
|
|
|
|
/>
|
|
|
|
</Pagination>
|
|
|
|
);
|
|
|
|
|
2024-09-05 22:29:12 +02:00
|
|
|
return (
|
|
|
|
<>
|
2024-09-25 19:48:28 +02:00
|
|
|
{isMeUser && (
|
2024-09-24 20:56:10 +02:00
|
|
|
<Alert variant="secondary">
|
|
|
|
<Trans t={t} i18nKey="user.own-profile-alert">
|
|
|
|
You are currently viewing your <strong>public</strong> profile.
|
|
|
|
<br />
|
2024-09-29 21:10:11 +02:00
|
|
|
<Link to="/settings/profile">Edit your profile</Link>
|
2024-09-24 20:56:10 +02:00
|
|
|
</Trans>
|
|
|
|
</Alert>
|
|
|
|
)}
|
2024-09-29 20:32:54 +02:00
|
|
|
<BaseProfile
|
|
|
|
name={user.username}
|
2024-09-29 21:10:11 +02:00
|
|
|
userI18nKeys={true}
|
2024-09-29 20:32:54 +02:00
|
|
|
profile={user}
|
|
|
|
customPreferences={user.custom_preferences}
|
|
|
|
/>
|
2024-09-26 02:15:54 +02:00
|
|
|
{(members.length > 0 || isMeUser) && (
|
|
|
|
<>
|
|
|
|
<hr />
|
|
|
|
<h2>
|
|
|
|
{user.member_title || t("user.heading.members")}{" "}
|
|
|
|
{isMeUser && (
|
|
|
|
// @ts-expect-error using as=Link causes an error here, even though it runs completely fine
|
2024-09-30 21:40:28 +02:00
|
|
|
<Button as={Link} to="/settings/new-member" variant="success">
|
2024-09-25 19:48:28 +02:00
|
|
|
<PersonPlusFill /> {t("user.create-member-button")}
|
|
|
|
</Button>
|
2024-09-26 02:15:54 +02:00
|
|
|
)}
|
|
|
|
</h2>
|
|
|
|
{pageCount > 1 && pagination}
|
|
|
|
<div className="grid">
|
|
|
|
{members.length === 0 ? (
|
|
|
|
<div>
|
|
|
|
<Trans t={t} i18nKey="user.no-members-blurb">
|
|
|
|
You don't have any members yet.
|
|
|
|
<br />
|
|
|
|
Members are sub-profiles that can have their own avatar, names, pronouns, and
|
|
|
|
preferred terms.
|
|
|
|
<br />
|
|
|
|
You can create a new member with the "Create member" button above.{" "}
|
|
|
|
<span className="text-muted">(only you can see this)</span>
|
|
|
|
</Trans>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="row row-cols-2 row-cols-md-3 row-cols-lg-4 row-cols-xl-5 text-center">
|
|
|
|
{members.map((member, i) => (
|
|
|
|
<MemberCard user={user} member={member} key={i} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{pageCount > 1 && pagination}
|
|
|
|
</>
|
|
|
|
)}
|
2024-09-05 22:29:12 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|