UserVoiceShow: Show in messages

This commit is contained in:
Nuckyz 2024-09-20 15:42:00 -03:00
parent 467157539c
commit 49b0a38c37
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
7 changed files with 53 additions and 34 deletions

View file

@ -175,7 +175,7 @@ export default definePlugin({
} }
if (settings.store.attemptToNavigateToHome) { if (settings.store.attemptToNavigateToHome) {
try { try {
NavigationRouter.transitionTo("/channels/@me"); NavigationRouter.transitionToGuild("@me");
} catch (err) { } catch (err) {
CrashHandlerLogger.debug("Failed to navigate to home", err); CrashHandlerLogger.debug("Failed to navigate to home", err);
} }

View file

@ -19,7 +19,7 @@
import * as DataStore from "@api/DataStore"; import * as DataStore from "@api/DataStore";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { ChannelStore, NavigationRouter, SelectedChannelStore, SelectedGuildStore } from "@webpack/common"; import { ChannelRouter, SelectedChannelStore, SelectedGuildStore } from "@webpack/common";
export interface LogoutEvent { export interface LogoutEvent {
type: "LOGOUT"; type: "LOGOUT";
@ -40,11 +40,6 @@ interface PreviousChannel {
let isSwitchingAccount = false; let isSwitchingAccount = false;
let previousCache: PreviousChannel | undefined; let previousCache: PreviousChannel | undefined;
function attemptToNavigateToChannel(guildId: string | null, channelId: string) {
if (!ChannelStore.hasChannel(channelId)) return;
NavigationRouter.transitionTo(`/channels/${guildId ?? "@me"}/${channelId}`);
}
export default definePlugin({ export default definePlugin({
name: "KeepCurrentChannel", name: "KeepCurrentChannel",
description: "Attempt to navigate to the channel you were in before switching accounts or loading Discord.", description: "Attempt to navigate to the channel you were in before switching accounts or loading Discord.",
@ -59,8 +54,9 @@ export default definePlugin({
if (!isSwitchingAccount) return; if (!isSwitchingAccount) return;
isSwitchingAccount = false; isSwitchingAccount = false;
if (previousCache?.channelId) if (previousCache?.channelId) {
attemptToNavigateToChannel(previousCache.guildId, previousCache.channelId); ChannelRouter.transitionToChannel(previousCache.channelId);
}
}, },
async CHANNEL_SELECT({ guildId, channelId }: ChannelSelectEvent) { async CHANNEL_SELECT({ guildId, channelId }: ChannelSelectEvent) {
@ -84,7 +80,7 @@ export default definePlugin({
await DataStore.set("KeepCurrentChannel_previousData", previousCache); await DataStore.set("KeepCurrentChannel_previousData", previousCache);
} else if (previousCache.channelId) { } else if (previousCache.channelId) {
attemptToNavigateToChannel(previousCache.guildId, previousCache.channelId); ChannelRouter.transitionToChannel(previousCache.channelId);
} }
} }
}); });

View file

@ -8,7 +8,7 @@ import { classNameFactory } from "@api/Styles";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
import { classes } from "@utils/misc"; import { classes } from "@utils/misc";
import { filters, findByCodeLazy, findByPropsLazy, findComponentByCodeLazy, findStoreLazy, mapMangledModuleLazy } from "@webpack"; import { filters, findByCodeLazy, findByPropsLazy, findComponentByCodeLazy, findStoreLazy, mapMangledModuleLazy } from "@webpack";
import { ChannelStore, GuildStore, IconUtils, match, NavigationRouter, P, PermissionsBits, PermissionStore, React, showToast, Text, Toasts, Tooltip, useMemo, UserStore, useStateFromStores } from "@webpack/common"; import { ChannelRouter, ChannelStore, GuildStore, IconUtils, match, P, PermissionsBits, PermissionStore, React, showToast, Text, Toasts, Tooltip, useMemo, UserStore, useStateFromStores } from "@webpack/common";
import { Channel } from "discord-types/general"; import { Channel } from "discord-types/general";
const cl = classNameFactory("vc-uvs-"); const cl = classNameFactory("vc-uvs-");
@ -24,6 +24,8 @@ const UserSummaryItem = findComponentByCodeLazy("defaultRenderUser", "showDefaul
const Avatar = findComponentByCodeLazy(".AVATAR_STATUS_TYPING_16;"); const Avatar = findComponentByCodeLazy(".AVATAR_STATUS_TYPING_16;");
const GroupDMAvatars = findComponentByCodeLazy(".AvatarSizeSpecs[", "getAvatarURL"); const GroupDMAvatars = findComponentByCodeLazy(".AvatarSizeSpecs[", "getAvatarURL");
const ActionButtonClasses = findByPropsLazy("actionButton", "highlight");
interface IconProps extends React.ComponentPropsWithoutRef<"div"> { interface IconProps extends React.ComponentPropsWithoutRef<"div"> {
size?: number; size?: number;
} }
@ -74,9 +76,10 @@ function LockedSpeakerIcon(props: IconProps) {
interface VoiceChannelTooltipProps { interface VoiceChannelTooltipProps {
channel: Channel; channel: Channel;
isLocked: boolean;
} }
function VoiceChannelTooltip({ channel }: VoiceChannelTooltipProps) { function VoiceChannelTooltip({ channel, isLocked }: VoiceChannelTooltipProps) {
const voiceStates = useStateFromStores([VoiceStateStore], () => VoiceStateStore.getVoiceStatesForChannel(channel.id)); const voiceStates = useStateFromStores([VoiceStateStore], () => VoiceStateStore.getVoiceStatesForChannel(channel.id));
const users = useMemo( const users = useMemo(
@ -113,7 +116,7 @@ function VoiceChannelTooltip({ channel }: VoiceChannelTooltipProps) {
<Text variant="text-sm/semibold">{channelName}</Text> <Text variant="text-sm/semibold">{channelName}</Text>
</div> </div>
<div className={cl("vc-members")}> <div className={cl("vc-members")}>
<SpeakerIcon size={18} /> {isLocked ? <LockedSpeakerIcon size={18} /> : <SpeakerIcon size={18} />}
<UserSummaryItem <UserSummaryItem
users={users} users={users}
renderIcon={false} renderIcon={false}
@ -127,13 +130,15 @@ function VoiceChannelTooltip({ channel }: VoiceChannelTooltipProps) {
interface VoiceChannelIndicatorProps { interface VoiceChannelIndicatorProps {
userId: string; userId: string;
size?: number; isMessageIndicator?: boolean;
isProfile?: boolean;
isActionButton?: boolean; isActionButton?: boolean;
shouldHighlight?: boolean;
} }
const clickTimers = {} as Record<string, any>; const clickTimers = {} as Record<string, any>;
export const VoiceChannelIndicator = ErrorBoundary.wrap(({ userId, size, isActionButton }: VoiceChannelIndicatorProps) => { export const VoiceChannelIndicator = ErrorBoundary.wrap(({ userId, isMessageIndicator, isProfile, isActionButton, shouldHighlight }: VoiceChannelIndicatorProps) => {
const channelId = useStateFromStores([VoiceStateStore], () => VoiceStateStore.getVoiceStateForUser(userId)?.channelId as string | undefined); const channelId = useStateFromStores([VoiceStateStore], () => VoiceStateStore.getVoiceStateForUser(userId)?.channelId as string | undefined);
const channel = channelId == null ? undefined : ChannelStore.getChannel(channelId); const channel = channelId == null ? undefined : ChannelStore.getChannel(channelId);
@ -165,7 +170,7 @@ export const VoiceChannelIndicator = ErrorBoundary.wrap(({ userId, size, isActio
selectVoiceChannel(channelId); selectVoiceChannel(channelId);
} else { } else {
clickTimers[channelId] = setTimeout(() => { clickTimers[channelId] = setTimeout(() => {
NavigationRouter.transitionTo(`/channels/${channel.getGuildId() ?? "@me"}/${channelId}`); ChannelRouter.transitionToChannel(channelId);
delete clickTimers[channelId]; delete clickTimers[channelId];
}, 250); }, 250);
} }
@ -173,16 +178,16 @@ export const VoiceChannelIndicator = ErrorBoundary.wrap(({ userId, size, isActio
return ( return (
<Tooltip <Tooltip
text={<VoiceChannelTooltip channel={channel} />} text={<VoiceChannelTooltip channel={channel} isLocked={isLocked} />}
tooltipClassName={cl("tooltip-container")} tooltipClassName={cl("tooltip-container")}
tooltipContentClassName={cl("tooltip-content")} tooltipContentClassName={cl("tooltip-content")}
> >
{props => { {props => {
const iconProps = { const iconProps: IconProps = {
...props, ...props,
onClick, className: classes(isMessageIndicator && cl("message-indicator"), (!isProfile && !isActionButton) && cl("speaker-margin"), isActionButton && ActionButtonClasses.actionButton, shouldHighlight && ActionButtonClasses.highlight),
size, size: isActionButton ? 20 : undefined,
className: isActionButton ? cl("indicator-action-button") : cl("speaker-padding") onClick
}; };
return isLocked ? return isLocked ?

View file

@ -19,6 +19,7 @@
import "./style.css"; import "./style.css";
import { addDecorator, removeDecorator } from "@api/MemberListDecorators"; import { addDecorator, removeDecorator } from "@api/MemberListDecorators";
import { addDecoration, removeDecoration } from "@api/MessageDecorations";
import { definePluginSettings } from "@api/Settings"; import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import definePlugin, { OptionType } from "@utils/types"; import definePlugin, { OptionType } from "@utils/types";
@ -37,13 +38,19 @@ const settings = definePluginSettings({
description: "Show a user's Voice Channel indicator in the member and DMs list", description: "Show a user's Voice Channel indicator in the member and DMs list",
default: true, default: true,
restartNeeded: true restartNeeded: true
},
showInMessages: {
type: OptionType.BOOLEAN,
description: "Show a user's Voice Channel indicator in messages",
default: true,
restartNeeded: true
} }
}); });
export default definePlugin({ export default definePlugin({
name: "UserVoiceShow", name: "UserVoiceShow",
description: "Shows an indicator when a user is in a Voice Channel", description: "Shows an indicator when a user is in a Voice Channel",
authors: [Devs.LordElias, Devs.Nuckyz], authors: [Devs.Nuckyz, Devs.LordElias],
settings, settings,
patches: [ patches: [
@ -52,7 +59,7 @@ export default definePlugin({
find: ".Messages.USER_PROFILE_LOAD_ERROR", find: ".Messages.USER_PROFILE_LOAD_ERROR",
replacement: { replacement: {
match: /(\.fetchError.+?\?)null/, match: /(\.fetchError.+?\?)null/,
replace: (_, rest) => `${rest}$self.VoiceChannelIndicator({userId:arguments[0]?.userId})` replace: (_, rest) => `${rest}$self.VoiceChannelIndicator({userId:arguments[0]?.userId,isProfile:true})`
}, },
predicate: () => settings.store.showInUserProfileModal predicate: () => settings.store.showInUserProfileModal
}, },
@ -79,8 +86,8 @@ export default definePlugin({
{ {
find: "null!=this.peopleListItemRef.current", find: "null!=this.peopleListItemRef.current",
replacement: { replacement: {
match: /\.actions,children:\[/, match: /\.actions,children:\[(?<=isFocused:(\i).+?)/,
replace: "$&$self.VoiceChannelIndicator({userId:this?.props?.user?.id,size:20,isActionButton:true})," replace: "$&$self.VoiceChannelIndicator({userId:this?.props?.user?.id,isActionButton:true,shouldHighlight:$1}),"
}, },
predicate: () => settings.store.showInMemberList predicate: () => settings.store.showInMemberList
} }
@ -90,10 +97,14 @@ export default definePlugin({
if (settings.store.showInMemberList) { if (settings.store.showInMemberList) {
addDecorator("UserVoiceShow", ({ user }) => user == null ? null : <VoiceChannelIndicator userId={user.id} />); addDecorator("UserVoiceShow", ({ user }) => user == null ? null : <VoiceChannelIndicator userId={user.id} />);
} }
if (settings.store.showInMessages) {
addDecoration("UserVoiceShow", ({ message }) => message?.author == null ? null : <VoiceChannelIndicator userId={message.author.id} isMessageIndicator />);
}
}, },
stop() { stop() {
removeDecorator("UserVoiceShow"); removeDecorator("UserVoiceShow");
removeDecoration("UserVoiceShow");
}, },
VoiceChannelIndicator VoiceChannelIndicator

View file

@ -13,16 +13,14 @@
color: var(--interactive-hover); color: var(--interactive-hover);
} }
.vc-uvs-speaker-padding { .vc-uvs-speaker-margin {
padding: 0 4px; margin-left: 4px;
} }
.vc-uvs-indicator-action-button { .vc-uvs-message-indicator {
background-color: var(--background-secondary); display: inline-flex;
border-radius: 100%; top: 2.5px;
height: 36px; position: relative;
width: 36px;
margin-left: 10px;
} }
.vc-uvs-tooltip-container { .vc-uvs-tooltip-container {

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Guild, GuildMember, User } from "discord-types/general"; import { Channel, Guild, GuildMember, User } from "discord-types/general";
import type { ReactNode } from "react"; import type { ReactNode } from "react";
import { LiteralUnion } from "type-fest"; import { LiteralUnion } from "type-fest";
@ -173,6 +173,11 @@ export interface NavigationRouter {
transitionToGuild(guildId: string, ...args: unknown[]): void; transitionToGuild(guildId: string, ...args: unknown[]): void;
} }
export interface ChannelRouter {
transitionToChannel: (channelId: string) => void;
transitionToThread: (channel: Channel) => void;
}
export interface IconUtils { export interface IconUtils {
getUserAvatarURL(user: User, canAnimate?: boolean, size?: number, format?: string): string; getUserAvatarURL(user: User, canAnimate?: boolean, size?: number, format?: string): string;
getDefaultAvatarURL(id: string, discriminator?: string): string; getDefaultAvatarURL(id: string, discriminator?: string): string;

View file

@ -149,6 +149,10 @@ export const NavigationRouter: t.NavigationRouter = mapMangledModuleLazy("Transi
back: filters.byCode("goBack()"), back: filters.byCode("goBack()"),
forward: filters.byCode("goForward()"), forward: filters.byCode("goForward()"),
}); });
export const ChannelRouter: t.ChannelRouter = mapMangledModuleLazy('"Thread must have a parent ID."', {
transitionToChannel: filters.byCode(".preload"),
transitionToThread: filters.byCode('"Thread must have a parent ID."')
});
export let SettingsRouter: any; export let SettingsRouter: any;
waitFor(["open", "saveAccountChanges"], m => SettingsRouter = m); waitFor(["open", "saveAccountChanges"], m => SettingsRouter = m);