Skip to content

Commit

Permalink
Merge pull request #19 from Tharki-God/main
Browse files Browse the repository at this point in the history
womp womp
  • Loading branch information
Puyodead1 committed Dec 10, 2023
2 parents 5493263 + 92ef831 commit 612f413
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 56 deletions.
39 changes: 22 additions & 17 deletions src/Components/PlatformIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ interface Props {
useStateFromStore: useStateFromStore;
SessionStore: SessionStore;
PresenceStore: PresenceStore;
getStatusColor: (status: string) => string;
useStatusFillColor: (status: string, desaturate?: boolean) => string;
profileBadge24: string;
user: User;
}

interface PropsWithUser extends Props {
user: User;
currentUser: User;
}

Expand Down Expand Up @@ -44,7 +44,7 @@ function TheRealPlatformIndicator(props: PropsWithUser): React.ReactElement | nu
PresenceStore,
useStateFromStore,
profileBadge24,
getStatusColor,
useStatusFillColor,
} = props;

const [icons, setIcons] = React.useState<any[]>([]);
Expand All @@ -53,17 +53,18 @@ function TheRealPlatformIndicator(props: PropsWithUser): React.ReactElement | nu
() => PresenceStore.getState().clientStatuses[user.id],
[user.id],
);

React.useEffect(() => {
if (!statuses) {
PresenceStore.getStatus(user.id);
}
const icons = Object.entries(statuses ?? {}).map(([platform, status]) => {
const icons = Object.entries(statuses ?? {}).map(([platform, status]) => () => {
const tooltip = `${platform[0].toUpperCase() + platform.slice(1)} - ${
status[0].toUpperCase() + status.slice(1)
}`;
const color = getStatusColor(status);
const color = useStatusFillColor(status);
const Icon = Icons[platform as Platforms] ?? Icons.desktop;
return <Icon color={`var(--${color}`} tooltip={tooltip} className={profileBadge24} />;
return <Icon color={`${color}`} tooltip={tooltip} className={profileBadge24} />;
});
setIcons(icons);
}, [JSON.stringify(statuses)]);
Expand Down Expand Up @@ -95,25 +96,29 @@ function TheRealPlatformIndicator(props: PropsWithUser): React.ReactElement | nu
}

if (!icons.length) return null;
return <div className="platform-indicators">{icons}</div>;
return (
<div className="platform-indicators">
{icons.map((Icon) => (
<Icon />
))}
</div>
);
}

const MemorizedTheRealPlatformIndicator = React.memo(
TheRealPlatformIndicator,
) as React.FC<PropsWithUser>;

function PlatformIndicator(props: Props) {
return ({ user }: { user: User }) => {
if (!user || user.bot) return null;
if (currentUser == null) currentUser = Replugged.common.users.getCurrentUser();
function PlatformIndicator({ user, ...props }: Props) {

Check failure on line 112 in src/Components/PlatformIndicator.tsx

View workflow job for this annotation

GitHub Actions / Run linters

Missing return type on function
if (!user || user.bot) return null;
currentUser ??= Replugged.common.users.getCurrentUser();

if (!currentUser) {
logger.warn("Failed to get current user!");
return null;
}
if (!currentUser) {
logger.warn("Failed to get current user!");
return null;
}

return <MemorizedTheRealPlatformIndicator {...props} user={user} currentUser={currentUser} />;
};
return <MemorizedTheRealPlatformIndicator {...props} user={user} currentUser={currentUser} />;
}

export default PlatformIndicator;
33 changes: 17 additions & 16 deletions src/Modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const modules: {
PresenceStore: PresenceStore | null;
messageHeaderModule: Record<string, AnyFunction> | null;
messageHeaderFnName: string | null;
getStatusColor: ((status: string) => string) | null;
useStatusFillColor: ((status: string, desature?: boolean) => string) | null;
profileBadgeMod: Record<string, string> | null;
useStateFromStore: useStateFromStore | null;
userBadgeClasses: Record<string, string> | null;
Expand All @@ -23,7 +23,7 @@ export const modules: {
PresenceStore: null,
messageHeaderModule: null,
messageHeaderFnName: null,
getStatusColor: null,
useStatusFillColor: null,
profileBadgeMod: null,
useStateFromStore: null,
userBadgeClasses: null,
Expand Down Expand Up @@ -53,22 +53,23 @@ export const modules: {
if (!modules.PresenceStore) return moduleFindFailed("PresenceStore");
debugLog(debug, "Found PresenceStore module");

debugLog(debug, "Waiting for getStatusColor function");
const getStatusColorMod = await webpack.waitForModule<Record<string, AnyFunction> | undefined>(
webpack.filters.byProps("getStatusColor"),
{
timeout: 10000,
},
);
if (!getStatusColorMod) return moduleFindFailed("getStatusColorMod");
// const getStatusColor = webpack.getFunctionBySource<(status: string) => string>(
// getStatusColorMod,
debugLog(debug, "Waiting for useStatusFillColor function");
const useStatusFillColorMod = await webpack.waitForModule<
Record<string, AnyFunction> | undefined
>(webpack.filters.byProps("useStatusFillColor"), {
timeout: 10000,
});
if (!useStatusFillColorMod) return moduleFindFailed("useStatusFillColorMod");
// const useStatusFillColor = webpack.getFunctionBySource<(status: string) => string>(
// useStatusFillColorMod,
// STATUS_COLOR_REGEX,
// );
// if (!getStatusColor) return moduleFindFailed("getStatusColor");
modules.getStatusColor = getStatusColorMod.getStatusColor as (status: string) => string;
debugLog(debug, "Found getStatusColor function");

// if (!useStatusFillColor) return moduleFindFailed("useStatusFillColor");
modules.useStatusFillColor = useStatusFillColorMod.useStatusFillColor as (
status: string,
desature?: boolean,
) => string;
debugLog(debug, "Found useStatusFillColor function");
debugLog(debug, "Waiting for profile badge classes module");
const profileBadgeMod = await webpack.waitForModule<Record<string, string> | undefined>(
webpack.filters.byProps("profileBadge24"),
Expand Down
71 changes: 48 additions & 23 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ import { User } from "discord-types/general";
import { ReactElement } from "react";
import { common, components, util } from "replugged";
import { AnyFunction } from "replugged/dist/types";
import platformIndicator from "./Components/PlatformIndicator";
import PlatformIndicatorComponent from "./Components/PlatformIndicator";
import { modules } from "./Modules";
import { ClientStatus, PlatformIndicatorsSettings } from "./interfaces";
import {
ClientStatus,
PlatformIndicatorsSettings,
Platforms,

Check warning on line 10 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Run linters

'Platforms' is defined but never used. Allowed unused vars must match /^_/u
PresenceStore,
SessionStore,
useStateFromStore,
} from "./interfaces";
import "./style.css";
import { addNewSettings, cfg, forceRerenderElement, inject, logger, resetSettings } from "./utils";

Expand Down Expand Up @@ -33,25 +40,27 @@ export async function start(): Promise<void> {

const res = await modules.init(debug);
if (!res) return;

const PlatformIndicator = platformIndicator({
const PlatformIndicatorProps = {
useStateFromStore: modules.useStateFromStore!,
SessionStore: modules.SessionStore!,
PresenceStore: modules.PresenceStore!,
getStatusColor: modules.getStatusColor!,
useStatusFillColor: modules.useStatusFillColor!,
profileBadge24: modules.profileBadgeMod!.profileBadge24,
});

patchMessageHeader(PlatformIndicator);
patchProfile(PlatformIndicator);
patchMemberList(PlatformIndicator);
patchDMList(PlatformIndicator);
};
patchMessageHeader(PlatformIndicatorProps);
patchProfile(PlatformIndicatorProps);
patchMemberList(PlatformIndicatorProps);
patchDMList(PlatformIndicatorProps);
rerenderRequired();
}

function patchMessageHeader(
PlatformIndicator: ({ user }: { user: User }) => JSX.Element | null,
): void {
function patchMessageHeader(PlatformIndicatorProps: {
useStateFromStore: useStateFromStore;
SessionStore: SessionStore;
PresenceStore: PresenceStore;
useStatusFillColor: (status: string, desaturate?: boolean) => string;
profileBadge24: string;
}): void {
if (!modules.messageHeaderModule || !modules.messageHeaderFnName) {
toast.toast("Unable to patch Message Header!", toast.Kind.FAILURE, {
duration: 5000,
Expand All @@ -63,7 +72,7 @@ function patchMessageHeader(
if (!cfg.get("renderInChat")) return args;
const user = args[0].message.author as User;
if (args[0].decorations?.["1"] && args[0].message && user) {
const icon = <PlatformIndicator user={user} />;
const icon = <PlatformIndicatorComponent user={user} {...PlatformIndicatorProps} />;
if (icon === null) return args; // to prevent adding an empty div
const a = <ErrorBoundary>{icon}</ErrorBoundary>;
args[0].decorations[1].push(a);
Expand All @@ -72,7 +81,13 @@ function patchMessageHeader(
});
}

function patchProfile(PlatformIndicator: ({ user }: { user: User }) => JSX.Element | null): void {
function patchProfile(PlatformIndicatorProps: {
useStateFromStore: useStateFromStore;
SessionStore: SessionStore;
PresenceStore: PresenceStore;
useStatusFillColor: (status: string, desaturate?: boolean) => string;
profileBadge24: string;
}): void {
if (!modules.userBadgeModule) {
toast.toast("Unable to patch User Profile Badges!", toast.Kind.FAILURE, {
duration: 5000,
Expand All @@ -86,7 +101,7 @@ function patchProfile(PlatformIndicator: ({ user }: { user: User }) => JSX.Eleme

const theChildren = res?.props?.children;
if (!theChildren || !user) return res;
const icon = <PlatformIndicator user={user} />;
const icon = <PlatformIndicatorComponent user={user} {...PlatformIndicatorProps} />;
if (icon === null) return res; // to prevent adding an empty div
const a = <ErrorBoundary>{icon}</ErrorBoundary>;
res.props.children = [a, ...theChildren];
Expand All @@ -103,9 +118,13 @@ function patchProfile(PlatformIndicator: ({ user }: { user: User }) => JSX.Eleme
});
}

function patchMemberList(
PlatformIndicator: ({ user }: { user: User }) => JSX.Element | null,
): void {
function patchMemberList(PlatformIndicatorProps: {
useStateFromStore: useStateFromStore;
SessionStore: SessionStore;
PresenceStore: PresenceStore;
useStatusFillColor: (status: string, desaturate?: boolean) => string;
profileBadge24: string;
}): void {
if (!modules.memberListModule) {
toast.toast("Unable to patch Member List!", toast.Kind.FAILURE, { duration: 5000 });
return;
Expand All @@ -118,7 +137,7 @@ function patchMemberList(
if (!cfg.get("renderInMemberList")) return res;

if (Array.isArray(res?.props?.decorators?.props?.children) && user) {
const icon = <PlatformIndicator user={user} />;
const icon = <PlatformIndicatorComponent user={user} {...PlatformIndicatorProps} />;
if (icon === null) return res; // to prevent adding an empty div
const a = <ErrorBoundary>{icon}</ErrorBoundary>;
res?.props?.decorators?.props?.children.push(a);
Expand All @@ -128,7 +147,13 @@ function patchMemberList(
);
}

function patchDMList(PlatformIndicator: ({ user }: { user: User }) => JSX.Element | null): void {
function patchDMList(PlatformIndicatorProps: {
useStateFromStore: useStateFromStore;
SessionStore: SessionStore;
PresenceStore: PresenceStore;
useStatusFillColor: (status: string, desaturate?: boolean) => string;
profileBadge24: string;
}): void {
if (!modules.dmListModule || !modules.dmListFnName) {
toast.toast("Unable to patch DM List!", toast.Kind.FAILURE, { duration: 5000 });
return;
Expand Down Expand Up @@ -158,7 +183,7 @@ function patchDMList(PlatformIndicator: ({ user }: { user: User }) => JSX.Elemen
if (!container) return res;
const a = (
<ErrorBoundary>
<PlatformIndicator user={user} />
<PlatformIndicatorComponent user={user} {...PlatformIndicatorProps} />
</ErrorBoundary>
);
if (Array.isArray(container.props.decorators)) {
Expand Down

0 comments on commit 612f413

Please sign in to comment.