nsarrazin HF Staff commited on
Commit
6cb4b46
·
1 Parent(s): ba9c926

fix(share): only give leafId when sharing a conversation

Browse files
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
- if (leafId) {
 
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
- alert("Please focus the document within 3 seconds by clicking somewhere or pressing Tab.");
18
- // Document Focus Error Handling
19
- setTimeout(async () => {
20
- if (document.hasFocus()) {
21
- await navigator.clipboard.writeText(url);
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
  }