From f1d86812e9a045b474f4a1c8cd3621fe17b8b806 Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Wed, 13 Mar 2024 00:58:37 +0000
Subject: mobile: Introduce a tab layout and move the sharing page into a modal

---
 packages/mobile/app/sharing.tsx | 69 ++++++++++++++++++++++++++++++++---------
 1 file changed, 55 insertions(+), 14 deletions(-)

(limited to 'packages/mobile/app/sharing.tsx')

diff --git a/packages/mobile/app/sharing.tsx b/packages/mobile/app/sharing.tsx
index 948fed05..c9b6c0bb 100644
--- a/packages/mobile/app/sharing.tsx
+++ b/packages/mobile/app/sharing.tsx
@@ -1,12 +1,16 @@
-import { useLocalSearchParams, useRouter } from "expo-router";
+import { Link, useLocalSearchParams, useRouter } from "expo-router";
 import { ShareIntent } from "expo-share-intent";
-import { useEffect, useMemo } from "react";
+import { useEffect, useMemo, useState } from "react";
 import { View, Text } from "react-native";
 
 import { api } from "@/lib/trpc";
 
-export default function Sharing() {
-  const router = useRouter();
+type Mode =
+  | { type: "idle" }
+  | { type: "success"; bookmarkId: string }
+  | { type: "error" };
+
+function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) {
   const params = useLocalSearchParams();
 
   const shareIntent = useMemo(() => {
@@ -18,30 +22,67 @@ export default function Sharing() {
     return null;
   }, [params]);
 
+  useEffect(() => {
+    if (!isPending && shareIntent?.text) {
+      mutate({ type: "link", url: shareIntent.text });
+    }
+  }, []);
+
   const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({
     onSuccess: (d) => {
-      router.replace(`bookmark/${d.id}`);
+      setMode({ type: "success", bookmarkId: d.id });
     },
     onError: () => {
-      router.replace("error");
+      setMode({ type: "error" });
     },
   });
 
-  useEffect(() => {
-    if (!isPending && shareIntent?.text) {
-      mutate({ type: "link", url: shareIntent.text });
+  return <Text className="text-4xl">Hoarding ...</Text>;
+}
+
+export default function Sharing() {
+  const router = useRouter();
+  const [mode, setMode] = useState<Mode>({ type: "idle" });
+
+  const isInModal = router.canGoBack();
+
+  let comp;
+  switch (mode.type) {
+    case "idle": {
+      comp = <SaveBookmark setMode={setMode} />;
+      break;
     }
-  }, []);
+    case "success": {
+      comp = <Text className="text-4xl">Hoarded!</Text>;
+      break;
+    }
+    case "error": {
+      comp = <Text className="text-4xl">Error!</Text>;
+      break;
+    }
+  }
 
+  // Auto dismiss the modal after saving.
   useEffect(() => {
-    if (!shareIntent) {
-      router.replace("/");
+    if (mode.type === "idle") {
+      return;
     }
-  }, []);
+
+    if (!isInModal) {
+      return;
+    }
+
+    const timeoutId = setTimeout(() => {
+      router.replace("../");
+    }, 2000);
+
+    return () => clearTimeout(timeoutId);
+  }, [mode.type]);
 
   return (
     <View className="flex-1 items-center justify-center gap-4">
-      <Text className="text-4xl">Hoarding ...</Text>
+      {comp}
+      {isInModal ? <Link href="../">Dismiss</Link> : <Link href="/">Home</Link>}
     </View>
   );
 }
-- 
cgit v1.2.3-70-g09d2

