-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
175 lines (163 loc) · 7.44 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { definePluginSettings } from "@api/Settings";
import { classNameFactory } from "@api/Styles";
import ErrorBoundary from "@components/ErrorBoundary";
import { Flex } from "@components/Flex";
import { getIntlMessage, openUserProfile } from "@utils/discord";
import { Margins } from "@utils/margins";
import { classes } from "@utils/misc";
import definePlugin, { OptionType } from "@utils/types";
import { findByPropsLazy, findComponentByCodeLazy, findStoreLazy } from "@webpack";
import { Clickable, Forms, RelationshipStore, Tooltip, UserStore, useStateFromStores } from "@webpack/common";
import { User } from "discord-types/general";
interface WatchingProps {
userIds: string[];
guildId?: string;
}
const cl = classNameFactory("whosWatching-");
function getUsername(user: any): string {
return RelationshipStore.getNickname(user.id) || user.globalName || user.username;
}
const settings = definePluginSettings({
showPanel: {
description: "Show spectators under screenshare panel",
type: OptionType.BOOLEAN,
default: true,
restartNeeded: true
},
});
function Watching({ userIds, guildId }: WatchingProps): JSX.Element {
// Missing Users happen when UserStore.getUser(id) returns null
// The client should automatically cache spectators, so this might not be possible but it's better to be sure just in case
let missingUsers = 0;
const users = userIds.map(id => UserStore.getUser(id)).filter(user => Boolean(user) ? true : (missingUsers += 1, false));
return (
<div className={cl("content")}>
{userIds.length ?
(<>
<Forms.FormTitle>{getIntlMessage("SPECTATORS", { numViewers: userIds.length })}</Forms.FormTitle>
<Flex flexDirection="column" style={{ gap: 6 }} >
{users.map(user => (
<Flex flexDirection="row" style={{ gap: 6, alignContent: "center" }} className={cl("user")} >
<img src={user.getAvatarURL(guildId)} style={{ borderRadius: 8, width: 16, height: 16 }} />
{getUsername(user)}
</Flex>
))}
{missingUsers > 0 && <span className={cl("more_users")}>{`+${getIntlMessage("NUM_USERS", { num: missingUsers })}`}</span>}
</Flex>
</>)
: (<span className={cl("no_viewers")}>No spectators</span>)}
</div>
);
}
const ApplicationStreamingStore = findStoreLazy("ApplicationStreamingStore");
const UserSummaryItem = findComponentByCodeLazy("defaultRenderUser", "showDefaultAvatarsForNullUsers");
const AvatarStyles = findByPropsLazy("moreUsers", "emptyUser", "avatarContainer", "clickableAvatar");
export default definePlugin({
name: "WhosWatching",
description: "Hover over the screenshare icon to view what users are watching your stream",
authors: [
{
name: "fres",
id: 843448897737064448n
}
],
settings: settings,
patches: [
{
find: ".Masks.STATUS_SCREENSHARE,width:32",
replacement: {
match: /jsx\)\((\i\.\i),{mask:/,
replace: "jsx)($self.component({OriginalComponent:$1}),{mask:"
}
},
{
predicate: () => settings.store.showPanel,
find: "this.renderEmbeddedActivity()",
replacement: {
match: /(?<=children.{0,50})"div"(?=.{0,500}this\.renderEmbeddedActivity\(\))/,
replace: "$self.WrapperComponent"
}
}
],
WrapperComponent: ErrorBoundary.wrap(props => {
const stream = useStateFromStores([ApplicationStreamingStore], () => ApplicationStreamingStore.getCurrentUserActiveStream());
if (!stream) return <div {...props}>{props.children}</div>;
const userIds: string[] = ApplicationStreamingStore.getViewerIds(stream);
let missingUsers = 0;
const users = userIds.map(id => UserStore.getUser(id)).filter(user => Boolean(user) ? true : (missingUsers += 1, false));
function renderMoreUsers(_label: string, count: number) {
const sliced = users.slice(count - 1);
return (
<Tooltip text={<Watching userIds={userIds} guildId={stream.guildId} />}>
{({ onMouseEnter, onMouseLeave }) => (
<div
className={AvatarStyles.moreUsers}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
+{sliced.length + missingUsers}
</div>
)}
</Tooltip>
);
}
return (
<>
<div {...props}>{props.children}</div>
<div className={classes(cl("spectators_panel"), Margins.top8)}>
{users.length ?
<>
<Forms.FormTitle tag="h3" style={{ marginTop: 8, marginBottom: 0, textTransform: "uppercase" }}>
{getIntlMessage("SPECTATORS", { numViewers: userIds.length })}
</Forms.FormTitle>
<UserSummaryItem
users={users}
count={userIds.length}
renderIcon={false}
max={12}
showDefaultAvatarsForNullUsers
renderMoreUsers={renderMoreUsers}
renderUser={(user: User) => (
<Clickable
className={AvatarStyles.clickableAvatar}
onClick={() => openUserProfile(user.id)}
>
<img
className={AvatarStyles.avatar}
src={user.getAvatarURL(void 0, 80, true)}
alt={user.username}
title={user.username}
/>
</Clickable>
)}
/>
</>
: <Forms.FormText>No spectators</Forms.FormText>
}
</div>
</>
);
}),
component: function ({ OriginalComponent }) {
return ErrorBoundary.wrap((props: any) => {
const stream = useStateFromStores(
[ApplicationStreamingStore],
() => ApplicationStreamingStore.getCurrentUserActiveStream()
);
if (!stream) return null;
const viewers = ApplicationStreamingStore.getViewerIds(stream);
return <Tooltip text={<Watching userIds={viewers} guildId={stream.guildId} />}>
{({ onMouseEnter, onMouseLeave }) => (
<div onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>
<OriginalComponent {...props} />
</div>
)}
</Tooltip>;
});
}
});