Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New plugin: SortForumsByUnread #3158

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/plugins/sortForumsByUnread/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SortForumsByUnread

Allows sorting forum posts so that unread posts are at the top

![Preview](https://github.com/user-attachments/assets/f49808ea-c369-4ac4-ac83-a01986349c3e)
71 changes: 71 additions & 0 deletions src/plugins/sortForumsByUnread/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/

import { findGroupChildrenByChildId } from "@api/ContextMenu";
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import { FluxDispatcher, Menu, ReadStateStore } from "@webpack/common";

const settings = definePluginSettings({
keepMenuOpenOnChange: {
type: OptionType.BOOLEAN,
description: "Keep the sort menu open after changing the unread setting"
}
})
.withPrivateSettings<{
sortByUnread: boolean;
}>();

export default definePlugin({
name: "SortForumsByUnread",
description: "Sort forum posts by unread status",
authors: [Devs.Suffocate],

settings,

contextMenus: {
"sort-and-view": (children, props) => {
const { sortByUnread } = settings.use(["sortByUnread"]);
const group = findGroupChildrenByChildId("sort-by-recent-activity", children);
if (!group) return;

group.splice(0, 0,
<Menu.MenuCheckboxItem
id="unread-first"
label="Unread first"
checked={sortByUnread}
action={() => {
settings.store.sortByUnread = !sortByUnread;
FluxDispatcher.dispatch({ type: "RESORT_THREADS", channelId: props.channel.id });
if(!settings.store.keepMenuOpenOnChange) props.closePopout();
}}
/>
);
}
},

patches: [
{
find: "{refreshThreadIds:!0}",
replacement: [
{
match: /(return function\((\i),(\i)\)\{)(.{0,75}\i===\i\.\i\.LATEST_ACTIVITY)/,
replace: "$1const unreadCompare=$self.comparePosts($2,$3);if(unreadCompare!==0)return unreadCompare;$4"
}
]
}
],

comparePosts: (a, b) => {
if (!settings.store?.sortByUnread) return 0;
const aUnread = ReadStateStore.hasTrackedUnread(a);
const bUnread = ReadStateStore.hasTrackedUnread(b);
if (aUnread && !bUnread) return -1;
if (!aUnread && bUnread) return 1;
return 0;
}
});
4 changes: 4 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({
name: "jamesbt365",
id: 158567567487795200n,
},
Suffocate: {
name: "Suffocate",
id: 772601756776923187n
}
} satisfies Record<string, Dev>);

// iife so #__PURE__ works correctly
Expand Down