Skip to content

Commit ac4fb40

Browse files
committed
feat: finished date filters
1 parent 0edead6 commit ac4fb40

File tree

3 files changed

+94
-50
lines changed

3 files changed

+94
-50
lines changed

src/background/store.js

+26-22
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,36 @@ export class Store {
2424
}
2525

2626
async init() {
27-
if (process.env.NODE_ENV === "development") {
28-
logger.info("STORE_0000");
27+
try {
28+
if (process.env.NODE_ENV === "development") {
29+
logger.info("STORE_0000");
30+
31+
await browser.storage.local.set({
32+
data: require("../../test/dummy-store.json"),
33+
});
34+
}
35+
36+
const { data } = await browser.storage.local.get("data");
37+
38+
// Fix for change in data format This piece of code runs only once after
39+
// installation, populates the firstVisit key, and that's it We can then use
40+
// firstVisit and lastVisit to filter based on date durations
41+
Object.keys(data).forEach((origin) => {
42+
console.log(origin, !data[origin].firstVisit);
43+
44+
if (!data[origin].firstVisit)
45+
data[origin] = {
46+
...data[origin],
47+
firstVisit: getFirstVisit(data[origin]),
48+
};
49+
});
2950

3051
await browser.storage.local.set({
31-
data: require("../../test/dummy-store.json"),
52+
data,
3253
});
54+
} catch (error) {
55+
this.logError(error);
3356
}
34-
35-
const { data } = await browser.storage.local.get("data");
36-
37-
// Fix for change in data format This piece of code runs only once after
38-
// installation, populates the firstVisit key, and that's it We can then use
39-
// firstVisit and lastVisit to filter based on date durations
40-
Object.keys(data).forEach((origin) => {
41-
console.log(origin, !data[origin].firstVisit);
42-
43-
if (!data[origin].firstVisit)
44-
data[origin] = {
45-
...data[origin],
46-
firstVisit: getFirstVisit(data[origin]),
47-
};
48-
});
49-
50-
await browser.storage.local.set({
51-
data,
52-
});
5357
}
5458

5559
/**

src/options/App.vue

+41-28
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
>
4343
Origin
4444
</th>
45+
<th
46+
class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider cursor-pointer"
47+
data-sortby="firstVisit"
48+
v-on:click="sort"
49+
v-on:keyup.enter="sort"
50+
v-on:keyup.space="sort"
51+
tabindex="0"
52+
aria-label="sort by first visit"
53+
>
54+
First Visit
55+
</th>
4556
<th
4657
class="px-5 py-3 border-b-2 border-gray-200 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider cursor-pointer"
4758
data-sortby="lastVisit"
@@ -75,6 +86,11 @@
7586
</p>
7687
</div>
7788
</td>
89+
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
90+
<p class="text-gray-900 whitespace-no-wrap">
91+
{{ entry.firstVisit }}
92+
</p>
93+
</td>
7894
<td class="px-5 py-5 border-b border-gray-200 bg-white text-sm">
7995
<p class="text-gray-900 whitespace-no-wrap">
8096
{{ entry.lastVisit }}
@@ -100,7 +116,12 @@
100116
</template>
101117

102118
<script>
103-
import { getFirstVisit, compare, humanizeEntry } from "../utils.js";
119+
import {
120+
getFirstVisit,
121+
compare,
122+
humanizeEntry,
123+
calcTotalTimeInRange,
124+
} from "../utils.js";
104125
105126
export default {
106127
name: "app",
@@ -133,41 +154,33 @@ export default {
133154
dateFrom: () => this.date.dateFrom.toLocaleDateString("en-CA"),
134155
dateTo: () => this.date.dateTo.toLocaleDateString("en-CA"),
135156
sortedHistory: function () {
136-
const dateFrom = new Date(this.date.dateFrom);
137-
const dateTo = new Date(this.date.dateTo);
157+
// handling for cleared inputs
158+
const dateFrom = this.date.dateFrom
159+
? new Date(this.date.dateFrom)
160+
: new Date(0);
161+
const dateTo = this.date.dateTo ? new Date(this.date.dateTo) : new Date();
138162
139163
return Array.from(this.history)
140-
.sort((a, b) => {
141-
const keyA = a[this.sortby];
142-
const keyB = b[this.sortby];
143-
144-
return compare(keyA, keyB, this.sortby, this.ascending);
145-
})
146-
.map(humanizeEntry)
147164
.filter(
148165
(entry) =>
149-
new Date(entry.firstVisit) >= dateFrom &&
150-
new Date(entry.lastVisit) <= dateTo,
166+
new Date(entry.lastVisit).getTime() >= dateFrom.getTime() &&
167+
new Date(entry.firstVisit).getTime() <= dateTo.getTime(),
151168
)
152169
.filter(
153170
(entry) =>
154171
(this.search && entry.origin.includes(this.search)) || !this.search,
155-
);
156-
// .filter((entry) =>
157-
// Object.keys(entry)
158-
// .some((key) => Number(key) >= from.year)
159-
// .some((year) =>
160-
// Object.keys(entry[year])
161-
// .some((key) => Number(key) >= from.month)
162-
// .some((month) =>
163-
// Object.keys(entry[year][month]).some(
164-
// (key) => Number(key) >= from.day,
165-
// ),
166-
// ),
167-
// ),
168-
// );
169-
// entry?.[from.year]?.[from.month]?.[from.day] ||
170-
// entry?.[to.year]?.[to.month]?.[to.day],
172+
)
173+
.map((entry) => ({
174+
...entry,
175+
totalTime: calcTotalTimeInRange(entry, dateFrom, dateTo),
176+
}))
177+
.sort((a, b) => {
178+
const keyA = a[this.sortby];
179+
const keyB = b[this.sortby];
180+
181+
return compare(keyA, keyB, this.sortby, this.ascending);
182+
})
183+
.map(humanizeEntry);
171184
},
172185
},
173186
methods: {

src/utils.js

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ export const getFirstVisit = (entry) => {
2626
return new Date(years[0], months[0], days[0]);
2727
};
2828

29+
/**
30+
* Returns the total time a website's been visited in a given date range. This
31+
* handles entries that are not populated with data from all included dates.
32+
* @param {import('./background/store').Entry} entry
33+
* @param {Date} from
34+
* @param {Date} to
35+
*/
36+
export const calcTotalTimeInRange = (entry, from, to) => {
37+
const dateFrom = new Date(from);
38+
const dateTo = new Date(to);
39+
let totalTime = 0;
40+
41+
while (dateFrom <= dateTo) {
42+
const year = dateFrom.getFullYear();
43+
const month = dateFrom.getMonth();
44+
const day = dateFrom.getDate();
45+
46+
if (typeof entry?.[year]?.[month]?.[day] === "number")
47+
totalTime += entry[year][month][day];
48+
49+
dateFrom.setDate(day + 1);
50+
}
51+
52+
return totalTime;
53+
};
54+
2955
export const compare = (valA, valB, filter, ascending) => {
3056
if (ascending)
3157
if (filter === "lastVisit" && isValidDate(valA) && isValidDate(valB))
@@ -44,6 +70,7 @@ export const humanizeDate = (date) =>
4470
export const humanizeEntry = (entry) => ({
4571
...entry,
4672
lastVisit: new Date(entry.lastVisit).toLocaleString([], dateLocaleOptions),
73+
firstVisit: new Date(entry.firstVisit).toLocaleString([], dateLocaleOptions),
4774
totalTime: humanizeDuration(entry.totalTime * 1000, { largest: 2 }),
4875
});
4976

0 commit comments

Comments
 (0)