aboutsummaryrefslogtreecommitdiff
path: root/extension/src/components/FolderBody.tsx
blob: a573a6ea7f3f120498878142d42c480db3c5d3f2 (plain) (blame)
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
import BookmarkTreeNode = browser.bookmarks.BookmarkTreeNode;
import Bookmark from "./Bookmark.tsx";
import FolderButton from "./FolderButton.tsx";
import {useContext} from "react";
import {Settings} from "./Body.tsx";

/**
 * A component that displays the contents of a bookmark folder
 *
 * @param props.data The BookmarkTreeNode with data for the folder
 * @constructor
 */
function FolderBody (props: {data: BookmarkTreeNode}) {
    const [settings, _] = useContext(Settings)

    if (!props.data.children) return;

    let content = [...props.data.children].sort(getSortFunction(settings.sort))
    if (settings.foldersFirst) {
        let [bookmarks, folders] = separateFolders(content)
        content = folders.concat(bookmarks)
    }

    return (
        <div className={"folderBody"}>
            {content.map(child =>
                child.children
                    ? <FolderButton data={child} />
                    : <Bookmark data={child} />
            )}
        </div>
    )
}

/**
 * Gets the correct sort function based on the sort setting
 * @param sort The sort setting state
 * @return The corresponding sort function
 */
function getSortFunction(sort: "from-bookmarks" | "alphabetical" | "recent"): ((a:BookmarkTreeNode, b:BookmarkTreeNode) => number) | undefined {
    switch (sort) {
        case "alphabetical": return (a, b) => {
            return a.title.localeCompare(b.title);
        }
        case "recent": return (a, b) => {
            // @ts-ignore
            return a.dateLastUsed - b.dateLastUsed
        }
    }
}

/**
 * Separate the folders and the bookmarks into two separate lists
 * @param content THe bookmark list
 * @returns tuple in the format [bookmarks, folders]
 */
function separateFolders(content: BookmarkTreeNode[]) {
    let bookmarks = [];
    let folders = [];
    for (let bookmarkTreeNode of content) {
        if (bookmarkTreeNode.children) {
            folders.push(bookmarkTreeNode)
        } else {
            bookmarks.push(bookmarkTreeNode)
        }
    }
    return [bookmarks, folders]
}



export default FolderBody;