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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
import { useEffect, useState } from "react";
import { ActivityIndicator, Pressable, Text, View } from "react-native";
import { useRouter } from "expo-router";
import { useShareIntentContext } from "expo-share-intent";
import { Button } from "@/components/ui/Button";
import useAppSettings from "@/lib/settings";
import { api } from "@/lib/trpc";
import { useUploadAsset } from "@/lib/upload";
import { z } from "zod";
import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks";
type Mode =
| { type: "idle" }
| { type: "success"; bookmarkId: string }
| { type: "alreadyExists"; bookmarkId: string }
| { type: "error" };
function SaveBookmark({ setMode }: { setMode: (mode: Mode) => void }) {
const onSaved = (d: ZBookmark & { alreadyExists: boolean }) => {
invalidateAllBookmarks();
setMode({
type: d.alreadyExists ? "alreadyExists" : "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) {
// This is a URL, else treated as text
mutate({ type: BookmarkTypes.LINK, url: shareIntent.text });
} else {
mutate({ type: BookmarkTypes.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: onSaved,
onError: () => {
setMode({ type: "error" });
},
});
return (
<View className="flex flex-row gap-3">
<Text className="text-4xl text-foreground">Hoarding</Text>
<ActivityIndicator />
</View>
);
}
export default function Sharing() {
const router = useRouter();
const [mode, setMode] = useState<Mode>({ type: "idle" });
let autoCloseTimeoutId: NodeJS.Timeout | null = null;
let comp;
switch (mode.type) {
case "idle": {
comp = <SaveBookmark setMode={setMode} />;
break;
}
case "alreadyExists":
case "success": {
comp = (
<View className="items-center gap-4">
<Text className="text-4xl text-foreground">
{mode.type === "alreadyExists" ? "Already Hoarded!" : "Hoarded!"}
</Text>
<View className="flex flex-row gap-2">
<Button
label="Add to List"
onPress={() => {
router.push(
`/dashboard/bookmarks/${mode.bookmarkId}/manage_lists`,
);
if (autoCloseTimeoutId) {
clearTimeout(autoCloseTimeoutId);
}
}}
/>
<Button
label="Manage Tags"
onPress={() => {
router.push(
`/dashboard/bookmarks/${mode.bookmarkId}/manage_tags`,
);
if (autoCloseTimeoutId) {
clearTimeout(autoCloseTimeoutId);
}
}}
/>
</View>
<Pressable onPress={() => router.replace("dashboard")}>
<Text className="text-muted-foreground">Dismiss</Text>
</Pressable>
</View>
);
break;
}
case "error": {
comp = <Text className="text-4xl text-foreground">Error!</Text>;
break;
}
}
// Auto dismiss the modal after saving.
useEffect(() => {
if (mode.type === "idle") {
return;
}
autoCloseTimeoutId = setTimeout(() => {
router.replace("dashboard");
}, 2000);
return () => clearTimeout(autoCloseTimeoutId!);
}, [mode.type]);
return (
<View className="flex-1 items-center justify-center gap-4">{comp}</View>
);
}
|