Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: refactor to typescript

ammarahm-ed54b8a3e789bb
diff --git a/apps/mobile/app/components/container/container-header.js b/apps/mobile/app/components/container/container-header.tsx
similarity index 89%
rename from apps/mobile/app/components/container/container-header.js
rename to apps/mobile/app/components/container/container-header.tsx
index 227315577..152a49c75 100644
--- a/apps/mobile/app/components/container/container-header.js
+++ b/apps/mobile/app/components/container/container-header.tsx
@@ -17,12 +17,12 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React from "react";
+import React, { PropsWithChildren } from "react";
import { View } from "react-native";
import { useSelectionStore } from "../../stores/use-selection-store";
import { useThemeColors } from "@notesnook/theme";
-export const ContainerHeader = ({ children }) => {
+export const ContainerHeader = (props: PropsWithChildren) => {
const { colors } = useThemeColors();
const selectionMode = useSelectionStore((state) => state.selectionMode);
@@ -34,7 +34,7 @@ export const ContainerHeader = ({ children }) => {
overflow: "hidden"
}}
>
- {children}
+ {props.children}
</View>
) : null;
};
diff --git a/apps/mobile/app/components/container/floating-button.js b/apps/mobile/app/components/container/floating-button.tsx
similarity index 73%
rename from apps/mobile/app/components/container/floating-button.js
rename to apps/mobile/app/components/container/floating-button.tsx
index 52e7981b2..1b6e6ad7c 100644
--- a/apps/mobile/app/components/container/floating-button.js
+++ b/apps/mobile/app/components/container/floating-button.tsx
@@ -17,8 +17,9 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { useThemeColors } from "@notesnook/theme";
import React, { useCallback, useEffect } from "react";
-import { Keyboard, Platform, View } from "react-native";
+import { Keyboard, View } from "react-native";
import Animated, {
Easing,
useAnimatedStyle,
@@ -34,14 +35,20 @@ import { getElevationStyle } from "../../utils/elevation";
import { SIZE, normalize } from "../../utils/size";
import NativeTooltip from "../../utils/tooltip";
import { PressableButton } from "../ui/pressable";
-import { useThemeColors } from "@notesnook/theme";
-export const FloatingButton = ({
+interface FloatingButton {
+ title?: string;
+ onPress: () => void;
+ color?: string;
+ alwaysVisible?: boolean;
+}
+
+const FloatingButton = ({
title,
onPress,
color,
- shouldShow = false
-}) => {
+ alwaysVisible = false
+}: FloatingButton) => {
const { colors } = useThemeColors();
const deviceMode = useSettingStore((state) => state.deviceMode);
const selectionMode = useSelectionStore((state) => state.selectionMode);
@@ -60,12 +67,8 @@ export const FloatingButton = ({
};
});
- useEffect(() => {
- animate(selectionMode ? 150 : 0);
- }, [animate, selectionMode]);
-
const animate = useCallback(
- (toValue) => {
+ (toValue: number) => {
translate.value = withTiming(toValue, {
duration: 250,
easing: Easing.elastic(1)
@@ -74,39 +77,39 @@ export const FloatingButton = ({
[translate]
);
- const onKeyboardHide = useCallback(async () => {
- editorState().keyboardState = false;
- if (deviceMode !== "mobile") return;
- animate(0);
- }, [animate, deviceMode]);
-
- const onKeyboardShow = useCallback(async () => {
- editorState().keyboardState = true;
- if (deviceMode !== "mobile") return;
- animate(150);
- }, [animate, deviceMode]);
+ useEffect(() => {
+ animate(selectionMode ? 150 : 0);
+ }, [animate, selectionMode]);
useEffect(() => {
- let sub1 = Keyboard.addListener("keyboardDidShow", onKeyboardShow);
- let sub2 = Keyboard.addListener("keyboardDidHide", onKeyboardHide);
+ const onKeyboardHide = () => {
+ editorState().keyboardState = false;
+ if (deviceMode !== "mobile") return;
+ animate(0);
+ };
+
+ const onKeyboardShow = () => {
+ editorState().keyboardState = true;
+ if (deviceMode !== "mobile") return;
+ animate(150);
+ };
+
+ const sub = [
+ Keyboard.addListener("keyboardDidShow", onKeyboardShow),
+ Keyboard.addListener("keyboardDidHide", onKeyboardHide)
+ ];
return () => {
- sub1?.remove();
- sub2?.remove();
+ sub.forEach((sub) => sub?.remove?.());
};
- }, [deviceMode, onKeyboardHide, onKeyboardShow]);
- const paddings = {
- ios: 20,
- android: 20,
- iPad: 20
- };
+ }, [deviceMode, animate]);
- return deviceMode !== "mobile" && !shouldShow ? null : (
+ return deviceMode !== "mobile" && !alwaysVisible ? null : (
<Animated.View
style={[
{
position: "absolute",
right: 12,
- bottom: paddings[Platform.isPad ? "iPad" : Platform.OS],
+ bottom: 20,
zIndex: 10
},
animatedStyle
@@ -115,16 +118,15 @@ export const FloatingButton = ({
<PressableButton
testID={notesnook.buttons.add}
type="accent"
- buttonType={{
- color: colors.static[color],
- text: colors.static.white
- }}
+ accentColor={colors.static[color as keyof typeof colors.static]}
customStyle={{
...getElevationStyle(5),
borderRadius: 100
}}
onLongPress={(event) => {
- NativeTooltip.show(event, title, NativeTooltip.POSITIONS.LEFT);
+ if (title) {
+ NativeTooltip.show(event, title, NativeTooltip.POSITIONS.LEFT);
+ }
}}
onPress={onPress}
>
@@ -146,3 +148,5 @@ export const FloatingButton = ({
</Animated.View>
);
};
+
+export { FloatingButton };
diff --git a/apps/mobile/app/components/container/index.js b/apps/mobile/app/components/container/index.tsx
similarity index 71%
rename from apps/mobile/app/components/container/index.js
rename to apps/mobile/app/components/container/index.tsx
index a829b78d0..e15361aa9 100644
--- a/apps/mobile/app/components/container/index.js
+++ b/apps/mobile/app/components/container/index.tsx
@@ -17,44 +17,40 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React from "react";
-import { KeyboardAvoidingView, Platform, View } from "react-native";
+import React, { PropsWithChildren } from "react";
+import { KeyboardAvoidingView, Platform } from "react-native";
import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets";
import useIsFloatingKeyboard from "../../hooks/use-is-floating-keyboard";
import { useSettingStore } from "../../stores/use-setting-store";
import { Header } from "../header";
import SelectionHeader from "../selection-header";
-export const Container = ({ children }) => {
+
+export const Container = ({ children }: PropsWithChildren) => {
const floating = useIsFloatingKeyboard();
const introCompleted = useSettingStore(
(state) => state.settings.introCompleted
);
const insets = useGlobalSafeAreaInsets();
+
return (
<KeyboardAvoidingView
behavior="padding"
enabled={Platform.OS === "ios" && !floating}
style={{
- flex: 1
+ flex: 1,
+ overflow: "hidden",
+ paddingTop: Platform.OS === "android" ? 0 : insets.top,
+ paddingBottom: Platform.OS === "android" ? 0 : insets.bottom
}}
>
- <View
- style={{
- flex: 1,
- overflow: "hidden",
- paddingTop: Platform.OS === "android" ? 0 : insets.top,
- paddingBottom: Platform.OS === "android" ? 0 : insets.bottom
- }}
- >
- {!introCompleted ? null : (
- <>
- <SelectionHeader />
- <Header title="Header" screen="Header" />
- </>
- )}
-
- {children}
- </View>
+ {!introCompleted ? null : (
+ <>
+ <SelectionHeader />
+ <Header />
+ </>
+ )}
+
+ {children}
</KeyboardAvoidingView>
);
};
diff --git a/apps/mobile/app/components/header/index.js b/apps/mobile/app/components/header/index.tsx
similarity index 98%
rename from apps/mobile/app/components/header/index.js
rename to apps/mobile/app/components/header/index.tsx
index 70bb2dfb4..99e981a78 100644
--- a/apps/mobile/app/components/header/index.js
+++ b/apps/mobile/app/components/header/index.tsx
@@ -43,7 +43,7 @@ const _Header = () => {
);
const onScroll = useCallback(
- (data) => {
+ (data: { x: number; y: number }) => {
if (data.y > 150) {
if (!hide) return;
setHide(false);
diff --git a/apps/mobile/app/components/header/left-menus.js b/apps/mobile/app/components/header/left-menus.tsx
similarity index 92%
rename from apps/mobile/app/components/header/left-menus.js
rename to apps/mobile/app/components/header/left-menus.tsx
index 6d5954908..5da228b8c 100644
--- a/apps/mobile/app/components/header/left-menus.js
+++ b/apps/mobile/app/components/header/left-menus.tsx
@@ -43,12 +43,6 @@ export const LeftMenus = () => {
return;
}
Navigation.goBack();
- if (
- useNavigationStore.getState().currentScreen.name === "Signup" ||
- useNavigationStore.getState().currentScreen.name === "Login"
- ) {
- tabBarRef.current.unlock();
- }
};
return isTablet && !canGoBack ? null : (
diff --git a/apps/mobile/app/components/header/right-menus.js b/apps/mobile/app/components/header/right-menus.tsx
similarity index 91%
rename from apps/mobile/app/components/header/right-menus.js
rename to apps/mobile/app/components/header/right-menus.tsx
index e73875004..8467f87fe 100644
--- a/apps/mobile/app/components/header/right-menus.js
+++ b/apps/mobile/app/components/header/right-menus.tsx
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { useRef } from "react";
import { Platform, StyleSheet, View } from "react-native";
+//@ts-ignore
import Menu from "react-native-reanimated-material-menu";
import { notesnook } from "../../../e2e/test.ids";
import Navigation from "../../services/navigation";
@@ -35,10 +36,13 @@ export const RightMenus = () => {
const { colors } = useThemeColors();
const { colors: contextMenuColors } = useThemeColors("contextMenu");
const deviceMode = useSettingStore((state) => state.deviceMode);
- const rightButtons = useNavigationStore((state) => state.headerRightButtons);
+ const buttons = useNavigationStore((state) => state.headerRightButtons);
const currentScreen = useNavigationStore((state) => state.currentScreen.name);
const buttonAction = useNavigationStore((state) => state.buttonAction);
- const menuRef = useRef();
+ const menuRef = useRef<{
+ show: () => void;
+ hide: () => void;
+ }>();
return (
<View style={styles.rightBtnContainer}>
@@ -46,9 +50,12 @@ export const RightMenus = () => {
<IconButton
onPress={async () => {
SearchService.prepareSearch();
- Navigation.navigate({
- name: "Search"
- });
+ Navigation.navigate(
+ {
+ name: "Search"
+ },
+ {}
+ );
}}
testID="icon-search"
name="magnify"
@@ -82,7 +89,7 @@ export const RightMenus = () => {
/>
) : null}
- {rightButtons && rightButtons.length > 0 ? (
+ {buttons && buttons.length > 0 ? (
<Menu
ref={menuRef}
animationDuration={200}
@@ -104,7 +111,7 @@ export const RightMenus = () => {
/>
}
>
- {rightButtons.map((item) => (
+ {buttons.map((item) => (
<Button
style={{
width: 150,
diff --git a/apps/mobile/app/components/header/title.js b/apps/mobile/app/components/header/title.tsx
similarity index 79%
rename from apps/mobile/app/components/header/title.js
rename to apps/mobile/app/components/header/title.tsx
index 1d6e591cd..97c83933e 100644
--- a/apps/mobile/app/components/header/title.js
+++ b/apps/mobile/app/components/header/title.tsx
@@ -17,8 +17,9 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import React, { useEffect, useState } from "react";
-import { Platform, View } from "react-native";
+import { useThemeColors } from "@notesnook/theme";
+import React, { useCallback, useEffect, useState } from "react";
+import { Platform } from "react-native";
import { db } from "../../common/database";
import Notebook from "../../screens/notebook";
import {
@@ -26,14 +27,12 @@ import {
eUnSubscribeEvent
} from "../../services/event-manager";
import useNavigationStore from "../../stores/use-navigation-store";
-import { useThemeColors } from "@notesnook/theme";
import { eScrollEvent } from "../../utils/events";
import { SIZE } from "../../utils/size";
-import Heading from "../ui/typography/heading";
-import { useCallback } from "react";
import Tag from "../ui/tag";
+import Heading from "../ui/typography/heading";
-const titleState = {};
+const titleState: { [name: string]: boolean } = {};
export const Title = () => {
const { colors } = useThemeColors();
@@ -42,12 +41,12 @@ export const Title = () => {
const isTopic = currentScreen?.name === "TopicNotes";
const [hide, setHide] = useState(
isNotebook
- ? typeof titleState[currentScreen.id] === "boolean"
- ? titleState[currentScreen.id]
+ ? typeof titleState[currentScreen.id as string] === "boolean"
+ ? titleState[currentScreen.id as string]
: true
: false
);
- const isHidden = titleState[currentScreen.id];
+ const isHidden = titleState[currentScreen.id as string];
const notebook =
isTopic && currentScreen.notebookId
? db.notebooks?.notebook(currentScreen.notebookId)?.data
@@ -56,18 +55,18 @@ export const Title = () => {
const isTag = currentScreen?.name === "TaggedNotes";
const onScroll = useCallback(
- (data) => {
+ (data: { x: number; y: number }) => {
if (currentScreen.name !== "Notebook") {
setHide(false);
return;
}
if (data.y > 150) {
if (!hide) return;
- titleState[currentScreen.id] = false;
+ titleState[currentScreen.id as string] = false;
setHide(false);
} else {
if (hide) return;
- titleState[currentScreen.id] = true;
+ titleState[currentScreen.id as string] = true;
setHide(true);
}
},
@@ -76,13 +75,13 @@ export const Title = () => {
useEffect(() => {
if (currentScreen.name === "Notebook") {
- let value =
- typeof titleState[currentScreen.id] === "boolean"
- ? titleState[currentScreen.id]
+ const value =
+ typeof titleState[currentScreen.id as string] === "boolean"
+ ? titleState[currentScreen.id as string]
: true;
setHide(value);
} else {
- setHide(titleState[currentScreen.id]);
+ setHide(titleState[currentScreen.id as string]);
}
}, [currentScreen.id, currentScreen.name]);
@@ -98,14 +97,7 @@ export const Title = () => {
Notebook.navigate(notebook, true);
}
return (
- <View
- style={{
- opacity: 1,
- flexShrink: 1,
- flexDirection: "row",
- alignItems: "center"
- }}
- >
+ <>
{!hide && !isHidden ? (
<Heading
onPress={navigateToNotebook}
@@ -122,10 +114,9 @@ export const Title = () => {
#
</Heading>
) : null}
- {title}
+ {title} <Tag visible={currentScreen.beta} text="BETA" />
</Heading>
) : null}
- <Tag visible={currentScreen.beta} text="BETA" />
- </View>
+ </>
);
};
diff --git a/apps/mobile/app/components/ui/sheet/index.js b/apps/mobile/app/components/ui/sheet/index.js
index d2c9048a1..d76bfdbd5 100644
--- a/apps/mobile/app/components/ui/sheet/index.js
+++ b/apps/mobile/app/components/ui/sheet/index.js
@@ -49,7 +49,6 @@ const SheetWrapper = ({
const largeTablet = deviceMode === "tablet";
const smallTablet = deviceMode === "smallTablet";
const dimensions = useSettingStore((state) => state.dimensions);
- const pitchBlack = useSettingStore((state) => state.settings.pitchBlack);
const insets = useGlobalSafeAreaInsets();
let width = dimensions.width > 600 ? 600 : 500;
@@ -121,19 +120,17 @@ const SheetWrapper = ({
}
onClose={_onClose}
>
- <BouncingView>
- {children}
- {bottomPadding ? (
- <View
- style={{
- height:
- Platform.OS === "ios" && insets.bottom !== 0
- ? insets.bottom + 5
- : 20
- }}
- />
- ) : null}
- </BouncingView>
+ {children}
+ {bottomPadding ? (
+ <View
+ style={{
+ height:
+ Platform.OS === "ios" && insets.bottom !== 0
+ ? insets.bottom + 5
+ : 20
+ }}
+ />
+ ) : null}
</ActionSheet>
</ScopedThemeProvider>
);
diff --git a/apps/mobile/app/components/ui/tag/index.tsx b/apps/mobile/app/components/ui/tag/index.tsx
index d54fd6308..dd658adbb 100644
--- a/apps/mobile/app/components/ui/tag/index.tsx
+++ b/apps/mobile/app/components/ui/tag/index.tsx
@@ -37,7 +37,7 @@ export default function Tag({
}) {
const { colors } = useThemeColors();
return !visible ? null : (
- <View
+ <Paragraph
style={{
backgroundColor: background || colors.primary.accent,
borderRadius: 100,
@@ -46,13 +46,14 @@ export default function Tag({
marginLeft: 2,
marginTop: -10,
height: 20,
- justifyContent: "center",
+ textAlignVertical: "center",
+ textAlign: "center",
...style
}}
+ color={textColor || colors.static.white}
+ size={SIZE.xxs}
>
- <Paragraph color={textColor || colors.static.white} size={SIZE.xxs}>
- {text}
- </Paragraph>
- </View>
+ {text}
+ </Paragraph>
);
}
diff --git a/apps/mobile/app/screens/home/index.tsx b/apps/mobile/app/screens/home/index.tsx
index 7e164031f..095b71f94 100755
--- a/apps/mobile/app/screens/home/index.tsx
+++ b/apps/mobile/app/screens/home/index.tsx
@@ -29,6 +29,7 @@ import SettingsService from "../../services/settings";
import useNavigationStore from "../../stores/use-navigation-store";
import { useNoteStore } from "../../stores/use-notes-store";
import { openEditor } from "../notes/common";
+
const prepareSearch = () => {
SearchService.update({
placeholder: "Type a keyword to search in notes",
diff --git a/apps/mobile/app/screens/trash/index.tsx b/apps/mobile/app/screens/trash/index.tsx
index 9ae5c4460..343d92f5d 100644
--- a/apps/mobile/app/screens/trash/index.tsx
+++ b/apps/mobile/app/screens/trash/index.tsx
@@ -114,7 +114,7 @@ export const Trash = ({ navigation, route }: NavigationProps<"Trash">) => {
<FloatingButton
title="Clear all trash"
onPress={onPressFloatingButton}
- shouldShow={true}
+ alwaysVisible={true}
/>
) : null}
</DelayLayout>
diff --git a/apps/mobile/native/babel.config.js b/apps/mobile/native/babel.config.js
index a00da577a..bf6a3dbcd 100644
--- a/apps/mobile/native/babel.config.js
+++ b/apps/mobile/native/babel.config.js
@@ -6,6 +6,7 @@ const configs = {
plugins: [
'@babel/plugin-transform-named-capturing-groups-regex',
'react-native-reanimated/plugin',
+ "@babel/plugin-transform-export-namespace-from"
]
},
test: {
@@ -22,6 +23,7 @@ const configs = {
'transform-remove-console',
'@babel/plugin-transform-named-capturing-groups-regex',
'react-native-reanimated/plugin',
+ "@babel/plugin-transform-export-namespace-from"
]
}
}