Plugin

PlatformIndicators

Adds platform indicators (Desktop, Mobile, Web...) to users

Appearance
index.tsx
Download

Source

src/plugins/platformIndicators/index.tsx
1import "./style.css";
2
3import { addProfileBadge, BadgePosition, BadgeUserArgs, ProfileBadge, removeProfileBadge } from "@api/Badges";
4import { addMemberListDecorator, removeMemberListDecorator } from "@api/MemberListDecorators";
5import { addMessageDecoration, removeMessageDecoration } from "@api/MessageDecorations";
6import { definePluginSettings } from "@api/Settings";
7import { Devs } from "@utils/constants";
8import definePlugin, { OptionType } from "@utils/types";
9import { DiscordPlatform, OnlineStatus, User } from "@vencord/discord-types";
10import { filters, findStoreLazy, mapMangledModuleLazy } from "@webpack";
11import { AuthenticationStore, PresenceStore, Tooltip, UserStore, useStateFromStores } from "@webpack/common";
12
13export interface Session {
14 sessionId: string;
15 status: string;
16 active: boolean;
17 clientInfo: {
18 version: number;
19 os: string;
20 client: string;
21 };
22}
23
24const SessionsStore = findStoreLazy("SessionsStore") as {
25 getSessions(): Record<string, Session>;
26};
27const { useStatusFillColor } = mapMangledModuleLazy([".5625*", "translate"], {
28 useStatusFillColor: filters.byCode(".hex")
29});
30
31const platformMap = {
32 embedded: "Console",
33 vr: "VR"
34};
35
36const badge: ProfileBadge = {
37 id: "vc_platform_indicator_wrapper",
38 getBadges,
39 position: BadgePosition.START,
40};
41
42const indicatorLocations = {
43 list: {
44 description: "In the member list",
45 onEnable: () => addMemberListDecorator("platform-indicator", ({ user }) =>
46 user && !user.bot ? <PlatformIndicator user={user} small={true} /> : null
47 ),
48 onDisable: () => removeMemberListDecorator("platform-indicator")
49 },
50 badges: {
51 description: "In user profiles, as badges",
52 onEnable: () => addProfileBadge(badge),
53 onDisable: () => removeProfileBadge(badge)
54 },
55 messages: {
56 description: "Inside messages",
57 onEnable: () => addMessageDecoration("platform-indicator", props => {
58 const user = props.message?.author;
59 return user && !user.bot ? <PlatformIndicator user={props.message?.author} /> : null;
60 }),
61 onDisable: () => removeMessageDecoration("platform-indicator")
62 }
63};
64
65const Icons = {
66 desktop: Icon("M4 2.5c-1.103 0-2 .897-2 2v11c0 1.104.897 2 2 2h7v2H7v2h10v-2h-4v-2h7c1.103 0 2-.896 2-2v-11c0-1.103-.897-2-2-2H4Zm16 2v9H4v-9h16Z"),
67 web: Icon("M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2Zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93Zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39Z"),
68 mobile: Icon("M 187 0 L 813 0 C 916.277 0 1000 83.723 1000 187 L 1000 1313 C 1000 1416.277 916.277 1500 813 1500 L 187 1500 C 83.723 1500 0 1416.277 0 1313 L 0 187 C 0 83.723 83.723 0 187 0 Z M 125 1000 L 875 1000 L 875 250 L 125 250 Z M 500 1125 C 430.964 1125 375 1180.964 375 1250 C 375 1319.036 430.964 1375 500 1375 C 569.036 1375 625 1319.036 625 1250 C 625 1180.964 569.036 1125 500 1125 Z", { viewBox: "0 0 1000 1500", height: 17, width: 17 }),
69 embedded: Icon("M14.8 2.7 9 3.1V47h3.3c1.7 0 6.2.3 10 .7l6.7.6V2l-4.2.2c-2.4.1-6.9.3-10 .5zm1.8 6.4c1 1.7-1.3 3.6-2.7 2.2C12.7 10.1 13.5 8 15 8c.5 0 1.2.5 1.6 1.1zM16 33c0 6-.4 10-1 10s-1-4-1-10 .4-10 1-10 1 4 1 10zm15-8v23.3l3.8-.7c2-.3 4.7-.6 6-.6H43V3h-2.2c-1.3 0-4-.3-6-.6L31 1.7V25z", { viewBox: "0 0 50 50" }),
70 vr: Icon("M8.46 8.64a1 1 0 0 1 1 1c0 .44-.3.8-.72.92l-.11.07c-.08.06-.2.19-.2.41a.99.99 0 0 1-.98.86h-.06a1 1 0 0 1-.94-1.05l.02-.32c.05-1.06.92-1.9 1.99-1.9ZM15.55 5a5.5 5.5 0 0 1 5.15 3.67h.3a2 2 0 0 1 2 2v3.18a2 2 0 0 1-2 1.99h-.2A4.54 4.54 0 0 1 16.55 19a4.45 4.45 0 0 1-3.6-1.83 1.2 1.2 0 0 0-1.9 0 4.44 4.44 0 0 1-3.9 1.82 4.54 4.54 0 0 1-3.94-3.15H3a2 2 0 0 1-2-2v-3.18c0-1.1.9-1.99 2-1.99h.3A5.5 5.5 0 0 1 8.46 5h7.09Zm-7.1 2C6.6 7 5.06 8.5 4.97 10.41l-.02.66v3.18c0 1.43 1.05 2.66 2.34 2.74.85.06 1.63-.32 2.14-1.01a3.2 3.2 0 0 1 2.57-1.3c1 0 1.97.48 2.57 1.3.5.69 1.3 1.08 2.14 1.01 1.3-.08 2.34-1.31 2.34-2.74l-.02-3.84a3.54 3.54 0 0 0-3.49-3.43H8.45Z", { viewBox: "0 4 24 16", height: 20, width: 20 }),
71} satisfies Record<DiscordPlatform, any>;
72
73const settings = definePluginSettings({
74 ...Object.fromEntries(
75 Object.entries(indicatorLocations).map(([key, value]) => {
76 return [key, {
77 type: OptionType.BOOLEAN,
78 description: `Show indicators ${value.description.toLowerCase()}`,
79 // onChange doesn't give any way to know which setting was changed, so restart required
80 restartNeeded: true,
81 default: true
82 }];
83 })
84 ),
85 colorMobileIndicator: {
86 type: OptionType.BOOLEAN,
87 description: "Whether to make the mobile indicator match the color of the user status.",
88 default: true,
89 restartNeeded: true
90 }
91});
92
93function Icon(path: string, opts?: { viewBox?: string; width?: number; height?: number; }) {
94 return ({ color, tooltip, small }: { color: string; tooltip: string; small: boolean; }) => (
95 <Tooltip text={tooltip}>
96 {tooltipProps => (
97 <svg
98 {...tooltipProps}
99 height={(opts?.height ?? 20) - (small ? 3 : 0)}
100 width={(opts?.width ?? 20) - (small ? 3 : 0)}
101 viewBox={opts?.viewBox ?? "0 0 24 24"}
102 fill={color}
103 >
104 <path d={path} />
105 </svg>
106 )}
107 </Tooltip>
108 );
109}
110
111function getPlatformTooltip(platform: DiscordPlatform): string {
112 return platformMap[platform] ?? platform.charAt(0).toUpperCase() + platform.slice(1);
113}
114
115const PlatformIcon = ({ platform, status, small }: { platform: DiscordPlatform, status: OnlineStatus; small: boolean; }) => {
116 const tooltip = getPlatformTooltip(platform as DiscordPlatform);
117
118 const Icon = Icons[platform] ?? Icons.desktop;
119
120 return <Icon color={useStatusFillColor(status)} tooltip={tooltip} small={small} />;
121};
122
123function ensureOwnStatus(user: User) {
124 if (user.id === AuthenticationStore.getId()) {
125 const sessions = SessionsStore.getSessions();
126 if (typeof sessions !== "object") return null;
127 const sortedSessions = Object.values(sessions).sort(({ status: a }, { status: b }) => {
128 if (a === b) return 0;
129 if (a === "online") return 1;
130 if (b === "online") return -1;
131 if (a === "idle") return 1;
132 if (b === "idle") return -1;
133 return 0;
134 });
135
136 const ownStatus = Object.values(sortedSessions).reduce((acc, curr) => {
137 if (curr.clientInfo.client !== "unknown")
138 acc[curr.clientInfo.client] = curr.status;
139 return acc;
140 }, {});
141
142 const { clientStatuses } = PresenceStore.getState();
143 clientStatuses[AuthenticationStore.getId()] = ownStatus;
144 }
145}
146
147function getBadges({ userId }: BadgeUserArgs): ProfileBadge[] {
148 const user = UserStore.getUser(userId);
149
150 if (!user || user.bot) return [];
151
152 ensureOwnStatus(user);
153
154 const status = PresenceStore.getClientStatus(user.id);
155 if (!status) return [];
156
157 return Object.entries(status).map(([platform, status]) => ({
158 key: `vc-platform-indicator-${platform}`,
159 id: `vc-platform-indicator-${platform}`,
160 component: () => (
161 <span className="vc-platform-indicator">
162 <PlatformIcon
163 key={platform}
164 platform={platform as DiscordPlatform}
165 status={status}
166 small={false}
167 />
168 </span>
169 ),
170 }));
171}
172
173const PlatformIndicator = ({ user, small = false }: { user: User; small?: boolean; }) => {
174 ensureOwnStatus(user);
175
176 const status = useStateFromStores([PresenceStore], () => PresenceStore.getClientStatus(user.id));
177 if (!status) return null;
178
179 const icons = Object.entries(status).map(([platform, status]) => (
180 <PlatformIcon
181 key={platform}
182 platform={platform as DiscordPlatform}
183 status={status}
184 small={small}
185 />
186 ));
187
188 if (!icons.length) return null;
189
190 return (
191 <span
192 className="vc-platform-indicator"
193 style={{ gap: "2px" }}
194 >
195 {icons}
196 </span>
197 );
198};
199
200export default definePlugin({
201 name: "PlatformIndicators",
202 description: "Adds platform indicators (Desktop, Mobile, Web...) to users",
203 tags: ["Appearance"],
204 authors: [Devs.kemo, Devs.TheSun, Devs.Nuckyz, Devs.Ven],
205 dependencies: ["MessageDecorationsAPI", "MemberListDecoratorsAPI"],
206 settings,
207
208 start() {
209 Object.entries(indicatorLocations).forEach(([key, value]) => {
210 if (settings.store[key]) value.onEnable();
211 });
212 },
213
214 stop() {
215 Object.entries(indicatorLocations).forEach(([_, value]) => {
216 value.onDisable();
217 });
218 },
219
220 patches: [
221 {
222 find: ".Masks.STATUS_ONLINE_MOBILE",
223 predicate: () => settings.store.colorMobileIndicator,
224 replacement: [
225 {
226 // Return the STATUS_ONLINE_MOBILE mask if the user is on mobile, no matter the status
227 match: /\.STATUS_TYPING;switch(?=.+?(if\(\i\)return \i\.\i\.Masks\.STATUS_ONLINE_MOBILE))/,
228 replace: ".STATUS_TYPING;$1;switch"
229 },
230 {
231 // Return the STATUS_ONLINE_MOBILE mask if the user is on mobile, no matter the status
232 match: /switch\(\i\)\{case \i\.\i\.ONLINE:(if\(\i\)return\{[^}]+\})/,
233 replace: "$1;$&"
234 }
235 ]
236 },
237 {
238 find: ".AVATAR_STATUS_MOBILE_16;",
239 predicate: () => settings.store.colorMobileIndicator,
240 replacement: [
241 {
242 // Return the AVATAR_STATUS_MOBILE size mask if the user is on mobile, no matter the status
243 match: /\i===\i\.\i\.ONLINE&&(?=.{0,70}\.AVATAR_STATUS_MOBILE_16;)/,
244 replace: ""
245 },
246 {
247 // Fix sizes for mobile indicators which aren't online
248 match: /(?<=\(\i\.status,)(\i)(?=,\{.{0,15}isMobile:(\i))/,
249 replace: &#039;$2?"online":$1&#039;
250 },
251 {
252 // Make isMobile true no matter the status
253 match: /(?<=\i&&!\i)&&\i===\i\.\i\.ONLINE/,
254 replace: ""
255 }
256 ]
257 },
258 {
259 find: "}isMobileOnline(",
260 predicate: () => settings.store.colorMobileIndicator,
261 replacement: {
262 // Make isMobileOnline return true no matter what is the user status
263 match: /(?<=\i\[\i\.\i\.MOBILE\])===\i\.\i\.ONLINE/,
264 replace: "!= null"
265 }
266 }
267 ]
268});
269