Skip to content

Commit 3d141cb

Browse files
committed
Use custom api url for external links
1 parent 2aa0780 commit 3d141cb

File tree

6 files changed

+52
-40
lines changed

6 files changed

+52
-40
lines changed

src/components/MainList.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { userLogout } from '../reducers/currentUser';
66
import { ReduxSelector } from '../types/store';
77
import { User } from '../types/user';
88
import changeExtensionState from '../utils/changeExtensionStatus';
9+
import { getWebsiteUrl } from '../utils/settings';
910

1011
export interface MainListProps {
1112
loggingEnabled: boolean;
@@ -15,10 +16,10 @@ const openOptionsPage = async (): Promise<void> => {
1516
await browser.runtime.openOptionsPage();
1617
};
1718

18-
export default function MainList({
19+
export default async function MainList({
1920
loggingEnabled,
2021
totalTimeLoggedToday,
21-
}: MainListProps): JSX.Element {
22+
}: MainListProps): Promise<JSX.Element> {
2223
const dispatch = useDispatch();
2324

2425
const user: User | undefined = useSelector(
@@ -53,6 +54,8 @@ export default function MainList({
5354
</div>
5455
) : null;
5556

57+
const url = await getWebsiteUrl();
58+
5659
return (
5760
<div>
5861
{user ? (
@@ -119,7 +122,7 @@ export default function MainList({
119122
<a
120123
target="_blank"
121124
rel="noreferrer"
122-
href="https://wakatime.com/login"
125+
href={`${url}/login`}
123126
className="list-group-item text-body-secondary"
124127
>
125128
<i className="fa fa-fw fa-sign-in me-2" />

src/components/NavBar.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import React from 'react';
22
import { useSelector } from 'react-redux';
33
import { ReduxSelector } from '../types/store';
44
import { User } from '../types/user';
5+
import { getWebsiteUrl } from '../utils/settings';
56

6-
export default function NavBar(): JSX.Element {
7+
export default async function NavBar(): Promise<Promise<JSX.Element>> {
78
const user: User | undefined = useSelector(
89
(selector: ReduxSelector) => selector.currentUser.user,
910
);
@@ -20,13 +21,14 @@ export default function NavBar(): JSX.Element {
2021
}
2122
};
2223

23-
const customRules = () => {
24+
const customRules = async () => {
2425
if (user) {
26+
const url = await getWebsiteUrl();
2527
return (
2628
<li className="mb-2">
2729
<a
2830
target="_blank"
29-
href="https://wakatime.com/settings/rules"
31+
href={`${url}/settings/rules`}
3032
rel="noreferrer"
3133
className="text-body-secondary link-underline link-underline-opacity-0 d-flex w-100 align-items-center"
3234
>
@@ -40,13 +42,14 @@ export default function NavBar(): JSX.Element {
4042
}
4143
};
4244

43-
const dashboard = () => {
45+
const dashboard = async () => {
4446
if (user) {
47+
const url = await getWebsiteUrl();
4548
return (
4649
<li className="mb-2">
4750
<a
4851
target="_blank"
49-
href="https://wakatime.com/dashboard"
52+
href={url}
5053
rel="noreferrer"
5154
className="text-body-secondary link-underline link-underline-opacity-0 d-flex w-100 align-items-center"
5255
>
@@ -84,8 +87,8 @@ export default function NavBar(): JSX.Element {
8487
<div className="collapse navbar-collapse mt-4" id="userInfoCollapse">
8588
{signedInAs()}
8689
<ul className="nav navbar-nav border-bottom pb-2">
87-
{customRules()}
88-
{dashboard()}
90+
{await customRules()}
91+
{await dashboard()}
8992
<li className="dropdown">
9093
<a
9194
href="#"

src/core/WakaTimeCore.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { openDB } from 'idb';
21
import browser, { Tabs } from 'webextension-polyfill';
3-
/* eslint-disable no-fallthrough */
4-
/* eslint-disable default-case */
2+
import { openDB } from 'idb';
53
import moment from 'moment';
64
import { v4 as uuid4 } from 'uuid';
5+
import config, { ExtensionStatus } from '../config/config';
6+
import { EntityType, Heartbeat, HeartbeatsBulkResponse } from '../types/heartbeats';
7+
import getDomainFromUrl, { getDomain } from '../utils/getDomainFromUrl';
8+
import { IS_EDGE, IS_FIREFOX, getOperatingSystem } from '../utils/operatingSystem';
9+
import { Settings, getApiUrl, getSettings } from '../utils/settings';
10+
711
import { OptionalHeartbeat } from '../types/sites';
812
import { changeExtensionStatus } from '../utils/changeExtensionStatus';
9-
import getDomainFromUrl, { getDomain } from '../utils/getDomainFromUrl';
10-
import { getOperatingSystem, IS_EDGE, IS_FIREFOX } from '../utils/operatingSystem';
11-
import { getSettings, Settings } from '../utils/settings';
12-
import { getApiUrl } from '../utils/user';
1313

14-
import config, { ExtensionStatus } from '../config/config';
15-
import { EntityType, Heartbeat, HeartbeatsBulkResponse } from '../types/heartbeats';
14+
/* eslint-disable no-fallthrough */
15+
/* eslint-disable default-case */
1616

1717
class WakaTimeCore {
1818
tabsWithDevtoolsOpen: Tabs.Tab[];
@@ -185,7 +185,7 @@ class WakaTimeCore {
185185
const request: RequestInit = {
186186
body: JSON.stringify(
187187
heartbeats.map((heartbeat) => {
188-
return { ...heartbeat, userAgent };
188+
return { ...heartbeat, plugin: userAgent };
189189
}),
190190
),
191191
credentials: 'omit',

src/reducers/currentUser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
22
import axios, { AxiosResponse } from 'axios';
33
import browser from 'webextension-polyfill';
4-
import config from '../config/config';
54
import { CurrentUser, User, UserPayload } from '../types/user';
6-
import { getApiUrl } from '../utils/user';
5+
6+
import config from '../config/config';
7+
import { getApiUrl } from '../utils/settings';
78

89
interface setUserAction {
910
payload: User | undefined;

src/utils/settings.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,21 @@ export const getSettings = async (): Promise<Settings> => {
8383
export const saveSettings = async (settings: Settings): Promise<void> => {
8484
return browser.storage.sync.set(settings);
8585
};
86+
87+
export const getApiUrl = async () => {
88+
const settings = await browser.storage.sync.get({
89+
apiUrl: config.apiUrl,
90+
});
91+
let apiUrl = (settings.apiUrl as string) || config.apiUrl;
92+
const suffixes = ['/', '.bulk', '/users/current/heartbeats', '/heartbeats', '/heartbeat'];
93+
for (const suffix of suffixes) {
94+
if (apiUrl.endsWith(suffix)) {
95+
apiUrl = apiUrl.slice(0, -suffix.length);
96+
}
97+
}
98+
return apiUrl;
99+
};
100+
101+
export const getWebsiteUrl = async () => {
102+
return (await getApiUrl()).replace('/api/v1', '').replace('://api.', '://');
103+
};

src/utils/user.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
import { AnyAction, Dispatch } from '@reduxjs/toolkit';
22
import axios, { AxiosResponse } from 'axios';
3-
import browser from 'webextension-polyfill';
4-
53
import moment from 'moment';
6-
import config from '../config/config';
4+
import browser from 'webextension-polyfill';
75
import { setApiKey, setLoggingEnabled, setTotalTimeLoggedToday } from '../reducers/configReducer';
8-
import { setUser } from '../reducers/currentUser';
96
import { GrandTotal, Summaries } from '../types/summaries';
107
import { ApiKeyPayload, AxiosUserResponse, User } from '../types/user';
11-
import changeExtensionState from './changeExtensionStatus';
128

13-
export const getApiUrl = async () => {
14-
const settings = await browser.storage.sync.get({
15-
apiUrl: config.apiUrl,
16-
});
17-
let apiUrl = (settings.apiUrl as string) || config.apiUrl;
18-
const suffixes = ['/', '.bulk', '/users/current/heartbeats', '/heartbeats', '/heartbeat'];
19-
for (const suffix of suffixes) {
20-
if (apiUrl.endsWith(suffix)) {
21-
apiUrl = apiUrl.slice(0, -suffix.length);
22-
}
23-
}
24-
return apiUrl;
25-
};
9+
import config from '../config/config';
10+
import { setUser } from '../reducers/currentUser';
11+
import changeExtensionState from './changeExtensionStatus';
12+
import { getApiUrl } from './settings';
2613

2714
/**
2815
* Checks if the user is logged in.

0 commit comments

Comments
 (0)