Add chat bar button api ~ fixes buttons for russian users

This commit is contained in:
Vendicated 2024-02-06 16:29:47 +01:00
parent 8938f4a3cf
commit bf977e0047
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
10 changed files with 325 additions and 307 deletions

View file

@ -16,12 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { addChatBarButton, ChatBarButton, removeChatBarButton } from "@api/ChatButtons";
import { addPreSendListener, removePreSendListener, SendListener } from "@api/MessageEvents";
import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types";
import { Button, ButtonLooks, ButtonWrapperClasses, React, Tooltip } from "@webpack/common";
import { React, useEffect, useState } from "@webpack/common";
let lastState = false;
@ -41,19 +41,15 @@ const settings = definePluginSettings({
}
});
function SilentMessageToggle(chatBoxProps: {
type: {
analyticsName: string;
};
}) {
const [enabled, setEnabled] = React.useState(lastState);
const SilentMessageToggle: ChatBarButton = (_, isMainChat) => {
const [enabled, setEnabled] = useState(lastState);
function setEnabledValue(value: boolean) {
if (settings.store.persistState) lastState = value;
setEnabled(value);
}
React.useEffect(() => {
useEffect(() => {
const listener: SendListener = (_, message) => {
if (enabled) {
if (settings.store.autoDisable) setEnabledValue(false);
@ -65,55 +61,37 @@ function SilentMessageToggle(chatBoxProps: {
return () => void removePreSendListener(listener);
}, [enabled]);
if (chatBoxProps.type.analyticsName !== "normal") return null;
if (!isMainChat) return null;
return (
<Tooltip text={enabled ? "Disable Silent Message" : "Enable Silent Message"}>
{tooltipProps => (
<div style={{ display: "flex" }}>
<Button
{...tooltipProps}
onClick={() => setEnabledValue(!enabled)}
size=""
look={ButtonLooks.BLANK}
innerClassName={ButtonWrapperClasses.button}
style={{ padding: "0 6px" }}
>
<div className={ButtonWrapperClasses.buttonWrapper}>
<svg width="24" height="24" viewBox="0 0 24 24">
<path fill="currentColor" mask="url(#_)" d="M18 10.7101C15.1085 9.84957 13 7.17102 13 4c0-.30736.0198-.6101.0582-.907C12.7147 3.03189 12.3611 3 12 3 8.686 3 6 5.686 6 9v5c0 1.657-1.344 3-3 3v1h18v-1c-1.656 0-3-1.343-3-3v-3.2899ZM8.55493 19c.693 1.19 1.96897 2 3.44497 2s2.752-.81 3.445-2H8.55493ZM18.2624 5.50209 21 2.5V1h-4.9651v1.49791h2.4411L16 5.61088V7h5V5.50209h-2.7376Z" />
{!enabled && <>
<mask id="_">
<path fill="#fff" d="M0 0h24v24H0Z" />
<path stroke="#000" stroke-width="5.99068" d="M0 24 24 0" />
</mask>
<path fill="var(--status-danger)" d="m21.178 1.70703 1.414 1.414L4.12103 21.593l-1.414-1.415L21.178 1.70703Z" />
</>}
</svg>
</div>
</Button>
</div>
)}
</Tooltip>
<ChatBarButton
tooltip={enabled ? "Disable Silent Message" : "Enable Silent Message"}
onClick={() => setEnabledValue(!enabled)}
buttonProps={{
style: { padding: "0 6px" }
}}
>
<svg width="24" height="24" viewBox="0 0 24 24">
<path fill="currentColor" mask="url(#_)" d="M18 10.7101C15.1085 9.84957 13 7.17102 13 4c0-.30736.0198-.6101.0582-.907C12.7147 3.03189 12.3611 3 12 3 8.686 3 6 5.686 6 9v5c0 1.657-1.344 3-3 3v1h18v-1c-1.656 0-3-1.343-3-3v-3.2899ZM8.55493 19c.693 1.19 1.96897 2 3.44497 2s2.752-.81 3.445-2H8.55493ZM18.2624 5.50209 21 2.5V1h-4.9651v1.49791h2.4411L16 5.61088V7h5V5.50209h-2.7376Z" />
{!enabled && <>
<mask id="_">
<path fill="#fff" d="M0 0h24v24H0Z" />
<path stroke="#000" stroke-width="5.99068" d="M0 24 24 0" />
</mask>
<path fill="var(--status-danger)" d="m21.178 1.70703 1.414 1.414L4.12103 21.593l-1.414-1.415L21.178 1.70703Z" />
</>}
</svg>
</ChatBarButton>
);
}
};
export default definePlugin({
name: "SilentMessageToggle",
authors: [Devs.Nuckyz, Devs.CatNoir],
description: "Adds a button to the chat bar to toggle sending a silent message.",
dependencies: ["MessageEventsAPI"],
dependencies: ["MessageEventsAPI", "ChatInputButtonAPI"],
settings,
patches: [
{
find: "ChannelTextAreaButtons",
replacement: {
match: /(\i)\.push.{1,30}disabled:(\i),.{1,20}\},"gift"\)\)/,
replace: "$&,(()=>{try{$2||$1.push($self.chatBarIcon(arguments[0]))}catch{}})()",
}
},
],
chatBarIcon: ErrorBoundary.wrap(SilentMessageToggle, { noop: true }),
start: () => addChatBarButton("SilentMessageToggle", SilentMessageToggle),
stop: () => removeChatBarButton("SilentMessageToggle")
});