This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
generated from kernel-addons/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
139 lines (124 loc) · 6.02 KB
/
renderer.js
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
/// <reference path="../webpack/types.d.ts" />
export default new (class PackageDownloader {
async start() {
const pluginLog = (msg, logFunc = console.info) =>
logFunc(`%c[${this.constructor.name}] `, "color:#14bbaa", msg)
pluginLog("Started successfully")
await Webpack.whenReady
const { React } = Webpack.common
const MiniPopover = Webpack.findByDisplayName("MiniPopover", {
default: true,
})
const Toasts = Object.assign(
{},
...Webpack.getByProps("showToast", "createToast", {
bulk: true,
})
)
const Tooltip = Webpack.findByDisplayName("Tooltip")
const funcCopy = MiniPopover.default
MiniPopover.default = (...args) => {
const props = args[0].children.at?.(-1)
? args[0].children.at(-1).props
: null
const Button = () => {
const [disabled, setDisabled] = React.useState(false)
const gitURL = props.message.content
.slice(props.message.content.indexOf("Repository"))
.match(
/((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})/
)
if (!disabled)
Object.values(kernel.packages.getPackages()).forEach(
(pkg) => {
if (pkg.path.split("/").at(-1) == gitURL[7])
setDisabled(true)
}
)
return [
React.createElement(
Tooltip,
{
position: "top",
text: disabled
? "Already Installed"
: "Install Package",
},
(args) =>
React.createElement(
MiniPopover.Button,
{
...args,
disabled: disabled,
onClick: async () => {
setDisabled(true)
const { reloadMessage, error } =
await window.installPackage(gitURL)
if (reloadMessage) {
Toasts.showToast(
Toasts.createToast(
`Successfully installed package! ${
reloadMessage
? `${reloadMessage}.`
: ""
}`,
Toasts.ToastType.SUCCESS
)
)
pluginLog(
`Successfully installed package! ${
reloadMessage
? `${reloadMessage}.`
: ""
}`
)
} else {
Toasts.showToast(
Toasts.createToast(
"Failed to install package, check console for error.",
Toasts.ToastType.ERROR
)
)
pluginLog(
`Package installation failed: ${error}`,
console.error
)
setDisabled(false)
}
},
},
React.createElement(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "16",
height: "16",
fill: "currentColor",
class: "bi bi-arrow-down-circle-fill",
viewBox: "0 0 16 16",
},
React.createElement("path", {
d: "M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8.5 4.5a.5.5 0 0 0-1 0v5.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V4.5z",
})
)
)
),
React.createElement(MiniPopover.Separator),
]
}
if (
props?.message &&
props.channel &&
props.channel?.id == "899717501120806963"
)
args[0].children.unshift(React.createElement(Button))
return funcCopy.apply(this, args)
}
Object.assign(MiniPopover.default, funcCopy)
pluginLog("Patched, ready to download packages!")
this.stop = () => {
MiniPopover.default = funcCopy
pluginLog("Stopped successfully")
}
}
})()