SilentMessageToggle: Option to persist state (#941)

This commit is contained in:
Nuckyz 2023-04-18 01:54:21 -03:00 committed by GitHub
parent 29749e93c7
commit c0ac6a4b86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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: {
<div style={{ display: "flex" }}>
<Button
{...tooltipProps}
onClick={() => setEnabled(prev => !prev)}
onClick={() => setEnabledValue(!enabled)}
size=""
look={ButtonLooks.BLANK}
innerClassName={ButtonWrapperClasses.button}
@ -79,6 +98,7 @@ export default definePlugin({
name: "SilentMessageToggle",
authors: [Devs.Nuckyz],
description: "Adds a button to the chat bar to toggle sending a silent message.",
settings,
patches: [
{
find: ".activeCommandOption",