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

292 lines
8.7 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";
2023-04-01 13:09:33 +05:00
import { View } from "react-native";
import { db } from "../../common/database";
import { FloatingButton } from "../../components/container/floating-button";
import DelayLayout from "../../components/delay-layout";
import List from "../../components/list";
2023-04-01 13:09:33 +05:00
import { IconButton } from "../../components/ui/icon-button";
import Paragraph from "../../components/ui/typography/paragraph";
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";
2023-04-01 13:09:33 +05:00
import { SIZE } from "../../utils/size";
2022-08-30 13:30:11 +05:00
import { NoteType, TopicType } from "../../utils/types";
2023-04-01 13:09:33 +05:00
import Notebook from "../notebook/index";
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";
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, {
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)) ||
(item.type === "topic" && !notes)
2022-08-30 18:27:09 +05:00
) {
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 () => {
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}
>
2023-03-16 21:22:21 +05:00
{route.name === "TopicNotes" ? (
<View
style={{
width: "100%",
paddingHorizontal: 12,
flexDirection: "row",
alignItems: "center"
// borderBottomWidth: 1,
global: implement the new theme engine (#2196) * mobile: theme * theme: add theme engine * mobile: migrate app colors to new theme engine * mobile: fixed some colors * mobile: fix colors * mobile: store theme info in store * theme: `ColorsType` -> `Variants` * theme: use explicit return type for `useThemeColors` * theme: add `backdrop` color * mobile: `const colors` -> `const {colors} * theme: add default pitch-black theme * mobile: manage theme state via theme-engine * mobile: add theme scopes * mobile: commit * mobile: fix button width on applock screen * mobile: fix typings * mobile: fix theme definition * web: add partial support for custom themes only context menus & popups are left. * theme: add dialog & sheet scopes * global: sync with master branch and make everything work again * mobile: fix theme-engine usage in editor & app * mobile: fix colors * mobile: fix colors * mobile: cleanup * mobile: fix status bar color incorrect on entering foreground * mobile: fix dark color scheme * web: move emotion theme provider to @notesnook/theme * editor: add support for theme enging * web: adjust hover & focus colors on list item * mobile: migrate share ext to theme engine * mobile: fix editor theme provider * clipper: add support for the new theme engine * mobile: fix statusbar color on switch from bg * misc: fix build * mobile: fix build * misc: fix colors * mobile: fix theme colors * mobile: fix bottom padding * server: add theme server * theme: add previewColors * server: support themes query pagination * mobile: add client from theme server * server: reset cache on sync repo * server: fix types * server: show ip & port on start server * server: theme updates * web: finalize new theme engine on web * editor: fix build * global: fix @emotion/react version to 11.11.1 * editor: update katex patch * web: fix imports * global: fix @trpc/* versions * global: a huge set of changes 1. get rid of ThemeVariant. All variants can now be accessed anywhere. 2. remove unnecessary button variants 3. make buttons more responsive 4. implement themes server * web: add support for theme search and theme switching * global: update lockfiles * mobile: fix error * theme: use vite-plugin-react to start theme server * web: add support for auto updating themes * mobile: update theme selector * mobile: update theme if new verison available * theme: add `isomorphic-fetch` package * global: update lockfiles * web: add theme details dialog * setup: add scope for themes server in bootstrap script * web: add production server url * web: update lockfile * web: update lockfile * mobile: remove `react-native-blob-util` * web: add support for endless scrolling in themes * web: bring back dark/light mode option in settings * web: fix colors in places * theme: add selected variant * global: use single typescript version across the projects * web: fix sort & group options not having submenus * web: apply selected variant where appropriate * ui: use unique id for all menu items * config: add ui scope for commits * theme: export button variant creation fn * web: fix only 1 theme showing in theme selector * web: fix navigation item hover & other colors * mobile: update theme * editor: fix toolbar group alignments * editor: set theme provider at app level * theme: use scope name to get current scope * mobile: fix color usage in message card * theme: remove caching * editor: bring back icons in table menus * theme: use zustand to manage theme engine state * web: fix login/signup theming * mobile: fix webpack build * misc: remove ThemeProvider usage * editor: adjust theming and styling of editor toolbar * mobile: refactor * editor: fix toolbar group padding everywhere * web: fix settings sidebar is not scrollable * web: add loading indicator for themes loading * mobile: fix warning * mobile: fix ui issues * web: fix Loader errors on build * theme: add getPreviewColors & validateTheme * theme: fix theme validation * mobile: load theme from file * mobile: fix share extension crash * mobile: rename state * theme: add sourceURL property * theme: refactor theme-engine * web: add support for loading theme from file * web: improve button hover interaction * mobile: fix floating button color * mobile: update theme * mobile: fix border radius of context menu * mobile: set sheet overlay color to theme backdrop * mobile: set sidemenu backdrop to theme backdrop --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2023-08-01 12:07:21 +05:00
// borderBottomColor: colors.secondary.background
2023-03-16 21:22:21 +05:00
}}
>
<Paragraph
onPress={() => {
Navigation.navigate(
{
name: "Notebooks"
},
{}
);
}}
size={SIZE.xs}
>
Notebooks
</Paragraph>
{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,
color:
route.name === "ColoredNotes"
? params.current?.item.title.toLowerCase()
: null
2022-04-24 05:59:14 +05:00
}}
placeholderData={placeholderData}
/>
{!isMonograph &&
route.name !== "TopicNotes" &&
(notes?.length > 0 || isFocused) ? (
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;