refactor(frontend): some degree of api wrapping
This commit is contained in:
parent
f4a63fc95e
commit
ba24815320
11 changed files with 365 additions and 222 deletions
|
@ -6,8 +6,7 @@ import NavItem from "./NavItem";
|
|||
import Logo from "./logo";
|
||||
import { useRecoilState } from "recoil";
|
||||
import { userState } from "../lib/state";
|
||||
import fetchAPI from "../lib/fetch";
|
||||
import { APIError, ErrorCode, MeUser } from "../lib/types";
|
||||
import { fetchAPI, APIError, ErrorCode, MeUser } from "../lib/api-fetch";
|
||||
|
||||
export default function Navigation() {
|
||||
const [user, setUser] = useRecoilState(userState);
|
||||
|
|
|
@ -1,14 +1,4 @@
|
|||
import Head from "next/head";
|
||||
import {
|
||||
Field,
|
||||
Member,
|
||||
Name,
|
||||
PartialPerson,
|
||||
Person,
|
||||
Pronoun,
|
||||
User,
|
||||
WordStatus,
|
||||
} from "../lib/types";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
import { userState } from "../lib/state";
|
||||
import { useRecoilValue } from "recoil";
|
||||
|
@ -23,12 +13,13 @@ import {
|
|||
import BlueLink from "./BlueLink";
|
||||
import React from "react";
|
||||
import Card from "./Card";
|
||||
import { Field, Label, LabelStatus, Person, User } from "../lib/api";
|
||||
|
||||
export default function PersonPage({ person }: { person: Person }) {
|
||||
return (
|
||||
<>
|
||||
<Head>
|
||||
<title key="title">{`${personFullHandle(person)} - pronouns.cc`}</title>
|
||||
<title key="title">{`${person.fullHandle()} - pronouns.cc`}</title>
|
||||
</Head>
|
||||
<PersonHead person={person} />
|
||||
<IsOwnUserPageNotice person={person} />
|
||||
|
@ -47,97 +38,42 @@ export default function PersonPage({ person }: { person: Person }) {
|
|||
<PersonAvatar person={person} />
|
||||
<PersonInfo person={person} />
|
||||
</div>
|
||||
<LabelList content={person.names ?? []} />
|
||||
<LabelList content={person.pronouns ?? []} />
|
||||
<FieldCardGrid fields={person.fields ?? []} />
|
||||
{"user" in person ? (
|
||||
<BlueLink to={personURL(person.user)}>{`< ${
|
||||
person.user.display_name ?? person.user.name
|
||||
}`}</BlueLink>
|
||||
<LabelList content={person.names} />
|
||||
<LabelList content={person.pronouns} />
|
||||
<FieldCardGrid fields={person.fields} />
|
||||
{person instanceof User ? (
|
||||
<MemberList user={person} />
|
||||
) : (
|
||||
<MemberList user={person as any as User} />
|
||||
<BlueLink
|
||||
to={person.relativeURL()}
|
||||
>{`< ${person.display()}`}</BlueLink>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** Full handle of a person. */
|
||||
function personFullHandle(person: Person) {
|
||||
return "user" in person
|
||||
? `&${person.name}@${person.user.name}`
|
||||
: `@${person.name}`;
|
||||
}
|
||||
|
||||
/** Short handle of a person. */
|
||||
function personShortHandle(person: Person) {
|
||||
return ("user" in person ? "&" : "@") + person.name;
|
||||
}
|
||||
|
||||
/** The user (account) associated with a person. */
|
||||
function personUser(person: Person) {
|
||||
return "user" in person ? person.user : person;
|
||||
}
|
||||
|
||||
/** The (relative) URL pointing to a person. */
|
||||
function personURL(person: PartialPerson | Member) {
|
||||
const domain =
|
||||
typeof window !== "undefined" ? window.location.origin : process.env.DOMAIN;
|
||||
return `${domain}/u/${"user" in person ? `${person.user.name}/` : ""}${
|
||||
person.name
|
||||
}`;
|
||||
}
|
||||
|
||||
function PersonHead({ person }: { person: Person }) {
|
||||
const {
|
||||
id,
|
||||
name,
|
||||
display_name,
|
||||
avatar_urls,
|
||||
bio,
|
||||
links,
|
||||
names,
|
||||
pronouns,
|
||||
fields,
|
||||
} = person;
|
||||
const { displayName, avatarUrls, bio, names, pronouns } = person;
|
||||
let description = "";
|
||||
if (
|
||||
names?.filter((name) => name.status === WordStatus.Favourite)?.length &&
|
||||
pronouns?.filter((pronoun) => pronoun.status === WordStatus.Favourite)
|
||||
?.length
|
||||
) {
|
||||
description = `${personShortHandle(person)} goes by ${names
|
||||
.filter((name) => name.status === WordStatus.Favourite)
|
||||
.map((name) => name.name)
|
||||
.join(", ")} and uses ${pronouns
|
||||
.filter((pronoun) => pronoun.status === WordStatus.Favourite)
|
||||
.map(
|
||||
(pronoun) =>
|
||||
pronoun.display_text ??
|
||||
pronoun.pronouns.split("/").slice(0, 2).join("/")
|
||||
)
|
||||
.join(", ")} pronouns.`;
|
||||
} else if (
|
||||
names?.filter((name) => name.status === WordStatus.Favourite)?.length
|
||||
) {
|
||||
description = `${personShortHandle(person)} goes by ${names
|
||||
.filter((name) => name.status === WordStatus.Favourite)
|
||||
.map((name) => name.name)
|
||||
.join(", ")}.`;
|
||||
} else if (
|
||||
pronouns?.filter((pronoun) => pronoun.status === WordStatus.Favourite)
|
||||
?.length
|
||||
) {
|
||||
description = `${personShortHandle(person)} uses ${pronouns
|
||||
.filter((pronoun) => pronoun.status === WordStatus.Favourite)
|
||||
.map(
|
||||
(pronoun) =>
|
||||
pronoun.display_text ??
|
||||
pronoun.pronouns.split("/").slice(0, 2).join("/")
|
||||
)
|
||||
.join(", ")} pronouns.`;
|
||||
const favNames = names.filter((x) => x.status === LabelStatus.Favourite);
|
||||
const favPronouns = pronouns.filter(
|
||||
(x) => x.status === LabelStatus.Favourite
|
||||
);
|
||||
if (favNames.length || favPronouns.length) {
|
||||
description = `${person.shortHandle()}${
|
||||
favNames.length
|
||||
? ` goes by ${favNames.map((x) => x.display()).join(", ")}`
|
||||
: ""
|
||||
}${favNames.length && favPronouns.length ? " and" : ""}${
|
||||
favPronouns.length
|
||||
? `uses ${favPronouns
|
||||
.map((x) => x.shortDisplay())
|
||||
.join(", ")} pronouns.`
|
||||
: ""
|
||||
}`;
|
||||
} else if (bio && bio !== "") {
|
||||
description = bio.slice(0, 500);
|
||||
description = `${bio.slice(0, 500)}${bio.length > 500 ? "…" : ""}`;
|
||||
}
|
||||
|
||||
return (
|
||||
|
@ -147,22 +83,20 @@ function PersonHead({ person }: { person: Person }) {
|
|||
key="og:title"
|
||||
property="og:title"
|
||||
content={
|
||||
display_name
|
||||
? `${display_name} (${personFullHandle(person)})`
|
||||
: personFullHandle(person)
|
||||
displayName
|
||||
? `${displayName} (${person.fullHandle()})`
|
||||
: person.fullHandle()
|
||||
}
|
||||
/>
|
||||
{avatar_urls && avatar_urls.length > 0 ? (
|
||||
<meta key="og:image" property="og:image" content={avatar_urls[0]} />
|
||||
) : (
|
||||
<></>
|
||||
{avatarUrls && avatarUrls.length > 0 && (
|
||||
<meta key="og:image" property="og:image" content={avatarUrls[0]} />
|
||||
)}
|
||||
<meta
|
||||
key="og:description"
|
||||
property="og:description"
|
||||
content={description}
|
||||
/>
|
||||
<meta key="og:url" property="og:url" content={personURL(person)} />
|
||||
<meta key="og:url" property="og:url" content={person.absoluteURL()} />
|
||||
</Head>
|
||||
);
|
||||
}
|
||||
|
@ -180,15 +114,15 @@ function IsOwnUserPageNotice({ person }: { person: Person }) {
|
|||
}
|
||||
|
||||
function MemberList({ user, className }: { user: User; className?: string }) {
|
||||
const partialMembers = user.members;
|
||||
const partialMembers = user.partialMembers;
|
||||
return (
|
||||
<div className={`mx-auto flex-col items-center ${className || ""}`}>
|
||||
<h1 className="text-2xl">Members</h1>
|
||||
<ul>
|
||||
{partialMembers?.map((partialMember) => (
|
||||
{partialMembers.map((partialMember) => (
|
||||
<li className='before:[content:"-_"]' key={partialMember.id}>
|
||||
<BlueLink to={`/u/${user.name}/${partialMember.name}`}>
|
||||
<span>{partialMember.display_name ?? partialMember.name}</span>
|
||||
<span>{partialMember.displayName ?? partialMember.name}</span>
|
||||
</BlueLink>
|
||||
</li>
|
||||
))}
|
||||
|
@ -198,12 +132,12 @@ function MemberList({ user, className }: { user: User; className?: string }) {
|
|||
}
|
||||
|
||||
function PersonAvatar({ person }: { person: Person }) {
|
||||
const { display_name, name, avatar_urls } = person;
|
||||
return avatar_urls && avatar_urls.length !== 0 ? (
|
||||
const { displayName, name, avatarUrls } = person;
|
||||
return avatarUrls && avatarUrls.length !== 0 ? (
|
||||
<FallbackImage
|
||||
className="max-w-xs rounded-full"
|
||||
urls={avatar_urls}
|
||||
alt={`${display_name || name}'s avatar`}
|
||||
urls={avatarUrls}
|
||||
alt={`${displayName || name}'s avatar`}
|
||||
/>
|
||||
) : (
|
||||
<></>
|
||||
|
@ -211,16 +145,16 @@ function PersonAvatar({ person }: { person: Person }) {
|
|||
}
|
||||
|
||||
function PersonInfo({ person }: { person: Person }) {
|
||||
const { display_name, name, bio, links } = person;
|
||||
const { displayName, name, bio, links } = person;
|
||||
return (
|
||||
<div className="flex flex-col">
|
||||
{/* name */}
|
||||
<h1 className="text-2xl font-bold">
|
||||
{display_name === null ? name : display_name}
|
||||
{displayName === null ? name : displayName}
|
||||
</h1>
|
||||
{/* handle */}
|
||||
<h3 className="text-xl font-light text-slate-600 dark:text-slate-400">
|
||||
{personFullHandle(person)}
|
||||
{person.fullHandle()}
|
||||
</h3>
|
||||
{/* bio */}
|
||||
{bio && (
|
||||
|
@ -229,7 +163,7 @@ function PersonInfo({ person }: { person: Person }) {
|
|||
</ReactMarkdown>
|
||||
)}
|
||||
{/* links */}
|
||||
{links?.length && (
|
||||
{links.length > 0 && (
|
||||
<div className="flex flex-col mx-auto lg:ml-auto">
|
||||
{links.map((link, index) => (
|
||||
<a
|
||||
|
@ -247,8 +181,8 @@ function PersonInfo({ person }: { person: Person }) {
|
|||
);
|
||||
}
|
||||
|
||||
function LabelList({ content }: { content: Name[] | Pronoun[] }) {
|
||||
return content?.length > 0 ? (
|
||||
function LabelList({ content }: { content: Label[] }) {
|
||||
return content.length > 0 ? (
|
||||
<div className="border-b border-slate-200 dark:border-slate-700">
|
||||
{content.map((label, index) => (
|
||||
<LabelLine key={index} label={label} />
|
||||
|
@ -259,63 +193,62 @@ function LabelList({ content }: { content: Name[] | Pronoun[] }) {
|
|||
);
|
||||
}
|
||||
|
||||
function LabelStatusIcon({ status }: { status: WordStatus }) {
|
||||
function LabelStatusIcon({ status }: { status: LabelStatus }) {
|
||||
return React.createElement(
|
||||
{
|
||||
[WordStatus.Favourite]: HeartFill,
|
||||
[WordStatus.Okay]: HandThumbsUp,
|
||||
[WordStatus.Jokingly]: EmojiLaughing,
|
||||
[WordStatus.FriendsOnly]: People,
|
||||
[WordStatus.Avoid]: HandThumbsDown,
|
||||
[LabelStatus.Favourite]: HeartFill,
|
||||
[LabelStatus.Okay]: HandThumbsUp,
|
||||
[LabelStatus.Jokingly]: EmojiLaughing,
|
||||
[LabelStatus.FriendsOnly]: People,
|
||||
[LabelStatus.Avoid]: HandThumbsDown,
|
||||
}[status],
|
||||
{ className: "inline" }
|
||||
);
|
||||
}
|
||||
|
||||
function LabelsLine({ labels }: { labels: Name[] | Pronoun[] }) {
|
||||
if (!labels?.length) return <></>;
|
||||
const status = labels[0].status;
|
||||
const text = labels
|
||||
.map((label) =>
|
||||
"name" in label
|
||||
? label.name
|
||||
: label.display_text ?? label.pronouns.split("/").slice(0, 2).join("/")
|
||||
)
|
||||
.join(", ");
|
||||
return (
|
||||
function LabelsLine({
|
||||
status,
|
||||
texts,
|
||||
}: {
|
||||
status: LabelStatus;
|
||||
texts: string[];
|
||||
}) {
|
||||
return !texts.length ? (
|
||||
<></>
|
||||
) : (
|
||||
<p
|
||||
className={`
|
||||
${status === WordStatus.Favourite ? "text-lg font-bold" : ""}
|
||||
${status === LabelStatus.Favourite ? "text-lg font-bold" : ""}
|
||||
${
|
||||
status === WordStatus.Avoid ? "text-slate-600 dark:text-slate-400" : ""
|
||||
status === LabelStatus.Avoid ? "text-slate-600 dark:text-slate-400" : ""
|
||||
}`}
|
||||
>
|
||||
<LabelStatusIcon status={status} /> {text}
|
||||
<LabelStatusIcon status={status} /> {texts.join(", ")}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
|
||||
function LabelLine({ label }: { label: Name | Pronoun }) {
|
||||
return <LabelsLine labels={[label] as Name[] | Pronoun[]} />;
|
||||
function LabelLine({ label }: { label: Label }) {
|
||||
return <LabelsLine status={label.status} texts={[label.display()]} />;
|
||||
}
|
||||
|
||||
function FieldCardGrid({ fields }: { fields: Field[] }) {
|
||||
return (
|
||||
<div className="flex flex-col md:flex-row gap-4 py-2 [&>*]:flex-1">
|
||||
{fields?.map((field, index) => (
|
||||
{fields.map((field, index) => (
|
||||
<FieldCard field={field} key={index} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const fieldEntryStatus: { [key in string]: WordStatus } = {
|
||||
favourite: WordStatus.Favourite,
|
||||
okay: WordStatus.Okay,
|
||||
jokingly: WordStatus.Jokingly,
|
||||
friends_only: WordStatus.FriendsOnly,
|
||||
avoid: WordStatus.Avoid,
|
||||
};
|
||||
const labelStatusOrder: LabelStatus[] = [
|
||||
LabelStatus.Favourite,
|
||||
LabelStatus.Okay,
|
||||
LabelStatus.Jokingly,
|
||||
LabelStatus.FriendsOnly,
|
||||
LabelStatus.Avoid,
|
||||
];
|
||||
|
||||
function FieldCard({
|
||||
field,
|
||||
|
@ -326,13 +259,13 @@ function FieldCard({
|
|||
}) {
|
||||
return (
|
||||
<Card title={field.name} draggable={draggable}>
|
||||
{Object.entries(fieldEntryStatus).map(([statusName, status], i) => (
|
||||
{labelStatusOrder.map((status, i) => (
|
||||
<LabelsLine
|
||||
key={i}
|
||||
labels={(field as any)[statusName]?.map((name: string) => ({
|
||||
name,
|
||||
status,
|
||||
}))}
|
||||
status={status}
|
||||
texts={field.labels
|
||||
.filter((x) => x.status === status)
|
||||
.map((x) => x.display())}
|
||||
/>
|
||||
))}
|
||||
</Card>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue