Skip to content

Commit

Permalink
ui(mobile): Merge the editors for notes and links
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Aug 26, 2024
1 parent b094b2c commit 49dc06e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
17 changes: 3 additions & 14 deletions apps/mobile/app/dashboard/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useRef } from "react";
import { Platform, View } from "react-native";
import * as Haptics from "expo-haptics";
import * as ImagePicker from "expo-image-picker";
import { useRouter } from "expo-router";
import NoteEditorModal from "@/components/bookmarks/NewBookmarkModal";
import UpdatingBookmarkList from "@/components/bookmarks/UpdatingBookmarkList";
import CustomSafeAreaView from "@/components/ui/CustomSafeAreaView";
Expand All @@ -20,7 +19,6 @@ function HeaderRight({
openNewBookmarkModal: () => void;
}) {
const { toast } = useToast();
const router = useRouter();
const { settings } = useAppSettings();
const { uploadAsset } = useUploadAsset(settings, {
onError: (e) => {
Expand All @@ -31,10 +29,8 @@ function HeaderRight({
<MenuView
onPressAction={async ({ nativeEvent }) => {
Haptics.selectionAsync();
if (nativeEvent.event === "note") {
if (nativeEvent.event === "new") {
openNewBookmarkModal();
} else if (nativeEvent.event === "link") {
router.navigate("dashboard/add-link");
} else if (nativeEvent.event === "library") {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
Expand All @@ -52,15 +48,8 @@ function HeaderRight({
}}
actions={[
{
id: "link",
title: "New Link",
image: Platform.select({
ios: "link",
}),
},
{
id: "note",
title: "New Note",
id: "new",
title: "New Bookmark",
image: Platform.select({
ios: "note.text",
}),
Expand Down
7 changes: 0 additions & 7 deletions apps/mobile/app/dashboard/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ export default function Dashboard() {
headerTransparent: true,
}}
/>
<Stack.Screen
name="add-link"
options={{
title: "New link",
presentation: "modal",
}}
/>
</StyledStack>
);
}
38 changes: 27 additions & 11 deletions apps/mobile/components/bookmarks/NewBookmarkModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { BookmarkTypes } from "@hoarder/shared/types/bookmarks";
import { Button } from "../ui/Button";
import { Input } from "../ui/Input";
import PageTitle from "../ui/PageTitle";
import { useToast } from "../ui/Toast";

const NoteEditorModal = React.forwardRef<
BottomSheetModal,
Expand All @@ -23,14 +24,18 @@ const NoteEditorModal = React.forwardRef<

const [text, setText] = useState("");
const [error, setError] = useState<string | undefined>();

const onSuccess = () => {
setText("");
dismiss();
};
const { toast } = useToast();

const { mutate: createBookmark } = useCreateBookmark({
onSuccess,
onSuccess: (resp) => {
if (resp.alreadyExists) {
toast({
message: "Bookmark already exists",
});
}
setText("");
dismiss();
},
onError: (e) => {
let message;
if (e.data?.zodError) {
Expand All @@ -43,6 +48,19 @@ const NoteEditorModal = React.forwardRef<
},
});

const onSubmit = () => {
const data = text.trim();
try {
const url = new URL(data);
if (url.protocol != "http:" && url.protocol != "https:") {
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
}
createBookmark({ type: BookmarkTypes.LINK, url: data });
} catch (e: unknown) {
createBookmark({ type: BookmarkTypes.TEXT, text: data });
}
};

return (
<View>
<BottomSheetModal
Expand All @@ -56,7 +74,7 @@ const NoteEditorModal = React.forwardRef<
)}
{...props}
>
<PageTitle title="New Note" />
<PageTitle title="New Bookmark" />
<BottomSheetView className="gap-2 p-4">
{error && (
<Text className="w-full text-center text-red-500">{error}</Text>
Expand All @@ -66,12 +84,10 @@ const NoteEditorModal = React.forwardRef<
multiline
placeholder="What's on your mind?"
autoFocus
autoCapitalize={"none"}
textAlignVertical="top"
/>
<Button
onPress={() => createBookmark({ type: BookmarkTypes.TEXT, text })}
label="Add Note"
/>
<Button onPress={onSubmit} label="Save" />
</BottomSheetView>
</BottomSheetModal>
</View>
Expand Down

0 comments on commit 49dc06e

Please sign in to comment.