diff --git a/src/pages/settings/panes/MyBots.tsx b/src/pages/settings/panes/MyBots.tsx index 90b7838b..f7e13b50 100644 --- a/src/pages/settings/panes/MyBots.tsx +++ b/src/pages/settings/panes/MyBots.tsx @@ -11,6 +11,7 @@ import { useEffect, useState } from "preact/hooks"; import { stopPropagation } from "../../../lib/stopPropagation"; import { useIntermediate } from "../../../context/intermediate/Intermediate"; +import { FileUploader } from "../../../context/revoltjs/FileUploads"; import { useClient } from "../../../context/revoltjs/RevoltClient"; import Tooltip from "../../../components/common/Tooltip"; @@ -54,15 +55,16 @@ const BotBadge = styled.div` interface Props { bot: Bot; - user: User; - onDelete(): Promise; - onUpdate(changes: Changes): Promise; + onDelete(): void; + onUpdate(changes: Changes): void; } -function BotCard({ bot, user, onDelete, onUpdate }: Props) { +function BotCard({ bot, onDelete, onUpdate }: Props) { + const client = useClient(); + const [user, setUser] = useState(client.users.get(bot._id)!); const [data, setData] = useState({ _id: bot._id, - username: user!.username, + username: user.username, public: bot.public, interactions_url: bot.interactions_url, }); @@ -79,13 +81,13 @@ function BotCard({ bot, user, onDelete, onUpdate }: Props) { const changes: Changes = {}; if (data.username !== user!.username) changes.name = data.username; if (data.public !== bot.public) changes.public = data.public; - if (data.interactions_url === '') - changes.remove = 'InteractionsURL'; + if (data.interactions_url === "") changes.remove = "InteractionsURL"; else if (data.interactions_url !== bot.interactions_url) changes.interactions_url = data.interactions_url; setSaving(true); try { - await onUpdate(changes); + await client.bots.edit(bot._id, changes); + onUpdate(changes); setEditMode(false); } catch (e) { // TODO error handling @@ -93,6 +95,25 @@ function BotCard({ bot, user, onDelete, onUpdate }: Props) { setSaving(false); } + async function editBotAvatar(avatar?: string) { + setSaving(true); + await client.request("PATCH", "/users/id", { + headers: { "x-bot-token": bot.token }, + transformRequest: (data, headers) => { + // Remove user headers for this request + delete headers["x-user-id"]; + delete headers["x-session-token"]; + return data; + }, + data: JSON.stringify(avatar ? { avatar } : { remove: "Avatar" }), + }); + + const res = await client.bots.fetch(bot._id); + if (!avatar) res.user.update({}, "Avatar"); + setUser(res.user); + setSaving(false); + } + return (
- - openScreen({ - id: "profile", - user_id: user!._id, - }) - } - /> + {!editMode ? ( + + openScreen({ + id: "profile", + user_id: user._id, + }) + } + /> + ) : ( + editBotAvatar(avatar)} + remove={() => editBotAvatar()} + defaultPreview={user.generateAvatarURL( + { max_side: 256 }, + true, + )} + previewURL={user.generateAvatarURL( + { max_side: 256 }, + true, + )} + /> + )} + {!editMode ? (
@@ -165,6 +208,7 @@ function BotCard({ bot, user, onDelete, onUpdate }: Props) { )} @@ -300,29 +347,33 @@ export const MyBots = observer(() => {

my bots {bots?.map((bot) => { - const user = client.users.get(bot._id)!; return ( - client.bots - .delete(bot._id) - .then(() => setBots(bots.filter((x) => x._id !== bot._id))) - + setBots(bots.filter((x) => x._id !== bot._id)) } onUpdate={(changes: Changes) => - client.bots.edit(bot._id, changes).then(() => setBots( + 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; + 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; }), - )) + ) } /> );