import { ListUl } from "@styled-icons/boxicons-regular"; import { Envelope, Edit, UserPlus, UserX, Group, InfoCircle, } from "@styled-icons/boxicons-solid"; import { observer } from "mobx-react-lite"; import { Link, useHistory } from "react-router-dom"; import { UserPermission, API } from "revolt.js"; import styles from "./UserProfile.module.scss"; import { Localizer, Text } from "preact-i18n"; import { useEffect, useLayoutEffect, useState } from "preact/hooks"; import { Button, Category, Error, IconButton, Modal, Preloader, } from "@revoltchat/ui"; import { noop } from "../../../../lib/js"; import ChannelIcon from "../../../../components/common/ChannelIcon"; import ServerIcon from "../../../../components/common/ServerIcon"; import Tooltip from "../../../../components/common/Tooltip"; import UserBadges from "../../../../components/common/user/UserBadges"; import UserIcon from "../../../../components/common/user/UserIcon"; import { Username } from "../../../../components/common/user/UserShort"; import UserStatus from "../../../../components/common/user/UserStatus"; import Markdown from "../../../../components/markdown/Markdown"; import { useSession } from "../../../../controllers/client/ClientController"; import { modalController } from "../../../../controllers/modals/ModalController"; import { ModalProps } from "../../types"; export const UserProfile = observer( ({ user_id, dummy, dummyProfile, ...props }: ModalProps<"user_profile">) => { const [profile, setProfile] = useState< undefined | null | API.UserProfile >(undefined); const [mutual, setMutual] = useState< undefined | null | API.MutualResponse >(undefined); const [isPublicBot, setIsPublicBot] = useState< undefined | null | boolean >(); const history = useHistory(); const session = useSession()!; const client = session.client!; const [tab, setTab] = useState("profile"); const user = client.users.get(user_id); if (!user) { if (props.onClose) useEffect(props.onClose, []); return null; } const users = mutual?.users.map((id) => client.users.get(id)); const mutualGroups = [...client.channels.values()].filter( (channel) => channel?.channel_type === "Group" && channel.recipient_ids!.includes(user_id), ); const mutualServers = mutual?.servers.map((id) => client.servers.get(id), ); useLayoutEffect(() => { if (!user_id) return; if (typeof profile !== "undefined") setProfile(undefined); if (typeof mutual !== "undefined") setMutual(undefined); if (typeof isPublicBot !== "undefined") setIsPublicBot(undefined); // eslint-disable-next-line }, [user_id]); useEffect(() => { if (dummy) { setProfile(dummyProfile); } }, [dummy, dummyProfile]); useEffect(() => { if (dummy) return; if (session.state === "Online" && typeof mutual === "undefined") { setMutual(null); user.fetchMutual().then(setMutual); } }, [mutual, session.state, dummy, user]); useEffect(() => { if (dummy) return; if (session.state === "Online" && typeof profile === "undefined") { setProfile(null); if (user.permission & UserPermission.ViewProfile) { user.fetchProfile().then(setProfile).catch(noop); } } }, [profile, session.state, dummy, user]); useEffect(() => { if ( session.state === "Online" && user.bot && typeof isPublicBot === "undefined" ) { setIsPublicBot(null); client.bots .fetchPublic(user._id) .then(() => setIsPublicBot(true)) .catch(noop); } }, [isPublicBot, session.state, user, client.bots]); const backgroundURL = profile && client.generateFileURL( profile.background as any, { width: 1000 }, true, ); const badges = user.badges ?? 0; const flags = user.flags ?? 0; const children = ( <>
user.avatar && modalController.push({ type: "image_viewer", attachment: user.avatar, }) } />
modalController.writeText(user.username) }> @{user.username} {user.status?.text && ( )}
{isPublicBot && ( )} {user.relationship === "Friend" && ( }> { props.onClose?.(); history.push(`/open/${user_id}`); }}> )} {user.relationship === "User" && !dummy && ( { props.onClose?.(); history.push(`/settings/profile`); }}> )} {!user.bot && flags != 2 && flags != 4 && (user.relationship === "Incoming" || user.relationship === "None" || user.relationship === null) && ( user.addFriend()}> )} {user.relationship === "Outgoing" && ( user.removeFriend()}> )}
setTab("profile")}>
{user.relationship !== "User" && ( <>
setTab("friends")}>
setTab("groups")}>
setTab("servers")}>
)}
{tab === "profile" && (profile?.content || badges > 0 || flags > 0 || user.bot ? (
{flags & 1 ? ( /** ! FIXME: i18n this area */ ) : undefined} {flags & 2 ? ( ) : undefined} {flags & 4 ? ( ) : undefined} {user.bot ? ( <>
bot owner
user.bot && modalController.push({ type: "user_profile", user_id: user.bot.owner, }) } className={styles.entry} key={user.bot.owner}>
) : undefined} {badges > 0 && (
)} {badges > 0 && ( )} {profile?.content && ( <>
)} {/*
*/}
) : (
))} {tab === "friends" && (users ? ( users.length === 0 ? (
) : (
{users.map( (x) => x && (
modalController.push({ type: "user_profile", user_id: x._id, }) } className={styles.entry} key={x._id}> {x.username}
), )}
) ) : ( ))} {tab === "groups" && (mutualGroups.length === 0 ? (
) : (
{mutualGroups.map( (x) => x?.channel_type === "Group" && (
{x.name}
), )}
))} {tab === "servers" && (!mutualServers || mutualServers.length === 0 ? (
) : (
{mutualServers.map( (x) => x && (
{x.name}
), )}
))}
); if (dummy) return
{children}
; return ( {children} ); }, );