2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
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
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-08-29 16:19:17 +05:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { FloatingButton } from "../../components/container/floating-button";
|
|
|
|
|
import DelayLayout from "../../components/delay-layout";
|
|
|
|
|
import List from "../../components/list";
|
|
|
|
|
import { useNavigationFocus } from "../../hooks/use-navigation-focus";
|
|
|
|
|
import {
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent
|
|
|
|
|
} from "../../services/event-manager";
|
2022-08-30 18:27:09 +05:00
|
|
|
import Navigation, { NavigationProps } from "../../services/navigation";
|
2022-08-26 16:19:39 +05:00
|
|
|
import SearchService from "../../services/search";
|
|
|
|
|
import useNavigationStore, {
|
|
|
|
|
HeaderRightButton,
|
2022-08-30 18:27:09 +05:00
|
|
|
NotesScreenParams,
|
2022-08-26 16:19:39 +05:00
|
|
|
RouteName
|
|
|
|
|
} from "../../stores/use-navigation-store";
|
|
|
|
|
import { useNoteStore } from "../../stores/use-notes-store";
|
2022-08-30 13:30:11 +05:00
|
|
|
import { NoteType, TopicType } from "../../utils/types";
|
2022-04-24 05:59:14 +05:00
|
|
|
import {
|
|
|
|
|
getAlias,
|
|
|
|
|
openEditor,
|
|
|
|
|
openMonographsWebpage,
|
|
|
|
|
setOnFirstSave,
|
|
|
|
|
toCamelCase
|
2022-08-26 16:19:39 +05:00
|
|
|
} from "./common";
|
2023-03-16 21:22:21 +05:00
|
|
|
import { View } from "react-native";
|
|
|
|
|
import { db } from "../../common/database";
|
|
|
|
|
import Paragraph from "../../components/ui/typography/paragraph";
|
|
|
|
|
import { IconButton } from "../../components/ui/icon-button";
|
|
|
|
|
import { useThemeStore } from "../../stores/use-theme-store";
|
|
|
|
|
import { SIZE } from "../../utils/size";
|
|
|
|
|
import Notebook from "../notebook/index";
|
2022-04-24 05:59:14 +05:00
|
|
|
export const WARNING_DATA = {
|
2022-08-26 16:19:39 +05:00
|
|
|
title: "Some notes in this topic are not synced"
|
2022-04-24 05:59:14 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const PLACEHOLDER_DATA = {
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Your notes",
|
|
|
|
|
paragraph: "You have not added any notes yet.",
|
|
|
|
|
button: "Add your first Note",
|
2022-04-24 05:59:14 +05:00
|
|
|
action: openEditor,
|
2022-08-26 16:19:39 +05:00
|
|
|
loading: "Loading your notes."
|
2022-04-24 05:59:14 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const MONOGRAPH_PLACEHOLDER_DATA = {
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Your monographs",
|
|
|
|
|
paragraph: "You have not published any notes as monographs yet.",
|
|
|
|
|
button: "Learn more about monographs",
|
2022-04-24 05:59:14 +05:00
|
|
|
action: openMonographsWebpage,
|
2022-08-26 16:19:39 +05:00
|
|
|
loading: "Loading published notes.",
|
|
|
|
|
type: "monographs",
|
|
|
|
|
buttonIcon: "information-outline"
|
2022-04-24 05:59:14 +05:00
|
|
|
};
|
|
|
|
|
|
2022-04-25 00:37:09 +05:00
|
|
|
export interface RouteProps<T extends RouteName> extends NavigationProps<T> {
|
|
|
|
|
get: (params: NotesScreenParams, grouped?: boolean) => NoteType[];
|
2022-08-27 15:23:11 +05:00
|
|
|
placeholderData: unknown;
|
2022-04-24 05:59:14 +05:00
|
|
|
onPressFloatingButton: () => void;
|
|
|
|
|
focusControl?: boolean;
|
|
|
|
|
canGoBack?: boolean;
|
|
|
|
|
rightButtons?: (params: NotesScreenParams) => HeaderRightButton[];
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 20:54:55 +05:00
|
|
|
function getItemType(routeName: RouteName) {
|
2022-08-26 16:19:39 +05:00
|
|
|
if (routeName === "TaggedNotes") return "tag";
|
|
|
|
|
if (routeName === "ColoredNotes") return "color";
|
|
|
|
|
if (routeName === "TopicNotes") return "topic";
|
|
|
|
|
if (routeName === "Monographs") return "monograph";
|
|
|
|
|
return "note";
|
2022-07-08 20:54:55 +05:00
|
|
|
}
|
|
|
|
|
|
2022-04-24 05:59:14 +05:00
|
|
|
const NotesPage = ({
|
|
|
|
|
route,
|
|
|
|
|
navigation,
|
|
|
|
|
get,
|
|
|
|
|
placeholderData,
|
|
|
|
|
onPressFloatingButton,
|
2022-06-13 10:55:34 +05:00
|
|
|
focusControl = true,
|
2022-04-24 05:59:14 +05:00
|
|
|
rightButtons
|
2022-08-26 16:19:39 +05:00
|
|
|
}: RouteProps<
|
|
|
|
|
"NotesPage" | "TaggedNotes" | "Monographs" | "ColoredNotes" | "TopicNotes"
|
|
|
|
|
>) => {
|
2023-03-16 21:22:21 +05:00
|
|
|
const colors = useThemeStore((state) => state.colors);
|
2022-04-24 05:59:14 +05:00
|
|
|
const params = useRef<NotesScreenParams>(route?.params);
|
2022-04-25 00:37:09 +05:00
|
|
|
const [notes, setNotes] = useState<NoteType[]>(get(route.params, true));
|
2022-08-26 16:19:39 +05:00
|
|
|
const loading = useNoteStore((state) => state.loading);
|
2022-06-13 10:55:34 +05:00
|
|
|
const [loadingNotes, setLoadingNotes] = useState(false);
|
2022-04-24 05:59:14 +05:00
|
|
|
const alias = getAlias(params.current);
|
2022-08-26 16:19:39 +05:00
|
|
|
const isMonograph = route.name === "Monographs";
|
2023-03-16 21:22:21 +05:00
|
|
|
const notebook =
|
|
|
|
|
route.name === "TopicNotes" && (params.current.item as TopicType).notebookId
|
|
|
|
|
? db.notebooks?.notebook((params.current.item as TopicType).notebookId)
|
|
|
|
|
?.data
|
|
|
|
|
: null;
|
2022-04-24 05:59:14 +05:00
|
|
|
const isFocused = useNavigationFocus(navigation, {
|
2022-08-26 16:19:39 +05:00
|
|
|
onFocus: (prev) => {
|
2022-04-24 05:59:14 +05:00
|
|
|
Navigation.routeNeedsUpdate(route.name, onRequestUpdate);
|
|
|
|
|
syncWithNavigation();
|
2022-06-11 15:12:50 +05:00
|
|
|
if (focusControl) return !prev.current;
|
2022-04-24 05:59:14 +05:00
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
onBlur: () => {
|
|
|
|
|
setOnFirstSave(null);
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
focusOnInit: !focusControl
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-30 18:27:09 +05:00
|
|
|
const prepareSearch = React.useCallback(() => {
|
|
|
|
|
const { item } = params.current;
|
|
|
|
|
SearchService.update({
|
|
|
|
|
placeholder: `Search in ${alias}`,
|
|
|
|
|
type: "notes",
|
|
|
|
|
title: item.type === "tag" ? "#" + alias : toCamelCase(item.title),
|
|
|
|
|
get: () => {
|
|
|
|
|
return get(params.current, false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, [alias, get]);
|
|
|
|
|
|
|
|
|
|
const syncWithNavigation = React.useCallback(() => {
|
2022-04-24 05:59:14 +05:00
|
|
|
const { item, title } = params.current;
|
2022-08-27 15:23:11 +05:00
|
|
|
const alias = getAlias(params.current);
|
2022-04-24 05:59:14 +05:00
|
|
|
useNavigationStore.getState().update(
|
|
|
|
|
{
|
|
|
|
|
name: route.name,
|
2022-07-08 19:01:51 +05:00
|
|
|
title: alias || title,
|
2022-04-24 05:59:14 +05:00
|
|
|
id: item?.id,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "notes",
|
2022-08-30 13:30:11 +05:00
|
|
|
notebookId: (item as TopicType).notebookId,
|
|
|
|
|
alias:
|
|
|
|
|
route.name === "ColoredNotes" ? toCamelCase(alias as string) : alias,
|
2022-08-26 16:19:39 +05:00
|
|
|
color:
|
|
|
|
|
route.name === "ColoredNotes" ? item.title?.toLowerCase() : undefined
|
2022-04-24 05:59:14 +05:00
|
|
|
},
|
|
|
|
|
params.current.canGoBack,
|
|
|
|
|
rightButtons && rightButtons(params.current)
|
|
|
|
|
);
|
|
|
|
|
SearchService.prepareSearch = prepareSearch;
|
2022-08-15 18:18:22 +05:00
|
|
|
useNavigationStore.getState().setButtonAction(onPressFloatingButton);
|
2022-04-24 05:59:14 +05:00
|
|
|
|
|
|
|
|
!isMonograph &&
|
|
|
|
|
setOnFirstSave({
|
2022-07-08 20:54:55 +05:00
|
|
|
type: getItemType(route.name),
|
2022-04-24 05:59:14 +05:00
|
|
|
id: item.id,
|
|
|
|
|
color: item.title,
|
2022-08-30 13:30:11 +05:00
|
|
|
notebook: (item as TopicType).notebookId
|
2022-04-24 05:59:14 +05:00
|
|
|
});
|
2022-08-30 18:27:09 +05:00
|
|
|
}, [
|
|
|
|
|
isMonograph,
|
|
|
|
|
onPressFloatingButton,
|
|
|
|
|
prepareSearch,
|
|
|
|
|
rightButtons,
|
|
|
|
|
route.name
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const onRequestUpdate = React.useCallback(
|
|
|
|
|
(data?: NotesScreenParams) => {
|
|
|
|
|
const isNew = data && data?.item?.id !== params.current?.item?.id;
|
|
|
|
|
if (data) params.current = data;
|
|
|
|
|
params.current.title = params.current.title || params.current.item.title;
|
|
|
|
|
const { item } = params.current;
|
|
|
|
|
try {
|
|
|
|
|
if (isNew) setLoadingNotes(true);
|
|
|
|
|
const notes = get(params.current, true) as NoteType[];
|
|
|
|
|
if (
|
|
|
|
|
(item.type === "tag" || item.type === "color") &&
|
|
|
|
|
(!notes || notes.length === 0)
|
|
|
|
|
) {
|
|
|
|
|
return Navigation.goBack();
|
|
|
|
|
}
|
2023-03-16 21:22:21 +05:00
|
|
|
if (notes.length === 0) setLoadingNotes(false);
|
2022-08-30 18:27:09 +05:00
|
|
|
setNotes(notes);
|
|
|
|
|
syncWithNavigation();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
2022-04-24 05:59:14 +05:00
|
|
|
}
|
2022-08-30 18:27:09 +05:00
|
|
|
},
|
|
|
|
|
[get, syncWithNavigation]
|
|
|
|
|
);
|
2022-04-24 05:59:14 +05:00
|
|
|
|
2022-06-13 10:55:34 +05:00
|
|
|
useEffect(() => {
|
2022-07-09 17:30:14 +05:00
|
|
|
if (loadingNotes) {
|
2023-03-16 21:22:21 +05:00
|
|
|
setTimeout(() => setLoadingNotes(false), 50);
|
2022-07-09 17:30:14 +05:00
|
|
|
}
|
2022-08-30 18:27:09 +05:00
|
|
|
}, [loadingNotes, notes]);
|
2022-06-13 10:55:34 +05:00
|
|
|
|
2022-04-24 05:59:14 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
eSubscribeEvent(route.name, onRequestUpdate);
|
|
|
|
|
return () => {
|
2022-08-04 19:25:02 +05:00
|
|
|
setOnFirstSave(null);
|
2022-04-24 05:59:14 +05:00
|
|
|
eUnSubscribeEvent(route.name, onRequestUpdate);
|
|
|
|
|
};
|
2022-08-30 18:27:09 +05:00
|
|
|
}, [onRequestUpdate, route.name]);
|
2022-04-24 05:59:14 +05:00
|
|
|
|
|
|
|
|
return (
|
2022-07-08 19:01:51 +05:00
|
|
|
<DelayLayout
|
2022-08-26 16:19:39 +05:00
|
|
|
color={
|
|
|
|
|
route.name === "ColoredNotes"
|
|
|
|
|
? params.current?.item.title.toLowerCase()
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
2022-07-08 19:01:51 +05:00
|
|
|
wait={loading || loadingNotes}
|
|
|
|
|
>
|
2023-03-16 21:22:21 +05:00
|
|
|
{route.name === "TopicNotes" ? (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
paddingHorizontal: 12,
|
|
|
|
|
flexDirection: "row",
|
|
|
|
|
alignItems: "center"
|
|
|
|
|
// borderBottomWidth: 1,
|
|
|
|
|
// borderBottomColor: colors.nav
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Paragraph
|
|
|
|
|
onPress={() => {
|
|
|
|
|
Navigation.navigate(
|
|
|
|
|
{
|
|
|
|
|
name: "Notebooks"
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.xs}
|
|
|
|
|
>
|
|
|
|
|
Notebooks
|
|
|
|
|
</Paragraph>
|
2023-03-31 12:56:20 +05:00
|
|
|
{notebook ? (
|
|
|
|
|
<>
|
|
|
|
|
<IconButton
|
|
|
|
|
name="chevron-right"
|
|
|
|
|
size={14}
|
|
|
|
|
customStyle={{ width: 25, height: 25 }}
|
|
|
|
|
/>
|
|
|
|
|
<Paragraph
|
|
|
|
|
onPress={() => {
|
|
|
|
|
Notebook.navigate(notebook, true);
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.xs}
|
|
|
|
|
>
|
|
|
|
|
{notebook.title}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
2023-03-16 21:22:21 +05:00
|
|
|
</View>
|
|
|
|
|
) : null}
|
2022-04-24 05:59:14 +05:00
|
|
|
<List
|
|
|
|
|
listData={notes}
|
|
|
|
|
type="notes"
|
|
|
|
|
refreshCallback={onRequestUpdate}
|
|
|
|
|
loading={loading || !isFocused}
|
|
|
|
|
screen="Notes"
|
|
|
|
|
headerProps={{
|
|
|
|
|
heading: params.current.title,
|
2022-08-26 16:19:39 +05:00
|
|
|
color:
|
|
|
|
|
route.name === "ColoredNotes"
|
|
|
|
|
? params.current?.item.title.toLowerCase()
|
|
|
|
|
: null
|
2022-04-24 05:59:14 +05:00
|
|
|
}}
|
|
|
|
|
placeholderData={placeholderData}
|
|
|
|
|
/>
|
|
|
|
|
|
2023-03-31 00:55:12 +05:00
|
|
|
{notes?.length > 0 ||
|
|
|
|
|
(isFocused && !isMonograph && route.name !== "TopicNotes") ? (
|
2022-04-24 05:59:14 +05:00
|
|
|
<FloatingButton title="Create a note" onPress={onPressFloatingButton} />
|
|
|
|
|
) : null}
|
2022-06-13 10:55:34 +05:00
|
|
|
</DelayLayout>
|
2022-04-24 05:59:14 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default NotesPage;
|