revite/src/pages/settings/channel/Overview.tsx

118 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-06-19 17:37:12 -04:00
import { Channels } from "revolt.js/dist/api/objects";
2021-07-05 06:23:23 -04:00
import styles from "./Panes.module.scss";
import { Text } from "preact-i18n";
2021-06-19 17:37:12 -04:00
import { useContext, useEffect, useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import TextAreaAutoSize from "../../../lib/TextAreaAutoSize";
2021-06-19 17:37:12 -04:00
import { FileUploader } from "../../../context/revoltjs/FileUploads";
2021-07-05 06:23:23 -04:00
import { AppContext } from "../../../context/revoltjs/RevoltClient";
import Button from "../../../components/ui/Button";
import InputBox from "../../../components/ui/InputBox";
2021-06-19 17:37:12 -04:00
interface Props {
2021-07-05 06:25:20 -04:00
channel:
| Channels.GroupChannel
| Channels.TextChannel
| Channels.VoiceChannel;
2021-06-19 17:37:12 -04:00
}
2021-07-01 12:36:34 -04:00
export default function Overview({ channel }: Props) {
2021-07-05 06:25:20 -04:00
const client = useContext(AppContext);
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
const [name, setName] = useState(channel.name);
const [description, setDescription] = useState(channel.description ?? "");
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
useEffect(() => setName(channel.name), [channel.name]);
useEffect(
() => setDescription(channel.description ?? ""),
[channel.description],
);
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
const [changed, setChanged] = useState(false);
function save() {
let changes: any = {};
if (name !== channel.name) changes.name = name;
if (description !== channel.description)
changes.description = description;
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
client.channels.edit(channel._id, changes);
setChanged(false);
}
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
return (
<div className={styles.overview}>
<div className={styles.row}>
<FileUploader
width={80}
height={80}
style="icon"
fileType="icons"
behaviour="upload"
maxFileSize={2_500_000}
onUpload={(icon) =>
client.channels.edit(channel._id, { icon })
}
previewURL={client.channels.getIconURL(
channel._id,
{ max_side: 256 },
true,
)}
remove={() =>
client.channels.edit(channel._id, { remove: "Icon" })
}
defaultPreview={
channel.channel_type === "Group"
? "/assets/group.png"
: undefined
}
/>
<div className={styles.name}>
<h3>
{channel.channel_type === "Group" ? (
<Text id="app.main.groups.name" />
) : (
<Text id="app.main.servers.channel_name" />
)}
</h3>
<InputBox
contrast
value={name}
maxLength={32}
onChange={(e) => {
setName(e.currentTarget.value);
if (!changed) setChanged(true);
}}
/>
</div>
</div>
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
<h3>
{channel.channel_type === "Group" ? (
<Text id="app.main.groups.description" />
) : (
<Text id="app.main.servers.channel_description" />
)}
</h3>
<TextAreaAutoSize
maxRows={10}
minHeight={60}
maxLength={1024}
value={description}
placeholder={"Add a description..."}
onChange={(ev) => {
setDescription(ev.currentTarget.value);
if (!changed) setChanged(true);
}}
/>
<p>
<Button onClick={save} contrast disabled={!changed}>
<Text id="app.special.modals.actions.save" />
</Button>
</p>
</div>
);
2021-06-19 17:37:12 -04:00
}