Skip to content

Commit a623407

Browse files
authored
Merge pull request #404 from penge/sort-by-created
Order notes by Latest Created
2 parents 6ef07cc + eeebbab commit a623407

File tree

17 files changed

+147
-147
lines changed

17 files changed

+147
-147
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
node_modules/*
12
dist/*
23
jest.config.ts
34
build.ts

src/i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116
"Current order:": "Current order:",
117117
"Orders": {
118118
"Alphabetical": "Alphabetical",
119-
"NewestFirst": "Newest first",
119+
"LatestCreated": "Latest Created",
120+
"LatestModified": "Latest Modified",
120121
"Custom": "Custom"
121122
},
122123
"Orders description": "When <b>\"Custom\"</b> is selected, you can <b>Drag & Drop</b> notes to change their order.",

src/notes.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import {
1919
} from "shared/storage/schema";
2020
import { setTheme as setThemeCore } from "themes/set-theme";
2121

22+
import Overview from "notes/components/Overview";
2223
import __Notification from "notes/components/Notification";
2324
import __Sidebar from "notes/components/Sidebar";
2425
import __Content from "notes/components/content/Content";
2526
import { ContentNote } from "notes/components/content/common";
2627
import __CommandPalette, { CommandPaletteProps } from "notes/components/CommandPalette";
2728
import __Toolbar from "notes/components/Toolbar";
28-
import range from "notes/range";
2929

3030
import __ContextMenu, { ContextMenuProps } from "notes/components/ContextMenu";
3131
import __RenameNoteModal, { RenameNoteModalProps } from "notes/components/modals/RenameNoteModal";
@@ -44,14 +44,14 @@ import {
4444
import sendMessage from "shared/messages/send";
4545

4646
import { getActiveFromUrl, getFocusOverride, isOverview } from "notes/location";
47-
import Overview from "notes/overview/Overview";
4847
import getFirstAvailableNoteName from "notes/filters/get-first-available-note-name";
4948
import notesHistory from "notes/history";
5049
import keyboardShortcuts, { KeyboardShortcut } from "notes/keyboard-shortcuts";
51-
import { useKeyboardShortcut } from "notes/hooks/use-keyboard-shortcut";
50+
import { useKeyboardShortcut } from "notes/components/hooks/use-keyboard-shortcut";
5251
import {
5352
Command, toggleSidebar, toggleToolbar, toggleFocusMode,
5453
} from "notes/commands";
54+
import range from "notes/range";
5555
import { exportNote } from "notes/export";
5656
import { notesToSidebarNotes } from "notes/adapters";
5757

Lines changed: 60 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,72 @@
1-
import { NotesObject, NotesOrder } from "shared/storage/schema";
2-
import { notesToSidebarNotes } from "..";
1+
import { Note, NotesObject, NotesOrder } from "shared/storage/schema";
2+
import * as sortNotes from "notes/sort";
3+
import { notesToSidebarNotes, SidebarNote } from "..";
34

4-
describe("notesToSidebarNotes()", () => {
5-
describe("unpinned notes only", () => {
6-
it("returns notes in correct order", () => {
7-
const notes: NotesObject = {
8-
Todo: {
9-
content: "my todo",
10-
createdTime: "CT-TODO",
11-
modifiedTime: "MT-TODO",
12-
},
13-
Article: {
14-
content: "my article",
15-
createdTime: "CT-ARTICLE",
16-
modifiedTime: "MT-ARTICLE",
17-
},
18-
Shopping: {
19-
content: "my shopping",
20-
createdTime: "CT-SHOPPING",
21-
modifiedTime: "MT-SHOPPING",
22-
},
23-
};
24-
25-
const sidebarNotes = notesToSidebarNotes(notes, NotesOrder.Alphabetical);
26-
expect(sidebarNotes).toEqual([
27-
{ name: "Article", ...notes.Article },
28-
{ name: "Shopping", ...notes.Shopping },
29-
{ name: "Todo", ...notes.Todo },
30-
]);
31-
});
32-
});
5+
const notRelevant: Pick<Note, "createdTime" | "modifiedTime"> = {
6+
createdTime: "",
7+
modifiedTime: "",
8+
};
339

34-
describe("pinned and unpinned notes", () => {
35-
it("returns notes in correct order", () => {
36-
const notes: NotesObject = {
37-
Todo: {
38-
content: "my todo",
39-
createdTime: "CT-TODO",
40-
modifiedTime: "MT-3",
41-
},
42-
Shopping: {
43-
content: "my shopping",
44-
createdTime: "CT-SHOPPING",
45-
modifiedTime: "MT-9",
46-
pinnedTime: "PT-SHOPPING",
47-
},
48-
Article: {
49-
content: "my article",
50-
createdTime: "CT-ARTICLE",
51-
modifiedTime: "MT-2",
52-
},
53-
Clipboard: {
54-
content: "my clipboard",
55-
createdTime: "CT-CLIPBOARD",
56-
modifiedTime: "MT-8",
57-
pinnedTime: "PT-CLIPBOARD",
58-
},
59-
};
10+
const notes: NotesObject = {
11+
Todo: {
12+
content: "my todo",
13+
...notRelevant,
14+
},
15+
Shopping: {
16+
content: "my shopping",
17+
...notRelevant,
18+
pinnedTime: "PT-2",
19+
},
20+
Article: {
21+
content: "my article",
22+
...notRelevant,
23+
},
24+
Clipboard: {
25+
content: "my clipboard",
26+
...notRelevant,
27+
pinnedTime: "PT-1",
28+
},
29+
};
6030

61-
const sidebarNotes = notesToSidebarNotes(notes, NotesOrder.Alphabetical);
62-
expect(sidebarNotes).toEqual([
63-
{ name: "Clipboard", ...notes.Clipboard },
64-
{ name: "Shopping", ...notes.Shopping },
65-
66-
{ name: "Article", ...notes.Article },
67-
{ name: "Todo", ...notes.Todo },
68-
]);
31+
describe("notesToSidebarNotes()", () => {
32+
it("returns notes in correct order", () => {
33+
const sidebarNotes = notesToSidebarNotes(notes, NotesOrder.Alphabetical);
34+
expect(sidebarNotes).toEqual([
35+
{ name: "Clipboard", ...notes.Clipboard },
36+
{ name: "Shopping", ...notes.Shopping },
6937

70-
const sidebarNotes2 = notesToSidebarNotes(notes, NotesOrder.NewestFirst);
71-
expect(sidebarNotes2).toEqual([
72-
{ name: "Shopping", ...notes.Shopping },
73-
{ name: "Clipboard", ...notes.Clipboard },
38+
{ name: "Article", ...notes.Article },
39+
{ name: "Todo", ...notes.Todo },
40+
]);
41+
});
7442

75-
{ name: "Todo", ...notes.Todo },
76-
{ name: "Article", ...notes.Article },
77-
]);
43+
describe("dependency on sortNotes()", () => {
44+
const expectedNotesArgument: SidebarNote[] = [
45+
{ name: "Todo", ...notes.Todo },
46+
{ name: "Shopping", ...notes.Shopping },
47+
{ name: "Article", ...notes.Article },
48+
{ name: "Clipboard", ...notes.Clipboard },
49+
];
7850

79-
const sidebarNotes3 = notesToSidebarNotes(notes, NotesOrder.Custom, ["Shopping", "Article"]);
80-
expect(sidebarNotes3).toEqual([
81-
{ name: "Clipboard", ...notes.Clipboard },
82-
{ name: "Shopping", ...notes.Shopping },
51+
const dummyReturn: SidebarNote[] = [{
52+
name: "dummy", ...notes.Todo,
53+
}];
8354

84-
{ name: "Todo", ...notes.Todo },
85-
{ name: "Article", ...notes.Article },
86-
]);
55+
it("calls sortNotes() with correct arguments", () => {
56+
const spy = jest.spyOn(sortNotes, "default").mockReturnValue(dummyReturn);
8757

88-
const sidebarNotes4 = notesToSidebarNotes(notes, NotesOrder.Custom, []);
89-
expect(sidebarNotes4).toEqual([
90-
{ name: "Shopping", ...notes.Shopping },
91-
{ name: "Clipboard", ...notes.Clipboard },
58+
const scenarios: Array<[NotesObject, NotesOrder, string[] | undefined]> = [
59+
[notes, NotesOrder.Alphabetical, undefined],
60+
[notes, NotesOrder.LatestCreated, undefined],
61+
[notes, NotesOrder.LatestModified, undefined],
62+
[notes, NotesOrder.Custom, ["Shopping", "Article"]],
63+
];
9264

93-
{ name: "Todo", ...notes.Todo },
94-
{ name: "Article", ...notes.Article },
95-
]);
65+
scenarios.forEach((scenario) => {
66+
const returnValue = notesToSidebarNotes(...scenario);
67+
expect(spy).toHaveBeenCalledWith(expectedNotesArgument, scenario[1], scenario[2]);
68+
expect(returnValue).toEqual(dummyReturn);
69+
});
9670
});
9771
});
9872
});

src/notes/components/CommandPalette.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
useRef, useState, useEffect, useMemo,
44
} from "preact/hooks";
55
import clsx from "clsx";
6-
import useBodyClass from "notes/hooks/use-body-class";
6+
import useBodyClass from "notes/components/hooks/use-body-class";
77
import { t, tString } from "i18n";
88
import {
99
Command, commands as contentCommands, toggleSidebar, toggleToolbar,

src/notes/components/Overlay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h } from "preact";
2-
import useBodyClass from "notes/hooks/use-body-class";
2+
import useBodyClass from "notes/components/hooks/use-body-class";
33

44
interface OverlayProps {
55
type: "to-rename" | "to-delete" | "to-create"
File renamed without changes.

src/notes/components/SidebarNotes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SidebarNote } from "notes/adapters";
77
import clsx from "clsx";
88
import SVG from "notes/components/SVG";
99
import LockSvgText from "svg/lock.svg";
10-
import { useKeyboardShortcut, KeyboardShortcut } from "notes/hooks/use-keyboard-shortcut";
10+
import { useKeyboardShortcut, KeyboardShortcut } from "notes/components/hooks/use-keyboard-shortcut";
1111
import sendMessage from "shared/messages/send";
1212

1313
export interface SidebarNotesProps {

src/notes/components/content/ContentHtml.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { h } from "preact";
22
import { useCallback, useEffect, useRef } from "preact/hooks";
33
import runUploadPreconditions from "background/google-drive/preconditions/run-upload-preconditions";
44
import { KeyboardShortcut } from "notes/keyboard-shortcuts";
5-
import { useKeyboardShortcut } from "notes/hooks/use-keyboard-shortcut";
5+
import { useKeyboardShortcut } from "notes/components/hooks/use-keyboard-shortcut";
66
import __range from "notes/range";
77
import { insideListItem } from "notes/content/list";
88
import { reinitTables } from "notes/content/table";

src/notes/components/content/ContentText.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { h } from "preact";
22
import { useCallback, useEffect, useRef } from "preact/hooks";
33
import { KeyboardShortcut } from "notes/keyboard-shortcuts";
4-
import { useKeyboardShortcut } from "notes/hooks/use-keyboard-shortcut";
4+
import { useKeyboardShortcut } from "notes/components/hooks/use-keyboard-shortcut";
55
import { InsertTabFactory } from "../../commands";
66
import { ContentProps, reattachEditNote } from "./common";
77

src/notes/components/modals/Modal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
useRef, useState, useEffect, useMemo, useCallback,
44
} from "preact/hooks";
55
import clsx from "clsx";
6-
import useBodyClass from "notes/hooks/use-body-class";
7-
import { useKeyboardShortcut, KeyboardShortcut } from "notes/hooks/use-keyboard-shortcut";
6+
import useBodyClass from "notes/components/hooks/use-body-class";
7+
import { useKeyboardShortcut, KeyboardShortcut } from "notes/components/hooks/use-keyboard-shortcut";
88

99
interface ModalProps {
1010
className?: string

src/notes/sort/__tests__/index.test.ts

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ const notes: SidebarNote[] = [
66
{
77
name: "Clipboard",
88
content: "my todo",
9-
createdTime: "CT1",
10-
modifiedTime: "MT4",
11-
},
12-
{
13-
name: "Todo",
14-
content: "my todo",
15-
createdTime: "CT2",
16-
modifiedTime: "MT8",
9+
createdTime: "CT-1",
10+
modifiedTime: "MT-4",
11+
pinnedTime: "PT-2",
1712
},
1813
{
1914
name: "Article",
2015
content: "my article",
21-
createdTime: "CT4",
22-
modifiedTime: "MT3",
16+
createdTime: "CT-4",
17+
modifiedTime: "MT-3",
18+
},
19+
{
20+
name: "Todo",
21+
content: "my todo",
22+
createdTime: "CT-2",
23+
modifiedTime: "MT-8",
24+
pinnedTime: "PT-1",
2325
},
26+
2427
{
2528
name: "Shopping",
2629
content: "my shopping",
27-
createdTime: "CT3",
28-
modifiedTime: "MT7",
30+
createdTime: "CT-3",
31+
modifiedTime: "MT-7",
2932
},
3033
];
3134

@@ -34,30 +37,46 @@ const nameMap = (item: SidebarNote) => item.name;
3437
test("sortNotes() sorts notes in Alphabetical order", () => {
3538
expect(
3639
sortNotes(notes, NotesOrder.Alphabetical).map(nameMap),
37-
).toEqual(["Article", "Clipboard", "Shopping", "Todo"]);
40+
).toEqual([
41+
"Clipboard", "Todo",
42+
"Article", "Shopping",
43+
]);
3844
});
3945

40-
test("sortNotes() sorts notes in NewestFirst order", () => {
46+
test("sortNotes() sorts notes in LatestCreated order", () => {
4147
expect(
42-
sortNotes(notes, NotesOrder.NewestFirst).map(nameMap),
43-
).toEqual(["Todo", "Shopping", "Clipboard", "Article"]);
48+
sortNotes(notes, NotesOrder.LatestCreated).map(nameMap),
49+
).toEqual([
50+
"Todo", "Clipboard",
51+
"Article", "Shopping",
52+
]);
4453
});
4554

46-
test("sortNotes() sorts notes in Custom order", () => {
47-
const custom = ["Article", "Todo", "Clipboard", "Shopping"];
55+
test("sortNotes() sorts notes in LatestModified order", () => {
4856
expect(
49-
sortNotes(notes, NotesOrder.Custom, custom).map(nameMap),
50-
).toEqual(custom);
57+
sortNotes(notes, NotesOrder.LatestModified).map(nameMap),
58+
).toEqual([
59+
"Todo", "Clipboard",
60+
"Shopping", "Article",
61+
]);
5162
});
5263

53-
test("sortNotes() returns original order when custom order is not provided", () => {
54-
const original = notes.map(nameMap);
55-
64+
test("sortNotes() sorts notes in Custom order", () => {
5665
expect(
57-
sortNotes(notes, NotesOrder.Custom).map(nameMap), // "custom" is not provided
58-
).toEqual(original);
66+
sortNotes(notes, NotesOrder.Custom, ["Article", "Todo", "Clipboard", "Shopping"]).map(nameMap),
67+
).toEqual([
68+
"Todo", "Clipboard",
69+
"Article", "Shopping",
70+
]);
71+
});
5972

60-
expect(
61-
sortNotes(notes, NotesOrder.Custom, []).map(nameMap), // "custom" is empty
62-
).toEqual(original);
73+
test("sortNotes() only groups by pinned and unpinned when no order is provided", () => {
74+
[undefined, []].forEach((order) => {
75+
expect(
76+
sortNotes(notes, NotesOrder.Custom, order).map(nameMap),
77+
).toEqual([
78+
"Clipboard", "Todo",
79+
"Article", "Shopping",
80+
]);
81+
});
6382
});

0 commit comments

Comments
 (0)