A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
web: allow notebook/tag level group&sort preferences Signed-off-by: 01zulfi <[email protected]>
01zulfi27b00d4ef9ff
diff --git a/apps/web/src/components/group-header/index.tsx b/apps/web/src/components/group-header/index.tsxindex 1050215a8..ce854b3ba 100644--- a/apps/web/src/components/group-header/index.tsx+++ b/apps/web/src/components/group-header/index.tsx@@ -45,6 +45,7 @@ import {} from "@notesnook/core";import { strings } from "@notesnook/intl";import { useStore as useSearchStore } from "../../stores/search-store";+import type { Context } from "../list-container/types";const groupByToTitleMap = {none: "None",@@ -61,6 +62,7 @@ type GroupingMenuOptions = {groupingKey: GroupingKey;refresh: () => void;isSearching?: boolean;+ context?: Context;};const groupByMenu: (options: GroupingMenuOptions) => MenuItem | null = (@@ -192,6 +194,33 @@ export function showSortMenu(groupingKey: GroupingKey, refresh: () => void) {);}+function getGroupOptions(+ context: Context | undefined,+ isSearching: boolean | undefined,+ groupingKey: GroupingKey+): GroupOptions {+ return isSearching+ ? db.settings.getGroupOptions("search")+ : context?.type === "notebook"+ ? db.settings.getNotebookGroupOptions(context.id)+ : context?.type === "tag"+ ? db.settings.getTagGroupOptions(context.id)+ : db.settings.getGroupOptions(groupingKey);+}++async function setGroupOptions(+ options: GroupingMenuOptions,+ groupOptions: GroupOptions+) {+ if (options.context?.type === "notebook") {+ await db.settings.setNotebookGroupOptions(options.context.id, groupOptions);+ } else if (options.context?.type === "tag") {+ await db.settings.setTagGroupOptions(options.context.id, groupOptions);+ } else {+ await db.settings.setGroupOptions(options.groupingKey, groupOptions);+ }+}+async function changeGroupOptions(options: GroupingMenuOptions,item: Omit<MenuButtonItem, "type">@@ -207,7 +236,9 @@ async function changeGroupOptions(? "dateModified": groupOptions.sortBy;}- await db.settings.setGroupOptions(options.groupingKey, groupOptions);++ await setGroupOptions(options, groupOptions);+if (options.groupingKey === "search")useSearchStore.setState({ sortOptions: groupOptions });options.refresh();@@ -235,6 +266,7 @@ type GroupHeaderProps = {onSelectGroup: () => void;isFocused: boolean;isSearching?: boolean;+ context?: Context;};function GroupHeader(props: GroupHeaderProps) {const {@@ -246,10 +278,12 @@ function GroupHeader(props: GroupHeaderProps) {refresh,onSelectGroup,isFocused,- isSearching+ isSearching,+ context} = props;+const [groupOptions, setGroupOptions] = useState(- db.settings.getGroupOptions(isSearching ? "search" : groupingKey)+ getGroupOptions(context, isSearching, groupingKey));const groupHeaderRef = useRef<HTMLDivElement>(null);const { openMenu, target } = useMenuTrigger();@@ -359,8 +393,10 @@ function GroupHeader(props: GroupHeaderProps) {groupByToTitleMap[groupOptions.groupBy || "default"]}`}onClick={() => {- const groupOptions = db.settings.getGroupOptions(- isSearching ? "search" : groupingKey+ const groupOptions = getGroupOptions(+ context,+ isSearching,+ groupingKey);setGroupOptions(groupOptions);@@ -368,7 +404,8 @@ function GroupHeader(props: GroupHeaderProps) {groupingKey: isSearching ? "search" : groupingKey,groupOptions,refresh,- isSearching+ isSearching,+ context};const groupBy = groupByMenu({...menuOptions,diff --git a/apps/web/src/components/list-container/index.tsx b/apps/web/src/components/list-container/index.tsxindex db35aa79c..83af42828 100644--- a/apps/web/src/components/list-container/index.tsx+++ b/apps/web/src/components/list-container/index.tsx@@ -370,6 +370,7 @@ function ItemRenderer({title={resolvedItem.group.title}isFocused={index === focusedGroupIndex}index={index}+ context={itemContext}onSelectGroup={async () => {if (!items.groups) return;diff --git a/apps/web/src/stores/note-store.ts b/apps/web/src/stores/note-store.tsindex fe1fe0e32..27b6b2d3c 100644--- a/apps/web/src/stores/note-store.ts+++ b/apps/web/src/stores/note-store.ts@@ -57,18 +57,23 @@ class NoteStore extends BaseStore<NoteStore> {};setContext = async (context?: Context) => {+ const groupOptions =+ context?.type === "notebook"+ ? db.settings.getNotebookGroupOptions(context.id)+ : context?.type === "tag"+ ? db.settings.getTagGroupOptions(context.id)+ : db.settings.getGroupOptions(+ context?.type === "favorite"+ ? "favorites"+ : context?.type === "archive"+ ? "archive"+ : "notes"+ );+this.set({context,contextNotes: context- ? await notesFromContext(context).grouped(- db.settings.getGroupOptions(- context.type === "favorite"- ? "favorites"- : context.type === "archive"- ? "archive"- : "notes"- )- )+ ? await notesFromContext(context).grouped(groupOptions): undefined});};diff --git a/packages/core/__tests__/settings.test.js b/packages/core/__tests__/settings.test.jsindex e1095b99f..3c335ee18 100644--- a/packages/core/__tests__/settings.test.js+++ b/packages/core/__tests__/settings.test.js@@ -47,3 +47,45 @@ test("save trash cleanup interval", () =>await db.settings.setTrashCleanupInterval(interval);expect(db.settings.getTrashCleanupInterval()).toBe(interval);}));++test("get notebook group options", () =>+ databaseTest().then(async (db) => {+ const notebookId = "test-notebook-id";+ const groupOptions = {+ groupBy: "year",+ sortBy: "title",+ sortDirection: "asc"+ };+ await db.settings.setNotebookGroupOptions(notebookId, groupOptions);+ expect(db.settings.getNotebookGroupOptions(notebookId)).toMatchObject(+ groupOptions+ );+ }));++test("get notebook group options fallback to notes options", () =>+ databaseTest().then(async (db) => {+ const notebookId = "non-existent-notebook-id";+ const defaultNotesOptions = db.settings.getGroupOptions("notes");+ const result = db.settings.getNotebookGroupOptions(notebookId);+ expect(result).toMatchObject(defaultNotesOptions);+ }));++test("get tag group options", () =>+ databaseTest().then(async (db) => {+ const tagId = "test-tag-id";+ const groupOptions = {+ groupBy: "year",+ sortBy: "title",+ sortDirection: "asc"+ };+ await db.settings.setTagGroupOptions(tagId, groupOptions);+ expect(db.settings.getTagGroupOptions(tagId)).toMatchObject(groupOptions);+ }));++test("get tag group options fallback to notes options", () =>+ databaseTest().then(async (db) => {+ const tagId = "non-existent-tag-id";+ const defaultTagsOptions = db.settings.getGroupOptions("notes");+ const result = db.settings.getTagGroupOptions(tagId);+ expect(result).toMatchObject(defaultTagsOptions);+ }));diff --git a/packages/core/src/collections/settings.ts b/packages/core/src/collections/settings.tsindex 3e14c11d1..ac90cb7ba 100644--- a/packages/core/src/collections/settings.ts+++ b/packages/core/src/collections/settings.ts@@ -64,6 +64,8 @@ const defaultSettings: SettingItemMap = {trashCleanupInterval: 7,profile: undefined,+ "groupOptions:notes:notebooks": {},+ "groupOptions:notes:tags": {},"groupOptions:trash": DEFAULT_GROUP_OPTIONS("trash"),"groupOptions:tags": DEFAULT_GROUP_OPTIONS("tags"),"groupOptions:notes": DEFAULT_GROUP_OPTIONS("notes"),@@ -150,6 +152,31 @@ export class Settings implements ICollection {return this.set(`groupOptions:${key}`, groupOptions);}+ getNotebookGroupOptions(notebookId: string) {+ const notebookOptions = this.get("groupOptions:notes:notebooks");+ return notebookOptions[notebookId] || this.get("groupOptions:notes");+ }++ async setNotebookGroupOptions(+ notebookId: string,+ groupOptions: GroupOptions+ ) {+ const notebookOptions = this.get("groupOptions:notes:notebooks");+ notebookOptions[notebookId] = groupOptions;+ return this.set("groupOptions:notes:notebooks", notebookOptions);+ }++ getTagGroupOptions(tagId: string) {+ const tagOptions = this.get("groupOptions:notes:tags");+ return tagOptions[tagId] || this.get("groupOptions:notes");+ }++ async setTagGroupOptions(tagId: string, groupOptions: GroupOptions) {+ const tagOptions = this.get("groupOptions:notes:tags");+ tagOptions[tagId] = groupOptions;+ return this.set("groupOptions:notes:tags", tagOptions);+ }+setToolbarConfig(platform: ToolbarConfigPlatforms, config: ToolbarConfig) {return this.set(`toolbarConfig:${platform}`, config);}diff --git a/packages/core/src/types.ts b/packages/core/src/types.tsindex 896d4653d..3e1e94778 100644--- a/packages/core/src/types.ts+++ b/packages/core/src/types.ts@@ -486,6 +486,10 @@ export type SettingItemMap = {defaultTag: string | undefined;profile: Profile | undefined;} & Record<`groupOptions:${GroupingKey}`, GroupOptions> &+ Record<+ `groupOptions:notes:notebooks` | `groupOptions:notes:tags`,+ Record<string, GroupOptions>+ > &Record<`toolbarConfig:${ToolbarConfigPlatforms}`, ToolbarConfig | undefined> &Record<`sideBarOrder:${SideBarSection}`, string[]> &Record<`sideBarHiddenItems:${SideBarHideableSection}`, string[]>;