|
| 1 | +/* |
| 2 | + * Vencord, a Discord client mod |
| 3 | + * Copyright (c) 2024 Vendicated and contributors |
| 4 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +import { definePluginSettings } from "@api/Settings"; |
| 8 | +import ErrorBoundary from "@components/ErrorBoundary"; |
| 9 | +import { Devs } from "@utils/constants"; |
| 10 | +import { getCurrentChannel } from "@utils/discord"; |
| 11 | +import definePlugin, { OptionType } from "@utils/types"; |
| 12 | +import { findByPropsLazy, findComponentByCodeLazy } from "@webpack"; |
| 13 | +import { ContextMenuApi, Menu, useEffect, useRef } from "@webpack/common"; |
| 14 | +import { User } from "discord-types/general"; |
| 15 | + |
| 16 | +interface UserProfileProps { |
| 17 | + popoutProps: Record<string, any>; |
| 18 | + currentUser: User; |
| 19 | + originalPopout: () => React.ReactNode; |
| 20 | +} |
| 21 | + |
| 22 | +const UserProfile = findComponentByCodeLazy("UserProfilePopoutWrapper: user cannot be undefined"); |
| 23 | +const styles = findByPropsLazy("accountProfilePopoutWrapper"); |
| 24 | + |
| 25 | +let openAlternatePopout = false; |
| 26 | +let accountPanelRef: React.MutableRefObject<Record<PropertyKey, any> | null> = { current: null }; |
| 27 | + |
| 28 | +const AccountPanelContextMenu = ErrorBoundary.wrap(() => { |
| 29 | + const { prioritizeServerProfile } = settings.use(["prioritizeServerProfile"]); |
| 30 | + |
| 31 | + return ( |
| 32 | + <Menu.Menu |
| 33 | + navId="vc-ap-server-profile" |
| 34 | + onClose={ContextMenuApi.closeContextMenu} |
| 35 | + > |
| 36 | + <Menu.MenuItem |
| 37 | + id="vc-ap-view-alternate-popout" |
| 38 | + label={prioritizeServerProfile ? "View Account Profile" : "View Server Profile"} |
| 39 | + disabled={getCurrentChannel()?.getGuildId() == null} |
| 40 | + action={e => { |
| 41 | + openAlternatePopout = true; |
| 42 | + accountPanelRef.current?.props.onMouseDown(); |
| 43 | + accountPanelRef.current?.props.onClick(e); |
| 44 | + }} |
| 45 | + /> |
| 46 | + <Menu.MenuCheckboxItem |
| 47 | + id="vc-ap-prioritize-server-profile" |
| 48 | + label="Prioritize Server Profile" |
| 49 | + checked={prioritizeServerProfile} |
| 50 | + action={() => settings.store.prioritizeServerProfile = !prioritizeServerProfile} |
| 51 | + /> |
| 52 | + </Menu.Menu> |
| 53 | + ); |
| 54 | +}, { noop: true }); |
| 55 | + |
| 56 | +const settings = definePluginSettings({ |
| 57 | + prioritizeServerProfile: { |
| 58 | + type: OptionType.BOOLEAN, |
| 59 | + description: "Prioritize Server Profile when left clicking your account panel", |
| 60 | + default: false |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +export default definePlugin({ |
| 65 | + name: "AccountPanelServerProfile", |
| 66 | + description: "Right click your account panel in the bottom left to view your profile in the current server", |
| 67 | + authors: [Devs.Nuckyz, Devs.relitrix], |
| 68 | + settings, |
| 69 | + |
| 70 | + patches: [ |
| 71 | + { |
| 72 | + find: ".Messages.ACCOUNT_SPEAKING_WHILE_MUTED", |
| 73 | + group: true, |
| 74 | + replacement: [ |
| 75 | + { |
| 76 | + match: /(?<=\.SIZE_32\)}\);)/, |
| 77 | + replace: "$self.useAccountPanelRef();" |
| 78 | + }, |
| 79 | + { |
| 80 | + match: /(\.AVATAR,children:.+?renderPopout:(\i)=>){(.+?)}(?=,position)(?<=currentUser:(\i).+?)/, |
| 81 | + replace: (_, rest, popoutProps, originalPopout, currentUser) => `${rest}$self.UserProfile({popoutProps:${popoutProps},currentUser:${currentUser},originalPopout:()=>{${originalPopout}}})` |
| 82 | + }, |
| 83 | + { |
| 84 | + match: /\.AVATAR,children:.+?(?=renderPopout:)/, |
| 85 | + replace: "$&onRequestClose:$self.onPopoutClose," |
| 86 | + }, |
| 87 | + { |
| 88 | + match: /(?<=.avatarWrapper,)/, |
| 89 | + replace: "ref:$self.accountPanelRef,onContextMenu:$self.openAccountPanelContextMenu," |
| 90 | + } |
| 91 | + ] |
| 92 | + } |
| 93 | + ], |
| 94 | + |
| 95 | + get accountPanelRef() { |
| 96 | + return accountPanelRef; |
| 97 | + }, |
| 98 | + |
| 99 | + useAccountPanelRef() { |
| 100 | + useEffect(() => () => { |
| 101 | + accountPanelRef.current = null; |
| 102 | + }, []); |
| 103 | + |
| 104 | + return (accountPanelRef = useRef(null)); |
| 105 | + }, |
| 106 | + |
| 107 | + openAccountPanelContextMenu(event: React.UIEvent) { |
| 108 | + ContextMenuApi.openContextMenu(event, AccountPanelContextMenu); |
| 109 | + }, |
| 110 | + |
| 111 | + onPopoutClose() { |
| 112 | + openAlternatePopout = false; |
| 113 | + }, |
| 114 | + |
| 115 | + UserProfile: ErrorBoundary.wrap(({ popoutProps, currentUser, originalPopout }: UserProfileProps) => { |
| 116 | + if ( |
| 117 | + (settings.store.prioritizeServerProfile && openAlternatePopout) || |
| 118 | + (!settings.store.prioritizeServerProfile && !openAlternatePopout) |
| 119 | + ) { |
| 120 | + return originalPopout(); |
| 121 | + } |
| 122 | + |
| 123 | + const currentChannel = getCurrentChannel(); |
| 124 | + if (currentChannel?.getGuildId() == null) { |
| 125 | + return originalPopout(); |
| 126 | + } |
| 127 | + |
| 128 | + return ( |
| 129 | + <div className={styles.accountProfilePopoutWrapper}> |
| 130 | + <UserProfile {...popoutProps} userId={currentUser.id} guildId={currentChannel.getGuildId()} channelId={currentChannel.id} /> |
| 131 | + </div> |
| 132 | + ); |
| 133 | + }, { noop: true }) |
| 134 | +}); |
0 commit comments