Skip to content

Commit 0c62354

Browse files
committed
Add auto-refresh functionality for hourly view every 30 seconds
1 parent 9af7c03 commit 0c62354

File tree

10 files changed

+105
-19
lines changed

10 files changed

+105
-19
lines changed

dist/dark.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/dark.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/dashboard.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/general.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/i18n.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/dashboard.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Dashboad.vue

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ import BlankView from './BlankView.vue';
6161
import TabList from '../components/TabList.vue';
6262
import TopSitesPieChart from './TopSitesPieChart.vue';
6363
import { TypeOfList } from '../utils/enums';
64-
import { onMounted, ref } from 'vue';
64+
import { onMounted, ref, onUnmounted, watch } from 'vue';
6565
import Browser from 'webextension-polyfill';
6666
6767
const { t } = useI18n();
6868
const chart = ref<TypeOfChart>();
69+
const refreshTimer = ref<number | null>(null);
6970
7071
enum TypeOfChart {
7172
Horly,
@@ -90,16 +91,60 @@ onMounted(async () => {
9091
// Default to hourly view
9192
chart.value = TypeOfChart.Horly;
9293
}
94+
95+
// Start refresh timer if hourly view is active
96+
if (chart.value === TypeOfChart.Horly) {
97+
startRefreshTimer();
98+
}
9399
} catch (error) {
94100
console.error('Error loading chart preference:', error);
95101
// Default to hourly view if error
96102
chart.value = TypeOfChart.Horly;
103+
startRefreshTimer();
97104
}
98105
});
99106
107+
// Clean up timer when component is unmounted
108+
onUnmounted(() => {
109+
stopRefreshTimer();
110+
});
111+
112+
// Create a simple event bus for component communication
113+
const refreshEvent = new CustomEvent('refresh-data');
114+
115+
// Function to emit the refresh event to child components
116+
function emitRefreshEvent() {
117+
window.dispatchEvent(refreshEvent);
118+
}
119+
120+
// Start the refresh timer for hourly view
121+
function startRefreshTimer() {
122+
if (refreshTimer.value === null) {
123+
// Set timer to refresh every 30 seconds (30000 ms)
124+
refreshTimer.value = window.setInterval(() => {
125+
emitRefreshEvent();
126+
}, 30000);
127+
}
128+
}
129+
130+
// Stop the refresh timer
131+
function stopRefreshTimer() {
132+
if (refreshTimer.value !== null) {
133+
window.clearInterval(refreshTimer.value);
134+
refreshTimer.value = null;
135+
}
136+
}
137+
100138
async function openChart(type: TypeOfChart) {
101139
chart.value = type;
102140
141+
// Start or stop refresh timer based on chart type
142+
if (type === TypeOfChart.Horly) {
143+
startRefreshTimer();
144+
} else {
145+
stopRefreshTimer();
146+
}
147+
103148
// Save preference to storage
104149
try {
105150
await Browser.storage.local.set({ [CHART_PREFERENCE_KEY]: type });

src/components/HourlyChart.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
LinearScale,
2222
CategoryScale,
2323
} from 'chart.js';
24-
import { onMounted, ref } from 'vue';
24+
import { onMounted, onUnmounted, ref } from 'vue';
2525
import { injectStorage } from '../storage/inject-storage';
2626
import { StorageDeserializeParam } from '../storage/storage-params';
2727
import { TimeInterval } from '../entity/time-interval';
@@ -210,5 +210,19 @@ async function buildChart() {
210210
isLoaded.value = true;
211211
}
212212
213-
onMounted(async () => await buildChart());
213+
// Handler for refresh events
214+
function handleRefresh() {
215+
buildChart();
216+
}
217+
218+
onMounted(async () => {
219+
await buildChart();
220+
221+
// Listen for refresh events from parent component
222+
window.addEventListener('refresh-data', handleRefresh);
223+
});
224+
225+
onUnmounted(() => {
226+
window.removeEventListener('refresh-data', handleRefresh);
227+
});
214228
</script>

src/components/TabList.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default {
4444
</script>
4545

4646
<script lang="ts" setup>
47-
import { computed, onMounted, ref } from 'vue';
47+
import { computed, onMounted, onUnmounted, ref } from 'vue';
4848
import { useI18n } from 'vue-i18n';
4949
import TabItem from '../components/TabItem.vue';
5050
import TabItemHeader from '../components/TabItemHeader.vue';
@@ -144,9 +144,23 @@ function getItem(tab: Tab): CurrentTabItem {
144144
};
145145
}
146146
147+
// Handler for refresh events
148+
function handleRefresh() {
149+
if (props.type === TypeOfList.Today || props.type === TypeOfList.Dashboard) {
150+
loadList(SortingBy.UsageTime);
151+
}
152+
}
153+
147154
onMounted(async () => {
148155
isLoading.value = true;
149156
await loadList(SortingBy.UsageTime);
157+
158+
// Listen for refresh events from parent component
159+
window.addEventListener('refresh-data', handleRefresh);
160+
});
161+
162+
onUnmounted(() => {
163+
window.removeEventListener('refresh-data', handleRefresh);
150164
});
151165
</script>
152166

src/components/TopSitesPieChart.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js';
3030
import ChartDataLabels from 'chartjs-plugin-datalabels';
3131
import { convertSummaryTimeToString } from '../utils/converter';
3232
import { injectStorage } from '../storage/inject-storage';
33-
import { onMounted, ref, watch } from 'vue';
33+
import { onMounted, onUnmounted, ref, watch } from 'vue';
3434
import { DARK_MODE_DEFAULT, StorageParams } from '../storage/storage-params';
3535
import { useTodayTabListSummary } from '../functions/useTodayTabListSummary';
3636
import { SortingBy } from '../utils/enums';
@@ -132,10 +132,23 @@ function setupChartData(labels: string[], timeValues: number[]) {
132132
};
133133
}
134134
135+
// Handler for refresh events
136+
function handleRefresh() {
137+
processTabsData();
138+
}
139+
135140
onMounted(async () => {
136141
darkMode.value = await settingsStorage.getValue(StorageParams.DARK_MODE, DARK_MODE_DEFAULT);
137142
ChartJS.register(ArcElement, Tooltip, Legend, ChartDataLabels);
138143
await processTabsData();
144+
145+
// Listen for refresh events from parent component
146+
window.addEventListener('refresh-data', handleRefresh);
147+
});
148+
149+
// Clean up event listener when component is unmounted
150+
onUnmounted(() => {
151+
window.removeEventListener('refresh-data', handleRefresh);
139152
});
140153
</script>
141154

0 commit comments

Comments
 (0)