webapp/src/components/Author/AuthorCard/AuthorCard.tsx

396 lines
13 KiB
TypeScript
Raw Normal View History

2024-04-08 14:48:58 +00:00
import type { Author, Community } from "../../../graphql/schema/core.gen";
2024-04-08 14:48:58 +00:00
import { openPage, redirectPage } from "@nanostores/router";
import { clsx } from "clsx";
import {
For,
Show,
createEffect,
createMemo,
createSignal,
onMount,
} from "solid-js";
2024-04-08 14:48:58 +00:00
import { useFollowing } from "../../../context/following";
import { useLocalize } from "../../../context/localize";
import { useSession } from "../../../context/session";
import { FollowingEntity, Topic } from "../../../graphql/schema/core.gen";
import { SubscriptionFilter } from "../../../pages/types";
import { router, useRouter } from "../../../stores/router";
import { isAuthor } from "../../../utils/isAuthor";
import { translit } from "../../../utils/ru2en";
import { isCyrillic } from "../../../utils/translate";
import { SharePopup, getShareUrl } from "../../Article/SharePopup";
import { Modal } from "../../Nav/Modal";
import { TopicBadge } from "../../Topic/TopicBadge";
import { Button } from "../../_shared/Button";
import { ShowOnlyOnClient } from "../../_shared/ShowOnlyOnClient";
import { AuthorBadge } from "../AuthorBadge";
import { Userpic } from "../Userpic";
2024-04-08 14:48:58 +00:00
import stylesButton from "../../_shared/Button/Button.module.scss";
import styles from "./AuthorCard.module.scss";
2022-09-09 11:53:35 +00:00
type Props = {
2024-04-08 14:48:58 +00:00
author: Author;
followers?: Author[];
following?: Array<Author | Topic>;
};
export const AuthorCard = (props: Props) => {
2024-04-08 14:48:58 +00:00
const { t, lang } = useLocalize();
const { author, isSessionLoaded, requireAuthentication } = useSession();
const [authorSubs, setAuthorSubs] = createSignal<
Array<Author | Topic | Community>
>([]);
const [subscriptionFilter, setSubscriptionFilter] =
createSignal<SubscriptionFilter>("all");
const [isFollowed, setIsFollowed] = createSignal<boolean>();
const isProfileOwner = createMemo(() => author()?.slug === props.author.slug);
const { setFollowing, isOwnerSubscribed } = useFollowing();
2024-02-04 17:40:15 +00:00
2024-02-05 11:11:46 +00:00
onMount(() => {
2024-04-08 14:48:58 +00:00
setAuthorSubs(props.following);
});
2024-02-05 11:11:46 +00:00
createEffect(() => {
2024-04-08 14:48:58 +00:00
setIsFollowed(isOwnerSubscribed(props.author?.id));
});
2022-12-07 20:11:32 +00:00
const name = createMemo(() => {
2024-04-08 14:48:58 +00:00
if (lang() !== "ru" && isCyrillic(props.author.name)) {
if (props.author.name === "Дискурс") {
return "Discours";
2022-12-07 20:11:32 +00:00
}
2024-04-08 14:48:58 +00:00
return translit(props.author.name);
2022-12-07 20:11:32 +00:00
}
2024-04-08 14:48:58 +00:00
return props.author.name;
});
2022-12-07 20:11:32 +00:00
2022-09-09 11:53:35 +00:00
// TODO: reimplement AuthorCard
2024-04-08 14:48:58 +00:00
const { changeSearchParams } = useRouter();
2022-12-17 03:27:00 +00:00
const initChat = () => {
2024-01-31 12:34:15 +00:00
// eslint-disable-next-line solid/reactivity
requireAuthentication(() => {
2024-04-08 14:48:58 +00:00
openPage(router, "inbox");
changeSearchParams({
initChat: props.author.id.toString(),
2024-04-08 14:48:58 +00:00
});
}, "discussions");
};
createEffect(() => {
if (props.following) {
2024-04-08 14:48:58 +00:00
if (subscriptionFilter() === "authors") {
setAuthorSubs(props.following.filter((s) => "name" in s));
} else if (subscriptionFilter() === "topics") {
setAuthorSubs(props.following.filter((s) => "title" in s));
} else if (subscriptionFilter() === "communities") {
setAuthorSubs(props.following.filter((s) => "title" in s));
} else {
2024-04-08 14:48:58 +00:00
setAuthorSubs(props.following);
}
}
2024-04-08 14:48:58 +00:00
});
2024-01-31 12:34:15 +00:00
const handleFollowClick = () => {
2024-04-08 14:48:58 +00:00
const value = !isFollowed();
2024-01-31 12:34:15 +00:00
requireAuthentication(() => {
2024-04-08 14:48:58 +00:00
setIsFollowed(value);
setFollowing(FollowingEntity.Author, props.author.slug, value);
}, "subscribe");
};
2023-11-13 18:56:47 +00:00
2024-01-31 12:34:15 +00:00
const followButtonText = createMemo(() => {
2024-02-05 11:11:46 +00:00
if (isOwnerSubscribed(props.author?.id)) {
return (
<>
2024-04-08 14:48:58 +00:00
<span class={stylesButton.buttonSubscribeLabel}>
{t("Following")}
</span>
<span class={stylesButton.buttonSubscribeLabelHovered}>
{t("Unfollow")}
</span>
</>
2024-04-08 14:48:58 +00:00
);
}
2024-04-08 14:48:58 +00:00
return t("Follow");
});
2023-10-13 06:10:24 +00:00
return (
2024-04-08 14:48:58 +00:00
<div class={clsx(styles.author, "row")}>
<div class="col-md-5">
<Userpic
2024-04-08 14:48:58 +00:00
size={"XL"}
name={props.author.name}
2023-12-03 10:22:42 +00:00
userpic={props.author.pic}
slug={props.author.slug}
class={styles.circlewrap}
/>
</div>
2024-04-08 14:48:58 +00:00
<div class={clsx("col-md-15 col-xl-13", styles.authorDetails)}>
2023-10-13 06:10:24 +00:00
<div class={styles.authorDetailsWrapper}>
<div class={styles.authorName}>{name()}</div>
2023-11-28 17:06:13 +00:00
<Show when={props.author.bio}>
<div class={styles.authorAbout} innerHTML={props.author.bio} />
</Show>
2023-10-13 06:10:24 +00:00
<Show
when={
(props.followers && props.followers.length > 0) ||
(props.following && props.following.length > 0)
}
>
<div class={styles.subscribersContainer}>
2023-10-16 22:14:43 +00:00
<Show when={props.followers && props.followers.length > 0}>
2024-01-27 06:21:48 +00:00
<a href="?m=followers" class={styles.subscribers}>
2023-10-16 22:14:43 +00:00
<For each={props.followers.slice(0, 3)}>
{(f) => (
2024-04-08 14:48:58 +00:00
<Userpic
size={"XS"}
name={f.name}
userpic={f.pic}
class={styles.subscribersItem}
/>
)}
2023-10-16 22:14:43 +00:00
</For>
<div class={styles.subscribersCounter}>
2024-04-08 14:48:58 +00:00
{t("SubscriberWithCount", {
count: props.followers.length ?? 0,
})}
2023-10-16 22:14:43 +00:00
</div>
</a>
</Show>
2023-10-13 21:29:11 +00:00
2023-10-16 22:14:43 +00:00
<Show when={props.following && props.following.length > 0}>
2024-01-27 06:21:48 +00:00
<a href="?m=following" class={styles.subscribers}>
2023-10-16 22:14:43 +00:00
<For each={props.following.slice(0, 3)}>
{(f) => {
2024-04-08 14:48:58 +00:00
if ("name" in f) {
return (
<Userpic
2024-04-08 14:48:58 +00:00
size={"XS"}
name={f.name}
2023-12-03 10:22:42 +00:00
userpic={f.pic}
class={styles.subscribersItem}
/>
2024-04-08 14:48:58 +00:00
);
2024-02-04 09:23:20 +00:00
}
2024-04-08 14:48:58 +00:00
if ("title" in f) {
return (
<Userpic
2024-04-08 14:48:58 +00:00
size={"XS"}
name={f.title}
userpic={f.pic}
class={styles.subscribersItem}
/>
2024-04-08 14:48:58 +00:00
);
2023-10-16 22:14:43 +00:00
}
2024-02-04 09:23:20 +00:00
2024-04-08 14:48:58 +00:00
return null;
2023-10-16 22:14:43 +00:00
}}
</For>
<div class={styles.subscribersCounter}>
2024-04-08 14:48:58 +00:00
{t("SubscriptionWithCount", {
count: props?.following.length ?? 0,
})}
2023-10-16 22:14:43 +00:00
</div>
</a>
</Show>
</div>
2023-10-13 06:10:24 +00:00
</Show>
</div>
<ShowOnlyOnClient>
2023-10-16 22:14:43 +00:00
<Show when={isSessionLoaded()}>
2023-10-17 06:11:44 +00:00
<Show when={props.author.links && props.author.links.length > 0}>
2023-10-16 22:14:43 +00:00
<div class={styles.authorSubscribeSocial}>
<For each={props.author.links}>
{(link) => (
<a
class={styles.socialLink}
2024-04-08 14:48:58 +00:00
href={link.startsWith("http") ? link : `https://${link}`}
2023-10-16 22:14:43 +00:00
target="_blank"
2024-02-04 09:23:20 +00:00
rel="nofollow noopener noreferrer"
2023-10-16 22:14:43 +00:00
>
<span class={styles.authorSubscribeSocialLabel}>
2024-04-08 14:48:58 +00:00
{link.startsWith("http") ? link : `https://${link}`}
2023-10-16 22:14:43 +00:00
</span>
</a>
)}
</For>
</div>
</Show>
<Show
when={isProfileOwner()}
fallback={
<div class={styles.authorActions}>
2024-02-05 11:11:46 +00:00
<Show when={authorSubs().length}>
<Button
onClick={handleFollowClick}
value={followButtonText()}
isSubscribeButton={true}
class={clsx({
[stylesButton.subscribed]: isFollowed(),
})}
/>
</Show>
2023-10-31 16:44:00 +00:00
<Button
2024-04-08 14:48:58 +00:00
variant={"secondary"}
value={t("Message")}
2023-10-31 16:44:00 +00:00
onClick={initChat}
2023-11-02 22:02:11 +00:00
class={styles.buttonWriteMessage}
2023-10-31 16:44:00 +00:00
/>
</div>
}
>
<div class={styles.authorActions}>
2023-10-16 22:14:43 +00:00
<Button
variant="secondary"
2024-04-08 14:48:58 +00:00
onClick={() => redirectPage(router, "profileSettings")}
2023-11-28 17:06:13 +00:00
value={
<>
2024-04-08 14:48:58 +00:00
<span class={styles.authorActionsLabel}>
{t("Edit profile")}
</span>
<span class={styles.authorActionsLabelMobile}>
{t("Edit")}
</span>
2023-11-28 17:06:13 +00:00
</>
}
2023-10-16 22:14:43 +00:00
/>
<SharePopup
title={props.author.name}
description={props.author.bio}
2023-12-03 10:22:42 +00:00
imageUrl={props.author.pic}
2024-04-08 14:48:58 +00:00
shareUrl={getShareUrl({
pathname: `/author/${props.author.slug}`,
})}
trigger={<Button variant="secondary" value={t("Share")} />}
2023-10-16 22:14:43 +00:00
/>
</div>
</Show>
2023-10-13 06:10:24 +00:00
</Show>
</ShowOnlyOnClient>
<Show when={props.followers}>
2024-04-08 14:48:58 +00:00
<Modal
variant="medium"
isResponsive={true}
name="followers"
maxHeight
>
2023-10-13 06:10:24 +00:00
<>
2024-04-08 14:48:58 +00:00
<h2>{t("Followers")}</h2>
2023-10-13 06:10:24 +00:00
<div class={styles.listWrapper}>
<div class="row">
<div class="col-24">
<For each={props.followers}>
2024-02-05 11:11:46 +00:00
{(follower: Author) => (
<AuthorBadge
author={follower}
isFollowed={{
loaded: Boolean(authorSubs()),
value: isOwnerSubscribed(follower.id),
}}
/>
)}
2023-10-13 06:10:24 +00:00
</For>
</div>
2023-09-02 22:14:34 +00:00
</div>
</div>
2023-10-13 06:10:24 +00:00
</>
</Modal>
</Show>
<Show when={props.following}>
2024-04-08 14:48:58 +00:00
<Modal
variant="medium"
isResponsive={true}
name="following"
maxHeight
>
2023-10-13 06:10:24 +00:00
<>
2024-04-08 14:48:58 +00:00
<h2>{t("Subscriptions")}</h2>
2023-10-13 06:10:24 +00:00
<ul class="view-switcher">
2024-04-08 14:48:58 +00:00
<li
class={clsx({
"view-switcher__item--selected":
subscriptionFilter() === "all",
})}
>
<button
type="button"
onClick={() => setSubscriptionFilter("all")}
>
{t("All")}
2023-10-13 06:10:24 +00:00
</button>
2024-04-08 14:48:58 +00:00
<span class="view-switcher__counter">
{props.following.length}
</span>
2023-10-13 06:10:24 +00:00
</li>
2024-04-08 14:48:58 +00:00
<li
class={clsx({
"view-switcher__item--selected":
subscriptionFilter() === "authors",
})}
>
<button
type="button"
onClick={() => setSubscriptionFilter("authors")}
>
{t("Authors")}
2023-10-13 06:10:24 +00:00
</button>
<span class="view-switcher__counter">
2024-04-08 14:48:58 +00:00
{props.following.filter((s) => "name" in s).length}
2023-10-13 06:10:24 +00:00
</span>
</li>
2024-04-08 14:48:58 +00:00
<li
class={clsx({
"view-switcher__item--selected":
subscriptionFilter() === "topics",
})}
>
<button
type="button"
onClick={() => setSubscriptionFilter("topics")}
>
{t("Topics")}
2023-10-13 06:10:24 +00:00
</button>
<span class="view-switcher__counter">
2024-04-08 14:48:58 +00:00
{props.following.filter((s) => "title" in s).length}
2023-10-13 06:10:24 +00:00
</span>
</li>
</ul>
<br />
<div class={styles.listWrapper}>
<div class="row">
<div class="col-24">
2024-01-31 12:34:15 +00:00
<For each={authorSubs()}>
2023-10-13 06:10:24 +00:00
{(subscription) =>
isAuthor(subscription) ? (
2024-02-05 11:11:46 +00:00
<AuthorBadge
isFollowed={{
loaded: Boolean(authorSubs()),
value: isOwnerSubscribed(subscription.id),
}}
author={subscription}
/>
2023-10-13 06:10:24 +00:00
) : (
2024-02-08 09:11:52 +00:00
<TopicBadge
isFollowed={{
loaded: Boolean(authorSubs()),
value: isOwnerSubscribed(subscription.id),
}}
topic={subscription}
/>
2023-10-13 06:10:24 +00:00
)
}
</For>
</div>
2023-09-02 22:14:34 +00:00
</div>
</div>
2023-10-13 06:10:24 +00:00
</>
</Modal>
</Show>
</div>
</div>
2024-04-08 14:48:58 +00:00
);
};