Skip to content

Commit

Permalink
chore: add hostname option to extension (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvelezpo authored Mar 15, 2023
1 parent ae22ab5 commit 1e9e233
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/components/Options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface State {
apiKey: string;
blacklist: string;
displayAlert: boolean;
hostname: string;
loading: boolean;
loggingStyle: string;
loggingType: string;
Expand All @@ -26,6 +27,7 @@ export default function Options(): JSX.Element {
apiKey: '',
blacklist: '',
displayAlert: false,
hostname: '',
loading: false,
loggingStyle: config.loggingStyle,
loggingType: config.loggingType,
Expand All @@ -41,6 +43,7 @@ export default function Options(): JSX.Element {
const items = await browser.storage.sync.get({
apiKey: config.apiKey,
blacklist: '',
hostname: config.hostname,
loggingStyle: config.loggingStyle,
loggingType: config.loggingType,
socialMediaSites: config.socialMediaSites,
Expand All @@ -52,6 +55,7 @@ export default function Options(): JSX.Element {
...state,
apiKey: items.apiKey as string,
blacklist: items.blacklist as string,
hostname: items.hostname as string,
loggingStyle: items.loggingStyle as string,
loggingType: items.loggingType as string,
socialMediaSites: items.socialMediaSites as string,
Expand Down Expand Up @@ -79,6 +83,7 @@ export default function Options(): JSX.Element {

const apiKey = state.apiKey;
const theme = state.theme;
const hostname = state.hostname;
const loggingType = state.loggingType;
const loggingStyle = state.loggingStyle;
const trackSocialMedia = state.trackSocialMedia;
Expand All @@ -91,6 +96,7 @@ export default function Options(): JSX.Element {
await browser.storage.sync.set({
apiKey,
blacklist,
hostname,
loggingStyle,
loggingType,
socialMediaSites,
Expand All @@ -105,6 +111,7 @@ export default function Options(): JSX.Element {
apiKey,
blacklist,
displayAlert: true,
hostname,
loggingStyle,
loggingType,
socialMediaSites,
Expand Down Expand Up @@ -248,6 +255,24 @@ export default function Options(): JSX.Element {
</div>
</div>

<div className="form-group">
<label htmlFor="theme" className="col-lg-2 control-label">
Hostname
</label>

<div className="col-lg-10">
<input
type="text"
className="form-control"
value={state.hostname}
onChange={(e) => setState({ ...state, hostname: e.target.value })}
/>
<span className="help-block">
Optional name of local machine. By default &apos;Unknown Hostname&apos;.
</span>
</div>
</div>

<div className="form-group row">
<div className="col-lg-10 col-lg-offset-2 space-between align-items-center">
<div
Expand Down
1 change: 1 addition & 0 deletions src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('wakatime config', () => {
https://www.udemy.com/
https://www.w3schools.com/",
"heartbeatApiUrl": "https://wakatime.com/api/v1/users/current/heartbeats",
"hostname": "",
"loggingEnabled": true,
"loggingStyle": "blacklist",
"loggingType": "domain",
Expand Down
4 changes: 4 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export interface Config {
* Url to which to send the heartbeat
*/
heartbeatApiUrl: string;

hostname: string;
/**
* Is logging enabled
*/
Expand Down Expand Up @@ -134,6 +136,8 @@ const config: Config = {
heartbeatApiUrl:
process.env.HEART_BEAT_API_URL ?? 'https://wakatime.com/api/v1/users/current/heartbeats',

hostname: '',

loggingEnabled: true,

loggingStyle: 'blacklist',
Expand Down
28 changes: 22 additions & 6 deletions src/core/WakaTimeCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class WakaTimeCore {
}
const items = await browser.storage.sync.get({
blacklist: '',
hostname: config.hostname,
loggingEnabled: config.loggingEnabled,
loggingStyle: config.loggingStyle,
socialMediaSites: config.socialMediaSites,
Expand Down Expand Up @@ -142,6 +143,7 @@ class WakaTimeCore {
if (!contains(currentActiveTab.url as string, items.blacklist as string)) {
await this.sendHeartbeat(
{
hostname: items.hostname as string,
project,
url: currentActiveTab.url as string,
},
Expand All @@ -160,7 +162,11 @@ class WakaTimeCore {
);
if (heartbeat.url) {
await this.sendHeartbeat(
{ ...heartbeat, project: heartbeat.project ?? project },
{
...heartbeat,
hostname: items.hostname as string,
project: heartbeat.project ?? project,
},
apiKey,
);
} else {
Expand Down Expand Up @@ -260,12 +266,12 @@ class WakaTimeCore {
if (loggingType == 'domain') {
heartbeat.url = getDomainFromUrl(heartbeat.url);
payload = this.preparePayload(heartbeat, 'domain');
await this.sendPostRequestToApi(payload, apiKey);
await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
}
// Send entity in heartbeat
else if (loggingType == 'url') {
payload = this.preparePayload(heartbeat, 'url');
await this.sendPostRequestToApi(payload, apiKey);
await this.sendPostRequestToApi(payload, apiKey, heartbeat.hostname);
}
}

Expand Down Expand Up @@ -333,13 +339,23 @@ class WakaTimeCore {
* @param method
* @returns {*}
*/
async sendPostRequestToApi(payload: Record<string, unknown>, apiKey = ''): Promise<void> {
async sendPostRequestToApi(
payload: Record<string, unknown>,
apiKey = '',
hostname = '',
): Promise<void> {
try {
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, {
const request: RequestInit = {
body: JSON.stringify(payload),
credentials: 'omit',
method: 'POST',
});
};
if (hostname) {
request.headers = {
'X-Machine-Name': hostname,
};
}
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, request);
await response.json();
} catch (err: unknown) {
if (this.db) {
Expand Down
2 changes: 1 addition & 1 deletion src/manifests/chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"page": "options.html"
},
"permissions": ["alarms", "tabs", "storage", "idle"],
"version": "3.0.6"
"version": "3.0.7"
}
2 changes: 1 addition & 1 deletion src/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
"storage",
"idle"
],
"version": "3.0.6"
"version": "3.0.7"
}
1 change: 1 addition & 0 deletions src/types/heartbeats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Datum {
}

export interface SendHeartbeat {
hostname: string;
project: string | null;
url: string;
}

0 comments on commit 1e9e233

Please sign in to comment.