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

197 lines
6.2 KiB
TypeScript
Raw Normal View History

2022-04-24 05:59:14 +05:00
import React, { useEffect, useRef, useState } from 'react';
2022-07-09 17:30:14 +05:00
import { InteractionManager } from 'react-native';
2022-04-24 05:59:14 +05:00
import { FloatingButton } from '../../components/container/floating-button';
2022-06-13 10:55:34 +05:00
import DelayLayout from '../../components/delay-layout';
2022-04-24 05:59:14 +05:00
import { RightMenus } from '../../components/header/right-menus';
import List from '../../components/list';
import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/event-manager';
import Navigation, { NavigationProps, NotesScreenParams } from '../../services/navigation';
import SearchService from '../../services/search';
2022-04-25 00:37:09 +05:00
import useNavigationStore, { HeaderRightButton, RouteName } from '../../stores/use-navigation-store';
2022-04-24 05:59:14 +05:00
import { useNoteStore } from '../../stores/use-notes-store';
import { useNavigationFocus } from '../../utils/hooks/use-navigation-focus';
import { NoteType } from '../../utils/types';
import {
getAlias,
isSynced,
openEditor,
openMonographsWebpage,
setOnFirstSave,
toCamelCase
} from './common';
export const WARNING_DATA = {
title: 'Some notes in this topic are not synced'
};
export const PLACEHOLDER_DATA = {
heading: 'Your notes',
paragraph: 'You have not added any notes yet.',
button: 'Add your first Note',
action: openEditor,
loading: 'Loading your notes.'
};
export const MONOGRAPH_PLACEHOLDER_DATA = {
heading: 'Your monographs',
paragraph: 'You have not published any notes as monographs yet.',
button: 'Learn more about monographs',
action: openMonographsWebpage,
loading: 'Loading published notes.',
type: 'monographs',
buttonIcon: 'information-outline'
};
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) {
if (routeName === 'TaggedNotes') return 'tag';
if (routeName === 'ColoredNotes') return 'color';
if (routeName === 'TopicNotes') return 'topic';
if (routeName === 'Monographs') return 'monograph';
return 'note';
}
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
}: RouteProps<'NotesPage' | 'TaggedNotes' | 'Monographs' | 'ColoredNotes' | 'TopicNotes'>) => {
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-04-24 05:59:14 +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);
const isMonograph = route.name === 'Monographs';
2022-04-25 00:37:09 +05:00
console.log(warning, 'isWarning', isSynced(route.params));
2022-04-24 05:59:14 +05:00
const isFocused = useNavigationFocus(navigation, {
onFocus: prev => {
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);
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-07-08 18:26:35 +05:00
type: 'notes',
//@ts-ignore
notebookId: item?.notebookId,
2022-07-08 19:01:51 +05:00
alias: route.name === 'ColoredNotes' ? toCamelCase(alias) : alias,
2022-07-08 18:26:35 +05:00
//@ts-ignore
color: item.type === 'color' ? item.title?.toLowerCase() : undefined
2022-04-24 05:59:14 +05:00
},
params.current.canGoBack,
rightButtons && rightButtons(params.current)
);
SearchService.prepareSearch = prepareSearch;
RightMenus.floatingButtonAction = onPressFloatingButton;
!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-06-13 10:55:34 +05:00
const isNew = 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-04-24 05:59:14 +05:00
if ((item.type === 'tag' || item.type === 'color') && (!notes || notes.length === 0)) {
return Navigation.goBack();
}
2022-04-25 00:37:09 +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 () => {
eUnSubscribeEvent(route.name, onRequestUpdate);
};
}, []);
const prepareSearch = () => {
const { item } = params.current;
SearchService.update({
placeholder: `Search in ${alias}`,
type: 'notes',
title: item.type === 'tag' ? '#' + alias : toCamelCase(item.title),
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
color={route.name === 'ColoredNotes' ? params.current?.item.title.toLowerCase() : undefined}
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-07-08 19:01:51 +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;