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

97 lines
3.0 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";
import React, { useCallback, useEffect, useState } from "react";
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 = (
id?: string,
items?: VirtualizedGrouping<Notebook>
) => {
const [item, refresh] = useDBItem(id, "notebook", items);
const [groupOptions, setGroupOptions] = useState(
db.settings.getGroupOptions("notebooks")
);
const [notebooks, setNotebooks] = useState<VirtualizedGrouping<Notebook>>();
const { totalNotes: nestedNotebookNotesCount } = useTotalNotes(
notebooks?.ids as string[],
"notebook"
);
const onRequestUpdate = React.useCallback(() => {
2023-11-24 15:11:38 +05:00
if (!id) return;
console.log("useNotebook.onRequestUpdate", id, Date.now());
2023-11-16 08:54:37 +05:00
db.relations
2023-11-24 15:11:38 +05:00
.from(
{
type: "notebook",
id: id
},
"notebook"
)
2023-11-16 08:54:37 +05:00
.selector.sorted(db.settings.getGroupOptions("notebooks"))
.then((notebooks) => {
setNotebooks(notebooks);
});
2023-11-24 15:11:38 +05:00
}, [id]);
2023-11-16 08:54:37 +05:00
useEffect(() => {
2023-11-24 15:11:38 +05:00
console.log("useNotebook.useEffect.onRequestUpdate");
2023-11-16 08:54:37 +05:00
onRequestUpdate();
}, [onRequestUpdate]);
2023-11-24 15:11:38 +05:00
const onUpdate = useCallback(
(type: string) => {
if (type !== "notebooks") return;
setGroupOptions({ ...(db.settings.getGroupOptions("notebooks") as any) });
onRequestUpdate();
console.log("useNotebook.onUpdate", id, Date.now());
},
[id, onRequestUpdate]
);
2023-11-16 08:54:37 +05:00
useEffect(() => {
const onNotebookUpdate = (id?: string) => {
if (typeof id === "string" && id !== id) return;
setImmediate(() => {
onRequestUpdate();
refresh();
});
};
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-11-22 11:16:15 +05:00
}, [onUpdate, onRequestUpdate, id, refresh]);
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
};
};