2021-08-30 12:18:42 -04:00
|
|
|
import { Key, Clipboard, Globe } from "@styled-icons/boxicons-regular";
|
|
|
|
import { LockAlt } from "@styled-icons/boxicons-solid";
|
2021-08-12 10:29:19 -04:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
import { Bot } from "revolt-api/types/Bots";
|
2021-08-30 12:18:42 -04:00
|
|
|
import styled from "styled-components";
|
2021-08-12 10:29:19 -04:00
|
|
|
|
2021-08-30 12:18:42 -04:00
|
|
|
import styles from "./Panes.module.scss";
|
|
|
|
import { Text } from "preact-i18n";
|
2021-08-12 10:29:19 -04:00
|
|
|
import { useEffect, useState } from "preact/hooks";
|
|
|
|
|
2021-08-30 12:18:42 -04:00
|
|
|
import { stopPropagation } from "../../../lib/stopPropagation";
|
|
|
|
|
2021-08-12 11:07:41 -04:00
|
|
|
import { useIntermediate } from "../../../context/intermediate/Intermediate";
|
2021-08-12 10:29:19 -04:00
|
|
|
import { useClient } from "../../../context/revoltjs/RevoltClient";
|
|
|
|
|
2021-08-30 12:18:42 -04:00
|
|
|
import Tooltip from "../../../components/common/Tooltip";
|
|
|
|
import UserIcon from "../../../components/common/user/UserIcon";
|
2021-08-12 10:29:19 -04:00
|
|
|
import Button from "../../../components/ui/Button";
|
|
|
|
import Checkbox from "../../../components/ui/Checkbox";
|
|
|
|
import InputBox from "../../../components/ui/InputBox";
|
|
|
|
import Overline from "../../../components/ui/Overline";
|
|
|
|
import Tip from "../../../components/ui/Tip";
|
2021-08-30 12:18:42 -04:00
|
|
|
import CategoryButton from "../../../components/ui/fluent/CategoryButton";
|
2021-08-30 15:10:55 -04:00
|
|
|
import { User } from "revolt.js/dist/maps/Users";
|
2021-08-12 10:29:19 -04:00
|
|
|
|
|
|
|
interface Data {
|
|
|
|
_id: string;
|
|
|
|
username: string;
|
|
|
|
public: boolean;
|
|
|
|
interactions_url?: string;
|
|
|
|
}
|
|
|
|
|
2021-08-30 15:10:55 -04:00
|
|
|
interface Changes {
|
|
|
|
name?: string;
|
|
|
|
public?: boolean;
|
|
|
|
interactions_url?: string;
|
|
|
|
remove?: "InteractionsURL";
|
|
|
|
}
|
|
|
|
|
2021-08-30 12:18:42 -04:00
|
|
|
const BotBadge = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
|
|
|
|
height: 1.3em;
|
|
|
|
padding: 0px 4px;
|
|
|
|
font-size: 0.7em;
|
|
|
|
user-select: none;
|
|
|
|
margin-inline-start: 2px;
|
|
|
|
text-transform: uppercase;
|
|
|
|
|
|
|
|
color: var(--foreground);
|
|
|
|
background: var(--accent);
|
|
|
|
border-radius: calc(var(--border-radius) / 2);
|
|
|
|
`;
|
|
|
|
|
2021-08-30 15:10:55 -04:00
|
|
|
interface Props {
|
|
|
|
bot: Bot;
|
|
|
|
user: User;
|
|
|
|
onDelete(): Promise<void>;
|
|
|
|
onUpdate(changes: Changes): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function BotCard({ bot, user, onDelete, onUpdate }: Props) {
|
|
|
|
const [data, setData] = useState<Data>({
|
|
|
|
_id: bot._id,
|
|
|
|
username: user!.username,
|
|
|
|
public: bot.public,
|
|
|
|
interactions_url: bot.interactions_url,
|
|
|
|
});
|
|
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const [editMode, setEditMode] = useState(false);
|
|
|
|
const [usernameRef, setUsernameRef] = useState<HTMLInputElement | null>(
|
|
|
|
null,
|
|
|
|
);
|
|
|
|
const [interactionsRef, setInteractionsRef] =
|
|
|
|
useState<HTMLInputElement | null>(null);
|
|
|
|
const { writeClipboard, openScreen } = useIntermediate();
|
2021-08-12 10:29:19 -04:00
|
|
|
|
2021-08-30 15:10:55 -04:00
|
|
|
async function save() {
|
|
|
|
const changes: Changes = {};
|
|
|
|
if (data.username !== user!.username) changes.name = data.username;
|
2021-08-12 10:29:19 -04:00
|
|
|
if (data.public !== bot.public) changes.public = data.public;
|
2021-08-30 15:10:55 -04:00
|
|
|
if (data.interactions_url === '')
|
|
|
|
changes.remove = 'InteractionsURL';
|
|
|
|
else if (data.interactions_url !== bot.interactions_url)
|
2021-08-12 10:29:19 -04:00
|
|
|
changes.interactions_url = data.interactions_url;
|
2021-08-30 15:10:55 -04:00
|
|
|
setSaving(true);
|
|
|
|
try {
|
|
|
|
await onUpdate(changes);
|
|
|
|
setEditMode(false);
|
|
|
|
} catch (e) {
|
|
|
|
// TODO error handling
|
|
|
|
}
|
|
|
|
setSaving(false);
|
2021-08-12 10:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-08-30 15:10:55 -04:00
|
|
|
<div
|
|
|
|
key={bot._id}
|
|
|
|
style={{
|
|
|
|
background: "var(--secondary-background)",
|
|
|
|
margin: "8px 0",
|
|
|
|
padding: "12px",
|
|
|
|
}}>
|
|
|
|
<div className={styles.infoheader}>
|
|
|
|
<div className={styles.container}>
|
|
|
|
<UserIcon
|
|
|
|
className={styles.avatar}
|
|
|
|
target={user!}
|
|
|
|
size={48}
|
|
|
|
onClick={() =>
|
|
|
|
openScreen({
|
|
|
|
id: "profile",
|
|
|
|
user_id: user!._id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{!editMode ? (
|
|
|
|
<div className={styles.userDetail}>
|
|
|
|
<div className={styles.userName}>
|
|
|
|
{user!.username}{" "}
|
|
|
|
<BotBadge>
|
|
|
|
<Text id="app.main.channel.bot" />
|
|
|
|
</BotBadge>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={styles.userid}>
|
|
|
|
<Tooltip
|
|
|
|
content={<Text id="app.special.copy" />}>
|
|
|
|
<a
|
|
|
|
onClick={() =>
|
|
|
|
writeClipboard(user!._id)
|
|
|
|
}>
|
|
|
|
{user!._id}
|
|
|
|
</a>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<InputBox
|
|
|
|
ref={setUsernameRef}
|
|
|
|
value={data.username}
|
|
|
|
disabled={saving}
|
|
|
|
onChange={(e) =>
|
|
|
|
setData({
|
|
|
|
...data,
|
|
|
|
username: e.currentTarget.value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{!editMode && (
|
|
|
|
<Tooltip
|
|
|
|
content={
|
|
|
|
bot.public
|
|
|
|
? "Bot is public. Anyone can invite it."
|
|
|
|
: "Bot is private. Only you can invite it."
|
|
|
|
}>
|
|
|
|
{bot.public ? (
|
|
|
|
<Globe size={24} />
|
|
|
|
) : (
|
|
|
|
<LockAlt size={24} />
|
|
|
|
)}
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
if (editMode) {
|
|
|
|
setData({
|
|
|
|
_id: bot._id,
|
|
|
|
username: user!.username,
|
|
|
|
public: bot.public,
|
|
|
|
interactions_url: bot.interactions_url,
|
|
|
|
});
|
|
|
|
usernameRef!.value = user!.username;
|
|
|
|
interactionsRef!.value = bot.interactions_url || "";
|
|
|
|
setEditMode(false);
|
|
|
|
} else setEditMode(true);
|
|
|
|
}}
|
|
|
|
contrast>
|
|
|
|
{editMode ? "Cancel" : "Edit"}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{!editMode && (
|
|
|
|
<CategoryButton
|
|
|
|
account
|
|
|
|
icon={<Key size={24} />}
|
|
|
|
onClick={() => writeClipboard(bot.token)}
|
|
|
|
description={
|
|
|
|
<>
|
|
|
|
{"••••••••••••••••••••••••••••••••••••"}{" "}
|
|
|
|
<a
|
|
|
|
onClick={(ev) =>
|
|
|
|
stopPropagation(
|
|
|
|
ev,
|
|
|
|
openScreen({
|
|
|
|
id: "token_reveal",
|
|
|
|
token: bot.token,
|
|
|
|
username: user!.username,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}>
|
|
|
|
<Text id="app.special.modals.actions.reveal" />
|
|
|
|
</a>
|
|
|
|
</>
|
2021-08-12 10:29:19 -04:00
|
|
|
}
|
2021-08-30 15:10:55 -04:00
|
|
|
action={<Clipboard size={18} />}>
|
|
|
|
Token
|
|
|
|
</CategoryButton>
|
|
|
|
)}
|
|
|
|
{editMode && (
|
|
|
|
<>
|
|
|
|
<Checkbox
|
|
|
|
checked={data.public}
|
|
|
|
disabled={saving}
|
|
|
|
contrast
|
|
|
|
description="Whether to allow other users to invite this bot."
|
|
|
|
onChange={(v) => setData({ ...data, public: v })}>
|
|
|
|
Public Bot
|
|
|
|
</Checkbox>
|
|
|
|
<h3>Interactions URL</h3>
|
|
|
|
<h5>This field is reserved for the future.</h5>
|
|
|
|
<InputBox
|
|
|
|
ref={setInteractionsRef}
|
|
|
|
value={data.interactions_url}
|
|
|
|
disabled={saving}
|
|
|
|
onChange={(e) =>
|
|
|
|
setData({
|
|
|
|
...data,
|
|
|
|
interactions_url:
|
|
|
|
e.currentTarget.value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className={styles.buttonRow}>
|
|
|
|
{editMode && (
|
|
|
|
<>
|
|
|
|
<Button accent onClick={save}>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
error
|
|
|
|
onClick={onDelete}>
|
|
|
|
Delete
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{!editMode && (
|
|
|
|
<Button
|
|
|
|
onClick={() =>
|
|
|
|
writeClipboard(`${window.origin}/bot/${bot._id}`)
|
|
|
|
}>
|
|
|
|
Copy Invite Link
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
2021-08-12 10:29:19 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MyBots = observer(() => {
|
|
|
|
const client = useClient();
|
|
|
|
const [bots, setBots] = useState<Bot[] | undefined>(undefined);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
client.bots.fetchOwned().then(({ bots }) => setBots(bots));
|
|
|
|
// eslint-disable-next-line
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const [name, setName] = useState("");
|
|
|
|
|
|
|
|
return (
|
2021-08-30 12:18:42 -04:00
|
|
|
<div className={styles.myBots}>
|
2021-08-12 10:29:19 -04:00
|
|
|
<Tip warning hideSeparator>
|
|
|
|
This section is under construction.
|
|
|
|
</Tip>
|
|
|
|
<Overline>create a new bot</Overline>
|
|
|
|
<p>
|
|
|
|
<InputBox
|
|
|
|
value={name}
|
|
|
|
contrast
|
|
|
|
onChange={(e) => setName(e.currentTarget.value)}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<Button
|
|
|
|
contrast
|
|
|
|
onClick={() =>
|
|
|
|
name.length > 0 &&
|
|
|
|
client.bots
|
|
|
|
.create({ name })
|
|
|
|
.then(({ bot }) => setBots([...(bots ?? []), bot]))
|
|
|
|
}>
|
|
|
|
create
|
|
|
|
</Button>
|
|
|
|
</p>
|
|
|
|
<Overline>my bots</Overline>
|
|
|
|
{bots?.map((bot) => {
|
2021-08-30 15:10:55 -04:00
|
|
|
const user = client.users.get(bot._id)!;
|
2021-08-12 10:29:19 -04:00
|
|
|
return (
|
2021-08-30 15:10:55 -04:00
|
|
|
<BotCard
|
2021-08-12 10:29:19 -04:00
|
|
|
key={bot._id}
|
2021-08-30 15:10:55 -04:00
|
|
|
bot={bot}
|
|
|
|
user={user}
|
|
|
|
onDelete={() =>
|
|
|
|
client.bots
|
2021-08-12 10:29:19 -04:00
|
|
|
.delete(bot._id)
|
2021-08-30 15:10:55 -04:00
|
|
|
.then(() => setBots(bots.filter((x) => x._id !== bot._id)))
|
|
|
|
|
|
|
|
}
|
|
|
|
onUpdate={(changes: Changes) =>
|
|
|
|
client.bots.edit(bot._id, changes).then(() => setBots(
|
|
|
|
bots.map((x) => {
|
|
|
|
if (x._id === bot._id) {
|
|
|
|
if ('public' in changes && typeof changes.public === 'boolean') x.public = changes.public;
|
|
|
|
if ('interactions_url' in changes) x.interactions_url = changes.interactions_url;
|
|
|
|
if (changes.remove === 'InteractionsURL') x.interactions_url = undefined;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
/>
|
2021-08-12 10:29:19 -04:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|