-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
145 lines (135 loc) · 4.7 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
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import definePlugin, { OptionType } from '@utils/types';
import { showNotification } from "@api/Notifications";
import { definePluginSettings } from '@api/Settings';
import { ModalContent, ModalFooter, ModalHeader, ModalRoot, openModal } from "@utils/modal";
import { Button, Forms, TextArea } from "@webpack/common";
let lastHeartbeatAt = 0;
const settings = definePluginSettings({
apiKey: {
type: OptionType.STRING,
description: 'API Key for wakatime',
default: 'CHANGEME',
isValid: (e: string) => {
if (e === "CHANGEME") return "Invalid Key: Please change the default API Key";
if (!e.startsWith("waka_")) return "Invalid Key: Key must start with 'waka_'";
return true;
},
},
debug: {
type: OptionType.BOOLEAN,
description: 'Enable debug mode',
default: false,
},
machineName: {
type: OptionType.STRING,
description: 'Machine name',
default: 'Vencord User',
},
projectName: {
type: OptionType.STRING,
description: "Project Name",
default: "Discord",
},
});
function enoughTimePassed() {
return lastHeartbeatAt + 120000 < Date.now();
}
async function sendHeartbeat(time) {
const key = settings.store.apiKey;
if (!key || key === 'CHANGEME') {
showNotification({
title: "WakaTime",
body: "No api key for wakatime is setup.",
color: "var(--red-360)",
// onClick: () => {
// openModal(modalProps => (
// <ModalRoot {...modalProps}>
// <ModalHeader >
// <Forms.FormTitle tag="h4">Theme Source</Forms.FormTitle>
// </ModalHeader>
// <ModalContent>
// <Forms.FormText style={{
// padding: "5px",
// }}>
// <TextArea onChange={setting}>
// </TextArea>
// </Forms.FormText>
// </ModalContent>
// <ModalFooter>
// <Button
// color={Button.Colors.RED}
// look={Button.Looks.OUTLINED}
// onClick={() => modalProps.onClose()}
// >
// Close
// </Button>
// </ModalFooter>
// </ModalRoot>
// ));
// },
// dismissOnClick: false
});
return;
}
if (settings.store.debug) {
console.log('Sending heartbeat to WakaTime API.');
}
const url = 'https://api.wakatime.com/api/v1/users/current/heartbeats';
const body = JSON.stringify({
time: time / 1000,
entity: 'Discord',
type: 'app',
project: settings.store.projectName ?? "Discord",
plugin: 'vencord/version discord-wakatime/v0.0.1',
});
const headers = {
Authorization: `Basic ${key}`,
'Content-Type': 'application/json',
'Content-Length': new TextEncoder().encode(body).length.toString(),
};
const machine = settings.store.machineName;
if (machine) headers['X-Machine-Name'] = machine;
const response = await fetch(url, {
method: 'POST',
body: body,
headers: headers,
});
const data = await response.text();
if (response.status < 200 || response.status >= 300) console.warn(`WakaTime API Error ${response.status}: ${data}`);
}
async function handleAction() {
const time = Date.now();
if (!enoughTimePassed()) return;
lastHeartbeatAt = time;
await sendHeartbeat(time);
}
export default definePlugin({
name: 'wakatime',
description: 'Wakatime plugin',
authors: [
{
id: 566766267046821888n,
name: 'Neon',
},
],
settings,
// It might be likely you could delete these and go make patches above!
start() {
console.log('Initializing WakaTime plugin v');
// if (readSetting(this.homeDirectory() + '/.wakatime.cfg', 'settings', 'debug') == 'true') {
// this.debug = true;
// console.log('WakaTime debug mode enabled');
// }
this.handler = handleAction.bind(this);
document.addEventListener('click', this.handler);
},
stop() {
console.log('Unloading WakaTime plugin');
document.removeEventListener('click', this.handler);
},
});