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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
import { Link, useLocalSearchParams, useRouter } from "expo-router";
import { ShareIntent } from "expo-share-intent";
import { useEffect, useMemo, useState } from "react";
import { View, Text } from "react-native";
import { api } from "@/lib/trpc";
type Mode =
| { type: "idle" }
| { type: "success"; bookmarkId: string }
| { type: "error" };
function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) {
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) {
mutate({ type: "link", url: shareIntent.text });
}
}, []);
const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({
onSuccess: (d) => {
invalidateAllBookmarks();
setMode({ type: "success", bookmarkId: d.id });
},
onError: () => {
setMode({ type: "error" });
},
});
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 (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">
{comp}
{isInModal ? <Link href="../">Dismiss</Link> : <Link href="/">Home</Link>}
</View>
);
}
|