Skip to content

Commit

Permalink
Add DM Favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
Cynosphere committed Jan 14, 2025
1 parent 21750ae commit 05a0987
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/dmFavorites/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ExtensionWebpackModule, Patch } from "@moonlight-mod/types";

export const patches: Patch[] = [
// actually implement the sorting logic
{
find: '"PrivateChannelSortStore"',
replace: {
match: "isFavorite:!1,",
replacement: `isFavorite:((require("common_stores").UserGuildSettingsStore.getChannelOverrides("null")[arguments[0].id]??{}).flags&2048)!==0,`
}
},

// icon indicator
{
find: ".interactiveSystemDM]:",
replace: {
match: /:null,((\(0,(\i)\.jsx\))\(\i,{"aria-label":)/,
replacement: (_, orig, createElement, ReactJSX) =>
`:null,${createElement}(require("dmFavorites_icon")?.default??${ReactJSX}.Fragment,arguments[0]),${orig}`
}
}
];

export const webpackModules: Record<string, ExtensionWebpackModule> = {
context: {
entrypoint: true,
dependencies: [
{ id: "react" },
{ ext: "spacepack", id: "spacepack" },
{ ext: "contextMenu", id: "contextMenu" },
{ ext: "common", id: "stores" },
"intl:",
'.dispatch({type:"USER_GUILD_SETTINGS_CHANNEL_UPDATE_BULK",'
]
},
icon: {
dependencies: [
{ id: "react" },
{ ext: "spacepack", id: "spacepack" },
{ ext: "common", id: "stores" },
{ id: "discord/components/common/index" }
]
}
};
14 changes: 14 additions & 0 deletions src/dmFavorites/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://moonlight-mod.github.io/manifest.schema.json",
"id": "dmFavorites",
"version": "1.0.0",
"meta": {
"name": "DM Favorites",
"tagline": "Implements favorited direct messages from mobile",
"authors": ["Cynosphere"],
"tags": ["chat", "qol"],
"source": "https://github.com/Cynosphere/moonlight-extensions"
},
"dependencies": ["common", "spacepack", "contextMenu"],
"apiLevel": 2
}
34 changes: 34 additions & 0 deletions src/dmFavorites/webpackModules/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from "@moonlight-mod/wp/react";
import spacepack from "@moonlight-mod/wp/spacepack_spacepack";
import { addItem, MenuItem } from "@moonlight-mod/wp/contextMenu_contextMenu";
import { UserGuildSettingsStore } from "@moonlight-mod/wp/common_stores";

const i18n = spacepack.findByCode("intl:")[0].exports;
const { updateChannelOverrideSettings } = spacepack.findByCode(
'.dispatch({type:"USER_GUILD_SETTINGS_CHANNEL_UPDATE_BULK",'
)[0].exports.Z;

function FavoriteDM(props: any) {
const id = props.channel.id;
const override = UserGuildSettingsStore.getChannelOverrides("null")[id] ?? {};
const isFavorite = (override.flags & 2048) !== 0;
const langKey = isFavorite ? "z7I3gY" : "N2c/Ul";

return (
<MenuItem
id="dmFavorites"
label={i18n.intl.string(i18n.t[langKey])}
action={() => {
if (isFavorite) {
override.flags &= ~2048;
} else {
override.flags |= 2048;
}

updateChannelOverrideSettings("@me", id, override);
}}
/>
);
}
addItem("user-context", FavoriteDM, "close-dm", true);
addItem("gdm-context", FavoriteDM, "leave-channel", true);
13 changes: 13 additions & 0 deletions src/dmFavorites/webpackModules/icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "@moonlight-mod/wp/react";
import spacepack from "@moonlight-mod/wp/spacepack_spacepack";
import { UserGuildSettingsStore } from "@moonlight-mod/wp/common_stores";

const { StarIcon } = spacepack.require("discord/components/common/index");

export default function FavoritedIcon(props: any) {
const id = props.channel.id;
const override = UserGuildSettingsStore.getChannelOverrides("null")[id] ?? {};
const isFavorite = (override.flags & 2048) !== 0;

return isFavorite ? <StarIcon color="currentColor" size="xxs" style={{ marginRight: "8px" }} /> : null;
}

0 comments on commit 05a0987

Please sign in to comment.