Skip to content

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

web: add feature limit checks to tag & notebook functions

Abdullah Atta05795ca05233
diff --git a/apps/web/src/common/index.ts b/apps/web/src/common/index.ts
index ef69cf90b..a1e3cd44e 100644
--- a/apps/web/src/common/index.ts
+++ b/apps/web/src/common/index.ts
@@ -20,9 +20,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import Config from "../utils/config";
import { hashNavigate, getCurrentHash } from "../navigation";
import { db } from "./db";
-import { isFeatureAvailable, sanitizeFilename } from "@notesnook/common";
+import {
+ areFeaturesAvailable,
+ FeatureId,
+ FeatureResult,
+ isFeatureAvailable,
+ sanitizeFilename
+} from "@notesnook/common";
import { useStore as useUserStore } from "../stores/user-store";
-import { HomePage, useStore as useSettingStore } from "../stores/setting-store";
+import { useStore as useAppStore } from "../stores/app-store";
+import {
+ HomePage,
+ ImageCompressionOptions,
+ useStore as useSettingStore
+} from "../stores/setting-store";
import { showToast } from "../utils/toast";
import { SUBSCRIPTION_STATUS } from "./constants";
import { readFile, showFilePicker } from "../utils/file-picker";
@@ -50,6 +61,8 @@ import { ConfirmDialog, showLogoutConfirmation } from "../dialogs/confirm";
import { Home } from "../components/icons";
import { MenuItem } from "@notesnook/ui";
import { showFeatureNotAllowedToast } from "./toasts";
+import { UpgradeDialog } from "../dialogs/buy-dialog/upgrade-dialog";
+import { setToolbarPreset } from "./toolbar-config";
export const CREATE_BUTTON_MAP = {
notes: {
@@ -459,7 +472,8 @@ export async function logout() {
export function createSetDefaultHomepageMenuItem(
id: string,
- type: HomePage["type"]
+ type: HomePage["type"],
+ availability: FeatureResult<"customHomepage">
) {
const homepage = useSettingStore.getState().homepage;
return {
@@ -467,15 +481,89 @@ export function createSetDefaultHomepageMenuItem(
type: "button",
title: strings.setAsHomepage(),
isChecked: homepage?.id === id && homepage?.type === type,
- onClick: async () => {
+ premium: !availability.isAllowed,
+ onClick: withFeatureCheck(availability, async () => {
if (homepage?.id === id && homepage?.type === type)
useSettingStore.getState().setHomepage();
else {
- const result = await isFeatureAvailable("customHomepage");
- if (!result.isAllowed) return showFeatureNotAllowedToast(result);
useSettingStore.getState().setHomepage({ id, type });
}
- },
+ }),
icon: Home.path
} as MenuItem;
}
+
+export async function checkFeature<TId extends FeatureId>(
+ idOrFeature: TId | FeatureResult<TId>,
+ { type = "dialog", value }: { value?: number; type?: "toast" | "dialog" } = {}
+) {
+ const result =
+ typeof idOrFeature === "object"
+ ? idOrFeature
+ : await isFeatureAvailable(idOrFeature, value);
+ if (!result.isAllowed) {
+ type === "dialog"
+ ? await UpgradeDialog.show({ feature: result })
+ : showFeatureNotAllowedToast(result);
+ return false;
+ }
+ return true;
+}
+
+export function withFeatureCheck<TId extends FeatureId>(
+ idOrFeature: TId | FeatureResult<TId> | undefined,
+ callback: (...args: any[]) => Promise<void> | void
+) {
+ return async (...args: any[]) => {
+ if (idOrFeature && !(await checkFeature(idOrFeature))) return;
+ await callback(...args);
+ };
+}
+
+async function resetFeatures() {
+ const features = await areFeaturesAvailable([
+ "customHomepage",
+ "customToolbarPreset",
+ "markdownShortcuts",
+ "fontLigatures",
+ "fullQualityImages",
+ "defaultSidebarTab",
+ "disableTrashCleanup",
+ "syncControls",
+ "fullOfflineMode"
+ ]);
+
+ if (!features.customHomepage.isAllowed)
+ useSettingStore.getState().setHomepage();
+
+ if (!features.customToolbarPreset.isAllowed)
+ await setToolbarPreset("default");
+
+ if (!features.markdownShortcuts.isAllowed)
+ useSettingStore.getState().toggleMarkdownShortcuts(false);
+
+ if (!features.fontLigatures.isAllowed)
+ useSettingStore.getState().toggleFontLigatures(false);
+
+ if (!features.fullQualityImages.isAllowed)
+ useSettingStore
+ .getState()
+ .setImageCompression(ImageCompressionOptions.ENABLE);
+
+ if (!features.defaultSidebarTab.isAllowed)
+ useSettingStore.getState().setDefaultSidebarTab("home");
+
+ if (!features.disableTrashCleanup.isAllowed)
+ useSettingStore.getState().setTrashCleanupInterval(7);
+
+ if (!features.syncControls.isAllowed) {
+ useAppStore.setState({
+ isSyncEnabled: true,
+ isAutoSyncEnabled: true,
+ isRealtimeSyncEnabled: true
+ });
+ }
+
+ if (!features.fullOfflineMode.isAllowed)
+ useSettingStore.getState().toggleFullOfflineMode(false);
+}
diff --git a/apps/web/src/components/notebook/index.tsx b/apps/web/src/components/notebook/index.tsx
index 5864f2a30..66cc848ae 100644
--- a/apps/web/src/components/notebook/index.tsx
+++ b/apps/web/src/components/notebook/index.tsx
@@ -44,11 +44,13 @@ import { store as appStore } from "../../stores/app-store";
import { Multiselect } from "../../common/multi-select";
import { strings } from "@notesnook/intl";
import { db } from "../../common/db";
-import { createSetDefaultHomepageMenuItem } from "../../common";
+import {
+ createSetDefaultHomepageMenuItem,
+ withFeatureCheck
+} from "../../common";
import { useStore as useNotebookStore } from "../../stores/notebook-store";
import { MoveNotebookDialog } from "../../dialogs/move-notebook-dialog";
-import { isFeatureAvailable } from "@notesnook/common";
-import { showFeatureNotAllowedToast } from "../../common/toasts";
+import { areFeaturesAvailable } from "@notesnook/common";
type NotebookProps = {
item: NotebookType;
@@ -182,8 +184,13 @@ export const notebookMenuItems: (
notebook: NotebookType,
ids?: string[],
context?: { refresh?: () => void; isRoot?: boolean }
-) => MenuItem[] = (notebook, ids = [], context) => {
+) => Promise<MenuItem[]> = async (notebook, ids = [], context) => {
const defaultNotebook = db.settings.getDefaultNotebook();
+ const features = await areFeaturesAvailable([
+ "shortcuts",
+ "defaultNotebookAndTag",
+ "customHomepage"
+ ]);
return [
{
type: "button",
@@ -209,25 +216,20 @@ export const notebookMenuItems: (
title: strings.setAsDefault(),
isChecked: defaultNotebook === notebook.id,
icon: NotebookIcon.path,
- onClick: async () => {
+ premium: !features.defaultNotebookAndTag.isAllowed,
+ onClick: withFeatureCheck(features.defaultNotebookAndTag, async () => {
const defaultNotebook = db.settings.getDefaultNotebook();
const isDefault = defaultNotebook === notebook.id;
- if (!isDefault) {
- const result = await isFeatureAvailable("defaultNotebookAndTag");
- if (!result.isAllowed) return showFeatureNotAllowedToast(result);
- }
await db.settings.setDefaultNotebook(
isDefault ? undefined : notebook.id
);
- }
- },
- {
- type: "lazy-loader",
- key: "sidebar-items-loader",
- items: async () => [
- createSetDefaultHomepageMenuItem(notebook.id, notebook.type)
- ]
+ })
},
+ createSetDefaultHomepageMenuItem(
+ notebook.id,
+ notebook.type,
+ features.customHomepage
+ ),
{
type: "button",
key: "shortcut",
@@ -237,7 +239,10 @@ export const notebookMenuItems: (
title: db.shortcuts.exists(notebook.id)
? strings.removeShortcut()
: strings.addShortcut(),
- onClick: () => appStore.addToShortcuts(notebook)
+ premium: !features.shortcuts.isAllowed,
+ onClick: withFeatureCheck(features.shortcuts, () =>
+ appStore.addToShortcuts(notebook)
+ )
},
{ key: "sep1", type: "separator" },
{
diff --git a/apps/web/src/components/tag/index.tsx b/apps/web/src/components/tag/index.tsx
index 688d8c853..92151fa36 100644
--- a/apps/web/src/components/tag/index.tsx
+++ b/apps/web/src/components/tag/index.tsx
@@ -31,9 +31,11 @@ import { useStore as useSelectionStore } from "../../stores/selection-store";
import { useStore as useNoteStore } from "../../stores/note-store";
import { Multiselect } from "../../common/multi-select";
import { strings } from "@notesnook/intl";
-import { createSetDefaultHomepageMenuItem } from "../../common";
-import { isFeatureAvailable } from "@notesnook/common";
-import { showFeatureNotAllowedToast } from "../../common/toasts";
+import {
+ createSetDefaultHomepageMenuItem,
+ withFeatureCheck
+} from "../../common";
+import { areFeaturesAvailable } from "@notesnook/common";
type TagProps = { item: TagType; totalNotes: number };
function Tag(props: TagProps) {
@@ -103,11 +105,16 @@ function Tag(props: TagProps) {
}
export default Tag;
-export const tagMenuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (
- tag,
- ids = []
-) => {
+export const tagMenuItems: (
+ tag: TagType,
+ ids?: string[]
+) => Promise<MenuItem[]> = async (tag, ids = []) => {
const defaultTag = db.settings.getDefaultTag();
+ const features = await areFeaturesAvailable([
+ "shortcuts",
+ "defaultNotebookAndTag",
+ "customHomepage"
+ ]);
return [
{
type: "button",
@@ -122,21 +129,14 @@ export const tagMenuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (
title: strings.setAsDefault(),
isChecked: defaultTag === tag.id,
icon: TagIcon.path,
- onClick: async () => {
+ premium: !features.defaultNotebookAndTag.isAllowed,
+ onClick: withFeatureCheck(features.defaultNotebookAndTag, async () => {
const defaultTag = db.settings.getDefaultTag();
const isDefault = defaultTag === tag.id;
- if (!isDefault) {
- const result = await isFeatureAvailable("defaultNotebookAndTag");
- if (!result.isAllowed) return showFeatureNotAllowedToast(result);
- }
await db.settings.setDefaultTag(isDefault ? undefined : tag.id);
- }
- },
- {
- type: "lazy-loader",
- key: "sidebar-items-loader",
- items: async () => [createSetDefaultHomepageMenuItem(tag.id, tag.type)]
+ })
},
+ createSetDefaultHomepageMenuItem(tag.id, tag.type, features.customHomepage),
{
type: "button",
key: "shortcut",
@@ -144,7 +144,10 @@ export const tagMenuItems: (tag: TagType, ids?: string[]) => MenuItem[] = (
? strings.removeShortcut()
: strings.addShortcut(),
icon: Shortcut.path,
- onClick: () => appStore.addToShortcuts(tag)
+ premium: !features.shortcuts.isAllowed,
+ onClick: withFeatureCheck(features.shortcuts, () =>
+ appStore.addToShortcuts(tag)
+ )
},
{ key: "sep", type: "separator" },
{