From 2172cae779fb24f9bcc8c54a0b6538da0b52bafd Mon Sep 17 00:00:00 2001 From: Dominik Date: Fri, 23 Dec 2022 04:16:17 +0100 Subject: [PATCH] [PlatformIndicators] Add own Status (#350) Co-authored-by: Dominik Co-authored-by: HypedDomi Co-authored-by: Ven --- src/plugins/platformIndicators.tsx | 59 ++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/plugins/platformIndicators.tsx b/src/plugins/platformIndicators.tsx index 8ca06775e..b7af8fb72 100644 --- a/src/plugins/platformIndicators.tsx +++ b/src/plugins/platformIndicators.tsx @@ -23,18 +23,20 @@ import { Settings } from "@api/settings"; import ErrorBoundary from "@components/ErrorBoundary"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; -import { findByCodeLazy } from "@webpack"; -import { PresenceStore, Tooltip } from "@webpack/common"; +import { findByCodeLazy, findByPropsLazy } from "@webpack"; +import { PresenceStore, Tooltip, UserStore } from "@webpack/common"; import { User } from "discord-types/general"; +const SessionStore = findByPropsLazy("getActiveSession"); + function Icon(path: string, viewBox = "0 0 24 24") { return ({ color, tooltip }: { color: string; tooltip: string; }) => ( {(tooltipProps: any) => ( @@ -64,10 +66,32 @@ const PlatformIcon = ({ platform, status }: { platform: Platform, status: string const getStatus = (id: string): Record => PresenceStore.getState()?.clientStatuses?.[id]; -const PlatformIndicator = ({ user }: { user: User; }) => { +const PlatformIndicator = ({ user, inline = false, marginLeft = "4px" }: { user: User; inline?: boolean; marginLeft?: string; }) => { if (!user || user.bot) return null; - const status = getStatus(user.id); + if (user.id === UserStore.getCurrentUser().id) { + const sessions = SessionStore.getSessions(); + if (typeof sessions !== "object") return null; + const sortedSessions = Object.values(sessions).sort(({ status: a }: any, { status: b }: any) => { + if (a === b) return 0; + if (a === "online") return 1; + if (b === "online") return -1; + if (a === "idle") return 1; + if (b === "idle") return -1; + return 0; + }); + + const ownStatus = Object.values(sortedSessions).reduce((acc: any, curr: any) => { + if (curr.clientInfo.client !== "unknown") + acc[curr.clientInfo.client] = curr.status; + return acc; + }, {}); + + const { clientStatuses } = PresenceStore.getState(); + clientStatuses[UserStore.getCurrentUser().id] = ownStatus; + } + + const status = PresenceStore.getState()?.clientStatuses?.[user.id] as Record; if (!status) return null; const icons = Object.entries(status).map(([platform, status]) => ( @@ -80,19 +104,24 @@ const PlatformIndicator = ({ user }: { user: User; }) => { if (!icons.length) return null; - const indicator = - {icons} - ; - - return indicator; + + ); }; const badge: ProfileBadge = { - component: PlatformIndicator, + component: p => , position: BadgePosition.START, shouldShow: userInfo => !!Object.keys(getStatus(userInfo.user.id) ?? {}).length, key: "indicator" @@ -119,7 +148,7 @@ const indicatorLocations = { i.key === "new-member")?.props.message?.author - } /> + } inline /> ), onDisable: () => removeDecoration("platform-indicator") @@ -166,7 +195,7 @@ export default definePlugin({ description: `Show indicators ${value.description.toLowerCase()}`, // onChange doesn't give any way to know which setting was changed, so restart required restartNeeded: true, - default: false + default: true }]; }) )