aboutsummaryrefslogtreecommitdiffstats
path: root/packages/mobile/app/sharing.tsx
blob: 948fed0543061718d8b711824427fef4285dc6fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { useLocalSearchParams, useRouter } from "expo-router";
import { ShareIntent } from "expo-share-intent";
import { useEffect, useMemo } from "react";
import { View, Text } from "react-native";

import { api } from "@/lib/trpc";

export default function Sharing() {
  const router = useRouter();
  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 { mutate, isPending } = api.bookmarks.createBookmark.useMutation({
    onSuccess: (d) => {
      router.replace(`bookmark/${d.id}`);
    },
    onError: () => {
      router.replace("error");
    },
  });

  useEffect(() => {
    if (!isPending && shareIntent?.text) {
      mutate({ type: "link", url: shareIntent.text });
    }
  }, []);

  useEffect(() => {
    if (!shareIntent) {
      router.replace("/");
    }
  }, []);

  return (
    <View className="flex-1 items-center justify-center gap-4">
      <Text className="text-4xl">Hoarding ...</Text>
    </View>
  );
}