Files
notesnook/apps/mobile/app/screens/notes/index.tsx

235 lines
6.9 KiB
TypeScript
Raw 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
2022-08-29 16:19:17 +05:00
import React, { useEffect, useRef, useState } from "react";
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";
import SearchService from "../../services/search";
import useNavigationStore, {
HeaderRightButton,
2022-08-30 18:27:09 +05:00
NotesScreenParams,
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
} from "./common";
2022-04-24 05:59:14 +05:00
export const WARNING_DATA = {
title: "Some notes in this topic are not synced"
2022-04-24 05:59:14 +05:00
};
export const PLACEHOLDER_DATA = {
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,
loading: "Loading your notes."
2022-04-24 05:59:14 +05:00
};
export const MONOGRAPH_PLACEHOLDER_DATA = {
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,
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[];
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) {
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
}: RouteProps<
"NotesPage" | "TaggedNotes" | "Monographs" | "ColoredNotes" | "TopicNotes"
>) => {
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));
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);
const isMonograph = route.name === "Monographs";
2022-04-24 05:59:14 +05:00
const isFocused = useNavigationFocus(navigation, {
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;
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,
type: "notes",
2022-08-30 13:30:11 +05:00
notebookId: (item as TopicType).notebookId,
alias:
route.name === "ColoredNotes" ? toCamelCase(alias as string) : alias,
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;
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();
}
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) {
setTimeout(() => setLoadingNotes(false), 300);
}
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 () => {
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
color={
route.name === "ColoredNotes"
? params.current?.item.title.toLowerCase()
: undefined
}
2022-07-08 19:01:51 +05:00
wait={loading || loadingNotes}
>
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,
color:
route.name === "ColoredNotes"
? params.current?.item.title.toLowerCase()
: null
2022-04-24 05:59:14 +05:00
}}
placeholderData={placeholderData}
/>
2022-07-08 18:26:35 +05:00
{notes?.length > 0 || (isFocused && !isMonograph) ? (
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;