File size: 927 Bytes
9405a81
fe2328e
2804c18
a799675
 
 
9405a81
 
a799675
 
c208e39
a799675
 
 
 
 
 
 
9405a81
a799675
 
 
 
 
9405a81
a799675
c208e39
a799675
9405a81
fe2328e
9405a81
 
 
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
import { base } from "$app/paths";
import { ERROR_MESSAGES, error } from "$lib/stores/errors";
import { share } from "./utils/share";
import { page } from "$app/stores";
import { get } from "svelte/store";
import { getShareUrl } from "./utils/getShareUrl";
export async function shareConversation(id: string, title: string) {
	try {
		if (id.length === 7) {
			const url = get(page).url;
			await share(getShareUrl(url, id), title);
		} else {
			const res = await fetch(`${base}/conversation/${id}/share`, {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
				},
			});

			if (!res.ok) {
				error.set("Error while sharing conversation, try again.");
				console.error("Error while sharing conversation: " + (await res.text()));
				return;
			}

			const { url } = await res.json();
			await share(url, title);
		}
	} catch (err) {
		error.set(ERROR_MESSAGES.default);
		console.error(err);
	}
}