import { decodeTime } from "ulid"; import { Link, useHistory } from "react-router-dom"; import { Localizer, Text } from "preact-i18n"; import styles from "./UserProfile.module.scss"; import Modal from "../../../components/ui/Modal"; import { Route } from "revolt.js/dist/api/routes"; import { Users } from "revolt.js/dist/api/objects"; import { useIntermediate } from "../Intermediate"; import { CashStack } from "@styled-icons/bootstrap"; import Preloader from "../../../components/ui/Preloader"; import Tooltip from '../../../components/common/Tooltip'; import IconButton from "../../../components/ui/IconButton"; import Markdown from '../../../components/markdown/Markdown'; import { UserPermission } from "revolt.js/dist/api/permissions"; import UserIcon from '../../../components/common/user/UserIcon'; import ChannelIcon from '../../../components/common/ChannelIcon'; import UserStatus from '../../../components/common/user/UserStatus'; import { Mail, Edit, UserPlus, Shield } from "@styled-icons/feather"; import { useContext, useEffect, useLayoutEffect, useState } from "preact/hooks"; import { AppContext, ClientStatus, StatusContext } from "../../revoltjs/RevoltClient"; import { useChannels, useForceUpdate, useUserPermission, useUsers } from "../../revoltjs/hooks"; interface Props { user_id: string; dummy?: boolean; onClose: () => void; dummyProfile?: Users.Profile; } enum Badges { Developer = 1, Translator = 2, Supporter = 4, ResponsibleDisclosure = 8, EarlyAdopter = 256 } export function UserProfile({ user_id, onClose, dummy, dummyProfile }: Props) { const { writeClipboard } = useIntermediate(); const [profile, setProfile] = useState( undefined ); const [mutual, setMutual] = useState< undefined | null | Route<"GET", "/users/id/mutual">["response"] >(undefined); const history = useHistory(); const client = useContext(AppContext); const status = useContext(StatusContext); const [tab, setTab] = useState("profile"); const ctx = useForceUpdate(); const all_users = useUsers(undefined, ctx); const channels = useChannels(undefined, ctx); const user = all_users.find(x => x!._id === user_id); const users = mutual?.users ? all_users.filter(x => mutual.users.includes(x!._id)) : undefined; if (!user) { useEffect(onClose, []); return null; } const permissions = useUserPermission(user!._id, ctx); useLayoutEffect(() => { if (!user_id) return; if (typeof profile !== 'undefined') setProfile(undefined); if (typeof mutual !== 'undefined') setMutual(undefined); }, [user_id]); if (dummy) { useLayoutEffect(() => { setProfile(dummyProfile); }, [dummyProfile]); } useEffect(() => { if (dummy) return; if ( status === ClientStatus.ONLINE && typeof mutual === "undefined" ) { setMutual(null); client.users .fetchMutual(user_id) .then(data => setMutual(data)); } }, [mutual, status]); useEffect(() => { if (dummy) return; if ( status === ClientStatus.ONLINE && typeof profile === "undefined" ) { setProfile(null); if (permissions & UserPermission.ViewProfile) { client.users .fetchProfile(user_id) .then(data => setProfile(data)) .catch(() => {}); } } }, [profile, status]); const mutualGroups = channels.filter( channel => channel?.channel_type === "Group" && channel.recipients.includes(user_id) ); const backgroundURL = profile && client.users.getBackgroundURL(profile, { width: 1000 }, true); const badges = (user.badges ?? 0) | (decodeTime(user._id) < 1623751765790 ? Badges.EarlyAdopter : 0); return (
writeClipboard(user.username)}> @{user.username} {user.status?.text && ( )}
{user.relationship === Users.Relationship.Friend && ( } > { onClose(); history.push(`/open/${user_id}`); }}> )} {user.relationship === Users.Relationship.User && ( { onClose(); if (dummy) return; history.push(`/settings/profile`); }}> )} {(user.relationship === Users.Relationship.Incoming || user.relationship === Users.Relationship.None) && ( client.users.addFriend(user.username)}> )}
setTab("profile")} >
{ user.relationship !== Users.Relationship.User && <>
setTab("friends")} >
setTab("groups")} >
}
{tab === "profile" &&
{ !(profile?.content || (badges > 0)) &&
} { (badges > 0) &&
} { (badges > 0) && (
{badges & Badges.Developer ? ( } > ) : ( <> )} {badges & Badges.Translator ? ( } > ) : ( <> )} {badges & Badges.EarlyAdopter ? ( } > ) : ( <> )} {badges & Badges.Supporter ? ( } > ) : ( <> )} {badges & Badges.ResponsibleDisclosure ? ( } > ) : ( <> )}
)} { profile?.content &&
} {/*
*/}
} {tab === "friends" && (users ? (
{users.length === 0 ? (
) : ( users.map( x => x && ( //
{x.username}
//
) ) )}
) : ( ))} {tab === "groups" && (
{mutualGroups.length === 0 ? (
) : ( mutualGroups.map( x => x?.channel_type === "Group" && (
{x.name}
) ) )}
)}
); }