-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshare-handler.tsx
More file actions
357 lines (330 loc) · 9.66 KB
/
share-handler.tsx
File metadata and controls
357 lines (330 loc) · 9.66 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { AlertTriangle, Bookmark, Check, X } from "@tamagui/lucide-icons";
import { useMutation } from "@tanstack/react-query";
import { useLocalSearchParams, useRouter } from "expo-router";
import { useShareIntentContext } from "expo-share-intent";
import React, { useEffect } from "react";
import {
Button,
Circle,
H2,
Paragraph,
Spinner,
Text,
View,
XStack,
YStack,
} from "tamagui";
import { apiClient } from "../src/lib/api-client";
export default function ShareHandler() {
const { hasShareIntent, shareIntent, resetShareIntent, error } =
useShareIntentContext();
const router = useRouter();
const params = useLocalSearchParams();
const createBookmarkMutation = useMutation({
mutationFn: ({
url,
metadata,
imageFile,
}: {
url: string;
metadata: Record<string, any>;
imageFile?: { uri: string; name: string; type: string };
}) => apiClient.createBookmark({ url, metadata, imageFile }),
onSuccess: () => {
// Show success for 2 seconds, then close
setTimeout(() => {
resetShareIntent();
router.dismiss();
}, 2000);
},
onError: () => {
// Show error UI for 3 seconds, then close
setTimeout(() => {
resetShareIntent();
router.dismiss();
}, 3000);
},
});
useEffect(() => {
// If no share intent, immediately redirect to tabs
if (!hasShareIntent && !shareIntent) {
resetShareIntent();
router.replace("/(tabs)");
return;
}
if (
hasShareIntent &&
shareIntent &&
!createBookmarkMutation.isPending &&
!createBookmarkMutation.isSuccess &&
!createBookmarkMutation.isError
) {
handleSharedContent();
}
}, [hasShareIntent, shareIntent]);
const handleSharedContent = () => {
if (!shareIntent) {
return;
}
let url = "";
let metadata: Record<string, any> = {};
// Handle different types of shared content
if (shareIntent.webUrl) {
// Direct URL sharing
url = shareIntent.webUrl;
metadata.title = shareIntent.text || "";
} else if (shareIntent.text) {
// Text content - check if it's a URL
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urlMatch = shareIntent.text.match(urlRegex);
if (urlMatch && urlMatch.length > 0) {
// Text contains URL
url = urlMatch[0];
metadata.title = shareIntent.text.replace(url, "").trim();
} else {
// Pure text - save as note
url = shareIntent.text;
metadata.isTextNote = true;
}
} else if (shareIntent.files && shareIntent.files.length > 0) {
// File content (images, etc.)
const file = shareIntent.files[0];
if (file && file.mimeType?.startsWith("image/")) {
// For images, we'll upload the file - URL will be replaced by S3 URL
url = `placeholder-image-upload-${Date.now()}`;
metadata = {
type: "image",
fileName: file.fileName || "Shared image",
mimeType: file.mimeType,
fileSize: file.size,
};
// Create image file object for upload
const imageFile = {
uri: file.path,
name:
file.fileName ||
`image-${Date.now()}.${file.mimeType.split("/")[1]}`,
type: file.mimeType,
};
if (!url) {
return;
}
createBookmarkMutation.mutate({ url, metadata, imageFile });
return;
} else if (file) {
// For non-image files, keep existing behavior
url = file.path;
metadata = {
type: "file",
fileName: file.fileName || "Shared file",
mimeType: file.mimeType,
fileSize: file.size,
};
}
}
if (!url) {
return;
}
createBookmarkMutation.mutate({ url, metadata });
};
const handleCancel = () => {
resetShareIntent();
router.dismiss();
};
// Error state
if (error) {
return (
<View flex={1} backgroundColor="$background">
<YStack
flex={1}
justifyContent="center"
alignItems="center"
padding="$4"
gap="$4"
>
<Circle
size={80}
backgroundColor="$destructive"
alignItems="center"
justifyContent="center"
>
<AlertTriangle size={40} color="$destructiveForeground" />
</Circle>
<YStack gap="$2" alignItems="center">
<H2 color="$destructive" textAlign="center">
Share Error
</H2>
<Paragraph color="$foreground" textAlign="center" opacity={0.8}>
{typeof error === "string"
? error
: (error as any)?.message || "Unable to process shared content"}
</Paragraph>
</YStack>
<Button
size="$4"
onPress={handleCancel}
backgroundColor="$destructive"
borderRadius="$4"
fontWeight="600"
>
<X size={20} color="$destructiveForeground" />
<Text color="$destructiveForeground" fontWeight="600">
Close
</Text>
</Button>
</YStack>
</View>
);
}
// Error state from API call
if (createBookmarkMutation.isError) {
return (
<View flex={1} backgroundColor="$background">
<YStack
flex={1}
justifyContent="center"
alignItems="center"
padding="$4"
gap="$4"
>
<Circle
size={80}
backgroundColor="$primary"
alignItems="center"
justifyContent="center"
>
<AlertTriangle size={40} color="$primaryForeground" />
</Circle>
<YStack gap="$2" alignItems="center">
<H2 color="$primary" textAlign="center">
Already Saved
</H2>
<Paragraph color="$foreground" textAlign="center" opacity={0.8}>
This bookmark already exists in your SaveIt collection
</Paragraph>
</YStack>
<Button
size="$4"
onPress={handleCancel}
backgroundColor="$primary"
borderRadius="$4"
fontWeight="600"
>
<X size={20} color="$primaryForeground" />
<Text color="$primaryForeground" fontWeight="600">
Close
</Text>
</Button>
</YStack>
</View>
);
}
// Success state
if (createBookmarkMutation.isSuccess) {
return (
<View flex={1} backgroundColor="$background">
<YStack
flex={1}
justifyContent="center"
alignItems="center"
padding="$4"
gap="$4"
>
<Circle
size={80}
backgroundColor="$primary"
alignItems="center"
justifyContent="center"
>
<Check size={40} color="$primaryForeground" />
</Circle>
<YStack gap="$2" alignItems="center">
<H2 color="$primary" textAlign="center">
Saved Successfully!
</H2>
<Paragraph color="$foreground" textAlign="center" opacity={0.8}>
Your bookmark has been added to SaveIt
</Paragraph>
</YStack>
<XStack alignItems="center" gap="$2" opacity={0.6}>
<Bookmark size={16} color="$foreground" />
<Text fontSize="$3" color="$foreground">
Added to your collection
</Text>
</XStack>
</YStack>
</View>
);
}
// Loading state
if (createBookmarkMutation.isPending) {
return (
<View flex={1} backgroundColor="$background">
<YStack
flex={1}
justifyContent="center"
alignItems="center"
padding="$4"
gap="$4"
>
<Circle
size={80}
backgroundColor="$primary"
alignItems="center"
justifyContent="center"
>
<Spinner size="large" color="$primaryForeground" />
</Circle>
<YStack gap="$2" alignItems="center">
<H2 color="$foreground" textAlign="center">
Saving Bookmark
</H2>
<Paragraph color="$foreground" textAlign="center" opacity={0.8}>
Adding to your SaveIt collection...
</Paragraph>
</YStack>
<XStack alignItems="center" gap="$2" opacity={0.6}>
<View
width={4}
height={4}
borderRadius="$10"
backgroundColor="$primary"
animation="bouncy"
animateOnly={["scale"]}
scale={1.2}
/>
<View
width={4}
height={4}
borderRadius="$10"
backgroundColor="$primary"
animation="bouncy"
animateOnly={["scale"]}
scale={1.2}
animationDelay={200}
/>
<View
width={4}
height={4}
borderRadius="$10"
backgroundColor="$primary"
animation="bouncy"
animateOnly={["scale"]}
scale={1.2}
animationDelay={400}
/>
</XStack>
</YStack>
</View>
);
}
// This should never be reached now since we redirect immediately
// if there's no share intent, but just in case show a loading state
return (
<View flex={1} backgroundColor="$background">
<YStack flex={1} justifyContent="center" alignItems="center" padding="$4">
<Spinner size="large" color="$primary" />
</YStack>
</View>
);
}