summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/firefox-view-places-query.sys.mjs
blob: 892390576994f179c9b8b652b662476fc9e9257d (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { PlacesQuery } from "resource://gre/modules/PlacesQuery.sys.mjs";

/**
 * Extension of PlacesQuery which provides additional caches for Firefox View.
 */
export class FirefoxViewPlacesQuery extends PlacesQuery {
  /** @type {Date} */
  #todaysDate = null;
  /** @type {Date} */
  #yesterdaysDate = null;

  get visitsFromToday() {
    if (this.cachedHistory == null || this.#todaysDate == null) {
      return [];
    }
    const mapKey = this.getStartOfDayTimestamp(this.#todaysDate);
    return this.cachedHistory.get(mapKey) ?? [];
  }

  get visitsFromYesterday() {
    if (this.cachedHistory == null || this.#yesterdaysDate == null) {
      return [];
    }
    const mapKey = this.getStartOfDayTimestamp(this.#yesterdaysDate);
    return this.cachedHistory.get(mapKey) ?? [];
  }

  /**
   * Get a list of visits per day for each day on this month, excluding today
   * and yesterday.
   *
   * @returns {HistoryVisit[][]}
   *   A list of visits for each day.
   */
  get visitsByDay() {
    const visitsPerDay = [];
    for (const [time, visits] of this.cachedHistory.entries()) {
      const date = new Date(time);
      if (
        this.#isSameDate(date, this.#todaysDate) ||
        this.#isSameDate(date, this.#yesterdaysDate)
      ) {
        continue;
      } else if (!this.#isSameMonth(date, this.#todaysDate)) {
        break;
      } else {
        visitsPerDay.push(visits);
      }
    }
    return visitsPerDay;
  }

  /**
   * Get a list of visits per month for each month, excluding this one, and
   * excluding yesterday's visits if yesterday happens to fall on the previous
   * month.
   *
   * @returns {HistoryVisit[][]}
   *   A list of visits for each month.
   */
  get visitsByMonth() {
    const visitsPerMonth = [];
    let previousMonth = null;
    for (const [time, visits] of this.cachedHistory.entries()) {
      const date = new Date(time);
      if (
        this.#isSameMonth(date, this.#todaysDate) ||
        this.#isSameDate(date, this.#yesterdaysDate)
      ) {
        continue;
      }
      const month = this.getStartOfMonthTimestamp(date);
      if (month !== previousMonth) {
        visitsPerMonth.push(visits);
      } else {
        visitsPerMonth[visitsPerMonth.length - 1] = visitsPerMonth
          .at(-1)
          .concat(visits);
      }
      previousMonth = month;
    }
    return visitsPerMonth;
  }

  formatRowAsVisit(row) {
    const visit = super.formatRowAsVisit(row);
    this.#normalizeVisit(visit);
    return visit;
  }

  formatEventAsVisit(event) {
    const visit = super.formatEventAsVisit(event);
    this.#normalizeVisit(visit);
    return visit;
  }

  /**
   * Normalize data for fxview-tabs-list.
   *
   * @param {HistoryVisit} visit
   *   The visit to format.
   */
  #normalizeVisit(visit) {
    visit.time = visit.date.getTime();
    visit.title = visit.title || visit.url;
    visit.icon = `page-icon:${visit.url}`;
    visit.primaryL10nId = "fxviewtabrow-tabs-list-tab";
    visit.primaryL10nArgs = JSON.stringify({
      targetURI: visit.url,
    });
    visit.secondaryL10nId = "fxviewtabrow-options-menu-button";
    visit.secondaryL10nArgs = JSON.stringify({
      tabTitle: visit.title || visit.url,
    });
  }

  async fetchHistory() {
    await super.fetchHistory();
    if (this.cachedHistoryOptions.sortBy === "date") {
      this.#setTodaysDate();
    }
  }

  handlePageVisited(event) {
    const visit = super.handlePageVisited(event);
    if (!visit) {
      return;
    }
    if (
      this.cachedHistoryOptions.sortBy === "date" &&
      (this.#todaysDate == null ||
        (visit.date.getTime() > this.#todaysDate.getTime() &&
          !this.#isSameDate(visit.date, this.#todaysDate)))
    ) {
      // If today's date has passed (or is null), it should be updated now.
      this.#setTodaysDate();
    }
  }

  #setTodaysDate() {
    const now = new Date();
    this.#todaysDate = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate()
    );
    this.#yesterdaysDate = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate() - 1
    );
  }

  /**
   * Given two date instances, check if their dates are equivalent.
   *
   * @param {Date} dateToCheck
   * @param {Date} date
   * @returns {boolean}
   *   Whether both date instances have equivalent dates.
   */
  #isSameDate(dateToCheck, date) {
    return (
      dateToCheck.getDate() === date.getDate() &&
      this.#isSameMonth(dateToCheck, date)
    );
  }

  /**
   * Given two date instances, check if their months are equivalent.
   *
   * @param {Date} dateToCheck
   * @param {Date} month
   * @returns {boolean}
   *   Whether both date instances have equivalent months.
   */
  #isSameMonth(dateToCheck, month) {
    return (
      dateToCheck.getMonth() === month.getMonth() &&
      dateToCheck.getFullYear() === month.getFullYear()
    );
  }
}