Feature/history toolbar (#44)

Add the history bar
This commit is contained in:
mrdrogdrog 2020-05-21 22:01:23 +02:00 committed by GitHub
parent 4c785b345b
commit d03e000bd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 681 additions and 303 deletions

View file

@ -1,24 +1,55 @@
import {HistoryEntry} from "../components/landing/pages/history/history";
import moment from "moment";
import {HistoryToolbarState} from "../components/landing/pages/history/history-toolbar/history-toolbar";
import {SortModeEnum} from "../components/sort-button/sort-button";
export function sortAndFilterEntries(entries: HistoryEntry[]): HistoryEntry[] {
return sortEntries(entries);
export function sortAndFilterEntries(entries: HistoryEntry[], viewState: HistoryToolbarState): HistoryEntry[] {
return sortEntries(filterByKeywordSearch(filterBySelectedTags(entries, viewState.selectedTags), viewState.keywordSearch), viewState);
}
function sortEntries(entries: HistoryEntry[]): HistoryEntry[] {
return entries.sort((a, b) => {
if (a.pinned && !b.pinned) {
function filterBySelectedTags(entries: HistoryEntry[], selectedTags: string[]): HistoryEntry[] {
return entries.filter(entry => {
return (selectedTags.length === 0 || arrayCommonCheck(entry.tags, selectedTags))
}
)
}
function arrayCommonCheck<T>(array1: T[], array2: T[]): boolean {
const foundElement = array1.find((element1) =>
array2.find((element2) =>
element2 === element1
)
)
return !!foundElement;
}
function filterByKeywordSearch(entries: HistoryEntry[], keywords: string): HistoryEntry[] {
const searchTerm = keywords.toLowerCase();
return entries.filter(entry => entry.title.toLowerCase().indexOf(searchTerm) !== -1);
}
function sortEntries(entries: HistoryEntry[], viewState: HistoryToolbarState): HistoryEntry[] {
return entries.sort((firstEntry, secondEntry) => {
if (firstEntry.pinned && !secondEntry.pinned) {
return -1;
}
if (!a.pinned && b.pinned) {
if (!firstEntry.pinned && secondEntry.pinned) {
return 1;
}
if (a.lastVisited < b.lastVisited) {
return -1;
if (viewState.titleSortDirection !== SortModeEnum.no) {
return firstEntry.title.localeCompare(secondEntry.title) * viewState.titleSortDirection;
}
if (a.lastVisited > b.lastVisited) {
return 1;
if (viewState.lastVisitedSortDirection !== SortModeEnum.no) {
if (firstEntry.lastVisited > secondEntry.lastVisited) {
return 1 * viewState.lastVisitedSortDirection;
}
if (firstEntry.lastVisited < secondEntry.lastVisited) {
return -1 * viewState.lastVisitedSortDirection;
}
}
return 0;
})
}
@ -37,7 +68,7 @@ export interface OldHistoryEntry {
export function loadHistoryFromLocalStore(): HistoryEntry[] {
const historyJsonString = window.localStorage.getItem("history");
if (historyJsonString === null) {
if (!historyJsonString) {
// if localStorage["history"] is empty we check the old localStorage["notehistory"]
// and convert it to the new format
const oldHistoryJsonString = window.localStorage.getItem("notehistory")