core: group reminders by active/inactive

This commit is contained in:
Abdullah Atta
2023-01-04 15:23:15 +05:00
committed by Abdullah Atta
parent 19f0ee97f2
commit 631142f852
2 changed files with 44 additions and 8 deletions

View File

@@ -208,3 +208,15 @@ export function getUpcomingReminder(reminders) {
});
return sorted[0];
}
/**
* @param {Reminder} reminder
*/
export function isReminderActive(reminder) {
const time = getUpcomingReminderTime(reminder);
return (
!reminder.disabled &&
(time > Date.now() || reminder.snoozeUntil > Date.now())
);
}

View File

@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { isReminderActive } from "../collections/reminders";
import "../types";
import { getWeekGroupFromTimestamp, MONTHS_FULL } from "./date";
@@ -131,16 +132,26 @@ export function groupArray(
addToGroup(groups, groupTitle, item);
});
let items = [];
for (let [groupTitle, groupItems] of groups.entries()) {
if (!groupItems.length) continue;
return flattenGroups(groups);
}
let group = { title: groupTitle, type: "header" };
items.push(group);
groupItems.forEach((item) => items.push(item));
}
/**
* @param {any[]} array
* @param {GroupOptions} options
* @returns Grouped array
*/
export function groupReminders(array) {
const groups = new Map([
["Active", []],
["Inactive", []]
]);
return items;
array.forEach((item) => {
const groupTitle = isReminderActive(item) ? "Active" : "Inactive";
addToGroup(groups, groupTitle, item);
});
return flattenGroups(groups);
}
function addToGroup(groups, groupTitle, item) {
@@ -154,3 +165,16 @@ function getFirstCharacter(str) {
if (!str) return "-";
return REGEX.exec(str)[0].toUpperCase();
}
function flattenGroups(groups) {
let items = [];
for (let [groupTitle, groupItems] of groups.entries()) {
if (!groupItems.length) continue;
let group = { title: groupTitle, type: "header" };
items.push(group);
groupItems.forEach((item) => items.push(item));
}
return items;
}