PluralKit/src/Components/Private/System.js

254 lines
8.3 KiB
JavaScript
Raw Normal View History

2021-06-26 23:06:51 +02:00
import React, { useState, useEffect } from "react";
import * as BS from "react-bootstrap";
2021-01-06 00:05:23 +01:00
import { useRouteMatch } from "react-router-dom";
2021-06-26 23:06:51 +02:00
import autosize from "autosize";
import "moment-timezone";
import Popup from "reactjs-popup";
import Twemoji from "react-twemoji";
2020-12-09 09:15:55 +01:00
2021-06-26 19:51:14 +02:00
import history from "../../History.js";
2021-06-26 23:06:51 +02:00
import defaultAvatar from "../../default_discord_avatar.png";
2020-12-11 16:35:25 +01:00
import { FaAddressCard } from "react-icons/fa";
2021-06-26 23:06:51 +02:00
import EditSystem from "./Edit/EditSystem.js";
import EditSystemPrivacy from "./Edit/EditSystemPrivacy.js";
2020-12-09 09:15:55 +01:00
export default function System(props) {
2021-06-26 23:06:51 +02:00
// match the url, if there's a member ID there, don't render this component at all
2021-07-31 13:03:29 +02:00
const match = useRouteMatch("/dash/:memberID");
2021-06-26 23:06:51 +02:00
// get the user from the localstorage
const [user, setUser] = useState(JSON.parse(localStorage.getItem("user")));
// bunch of useState stuff, used in the useEffect() hook below
const [name, setName] = useState("");
const [tag, setTag] = useState("");
const [timezone, setTimezone] = useState("");
const [avatar, setAvatar] = useState("");
2021-08-06 12:16:31 +02:00
const [banner, setBanner] = useState("");
2021-06-26 23:06:51 +02:00
const [desc, setDesc] = useState("");
const [editDesc, setEditDesc] = useState("");
// more useState, this time to actually handle state
// TODO: name them something more intuitive maybe?
const [editMode, setEditMode] = useState(false);
const [privacyEdit, setPrivacyEdit] = useState(false);
const [privacyView, setPrivacyView] = useState(false);
const [errorAlert, setErrorAlert] = useState(false);
// this useEffect does a couple of things after the user is gotten from localstorage
useEffect(() => {
// first require the discord markdown parser
const { toHTML } = require("../../Functions/discord-parser.js");
// check if there's a name object in user, if it's null, set name to a blank string, otherwise set name to user.name
2020-12-14 22:26:15 +01:00
if (user.name) {
2021-06-26 23:06:51 +02:00
setName(user.name);
} else setName("");
2020-12-14 22:26:15 +01:00
2021-06-26 23:06:51 +02:00
// same as above, but with the user tag instead
2020-12-11 16:35:25 +01:00
if (user.tag) {
2021-06-26 23:06:51 +02:00
setTag(user.tag);
} else setTag("");
2020-12-11 16:35:25 +01:00
2021-06-26 23:06:51 +02:00
// same as above but with timezone
2020-12-11 16:35:25 +01:00
if (user.tz) {
2021-06-26 23:06:51 +02:00
setTimezone(user.tz);
} else setTimezone("");
2020-12-11 16:35:25 +01:00
2021-06-26 23:06:51 +02:00
// also trims the avatar url so that 1. pngs won't be converted to jpegs and 2. won't be resized to 256x256
2020-12-11 16:35:25 +01:00
if (user.avatar_url) {
2021-06-26 23:06:51 +02:00
var avatarsmall = user.avatar_url.replace("&format=jpeg", "");
setAvatar(avatarsmall.replace("?width=256&height=256", ""));
} else setAvatar("");
2020-12-11 00:53:43 +01:00
2021-08-06 12:16:31 +02:00
if (user.banner) {
setBanner(user.banner);
} else setBanner("");
2021-06-26 23:06:51 +02:00
// same as above, but with descriptions
// two description variables! one is just the plain description, the other is parsed and converted to html
2020-12-09 09:15:55 +01:00
if (user.description) {
2021-06-26 23:06:51 +02:00
setDesc(toHTML(user.description));
setEditDesc(user.description);
} else {
setDesc("(no description)");
setEditDesc("");
2020-12-11 16:35:25 +01:00
}
2021-08-06 12:16:31 +02:00
}, [user.description, user.tag, user.avatar_url, user.tz, user.name, user.banner]);
2021-06-26 23:06:51 +02:00
// this just resizes the textarea when filled with larger amounts of text
useEffect(() => {
autosize(document.querySelector("textarea"));
});
if (match) return null;
return (
2021-08-06 12:16:31 +02:00
<>
{ user.banner && !localStorage.getItem("hidebanners") ? <div className="banner" style={{backgroundImage: `url(${user.banner})`}} alt=""/> : ""}
2021-06-26 23:06:51 +02:00
<BS.Card className="mb-3 mt-3 w-100">
<BS.Card.Header className="d-flex align-items-center justify-content-between">
<BS.Card.Title className="float-left">
<FaAddressCard className="mr-4 float-left" /> {name} ({user.id})
</BS.Card.Title>
{user.avatar_url ? (
<Popup
trigger={
<BS.Image
src={`${user.avatar_url}`}
style={{ width: 50, height: 50 }}
tabIndex="0"
className="float-right"
roundedCircle
/>
}
className="avatar"
modal
>
{(close) => (
<div className="text-center w-100 m-0" onClick={() => close()}>
<BS.Image
src={`${avatar}`}
style={{ "max-width": 640, height: "auto" }}
thumbnail
/>
</div>
)}
</Popup>
) : (
<BS.Image
src={defaultAvatar}
style={{ width: 50, height: 50 }}
className="float-right"
roundedCircle
/>
)}
</BS.Card.Header>
<BS.Card.Body>
{errorAlert ? (
<BS.Alert variant="danger">
Something went wrong, please try logging in and out again.
</BS.Alert>
) : (
""
)}
{editMode ? (
<EditSystem
editDesc={editDesc}
name={name}
tag={tag}
timezone={timezone}
avatar={avatar}
2021-08-06 12:16:31 +02:00
banner={banner}
2021-06-26 23:06:51 +02:00
setErrorAlert={setErrorAlert}
user={user}
setUser={setUser}
setEditMode={setEditMode}
/>
) : (
<>
2020-12-11 16:35:25 +01:00
<BS.Row>
2021-06-26 23:06:51 +02:00
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>ID:</b> {user.id}
</BS.Col>
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Tag:</b> {tag}
</BS.Col>
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Timezone:</b> {timezone}
</BS.Col>
{privacyView ? (
""
) : (
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Privacy:</b>{" "}
<BS.Button
variant="light"
size="sm"
onClick={() => setPrivacyView(true)}
>
View
</BS.Button>
</BS.Col>
)}
2020-12-11 16:35:25 +01:00
</BS.Row>
2021-06-26 23:06:51 +02:00
{privacyEdit ? (
<EditSystemPrivacy
setErrorAlert={setErrorAlert}
setUser={setUser}
user={user}
setPrivacyEdit={setPrivacyEdit}
/>
) : privacyView ? (
<>
<hr />
<h5>Viewing privacy settings</h5>
<BS.Row>
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Description:</b> {user.description_privacy}
</BS.Col>
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Member list: </b>
{user.member_list_privacy}
</BS.Col>
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Front:</b> {user.front_privacy}
</BS.Col>
<BS.Col className="mb-lg-3" xs={12} lg={3}>
<b>Front history:</b> {user.front_history_privacy}
</BS.Col>
</BS.Row>
<BS.Button
variant="light"
onClick={() => setPrivacyView(false)}
>
Exit
</BS.Button>{" "}
<BS.Button
variant="primary"
onClick={() => setPrivacyEdit(true)}
>
Edit
</BS.Button>
<hr />
</>
) : (
""
)}
<p>
<b>Description:</b>
</p>
{localStorage.getItem("twemoji") ? (
<Twemoji options={{ className: "twemoji" }}>
<p dangerouslySetInnerHTML={{ __html: desc }}></p>
</Twemoji>
) : (
<p dangerouslySetInnerHTML={{ __html: desc }}></p>
)}
{privacyEdit ? (
""
) : privacyView ? (
""
) : (
<>
<BS.Button variant="light" onClick={() => setEditMode(true)}>
Edit
</BS.Button>
<BS.Button
variant="primary"
className="float-right"
2021-07-31 13:03:29 +02:00
onClick={() => history.push(`/profile/${user.id}`)}
2021-06-26 23:06:51 +02:00
>
Profile
</BS.Button>
</>
)}
</>
)}
</BS.Card.Body>
</BS.Card>
2021-08-06 12:16:31 +02:00
</>
2021-06-26 23:06:51 +02:00
);
2020-12-09 09:15:55 +01:00
}