From c0ac6a4b8696c3506e7b900cbac11b6b984495d8 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Tue, 18 Apr 2023 01:54:21 -0300 Subject: SilentMessageToggle: Option to persist state (#941) --- src/plugins/silentMessageToggle.tsx | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/silentMessageToggle.tsx b/src/plugins/silentMessageToggle.tsx index df52f41..f90ba8a 100644 --- a/src/plugins/silentMessageToggle.tsx +++ b/src/plugins/silentMessageToggle.tsx @@ -17,22 +17,41 @@ */ import { addPreSendListener, removePreSendListener, SendListener } from "@api/MessageEvents"; +import { definePluginSettings } from "@api/settings"; import ErrorBoundary from "@components/ErrorBoundary"; import { Devs } from "@utils/constants"; -import definePlugin from "@utils/types"; +import definePlugin, { OptionType } from "@utils/types"; import { Button, ButtonLooks, ButtonWrapperClasses, React, Tooltip } from "@webpack/common"; +let lastState = false; + +const settings = definePluginSettings({ + persistState: { + type: OptionType.BOOLEAN, + description: "Whether to persist the state of the silent message toggle when changing channels", + default: false, + onChange(newValue: boolean) { + if (newValue === false) lastState = false; + } + } +}); + function SilentMessageToggle(chatBoxProps: { type: { analyticsName: string; }; }) { - const [enabled, setEnabled] = React.useState(false); + const [enabled, setEnabled] = React.useState(lastState); + + function setEnabledValue(value: boolean) { + if (settings.store.persistState) lastState = value; + setEnabled(value); + } React.useEffect(() => { const listener: SendListener = (_, message) => { if (enabled) { - setEnabled(false); + setEnabledValue(false); if (!message.content.startsWith("@silent ")) message.content = "@silent " + message.content; } }; @@ -49,7 +68,7 @@ function SilentMessageToggle(chatBoxProps: {