2022-08-26 16:19:39 +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";
|
|
|
|
|
import Navigation, {
|
|
|
|
|
NavigationProps,
|
|
|
|
|
NotesScreenParams
|
|
|
|
|
} from "../../services/navigation";
|
|
|
|
|
import SearchService from "../../services/search";
|
|
|
|
|
import useNavigationStore, {
|
|
|
|
|
HeaderRightButton,
|
|
|
|
|
RouteName
|
|
|
|
|
} from "../../stores/use-navigation-store";
|
|
|
|
|
import { useNoteStore } from "../../stores/use-notes-store";
|
|
|
|
|
import { NoteType } from "../../utils/types";
|
2022-04-24 05:59:14 +05:00
|
|
|
import {
|
|
|
|
|
getAlias,
|
|
|
|
|
isSynced,
|
|
|
|
|
openEditor,
|
|
|
|
|
openMonographsWebpage,
|
|
|
|
|
setOnFirstSave,
|
|
|
|
|
toCamelCase
|
2022-08-26 16:19:39 +05:00
|
|
|
} from "./common";
|
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-04-24 05:59:14 +05:00
|
|
|
placeholderData: any;
|
|
|
|
|
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
|
|
|
canGoBack,
|
|
|
|
|
rightButtons
|
2022-08-26 16:19:39 +05:00
|
|
|
}: 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 [warning, setWarning] = useState(!isSynced(route.params));
|
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";
|
|
|
|
|
console.log(warning, "isWarning", isSynced(route.params));
|
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
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const syncWithNavigation = () => {
|
|
|
|
|
const { item, title } = params.current;
|
2022-07-08 18:26:35 +05:00
|
|
|
//@ts-ignore
|
2022-07-08 19:01:51 +05:00
|
|
|
let alias = getAlias(params.current);
|
2022-08-26 16:19:39 +05:00
|
|
|
console.log(alias, title, "syncWithNavigation", 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-07-08 18:26:35 +05:00
|
|
|
//@ts-ignore
|
|
|
|
|
notebookId: item?.notebookId,
|
2022-08-26 16:19:39 +05:00
|
|
|
alias: route.name === "ColoredNotes" ? toCamelCase(alias) : alias,
|
2022-07-08 18:26:35 +05:00
|
|
|
//@ts-ignore
|
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,
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
notebook: item.notebookId
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onRequestUpdate = (data?: NotesScreenParams) => {
|
2022-07-20 13:13:39 +05:00
|
|
|
const isNew = data && data?.item?.id !== params.current?.item?.id;
|
2022-07-09 17:30:14 +05:00
|
|
|
if (data) params.current = data;
|
2022-04-24 05:59:14 +05:00
|
|
|
params.current.title = params.current.title || params.current.item.title;
|
|
|
|
|
const { item } = params.current;
|
|
|
|
|
try {
|
2022-06-13 10:55:34 +05:00
|
|
|
if (isNew) setLoadingNotes(true);
|
2022-04-25 00:37:09 +05:00
|
|
|
let notes = get(params.current, true) as NoteType[];
|
2022-08-26 16:19:39 +05:00
|
|
|
if (
|
|
|
|
|
(item.type === "tag" || item.type === "color") &&
|
|
|
|
|
(!notes || notes.length === 0)
|
|
|
|
|
) {
|
2022-04-24 05:59:14 +05:00
|
|
|
return Navigation.goBack();
|
|
|
|
|
}
|
2022-08-26 16:19:39 +05:00
|
|
|
if (item.type === "topic") setWarning(!isSynced(params.current));
|
2022-04-24 05:59:14 +05:00
|
|
|
setNotes(notes);
|
|
|
|
|
syncWithNavigation();
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-13 10:55:34 +05:00
|
|
|
useEffect(() => {
|
2022-07-09 17:30:14 +05:00
|
|
|
if (loadingNotes) {
|
|
|
|
|
setTimeout(() => setLoadingNotes(false), 300);
|
|
|
|
|
}
|
2022-06-13 10:55:34 +05:00
|
|
|
}, [notes]);
|
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const prepareSearch = () => {
|
|
|
|
|
const { item } = params.current;
|
|
|
|
|
SearchService.update({
|
|
|
|
|
placeholder: `Search in ${alias}`,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "notes",
|
|
|
|
|
title: item.type === "tag" ? "#" + alias : toCamelCase(item.title),
|
2022-04-24 05:59:14 +05:00
|
|
|
get: () => {
|
2022-04-25 00:37:09 +05:00
|
|
|
return get(params.current, false);
|
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}
|
|
|
|
|
>
|
2022-04-24 05:59:14 +05:00
|
|
|
<List
|
|
|
|
|
listData={notes}
|
|
|
|
|
warning={warning ? WARNING_DATA : null}
|
|
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
|
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;
|