forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseTodayTabListSummary.ts
More file actions
47 lines (40 loc) · 1.45 KB
/
useTodayTabListSummary.ts
File metadata and controls
47 lines (40 loc) · 1.45 KB
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
import { TabListSummary } from '../dto/tabListSummary';
import { Tab } from '../entity/tab';
import { injectTabsRepository } from '../repository/inject-tabs-repository';
import { SortingBy } from '../utils/enums';
import { todayLocalDate } from '../utils/date';
export async function useTodayTabListSummary(sortingBy: SortingBy): Promise<TabListSummary | null> {
const repo = await injectTabsRepository();
const unSortedTabs = repo.getTodayTabs();
let tabs: Tab[] = [];
if (unSortedTabs.length == 0) return null;
tabs = unSortedTabs.sort(function (a: Tab, b: Tab) {
return sortingBy == SortingBy.UsageTime
? b.days.find(s => s.date === todayLocalDate())!.summary -
a.days.find(s => s.date === todayLocalDate())!.summary
: b.days.find(s => s.date === todayLocalDate())!.counter -
a.days.find(s => s.date === todayLocalDate())!.counter;
});
const summaryTimeList = tabs?.map(function (tab) {
return tab.days.find(day => day.date === todayLocalDate())!.summary;
});
const siteList = tabs?.map(function (tab) {
return tab.url;
});
const timeForChart = summaryTimeList?.slice(0, 10);
const sitesForChart = siteList?.slice(0, 10);
const summaryTime =
summaryTimeList != undefined && summaryTimeList.length > 0
? summaryTimeList.reduce(function (a, b) {
return a + b;
})
: 0;
return {
tabs,
summaryTime,
chart: {
timeForChart,
sitesForChart,
},
};
}