Files
notesnook/apps/mobile/app/hooks/use-notebook.ts

97 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-11-16 08:54:37 +05:00
/*
This file is part of the Notesnook project (https://notesnook.com/)
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/>.
*/
import { Notebook, VirtualizedGrouping } from "@notesnook/core";
2023-12-16 11:30:41 +05:00
import React, { useEffect, useState } from "react";
2023-11-16 08:54:37 +05:00
import { db } from "../common/database";
import { eSubscribeEvent, eUnSubscribeEvent } from "../services/event-manager";
2023-11-24 15:11:38 +05:00
import { eGroupOptionsUpdated, eOnNotebookUpdated } from "../utils/events";
2023-11-16 08:54:37 +05:00
import { useDBItem, useTotalNotes } from "./use-db-item";
export const useNotebook = (
2023-12-08 10:13:43 +05:00
id?: string | number,
items?: VirtualizedGrouping<Notebook>,
nestedNotebooks?: boolean
2023-11-16 08:54:37 +05:00
) => {
const [item, refresh] = useDBItem(id, "notebook", items);
2023-12-16 11:30:41 +05:00
const groupOptions = db.settings.getGroupOptions("notebooks");
2023-11-16 08:54:37 +05:00
const [notebooks, setNotebooks] = useState<VirtualizedGrouping<Notebook>>();
2023-12-08 10:13:43 +05:00
const { totalNotes: nestedNotebookNotesCount, getTotalNotes } =
useTotalNotes("notebook");
2023-11-16 08:54:37 +05:00
const onRequestUpdate = React.useCallback(() => {
2023-12-08 10:13:43 +05:00
if (!item?.id) return;
const selector = db.relations.from(
{
type: "notebook",
id: item.id
},
"notebook"
).selector;
selector.ids().then((notebookIds) => {
getTotalNotes(notebookIds);
});
selector
.sorted(db.settings.getGroupOptions("notebooks"))
2023-11-16 08:54:37 +05:00
.then((notebooks) => {
setNotebooks(notebooks);
});
2023-12-08 10:13:43 +05:00
}, [getTotalNotes, item?.id]);
2023-11-16 08:54:37 +05:00
useEffect(() => {
2023-12-08 10:13:43 +05:00
if (nestedNotebooks) {
onRequestUpdate();
}
}, [item?.id, onRequestUpdate, nestedNotebooks]);
2023-11-16 08:54:37 +05:00
useEffect(() => {
const onNotebookUpdate = (id?: string) => {
if (typeof id === "string" && id !== id) return;
setImmediate(() => {
2023-12-08 10:13:43 +05:00
if (nestedNotebooks) {
onRequestUpdate();
}
2023-11-16 08:54:37 +05:00
refresh();
});
};
2023-12-16 11:30:41 +05:00
const onUpdate = (type: string) => {
if (type !== "notebooks") return;
onRequestUpdate();
};
2023-11-24 15:11:38 +05:00
eSubscribeEvent(eGroupOptionsUpdated, onUpdate);
2023-11-16 08:54:37 +05:00
eSubscribeEvent(eOnNotebookUpdated, onNotebookUpdate);
return () => {
2023-11-24 15:11:38 +05:00
eUnSubscribeEvent(eGroupOptionsUpdated, onUpdate);
2023-11-16 08:54:37 +05:00
eUnSubscribeEvent(eOnNotebookUpdated, onNotebookUpdate);
};
2023-12-16 11:30:41 +05:00
}, [onRequestUpdate, item?.id, refresh, nestedNotebooks]);
2023-11-16 08:54:37 +05:00
return {
notebook: item,
nestedNotebookNotesCount,
2023-11-24 15:11:38 +05:00
nestedNotebooks: item ? notebooks : undefined,
2023-11-16 08:54:37 +05:00
onUpdate: onRequestUpdate,
groupOptions
};
};