Skip to content

Commit

Permalink
Add custom action sheet to choose the server to add a emoji to
Browse files Browse the repository at this point in the history
  • Loading branch information
FieryFlames committed Jan 28, 2023
1 parent cef2897 commit 9311f5b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
49 changes: 49 additions & 0 deletions src/components/AddToServerRow.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { findByDisplayName, findByProps } from "@vendetta/metro";
import { getAssetIDByName } from "@vendetta/ui/assets";
import { FormRow } from "@vendetta/ui/components/Forms";
import { showToast } from "@vendetta/ui/toasts";
import fetchImageAsDataURL from "../functions/fetchImageAsDataURL";

// Components
const { default: GuildIcon, GuildIconSizes } = findByProps("GuildIconSizes");
const Icon = findByDisplayName("Icon");

// Misc
const Emojis = findByProps("uploadEmoji");
const LazyActionSheet = findByProps("openLazy", "hideActionSheet");

const Add = getAssetIDByName("ic_add_24px");
const Checkmark = getAssetIDByName("Check");

export default function AddToServerRow({ guild, emojiNode }) {
const addToServerCallback = () => {
// Fetch image
fetchImageAsDataURL(emojiNode.src, (dataUrl) => {
// Upload it to Discord
Emojis.uploadEmoji({
guildId: guild.id,
image: dataUrl,
name: emojiNode.alt,
roles: undefined
}).then(() => {
// Let user know it was added
showToast(`Added ${emojiNode.alt} to ${guild.name}`, Checkmark);
// Close the sheet
LazyActionSheet.hideActionSheet();
});
});
};

return (<FormRow
leading={
<GuildIcon
guild={guild}
size={GuildIconSizes.LARGE}
animate={false}
/>
}
label={guild.name}
trailing={<Icon source={Add} />}
onPress={addToServerCallback}
/>)
}
8 changes: 5 additions & 3 deletions src/components/StealButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { setString, setImage } from "@vendetta/metro/common/clipboard";
import { getAssetIDByName } from "@vendetta/ui/assets";
import { showToast } from "@vendetta/ui/toasts";
import fetchImageAsDataURL from "../functions/fetchImageAsDataURL";
import { showAddToServerActionSheet } from "../sheets/AddToServerActionSheet";

const {
default: Button,
Expand All @@ -18,10 +19,10 @@ const CopyFile = getAssetIDByName('ic_message_copy');

export default function StealButtons({ emojiNode }) {
const buttons = [
/*{
{
text: "Add to Server",
callback: () => { }
},*/
callback: () => showAddToServerActionSheet(emojiNode)
},
{
text: "Copy URL to clipboard",
callback: () => {
Expand All @@ -45,6 +46,7 @@ export default function StealButtons({ emojiNode }) {
})

]

return <>
{buttons.map(({ text, callback }) =>
<Button
Expand Down
36 changes: 36 additions & 0 deletions src/sheets/AddToServerActionSheet.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { find, findByProps } from "@vendetta/metro";
import { Permissions } from "@vendetta/metro/common/constants";
import { FormDivider } from "@vendetta/ui/components/Forms";
import AddToServerRow from "../components/AddToServerRow";

const LazyActionSheet = findByProps("openLazy", "hideActionSheet");

// Components
const ActionSheet = find((m) => m.default && m.default.render && m.default.render.name == "ActionSheet").default.render;
const { BottomSheetScrollView } = findByProps("BottomSheetScrollView");

// Stores
const GuildStore = findByProps("getGuilds");
const PermissionsStore = findByProps("can", "_dispatcher");

// function to easily show the sheet
export function showAddToServerActionSheet(emojiNode) {
LazyActionSheet.openLazy(() => Promise.resolve(AddToServerActionSheet), "AddToServerActionSheet", { emojiNode: emojiNode });
};

// The sheet itself
export default function AddToServerActionSheet({ emojiNode }) {
// Get guilds as a Array of ID and value pairs, and filter out guilds the user can't edit emojis in
const guilds = Object.entries(GuildStore.getGuilds()).filter(([_, guild]) => PermissionsStore.can(Permissions.MANAGE_GUILD_EXPRESSIONS, guild));

return (<ActionSheet>
<BottomSheetScrollView contentContainerStyle={{ paddingBottom: 16 }}>
{guilds.map(([_, guild], num) =>
<>
<AddToServerRow guild={guild} emojiNode={emojiNode} />
{num !== guilds.length - 1 && <FormDivider />}
</>
)}
</BottomSheetScrollView>
</ActionSheet>)
};

0 comments on commit 9311f5b

Please sign in to comment.