Skip to content

Commit c208e39

Browse files
Mishiggary149nsarrazin
authored
Update share behaviour (#645)
* Update share behaviour * Simplify * format * wording * lint --------- Co-authored-by: Victor Mustar <victor.mustar@gmail.com> Co-authored-by: Nathan Sarrazin <sarrazin.nathan@gmail.com>
1 parent aab7222 commit c208e39

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/lib/components/chat/ChatWindow.svelte

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script lang="ts">
22
import type { Message } from "$lib/types/Message";
3-
import { createEventDispatcher } from "svelte";
3+
import { createEventDispatcher, onDestroy } from "svelte";
44
55
import CarbonSendAltFilled from "~icons/carbon/send-alt-filled";
66
import CarbonExport from "~icons/carbon/export";
77
import CarbonStopFilledAlt from "~icons/carbon/stop-filled-alt";
88
import CarbonClose from "~icons/carbon/close";
9+
import CarbonCheckmark from "~icons/carbon/checkmark";
910
1011
import EosIconsLoading from "~icons/eos-icons/loading";
1112
@@ -38,6 +39,9 @@
3839
3940
let loginModalOpen = false;
4041
let message: string;
42+
let timeout: ReturnType<typeof setTimeout>;
43+
let isSharedRecently = false;
44+
$: $page.params.id && (isSharedRecently = false);
4145
4246
const dispatch = createEventDispatcher<{
4347
message: string;
@@ -73,6 +77,23 @@
7377
$: sources = files.map((file) => file2base64(file));
7478
7579
const settings = useSettingsStore();
80+
81+
function onShare() {
82+
dispatch("share");
83+
isSharedRecently = true;
84+
if (timeout) {
85+
clearTimeout(timeout);
86+
}
87+
timeout = setTimeout(() => {
88+
isSharedRecently = false;
89+
}, 2000);
90+
}
91+
92+
onDestroy(() => {
93+
if (timeout) {
94+
clearTimeout(timeout);
95+
}
96+
});
7697
</script>
7798

7899
<div class="relative min-h-0 min-w-0">
@@ -226,12 +247,19 @@
226247
</p>
227248
{#if messages.length}
228249
<button
229-
class="flex flex-none items-center hover:text-gray-400 hover:underline max-sm:rounded-lg max-sm:bg-gray-50 max-sm:px-2.5 dark:max-sm:bg-gray-800"
250+
class="flex flex-none items-center hover:text-gray-400 max-sm:rounded-lg max-sm:bg-gray-50 max-sm:px-2.5 dark:max-sm:bg-gray-800"
230251
type="button"
231-
on:click={() => dispatch("share")}
252+
class:hover:underline={!isSharedRecently}
253+
on:click={onShare}
254+
disabled={isSharedRecently}
232255
>
233-
<CarbonExport class="text-[.6rem] sm:mr-1.5 sm:text-primary-500" />
234-
<div class="max-sm:hidden">Share this conversation</div>
256+
{#if isSharedRecently}
257+
<CarbonCheckmark class="text-[.6rem] sm:mr-1.5 sm:text-green-600" />
258+
<div class="text-green-600 max-sm:hidden">Link copied to clipboard</div>
259+
{:else}
260+
<CarbonExport class="text-[.6rem] sm:mr-1.5 sm:text-primary-500" />
261+
<div class="max-sm:hidden">Share this conversation</div>
262+
{/if}
235263
</button>
236264
{/if}
237265
</div>

src/lib/shareConversation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function shareConversation(id: string, title: string) {
88
try {
99
if (id.length === 7) {
1010
const url = get(page).url;
11-
share(getShareUrl(url, id), title);
11+
await share(getShareUrl(url, id), title);
1212
} else {
1313
const res = await fetch(`${base}/conversation/${id}/share`, {
1414
method: "POST",
@@ -24,7 +24,7 @@ export async function shareConversation(id: string, title: string) {
2424
}
2525

2626
const { url } = await res.json();
27-
share(url, title);
27+
await share(url, title);
2828
}
2929
} catch (err) {
3030
error.set(ERROR_MESSAGES.default);

src/lib/utils/share.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export function share(url: string, title: string) {
1+
export async function share(url: string, title: string) {
22
if (navigator.share) {
33
navigator.share({ url, title });
44
} else {
5-
prompt("Copy this public url to share:", url);
5+
await navigator.clipboard.writeText(url);
66
}
77
}

0 commit comments

Comments
 (0)