2022-08-30 09:59:53 +05:00
|
|
|
import { groupArray } from "@notesnook/core/utils/grouping";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { qclone } from "qclone";
|
2022-08-29 16:19:17 +05:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { db } from "../../common/database";
|
|
|
|
|
import { FloatingButton } from "../../components/container/floating-button";
|
|
|
|
|
import DelayLayout from "../../components/delay-layout";
|
|
|
|
|
import List from "../../components/list";
|
|
|
|
|
import { NotebookHeader } from "../../components/list-items/headers/notebook-header";
|
|
|
|
|
import { useNavigationFocus } from "../../hooks/use-navigation-focus";
|
|
|
|
|
import {
|
|
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent
|
|
|
|
|
} from "../../services/event-manager";
|
|
|
|
|
import Navigation, {
|
|
|
|
|
NavigationProps,
|
|
|
|
|
NotebookScreenParams
|
|
|
|
|
} from "../../services/navigation";
|
|
|
|
|
import SearchService from "../../services/search";
|
|
|
|
|
import useNavigationStore from "../../stores/use-navigation-store";
|
|
|
|
|
import {
|
|
|
|
|
eOnNewTopicAdded,
|
|
|
|
|
eOpenAddNotebookDialog,
|
|
|
|
|
eOpenAddTopicDialog
|
|
|
|
|
} from "../../utils/events";
|
|
|
|
|
import { NotebookType } from "../../utils/types";
|
|
|
|
|
const Notebook = ({ route, navigation }: NavigationProps<"Notebook">) => {
|
2022-04-24 05:59:14 +05:00
|
|
|
const [topics, setTopics] = useState(
|
2022-08-26 16:19:39 +05:00
|
|
|
groupArray(
|
|
|
|
|
qclone(route?.params.item?.topics) || [],
|
|
|
|
|
db.settings?.getGroupOptions("topics")
|
|
|
|
|
)
|
2022-04-24 05:59:14 +05:00
|
|
|
);
|
|
|
|
|
const params = useRef<NotebookScreenParams>(route?.params);
|
|
|
|
|
useNavigationFocus(navigation, {
|
|
|
|
|
onFocus: () => {
|
|
|
|
|
Navigation.routeNeedsUpdate(route.name, onRequestUpdate);
|
|
|
|
|
syncWithNavigation();
|
2022-08-15 18:18:22 +05:00
|
|
|
useNavigationStore.getState().setButtonAction(onPressFloatingButton);
|
2022-04-24 05:59:14 +05:00
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
onBlur: () => false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const syncWithNavigation = () => {
|
|
|
|
|
useNavigationStore.getState().update(
|
|
|
|
|
{
|
|
|
|
|
name: route.name,
|
|
|
|
|
title: params.current?.title,
|
|
|
|
|
id: params.current?.item?.id,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "notebook"
|
2022-04-24 05:59:14 +05:00
|
|
|
},
|
|
|
|
|
params.current?.canGoBack
|
|
|
|
|
);
|
|
|
|
|
SearchService.prepareSearch = prepareSearch;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onRequestUpdate = (data?: NotebookScreenParams) => {
|
|
|
|
|
if (data) params.current = data;
|
|
|
|
|
params.current.title = params.current.item.title;
|
|
|
|
|
try {
|
2022-08-27 15:23:11 +05:00
|
|
|
const notebook = db.notebooks?.notebook(params?.current?.item?.id)
|
2022-08-26 16:19:39 +05:00
|
|
|
?.data as NotebookType;
|
2022-04-24 05:59:14 +05:00
|
|
|
if (notebook) {
|
|
|
|
|
params.current.item = notebook;
|
2022-08-26 16:19:39 +05:00
|
|
|
setTopics(
|
|
|
|
|
groupArray(
|
|
|
|
|
qclone(notebook.topics),
|
|
|
|
|
db.settings?.getGroupOptions("topics")
|
|
|
|
|
)
|
|
|
|
|
);
|
2022-04-24 05:59:14 +05:00
|
|
|
syncWithNavigation();
|
|
|
|
|
}
|
2022-08-27 15:23:11 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2022-04-24 05:59:14 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
eSubscribeEvent(eOnNewTopicAdded, onRequestUpdate);
|
|
|
|
|
return () => {
|
|
|
|
|
eUnSubscribeEvent(eOnNewTopicAdded, onRequestUpdate);
|
|
|
|
|
};
|
|
|
|
|
}, [topics]);
|
|
|
|
|
|
|
|
|
|
const prepareSearch = () => {
|
|
|
|
|
SearchService.update({
|
|
|
|
|
placeholder: `Search in "${params.current.title}"`,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "topics",
|
2022-04-24 05:59:14 +05:00
|
|
|
title: params.current.title,
|
|
|
|
|
get: () => {
|
2022-08-27 15:23:11 +05:00
|
|
|
const notebook = db.notebooks?.notebook(params?.current?.item?.id)
|
2022-08-26 16:19:39 +05:00
|
|
|
?.data as NotebookType;
|
2022-04-24 05:59:14 +05:00
|
|
|
return notebook?.topics;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onPressFloatingButton = () => {
|
2022-08-27 15:23:11 +05:00
|
|
|
const n = params.current.item;
|
2022-04-24 05:59:14 +05:00
|
|
|
eSendEvent(eOpenAddTopicDialog, { notebookId: n.id });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const PLACEHOLDER_DATA = {
|
|
|
|
|
heading: params.current.item?.title,
|
2022-08-26 16:19:39 +05:00
|
|
|
paragraph: "You have not added any topics yet.",
|
|
|
|
|
button: "Add first topic",
|
2022-04-24 05:59:14 +05:00
|
|
|
action: onPressFloatingButton,
|
2022-08-26 16:19:39 +05:00
|
|
|
loading: "Loading notebook topics"
|
2022-04-24 05:59:14 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2022-06-13 10:55:34 +05:00
|
|
|
<DelayLayout>
|
2022-04-24 05:59:14 +05:00
|
|
|
<List
|
|
|
|
|
listData={topics}
|
|
|
|
|
type="topics"
|
|
|
|
|
refreshCallback={() => {
|
|
|
|
|
onRequestUpdate();
|
|
|
|
|
}}
|
|
|
|
|
screen="Notebook"
|
|
|
|
|
headerProps={{
|
|
|
|
|
heading: params.current.title
|
|
|
|
|
}}
|
|
|
|
|
ListHeader={
|
|
|
|
|
<NotebookHeader
|
|
|
|
|
onEditNotebook={() => {
|
|
|
|
|
eSendEvent(eOpenAddNotebookDialog, params.current.item);
|
|
|
|
|
}}
|
|
|
|
|
notebook={params.current.item}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
placeholderData={PLACEHOLDER_DATA}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<FloatingButton title="Add new topic" onPress={onPressFloatingButton} />
|
2022-06-13 10:55:34 +05:00
|
|
|
</DelayLayout>
|
2022-04-24 05:59:14 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notebook.navigate = (item: NotebookType, canGoBack: boolean) => {
|
|
|
|
|
if (!item) return;
|
2022-08-26 16:19:39 +05:00
|
|
|
Navigation.navigate<"Notebook">(
|
2022-04-24 05:59:14 +05:00
|
|
|
{
|
|
|
|
|
title: item.title,
|
2022-08-26 16:19:39 +05:00
|
|
|
name: "Notebook",
|
2022-04-24 05:59:14 +05:00
|
|
|
id: item.id,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "notebook"
|
2022-04-24 05:59:14 +05:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: item.title,
|
|
|
|
|
item: item,
|
|
|
|
|
canGoBack
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Notebook;
|