Spaces:
Running
Running
fix(share): only give leafId when sharing a conversation
Browse files- src/lib/shareConversation.ts +2 -2
- src/lib/utils/share.ts +9 -12
src/lib/shareConversation.ts
CHANGED
@@ -8,7 +8,7 @@ export async function shareConversation(id: string, title: string) {
|
|
8 |
try {
|
9 |
if (id.length === 7) {
|
10 |
const url = get(page).url;
|
11 |
-
await share(getShareUrl(url, id), title);
|
12 |
} else {
|
13 |
const res = await fetch(`${base}/conversation/${id}/share`, {
|
14 |
method: "POST",
|
@@ -24,7 +24,7 @@ export async function shareConversation(id: string, title: string) {
|
|
24 |
}
|
25 |
|
26 |
const { url } = await res.json();
|
27 |
-
await share(url, title);
|
28 |
}
|
29 |
} catch (err) {
|
30 |
error.set(ERROR_MESSAGES.default);
|
|
|
8 |
try {
|
9 |
if (id.length === 7) {
|
10 |
const url = get(page).url;
|
11 |
+
await share(getShareUrl(url, id), title, true);
|
12 |
} else {
|
13 |
const res = await fetch(`${base}/conversation/${id}/share`, {
|
14 |
method: "POST",
|
|
|
24 |
}
|
25 |
|
26 |
const { url } = await res.json();
|
27 |
+
await share(url, title, true);
|
28 |
}
|
29 |
} catch (err) {
|
30 |
error.set(ERROR_MESSAGES.default);
|
src/lib/utils/share.ts
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
import { browser } from "$app/environment";
|
2 |
|
3 |
-
export async function share(url: string, title: string) {
|
4 |
if (!browser) return;
|
|
|
5 |
// Retrieve the leafId from localStorage
|
6 |
const leafId = localStorage.getItem("leafId");
|
7 |
-
|
|
|
8 |
// Use URL and URLSearchParams to add the leafId parameter
|
9 |
const shareUrl = new URL(url);
|
10 |
shareUrl.searchParams.append("leafId", leafId);
|
@@ -14,15 +16,10 @@ export async function share(url: string, title: string) {
|
|
14 |
if (navigator.share) {
|
15 |
navigator.share({ url, title });
|
16 |
} else {
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
} else {
|
23 |
-
console.error("Document is not focused. Unable to write to clipboard.");
|
24 |
-
alert("Document is not focused. Please try again.");
|
25 |
-
}
|
26 |
-
}, 3000); // 3-second delay to allow focusing
|
27 |
}
|
28 |
}
|
|
|
1 |
import { browser } from "$app/environment";
|
2 |
|
3 |
+
export async function share(url: string, title: string, appendLeafId: boolean = false) {
|
4 |
if (!browser) return;
|
5 |
+
|
6 |
// Retrieve the leafId from localStorage
|
7 |
const leafId = localStorage.getItem("leafId");
|
8 |
+
|
9 |
+
if (appendLeafId && leafId) {
|
10 |
// Use URL and URLSearchParams to add the leafId parameter
|
11 |
const shareUrl = new URL(url);
|
12 |
shareUrl.searchParams.append("leafId", leafId);
|
|
|
16 |
if (navigator.share) {
|
17 |
navigator.share({ url, title });
|
18 |
} else {
|
19 |
+
if (document.hasFocus()) {
|
20 |
+
await navigator.clipboard.writeText(url);
|
21 |
+
} else {
|
22 |
+
alert("Document is not focused. Please try again.");
|
23 |
+
}
|
|
|
|
|
|
|
|
|
|
|
24 |
}
|
25 |
}
|