-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
79 lines (57 loc) · 2.7 KB
/
Copy pathindex.jsx
File metadata and controls
79 lines (57 loc) · 2.7 KB
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
const { React, getModule, getModuleByDisplayName, constants: { Durations }, i18n: { Messages } } = require('powercord/webpack');
const { inject, uninject } = require('powercord/injector');
const { findInReactTree } = require('powercord/util');
const { Plugin } = require('powercord/entities');
const SlowmodeStore = require('./lib/Store');
const ErrorBoundary = require('./components/ErrorBoundary');
const moment = getModule([ 'momentProperties' ], false);
module.exports = class SlowmodeCounter extends Plugin {
constructor () {
super();
this.slowmodeStore.initializeIfNeeded();
}
get slowmodeStore () {
return SlowmodeStore;
}
startPlugin () {
const _this = this;
const { SlowmodeType } = getModule([ 'SlowmodeType' ], false);
const { useStateFromStores } = getModule([ 'useStateFromStores' ], false);
const SlowmodeCooldown = ({ channel, isThreadCreation, renderCounterOnly }) => {
const { slowmodeCooldownGuess } = useStateFromStores([ SlowmodeStore ], () => ({
slowmodeCooldownGuess: SlowmodeStore.getSlowmodeCooldownGuess(channel.id, isThreadCreation ? SlowmodeType.CreateThread : SlowmodeType.SendMessage)
}));
return `${renderCounterOnly ? '' : Messages.CHANNEL_SLOWMODE_DESC_IMMUNE}${slowmodeCooldownGuess > 0 ? ` ${this.millisecondsToReadableFormat(slowmodeCooldownGuess)}` : ''}`;
};
const TypingUsers = getModuleByDisplayName('FluxContainer(TypingUsers)', false).prototype?.render?.call({ memoizedGetStateFromStores: () => ({}) })?.type;
if (!TypingUsers) return this.error('Missing “TypingUsers” component; skipping injection');
inject('force-slowmode-timer', TypingUsers.prototype, 'render', function (_, res) {
const { channel, isBypassSlowmode, isThreadCreation } = this.props;
if (channel.rateLimitPerUser === 0 || !isBypassSlowmode) return res;
const Tooltip = findInReactTree(res, n => typeof n.children === 'function');
if (!Tooltip) return res;
const { children } = Tooltip;
Tooltip.children = (props) => {
const res = children(props);
res.props.children[0] = <ErrorBoundary main={_this}>
<SlowmodeCooldown channel={channel} isThreadCreation={isThreadCreation} renderCounterOnly={res.props.children[0] !== Messages.CHANNEL_SLOWMODE_DESC_IMMUNE} />
</ErrorBoundary>;
return res;
};
return res;
});
}
millisecondsToReadableFormat (ms) {
const duration = moment.duration(ms);
const seconds = String(duration.seconds()).padStart(2, '0');
if (ms > 1e3 * Durations.HOUR) {
const minutes = String(duration.minutes()).padStart(2, '0');
return duration.hours() + ':' + minutes + ':' + seconds;
} else {
return duration.minutes() + ':' + seconds;
}
}
pluginWillUnload () {
uninject('force-slowmode-timer');
}
}