Files
notesnook/apps/mobile/app/utils/functions.ts

219 lines
6.3 KiB
TypeScript
Raw Permalink Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2024-11-27 13:55:41 +05:00
import { ItemType } from "@notesnook/core";
import { strings } from "@notesnook/intl";
import { Linking } from "react-native";
2023-06-10 12:17:23 +05:00
import { db } from "../common/database";
import { presentDialog } from "../components/dialog/functions";
2025-02-15 12:32:16 +05:00
import {
useSideMenuNotebookSelectionStore,
useSideMenuTagsSelectionStore
} from "../components/side-menu/stores";
2023-08-29 20:42:45 +05:00
import { eSendEvent, ToastManager } from "../services/event-manager";
import Navigation from "../services/navigation";
import { useMenuStore } from "../stores/use-menu-store";
2024-12-06 10:27:42 +05:00
import { useNotebookStore } from "../stores/use-notebook-store";
2023-01-07 16:03:54 +05:00
import { useRelationStore } from "../stores/use-relation-store";
2024-11-27 13:55:41 +05:00
import { useTagStore } from "../stores/use-tag-store";
import { eUpdateNoteInEditor } from "./events";
import { unlockVault } from "./unlock-vault";
2020-11-14 10:02:38 +05:00
export const valueLimiter = (value: number, min: number, max: number) => {
return value < min ? min : value > max ? max : value;
};
export function getObfuscatedEmail(email: string) {
if (!email) return "";
const [username, provider] = email.split("@");
if (username.length === 1) return `****@${provider}`;
return email.replace(/(.{3})(.*)(?=@)/, function (gp1, gp2, gp3) {
for (let i = 0; i < gp3.length; i++) {
gp2 += "*";
}
return gp2;
});
}
2024-11-27 13:55:41 +05:00
function confirmDeleteAllNotes(
items: string[],
type: "notebook",
context?: string
) {
return new Promise<{ delete: boolean; deleteNotes: boolean }>((resolve) => {
presentDialog({
2024-11-27 13:55:41 +05:00
title: strings.doActions.delete.notebook(items.length),
2024-11-07 12:23:21 +05:00
positiveText: strings.delete(),
negativeText: strings.cancel(),
positivePress: async (_inputValue, value) => {
setTimeout(() => {
resolve({ delete: true, deleteNotes: value });
});
},
onClose: () => {
setTimeout(() => {
2024-11-27 13:55:41 +05:00
resolve({ delete: false, deleteNotes: false });
});
},
context: context,
check: {
2024-11-27 13:55:41 +05:00
info: strings.deleteContainingNotes(items.length),
type: "transparent"
}
});
});
}
2024-11-27 13:55:41 +05:00
async function deleteNotebook(id: string, deleteNotes: boolean) {
2023-11-16 08:54:37 +05:00
const notebook = await db.notebooks.notebook(id);
2024-11-27 13:55:41 +05:00
if (!notebook) return;
2023-11-16 08:54:37 +05:00
if (deleteNotes) {
const noteRelations = await db.relations.from(notebook, "note").get();
2023-11-27 16:33:09 +05:00
if (noteRelations?.length) {
2024-02-10 22:30:18 +05:00
await db.notes.moveToTrash(
...noteRelations.map((relation) => relation.toId)
);
2023-11-27 16:33:09 +05:00
}
2023-11-16 08:54:37 +05:00
}
2024-02-10 22:30:18 +05:00
await db.notebooks.moveToTrash(id);
2023-11-16 08:54:37 +05:00
}
2024-11-27 13:55:41 +05:00
export const deleteItems = async (
type: ItemType,
itemIds: string[],
context?: string
) => {
2023-11-24 15:11:38 +05:00
if (type === "reminder") {
2024-11-27 13:55:41 +05:00
await db.reminders.remove(...itemIds);
2023-01-07 16:03:54 +05:00
useRelationStore.getState().update();
2023-11-24 15:11:38 +05:00
} else if (type === "note") {
let someNotesLocked = false;
for (const id of itemIds) {
if (
await db.vaults.itemExists({
id: id,
type: "note"
})
) {
someNotesLocked = true;
break;
}
}
if (someNotesLocked) {
const unlocked = await unlockVault({
title: strings.unlockVault(),
paragraph: strings.unlockVaultDesc(),
context: "global",
requirePassword: true
});
if (!unlocked) return;
}
2024-11-27 13:55:41 +05:00
for (const id of itemIds) {
2023-11-24 15:11:38 +05:00
if (db.monographs.isPublished(id)) {
2023-08-29 20:42:45 +05:00
ToastManager.show({
2024-08-13 15:13:46 +05:00
heading: strings.someNotesPublished(),
message: strings.unpublishToDelete(),
2023-01-07 16:03:54 +05:00
type: "error",
context: "global"
});
continue;
}
2023-11-24 15:11:38 +05:00
await db.notes.moveToTrash(id);
2024-03-14 10:43:25 +05:00
eSendEvent(
eUpdateNoteInEditor,
{
type: "trash",
id: id,
itemType: "note"
},
true
);
2023-01-07 16:03:54 +05:00
}
2023-11-24 15:11:38 +05:00
} else if (type === "notebook") {
2024-11-27 13:55:41 +05:00
const result = await confirmDeleteAllNotes(itemIds, "notebook", context);
2023-09-12 17:07:26 +05:00
if (!result.delete) return;
2024-11-27 13:55:41 +05:00
for (const id of itemIds) {
2023-11-24 15:11:38 +05:00
await deleteNotebook(id, result.deleteNotes);
}
2025-02-15 12:32:16 +05:00
useSideMenuNotebookSelectionStore.setState({
enabled: false,
selection: {}
});
2024-03-22 08:35:35 +05:00
} else if (type === "tag") {
presentDialog({
2024-11-27 13:55:41 +05:00
title: strings.doActions.delete.tag(itemIds.length),
2024-08-13 15:13:46 +05:00
positiveText: strings.delete(),
negativeText: strings.cancel(),
paragraph: strings.actionConfirmations.delete.tag(2),
2024-03-22 08:35:35 +05:00
positivePress: async () => {
2024-11-27 13:55:41 +05:00
await db.tags.remove(...itemIds);
2024-03-22 08:35:35 +05:00
useTagStore.getState().refresh();
useRelationStore.getState().update();
2025-02-15 12:32:16 +05:00
useSideMenuTagsSelectionStore.setState({
enabled: false,
selection: {}
});
2024-03-22 08:35:35 +05:00
},
context: context
});
return;
2020-11-14 12:25:57 +05:00
}
2020-11-14 10:02:38 +05:00
2024-11-27 13:55:41 +05:00
const deletedIds = [...itemIds];
2023-11-24 15:11:38 +05:00
if (type === "notebook" || type === "note") {
2024-11-27 13:55:41 +05:00
const message = strings.actions.movedToTrash[type](itemIds.length);
2023-08-29 20:42:45 +05:00
ToastManager.show({
2021-02-20 15:03:02 +05:00
heading: message,
type: "success",
2021-02-20 15:03:02 +05:00
func: async () => {
await db.trash.restore(...deletedIds);
2023-04-01 14:06:16 +05:00
Navigation.queueRoutesForUpdate();
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setMenuPins();
useMenuStore.getState().setColorNotes();
2023-08-29 20:42:45 +05:00
ToastManager.hide();
2023-11-24 15:11:38 +05:00
if (type === "notebook") {
useNotebookStore.getState().refresh();
2023-11-24 15:11:38 +05:00
}
2020-11-14 12:25:57 +05:00
},
actionText: "Undo"
2021-02-20 15:03:02 +05:00
});
2024-09-05 15:49:36 +05:00
} else {
ToastManager.show({
2024-11-27 13:55:41 +05:00
heading: strings.actions.deleted.unknown(type, itemIds.length),
2024-09-05 15:49:36 +05:00
type: "success"
});
2020-11-14 12:25:57 +05:00
}
2023-11-16 08:54:37 +05:00
2023-04-01 14:06:16 +05:00
Navigation.queueRoutesForUpdate();
2021-06-05 21:10:20 +05:00
useMenuStore.getState().setColorNotes();
2023-11-24 15:11:38 +05:00
if (type === "notebook") {
useNotebookStore.getState().refresh();
2023-11-24 15:11:38 +05:00
}
useMenuStore.getState().setMenuPins();
2021-01-01 15:29:10 +05:00
};
2021-02-10 10:09:48 +05:00
2024-11-27 13:55:41 +05:00
export const openLinkInBrowser = async (link: string) => {
Linking.openURL(link);
2021-02-16 16:11:10 +05:00
};