Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix restore notebooks over limit for free users (#6812)

01zulfi55b441d14e44
diff --git a/apps/web/src/components/trash-item/index.tsx b/apps/web/src/components/trash-item/index.tsx
index 95d4b78c1..7a9c6a137 100644
--- a/apps/web/src/components/trash-item/index.tsx
+++ b/apps/web/src/components/trash-item/index.tsx
@@ -83,7 +83,6 @@ const menuItems: (item: TrashItemType, ids?: string[]) => MenuItem[] = (
icon: Restore.path,
onClick: async () => {
await store.restore(...ids);
- showToast("success", strings.action("item", ids.length, "restored"));
},
multiSelect: true
},
diff --git a/apps/web/src/stores/trash-store.ts b/apps/web/src/stores/trash-store.ts
index da3afbfd3..ae4e8d3b6 100644
--- a/apps/web/src/stores/trash-store.ts
+++ b/apps/web/src/stores/trash-store.ts
@@ -24,6 +24,8 @@ import { store as appStore } from "./app-store";
import { store as noteStore } from "./note-store";
import { store as notebookStore } from "./notebook-store";
import { TrashItem, VirtualizedGrouping } from "@notesnook/core";
+import { showToast } from "../utils/toast";
+import { strings } from "@notesnook/intl";
class TrashStore extends BaseStore<TrashStore> {
trash: VirtualizedGrouping<TrashItem> | undefined = undefined;
@@ -41,7 +43,9 @@ class TrashStore extends BaseStore<TrashStore> {
};
restore = async (...ids: string[]) => {
- await db.trash.restore(...ids);
+ const restored = await db.trash.restore(...ids);
+ if (restored === false) return;
+ showToast("success", strings.action("item", ids.length, "restored"));
await this.get().refresh();
await appStore.refreshNavItems();
await noteStore.refresh();
diff --git a/packages/core/src/collections/notebooks.ts b/packages/core/src/collections/notebooks.ts
index 57a0b0269..392e1d015 100644
--- a/packages/core/src/collections/notebooks.ts
+++ b/packages/core/src/collections/notebooks.ts
@@ -25,7 +25,11 @@ import { SQLCollection } from "../database/sql-collection.js";
import { isFalse } from "../database/index.js";
import { sql } from "@streetwriters/kysely";
import { deleteItems } from "../utils/array.js";
-import { CHECK_IDS, checkIsUserPremium } from "../common.js";
+import {
+ CHECK_IDS,
+ checkIsUserPremium,
+ FREE_NOTEBOOKS_LIMIT
+} from "../common.js";
export class Notebooks implements ICollection {
name = "notebooks";
@@ -62,7 +66,7 @@ export class Notebooks implements ICollection {
if (
!oldNotebook &&
- (await this.all.count()) >= 20 &&
+ (await this.all.count()) >= FREE_NOTEBOOKS_LIMIT &&
!(await checkIsUserPremium(CHECK_IDS.notebookAdd))
)
return;
diff --git a/packages/core/src/collections/trash.ts b/packages/core/src/collections/trash.ts
index 6612ba206..027a5d1a6 100644
--- a/packages/core/src/collections/trash.ts
+++ b/packages/core/src/collections/trash.ts
@@ -29,6 +29,11 @@ import {
} from "../utils/grouping.js";
import { sql } from "@streetwriters/kysely";
import { MAX_SQL_PARAMETERS } from "../database/sql-collection.js";
+import {
+ CHECK_IDS,
+ checkIsUserPremium,
+ FREE_NOTEBOOKS_LIMIT
+} from "../common.js";
export default class Trash {
collections = ["notes", "notebooks"] as const;
@@ -192,6 +197,13 @@ export default class Trash {
}
if (notebookIds.length > 0) {
+ const notebooksLimitReached =
+ (await this.db.notebooks.all.count()) + notebookIds.length >
+ FREE_NOTEBOOKS_LIMIT;
+ const isUserPremium = await checkIsUserPremium(CHECK_IDS.notebookAdd);
+ if (notebooksLimitReached && !isUserPremium) {
+ return false;
+ }
const ids = [...notebookIds, ...(await this.subNotebooks(notebookIds))];
await this.db.notebooks.collection.update(ids, {
type: "notebook",
diff --git a/packages/core/src/common.ts b/packages/core/src/common.ts
index b7add5b02..5b78d1b78 100644
--- a/packages/core/src/common.ts
+++ b/packages/core/src/common.ts
@@ -142,3 +142,5 @@ export const DATE_FORMATS = [
export const TIME_FORMATS = ["12-hour", "24-hour"];
export const CURRENT_DATABASE_VERSION = 6.1;
+
+export const FREE_NOTEBOOKS_LIMIT = 20;