feat: switch to revolt.js@6.0.0 + new revolt-api

This commit is contained in:
Paul Makles
2022-04-09 15:47:04 +01:00
parent dc3925c003
commit b2f4411850
91 changed files with 528 additions and 460 deletions

View File

@@ -1,9 +1,9 @@
// ! FIXME: this code is garbage, need to replace
import { Key, Clipboard, Globe, Plus } from "@styled-icons/boxicons-regular";
import { LockAlt, HelpCircle } from "@styled-icons/boxicons-solid";
import type { AxiosError } from "axios";
import { observer } from "mobx-react-lite";
import { Bot } from "revolt-api/types/Bots";
import { Profile as ProfileI } from "revolt-api/types/Users";
import { API } from "revolt.js";
import { User } from "revolt.js/dist/maps/Users";
import styled from "styled-components/macro";
@@ -43,7 +43,7 @@ interface Changes {
name?: string;
public?: boolean;
interactions_url?: string;
remove?: "InteractionsURL";
remove?: "InteractionsURL"[];
}
const BotBadge = styled.div`
@@ -62,7 +62,7 @@ const BotBadge = styled.div`
`;
interface Props {
bot: Bot;
bot: API.Bot;
onDelete(): void;
onUpdate(changes: Changes): void;
}
@@ -75,7 +75,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
_id: bot._id,
username: user.username,
public: bot.public,
interactions_url: bot.interactions_url,
interactions_url: bot.interactions_url as any,
});
const [error, setError] = useState<string | JSX.Element>("");
const [saving, setSaving] = useState(false);
@@ -87,23 +87,21 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
useState<HTMLInputElement | null>(null);
const { writeClipboard, openScreen } = useIntermediate();
const [profile, setProfile] = useState<undefined | ProfileI>(undefined);
const [profile, setProfile] = useState<undefined | API.UserProfile>(
undefined,
);
const refreshProfile = useCallback(() => {
client
.request(
"GET",
`/users/${bot._id}/profile` as "/users/id/profile",
{
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;
},
client.api
.get(`/users/${bot._id as ""}/profile`, undefined, {
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;
},
)
})
.then((profile) => setProfile(profile ?? {}));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [user, setProfile]);
@@ -122,14 +120,14 @@ function BotCard({ bot, 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);
setError("");
try {
await client.bots.edit(bot._id, changes);
if (changed) await editBotContent(profile?.content);
if (changed) await editBotContent(profile?.content ?? undefined);
onUpdate(changes);
setChanged(false);
setEditMode(false);
@@ -152,19 +150,22 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
async function editBotAvatar(avatar?: string) {
setSaving(true);
setError("");
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;
await client.api.patch(
"/users/@me",
avatar ? { avatar } : { remove: ["Avatar"] },
{
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");
if (!avatar) res.user.update({}, ["Avatar"]);
setUser(res.user);
setSaving(false);
}
@@ -172,20 +173,21 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
async function editBotBackground(background?: string) {
setSaving(true);
setError("");
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;
await client.api.patch(
"/users/@me",
background
? { profile: { background } }
: { remove: ["ProfileBackground"] },
{
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(
background
? { profile: { background } }
: { remove: "ProfileBackground" },
),
});
);
if (!background) setProfile({ ...profile, background: undefined });
else refreshProfile();
@@ -195,20 +197,19 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
async function editBotContent(content?: string) {
setSaving(true);
setError("");
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;
await client.api.patch(
"/users/@me",
content ? { profile: { content } } : { remove: ["ProfileContent"] },
{
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(
content
? { profile: { content } }
: { remove: "ProfileContent" },
),
});
);
if (!content) setProfile({ ...profile, content: undefined });
else refreshProfile();
@@ -333,7 +334,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
_id: bot._id,
username: user!.username,
public: bot.public,
interactions_url: bot.interactions_url,
interactions_url: bot.interactions_url as any,
});
usernameRef!.value = user!.username;
interactionsRef!.value = bot.interactions_url || "";
@@ -521,7 +522,7 @@ function BotCard({ bot, onDelete, onUpdate }: Props) {
export const MyBots = observer(() => {
const client = useClient();
const [bots, setBots] = useState<Bot[] | undefined>(undefined);
const [bots, setBots] = useState<API.Bot[] | undefined>(undefined);
useEffect(() => {
client.bots.fetchOwned().then(({ bots }) => setBots(bots));
@@ -582,7 +583,7 @@ export const MyBots = observer(() => {
changes.interactions_url;
if (
changes.remove ===
"InteractionsURL"
["InteractionsURL"]
)
x.interactions_url = undefined;
}