From e4659ed7c3480750df88c182f8bcf6660f98c2c3 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Sat, 7 Oct 2023 23:04:17 -0300 Subject: [PATCH] Rewrite IgnoreActivities (#1693) --- src/plugins/ignoreActivities/index.tsx | 251 ++++++++++--------------- 1 file changed, 96 insertions(+), 155 deletions(-) diff --git a/src/plugins/ignoreActivities/index.tsx b/src/plugins/ignoreActivities/index.tsx index 6d58eb48..4809e889 100644 --- a/src/plugins/ignoreActivities/index.tsx +++ b/src/plugins/ignoreActivities/index.tsx @@ -1,27 +1,17 @@ /* - * Vencord, a modification for Discord's desktop app - * Copyright (c) 2022 Vendicated and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . -*/ + * Vencord, a Discord client mod + * Copyright (c) 2023 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ import * as DataStore from "@api/DataStore"; +import { definePluginSettings } from "@api/Settings"; +import { getSettingStoreLazy } from "@api/SettingsStore"; import ErrorBoundary from "@components/ErrorBoundary"; import { Devs } from "@utils/constants"; import { useForceUpdater } from "@utils/react"; import definePlugin from "@utils/types"; -import { findByPropsLazy, findStoreLazy } from "@webpack"; +import { findStoreLazy } from "@webpack"; import { Tooltip } from "webpack/common"; const enum ActivitiesTypes { @@ -31,203 +21,154 @@ const enum ActivitiesTypes { interface IgnoredActivity { id: string; + name: string; type: ActivitiesTypes; } -const RegisteredGamesClasses = findByPropsLazy("overlayToggleIconOff", "overlayToggleIconOn"); -const TryItOutClasses = findByPropsLazy("tryItOutBadge", "tryItOutBadgeIcon"); -const BaseShapeRoundClasses = findByPropsLazy("baseShapeRound", "baseShapeRoundLeft", "baseShapeRoundRight"); const RunningGameStore = findStoreLazy("RunningGameStore"); +const ShowCurrentGame = getSettingStoreLazy("status", "showCurrentGame"); -function ToggleIconOff() { - return ( - - - - - - - ); -} - -function ToggleIconOn({ forceWhite }: { forceWhite?: boolean; }) { - return ( - - - - ); -} - -function ToggleActivityComponent({ activity, forceWhite, forceLeftMargin }: { activity: IgnoredActivity; forceWhite?: boolean; forceLeftMargin?: boolean; }) { +function ToggleIcon(activity: IgnoredActivity, tooltipText: string, path: string, fill: string) { const forceUpdate = useForceUpdater(); return ( - - {({ onMouseLeave, onMouseEnter }) => ( -
+ {tooltipProps => ( +
+ + + + )}
); } -function ToggleActivityComponentWithBackground({ activity }: { activity: IgnoredActivity; }) { - return ( -
- -
- ); +const ToggleIconOn = (activity: IgnoredActivity, fill: string) => ToggleIcon(activity, "Disable Activity", "M480-320q75 0 127.5-52.5T660-500q0-75-52.5-127.5T480-680q-75 0-127.5 52.5T300-500q0 75 52.5 127.5T480-320Zm0-72q-45 0-76.5-31.5T372-500q0-45 31.5-76.5T480-608q45 0 76.5 31.5T588-500q0 45-31.5 76.5T480-392Zm0 192q-146 0-266-81.5T40-500q54-137 174-218.5T480-800q146 0 266 81.5T920-500q-54 137-174 218.5T480-200Zm0-300Zm0 220q113 0 207.5-59.5T832-500q-50-101-144.5-160.5T480-720q-113 0-207.5 59.5T128-500q50 101 144.5 160.5T480-280Z", fill); +const ToggleIconOff = (activity: IgnoredActivity, fill: string) => ToggleIcon(activity, "Enable Activity", "m644-428-58-58q9-47-27-88t-93-32l-58-58q17-8 34.5-12t37.5-4q75 0 127.5 52.5T660-500q0 20-4 37.5T644-428Zm128 126-58-56q38-29 67.5-63.5T832-500q-50-101-143.5-160.5T480-720q-29 0-57 4t-55 12l-62-62q41-17 84-25.5t90-8.5q151 0 269 83.5T920-500q-23 59-60.5 109.5T772-302Zm20 246L624-222q-35 11-70.5 16.5T480-200q-151 0-269-83.5T40-500q21-53 53-98.5t73-81.5L56-792l56-56 736 736-56 56ZM222-624q-29 26-53 57t-41 67q50 101 143.5 160.5T480-280q20 0 39-2.5t39-5.5l-36-38q-11 3-21 4.5t-21 1.5q-75 0-127.5-52.5T300-500q0-11 1.5-21t4.5-21l-84-82Zm319 93Zm-151 75Z", fill); + +function ToggleActivityComponent(activity: IgnoredActivity, isPlaying = false) { + if (getIgnoredActivities().some(act => act.id === activity.id)) return ToggleIconOff(activity, "var(--status-danger)"); + return ToggleIconOn(activity, isPlaying ? "var(--green-300)" : "var(--primary-400)"); } -function handleActivityToggle(e: React.MouseEvent, activity: IgnoredActivity, forceUpdateComponent: () => void) { +function handleActivityToggle(e: React.MouseEvent, activity: IgnoredActivity, forceUpdateButton: () => void) { e.stopPropagation(); - if (ignoredActivitiesCache.has(activity.id)) ignoredActivitiesCache.delete(activity.id); - else ignoredActivitiesCache.set(activity.id, activity); - forceUpdateComponent(); - saveCacheToDatastore(); + + const ignoredActivityIndex = getIgnoredActivities().findIndex(act => act.id === activity.id); + if (ignoredActivityIndex === -1) settings.store.ignoredActivities = getIgnoredActivities().concat(activity); + else settings.store.ignoredActivities = getIgnoredActivities().filter((_, index) => index !== ignoredActivityIndex); + + // Trigger activities recalculation + ShowCurrentGame?.updateSetting(old => old); + forceUpdateButton(); } -async function saveCacheToDatastore() { - await DataStore.set("IgnoreActivities_ignoredActivities", ignoredActivitiesCache); -} +const settings = definePluginSettings({}).withPrivateSettings<{ + ignoredActivities: IgnoredActivity[]; +}>(); -let ignoredActivitiesCache = new Map(); +function getIgnoredActivities() { + return settings.store.ignoredActivities ??= []; +} export default definePlugin({ name: "IgnoreActivities", authors: [Devs.Nuckyz], - description: "Ignore certain activities (like games and actual activities) from showing up on your status. You can configure which ones are ignored from the Registered Games and Activities tabs.", + description: "Ignore activities from showing up on your status ONLY. You can configure which ones are ignored from the Registered Games and Activities tabs.", + + dependencies: ["SettingsStoreAPI"], + settings, + patches: [ + { + find: '.displayName="LocalActivityStore"', + replacement: [ + { + match: /LISTENING.+?\)\);(?<=(\i)\.push.+?)/, + replace: (m, activities) => `${m}${activities}=${activities}.filter($self.isActivityNotIgnored);` + } + ] + }, { find: ".Messages.SETTINGS_GAMES_TOGGLE_OVERLAY", replacement: { - match: /!(\i)(\)return null;var \i=(\i)\.overlay.+?children:)(\[.{0,70}overlayStatusText.+?\])(?=}\)}\(\))/, - replace: (_, platformCheck, restWithoutPlatformCheck, props, children) => "false" - + `${restWithoutPlatformCheck}` - + `(${platformCheck}?${children}:[])` - + `.concat(Vencord.Plugins.plugins.IgnoreActivities.renderToggleGameActivityButton(${props}))` + match: /\(\)\.removeGame.+?null(?<=(\i)\?\i=\i\.\i\.Messages\.SETTINGS_GAMES_NOW_PLAYING_STATE.+?=(\i)\.overlay.+?)/, + replace: (m, nowPlaying, props) => `${m},$self.renderToggleGameActivityButton(${props},${nowPlaying})` } }, { find: ".overlayBadge", replacement: [ { - match: /(?<=\(\)\.badgeContainer,children:).{0,50}?name:(\i)\.name.+?null/, - replace: (m, props) => `[${m},$self.renderToggleActivityButton(${props})]` + match: /(?<=\(\)\.activityTitleText.+?children:(\i)\.name.*?}\),)/, + replace: (_, props) => `$self.renderToggleActivityButton(${props}),` }, { - match: /(?<=\(\)\.badgeContainer,children:).{0,50}?name:(\i\.application)\.name.+?null/, - replace: (m, props) => `${m},$self.renderToggleActivityButton(${props})` + match: /(?<=\(\)\.activityCardDetails.+?children:(\i\.application)\.name.*?}\),)/, + replace: (_, props) => `$self.renderToggleActivityButton(${props}),` } ] - }, - { - find: '.displayName="LocalActivityStore"', - replacement: { - match: /LISTENING.+?\)\);(?<=(\i)\.push.+?)/, - replace: (m, activities) => `${m}${activities}=${activities}.filter($self.isActivityNotIgnored);` - } } ], async start() { - const ignoredActivitiesData = await DataStore.get>("IgnoreActivities_ignoredActivities") ?? new Map(); - /** Migrate old data */ - if (Array.isArray(ignoredActivitiesData)) { - for (const id of ignoredActivitiesData) { - ignoredActivitiesCache.set(id, { id, type: ActivitiesTypes.Game }); - } + const oldIgnoredActivitiesData = await DataStore.get>("IgnoreActivities_ignoredActivities"); - await saveCacheToDatastore(); - } else ignoredActivitiesCache = ignoredActivitiesData; + if (oldIgnoredActivitiesData != null) { + settings.store.ignoredActivities = Array.from(oldIgnoredActivitiesData.values()) + .map(activity => ({ ...activity, name: "Unknown Name" })); - if (ignoredActivitiesCache.size !== 0) { - const gamesSeen: { id?: string; exePath: string; }[] = RunningGameStore.getGamesSeen(); + DataStore.del("IgnoreActivities_ignoredActivities"); + } - for (const ignoredActivity of ignoredActivitiesCache.values()) { + if (getIgnoredActivities().length !== 0) { + const gamesSeen = RunningGameStore.getGamesSeen() as { id?: string; exePath: string; }[]; + + for (const [index, ignoredActivity] of getIgnoredActivities().entries()) { if (ignoredActivity.type !== ActivitiesTypes.Game) continue; if (!gamesSeen.some(game => game.id === ignoredActivity.id || game.exePath === ignoredActivity.id)) { - /** Custom added game which no longer exists */ - ignoredActivitiesCache.delete(ignoredActivity.id); + getIgnoredActivities().splice(index, 1); } } - - await saveCacheToDatastore(); } }, - renderToggleGameActivityButton(props: { id?: string; exePath: string; }) { - return ( - - - - ); - }, - - renderToggleActivityButton(props: { id: string; }) { - return ( - - - - ); - }, - isActivityNotIgnored(props: { type: number; application_id?: string; name?: string; }) { - if (props.type === 0) { - if (props.application_id !== undefined) return !ignoredActivitiesCache.has(props.application_id); + if (props.type === 0 || props.type === 3) { + if (props.application_id != null) return !getIgnoredActivities().some(activity => activity.id === props.application_id); else { const exePath = RunningGameStore.getRunningGames().find(game => game.name === props.name)?.exePath; - if (exePath) return !ignoredActivitiesCache.has(exePath); + if (exePath) return !getIgnoredActivities().some(activity => activity.id === exePath); } } return true; + }, + + renderToggleGameActivityButton(props: { id?: string; name: string, exePath: string; }, nowPlaying: boolean) { + return ( + +
+ {ToggleActivityComponent({ id: props.id ?? props.exePath, name: props.name, type: ActivitiesTypes.Game }, nowPlaying)} +
+
+ ); + }, + + renderToggleActivityButton(props: { id: string; name: string; }) { + return ( + + {ToggleActivityComponent({ id: props.id, name: props.name, type: ActivitiesTypes.Embedded })} + + ); } });