From 04572a8e5081b1e4871e273cde9dbaaa44c52fe0 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 13 Mar 2024 21:43:44 +0000 Subject: structure: Create apps dir and copy tooling dir from t3-turbo repo --- apps/mobile/app/sharing.tsx | 99 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 apps/mobile/app/sharing.tsx (limited to 'apps/mobile/app/sharing.tsx') diff --git a/apps/mobile/app/sharing.tsx b/apps/mobile/app/sharing.tsx new file mode 100644 index 00000000..64bbd933 --- /dev/null +++ b/apps/mobile/app/sharing.tsx @@ -0,0 +1,99 @@ +import { Link, useLocalSearchParams, useRouter } from "expo-router"; +import { ShareIntent, useShareIntent } from "expo-share-intent"; +import { useEffect, useMemo, useState } from "react"; +import { View, Text } from "react-native"; +import { z } from "zod"; + +import { api } from "@/lib/trpc"; + +type Mode = + | { type: "idle" } + | { type: "success"; bookmarkId: string } + | { type: "error" }; + +function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) { + // Desperate attempt to fix sharing duplication + const { hasShareIntent, resetShareIntent } = useShareIntent(); + + const params = useLocalSearchParams(); + + const shareIntent = useMemo(() => { + if (params && params.shareIntent) { + if (typeof params.shareIntent === "string") { + return JSON.parse(params.shareIntent) as ShareIntent; + } + } + return null; + }, [params]); + + const invalidateAllBookmarks = + api.useUtils().bookmarks.getBookmarks.invalidate; + + useEffect(() => { + if (!isPending && shareIntent?.text) { + const val = z.string().url(); + if (val.safeParse(shareIntent.text).success) { + // This is a URL, else treated as text + mutate({ type: "link", url: shareIntent.text }); + } else { + mutate({ type: "text", text: shareIntent.text }); + } + } + if (hasShareIntent) { + resetShareIntent(); + } + }, []); + + const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({ + onSuccess: (d) => { + invalidateAllBookmarks(); + setMode({ type: "success", bookmarkId: d.id }); + }, + onError: () => { + setMode({ type: "error" }); + }, + }); + + return Hoarding ...; +} + +export default function Sharing() { + const router = useRouter(); + const [mode, setMode] = useState({ type: "idle" }); + + let comp; + switch (mode.type) { + case "idle": { + comp = ; + break; + } + case "success": { + comp = Hoarded!; + break; + } + case "error": { + comp = Error!; + break; + } + } + + // Auto dismiss the modal after saving. + useEffect(() => { + if (mode.type === "idle") { + return; + } + + const timeoutId = setTimeout(() => { + router.replace("dashboard"); + }, 2000); + + return () => clearTimeout(timeoutId); + }, [mode.type]); + + return ( + + {comp} + Dismiss + + ); +} -- cgit v1.2.3-70-g09d2