From 785a5b574992296e187a66412dd42f7b4a686353 Mon Sep 17 00:00:00 2001
From: Mohamed Bassem <me@mbassem.com>
Date: Tue, 19 Mar 2024 00:33:11 +0000
Subject: Feature: Add support for uploading images and automatically inferring
 their tags (#2)

* feature: Experimental support for asset uploads

* feature(web): Add new bookmark type asset

* feature: Add support for automatically tagging images

* fix: Add support for image assets in preview page

* use next Image for fetching the images

* Fix auth and error codes in the route handlers

* Add support for image uploads on mobile

* Fix typing of upload requests

* Remove the ugly dragging box

* Bump mobile version to 1.3

* Change the editor card placeholder to mention uploading images

* Fix a typo

* Change ios icon for photo library

* Silence typescript error
---
 apps/mobile/app/sharing.tsx | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

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

diff --git a/apps/mobile/app/sharing.tsx b/apps/mobile/app/sharing.tsx
index e8b0ad09..2f9dbb27 100644
--- a/apps/mobile/app/sharing.tsx
+++ b/apps/mobile/app/sharing.tsx
@@ -2,8 +2,11 @@ import { useEffect, useState } from "react";
 import { Text, View } from "react-native";
 import { Link, useRouter } from "expo-router";
 import { useShareIntentContext } from "expo-share-intent";
+import useAppSettings from "@/lib/settings";
 import { api } from "@/lib/trpc";
+import { useUploadAsset } from "@/lib/upload";
 import { z } from "zod";
+import type { ZBookmark } from "@hoarder/trpc/types/bookmarks";
 
 type Mode =
   | { type: "idle" }
@@ -11,12 +14,28 @@ type Mode =
   | { type: "error" };
 
 function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) {
-  const { hasShareIntent, shareIntent, resetShareIntent } = useShareIntentContext();
+  const onSaved = (d: ZBookmark) => {
+    invalidateAllBookmarks();
+    setMode({ type: "success", bookmarkId: d.id });
+  };
+
+  const { hasShareIntent, shareIntent, resetShareIntent } =
+    useShareIntentContext();
+  const { settings, isLoading } = useAppSettings();
+  const { uploadAsset } = useUploadAsset(settings, {
+    onSuccess: onSaved,
+    onError: () => {
+      setMode({ type: "error" });
+    },
+  });
 
   const invalidateAllBookmarks =
     api.useUtils().bookmarks.getBookmarks.invalidate;
 
   useEffect(() => {
+    if (isLoading) {
+      return;
+    }
     if (!isPending && shareIntent?.text) {
       const val = z.string().url();
       if (val.safeParse(shareIntent.text).success) {
@@ -25,17 +44,20 @@ function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) {
       } else {
         mutate({ type: "text", text: shareIntent.text });
       }
+    } else if (!isPending && shareIntent?.files) {
+      uploadAsset({
+        type: shareIntent.files[0].type,
+        name: shareIntent.files[0].fileName ?? "",
+        uri: shareIntent.files[0].path,
+      });
     }
     if (hasShareIntent) {
       resetShareIntent();
     }
-  }, []);
+  }, [isLoading]);
 
   const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({
-    onSuccess: (d) => {
-      invalidateAllBookmarks();
-      setMode({ type: "success", bookmarkId: d.id });
-    },
+    onSuccess: onSaved,
     onError: () => {
       setMode({ type: "error" });
     },
-- 
cgit v1.3-1-g0d28

