Skip to content

Commit 6cd1d47

Browse files
committed
Refact tab
1 parent fe8920b commit 6cd1d47

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-39
lines changed
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
1-
import { computed } from 'vue';
1+
import { computed, ref } from 'vue';
2+
import { SettingsTab } from '../utils/enums';
3+
import { getStringTab } from '../utils/extension-tabs';
24

35
export const QUERY_PARAMS_DASHBOARD = 'dashboard.html';
46
export const QUERY_PARAMS_DASHBOARD_TAB = 'tab';
5-
export const QUERY_PARAMS_DASHBOARD_TAB_SETTINGS = 'settings';
7+
export const QUERY_PARAMS_DASHBOARD_TAB_LIMITS = 'limits';
68
export const QUERY_PARAMS_BLOCK = 'block.html';
79
export const QUERY_PARAMS_BLOCK_DOMAIN = 'domain';
810

911
export function useExtensionPage() {
10-
const urlObj = new URL(location.href);
12+
const urlObj = ref(new URL(location.href));
1113

1214
const isLimitPage = computed(
1315
() =>
14-
urlObj.hostname == __APP_ID__ &&
15-
urlObj.pathname.includes(QUERY_PARAMS_DASHBOARD) &&
16-
urlObj.searchParams.get(QUERY_PARAMS_DASHBOARD_TAB) == QUERY_PARAMS_DASHBOARD_TAB_SETTINGS,
16+
urlObj.value.hostname == __APP_ID__ &&
17+
urlObj.value.pathname.includes(QUERY_PARAMS_DASHBOARD) &&
18+
urlObj.value.searchParams.get(QUERY_PARAMS_DASHBOARD_TAB) ==
19+
QUERY_PARAMS_DASHBOARD_TAB_LIMITS,
1720
);
1821

1922
const isBlockPage = computed(
2023
() =>
21-
urlObj.hostname == __APP_ID__ &&
22-
urlObj.pathname.includes(QUERY_PARAMS_BLOCK) &&
23-
urlObj.searchParams.get(QUERY_PARAMS_BLOCK_DOMAIN) == 'youtube.com',
24+
urlObj.value.hostname == __APP_ID__ &&
25+
urlObj.value.pathname.includes(QUERY_PARAMS_BLOCK) &&
26+
urlObj.value.searchParams.get(QUERY_PARAMS_BLOCK_DOMAIN) == 'youtube.com',
2427
);
2528

29+
function updateTab(tab: SettingsTab) {
30+
let targetTab = getStringTab(tab);
31+
const currentTab = urlObj.value.searchParams.get(QUERY_PARAMS_DASHBOARD_TAB);
32+
if (window.history.replaceState && currentTab) {
33+
const sourceUrl = `tab=${currentTab}`;
34+
const targetUrl = `tab=${targetTab}`;
35+
window.history.replaceState(
36+
location.href,
37+
document.title,
38+
location.href.replace(sourceUrl, targetUrl),
39+
);
40+
urlObj.value = new URL(location.href);
41+
}
42+
}
43+
2644
return {
2745
isLimitPage,
2846
isBlockPage,
47+
updateTab,
2948
};
3049
}

src/pages/Dashboard.vue

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
</template>
136136

137137
<script lang="ts" setup>
138-
import { onMounted, ref } from 'vue';
138+
import { computed, onMounted, ref, watch } from 'vue';
139139
import { useI18n } from 'vue-i18n';
140140
import GeneralSettings from '../components/GeneralSettings.vue';
141141
import WhiteList from '../components/WhiteList.vue';
@@ -144,35 +144,46 @@ import DailyNotifications from '../components/Notifications.vue';
144144
import About from '../components/About.vue';
145145
import { SettingsTab } from '../utils/enums';
146146
import DashboadContainer from '../components/DashboadContainer.vue';
147+
import { useExtensionPage } from '../compositions/useExtensionPage';
148+
import { getEnumValueTab } from '../utils/extension-tabs';
147149
148150
const { t } = useI18n();
151+
const extensionPage = useExtensionPage();
149152
150153
const selectedTab = ref<SettingsTab>();
154+
const currentUrl = ref(new URL(location.href));
151155
const selectedWebsite = ref<string>();
152156
157+
watch(currentUrl, () => {
158+
getCurrentTab();
159+
});
160+
153161
onMounted(() => {
154-
const urlObj = new URL(location.href);
155-
const tabName = urlObj.searchParams.get('tab');
162+
getCurrentTab();
163+
});
164+
165+
function getCurrentTab() {
166+
const tabName = currentUrl.value.searchParams.get('tab');
156167
if (tabName != null && tabName != '') {
157-
switch (tabName) {
158-
case 'dashboard':
159-
selectedTab.value = SettingsTab.Dashboard;
160-
break;
161-
case 'settings':
162-
selectedTab.value = SettingsTab.GeneralSettings;
163-
break;
164-
case 'website-stats':
165-
selectedTab.value = SettingsTab.WebsiteStats;
166-
const domain = urlObj.searchParams.get('website');
167-
if (domain != null && domain != '') selectedWebsite.value = domain;
168-
else selectedTab.value = SettingsTab.Dashboard;
169-
break;
168+
selectedTab.value = getEnumValueTab(tabName);
169+
const domain = currentUrl.value.searchParams.get('website');
170+
if (selectedTab.value == SettingsTab.WebsiteStats) {
171+
if (domain != null && domain != '') selectedWebsite.value = domain;
172+
else selectedTab.value = SettingsTab.Dashboard;
173+
} else if (domain != null && domain != '') {
174+
window.history.replaceState(
175+
location.href,
176+
document.title,
177+
location.href.replace(`&website=${domain}`, ''),
178+
);
170179
}
171-
} else selectedTab.value = selectedTab.value = SettingsTab.Dashboard;
172-
});
180+
}
181+
}
173182
174183
function selectTab(value: SettingsTab) {
175184
selectedTab.value = value;
185+
extensionPage.updateTab(value);
186+
currentUrl.value = new URL(location.href);
176187
}
177188
</script>
178189

src/utils/extension-tabs.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { SettingsTab } from './enums';
2+
3+
export function getStringTab(tab: SettingsTab) {
4+
switch (tab) {
5+
case SettingsTab.Dashboard:
6+
return 'dashboard';
7+
case SettingsTab.WebsiteStats:
8+
return 'website-stats';
9+
case SettingsTab.GeneralSettings:
10+
return 'settings';
11+
case SettingsTab.About:
12+
return 'about';
13+
case SettingsTab.Limits:
14+
return 'limits';
15+
case SettingsTab.WhiteList:
16+
return 'whitelist';
17+
case SettingsTab.Notifications:
18+
return 'notifications';
19+
}
20+
}
21+
22+
export function getEnumValueTab(tab: string) {
23+
switch (tab) {
24+
case 'dashboard':
25+
return SettingsTab.Dashboard;
26+
case 'website-stats':
27+
return SettingsTab.WebsiteStats;
28+
case 'settings':
29+
return SettingsTab.GeneralSettings;
30+
case 'about':
31+
return SettingsTab.About;
32+
case 'limits':
33+
return SettingsTab.Limits;
34+
case 'whitelist':
35+
return SettingsTab.WhiteList;
36+
case 'notifications':
37+
return SettingsTab.Notifications;
38+
}
39+
}

src/utils/open-page.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
import Browser from 'webextension-polyfill';
22
import { SettingsTab } from './enums';
3+
import { getStringTab } from './extension-tabs';
34

45
export async function openPage(tab: SettingsTab, domain?: string) {
56
function getDomain() {
67
return domain != undefined && tab == SettingsTab.WebsiteStats ? `&website=${domain}` : '';
78
}
89

9-
let tabName = '';
10-
switch (tab) {
11-
case SettingsTab.Dashboard:
12-
tabName = 'dashboard';
13-
break;
14-
case SettingsTab.WebsiteStats:
15-
tabName = 'website-stats';
16-
break;
17-
case SettingsTab.GeneralSettings:
18-
tabName = 'settings';
19-
break;
20-
}
10+
let tabName = getStringTab(tab);
11+
2112
const url = Browser.runtime.getURL(
2213
`src/dashboard.html${tabName != '' ? `?tab=${tabName}` : ''}${getDomain()}`,
2314
);

0 commit comments

Comments
 (0)