chat-ui / src /routes /r /[id] /+page.svelte
coyotte508's picture
coyotte508 HF staff
✨ Retry messages (#121)
4a6603b unverified
raw
history blame
2.09 kB
<script lang="ts">
import { goto } from "$app/navigation";
import { base } from "$app/paths";
import { page } from "$app/stores";
import ChatWindow from "$lib/components/chat/ChatWindow.svelte";
import { ERROR_MESSAGES, error } from "$lib/stores/errors";
import { pendingMessage } from "$lib/stores/pendingMessage";
import { pendingMessageIdToRetry } from "$lib/stores/pendingMessageIdToRetry";
import type { PageData } from "./$types";
export let data: PageData;
let loading = false;
async function createConversation() {
try {
loading = true;
const res = await fetch(`${base}/conversation`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
fromShare: $page.params.id,
}),
});
if (!res.ok) {
error.set("Error while creating conversation, try again.");
console.error("Error while creating conversation: " + (await res.text()));
return;
}
const { conversationId } = await res.json();
return conversationId;
} catch (err) {
error.set(ERROR_MESSAGES.default);
console.error(String(err));
throw err;
}
}
async function shareConversation() {
const url = `${window.location.origin}${window.location.pathname}`;
if (navigator.share) {
navigator.share({
title: data.title,
text: "Share this chat with others",
url,
});
} else {
prompt("Share this link with your friends:", url);
}
}
</script>
<svelte:head>
<title>{data.title}</title>
</svelte:head>
<ChatWindow
on:message={(ev) =>
createConversation()
.then((convId) => {
$pendingMessage = ev.detail;
return goto(`${base}/conversation/${convId}`, { invalidateAll: true });
})
.finally(() => (loading = false))}
on:share={shareConversation}
on:retry={(ev) =>
createConversation()
.then((convId) => {
$pendingMessageIdToRetry = ev.detail.id;
$pendingMessage = ev.detail.content;
return goto(`${base}/conversation/${convId}`, { invalidateAll: true });
})
.finally(() => (loading = false))}
messages={data.messages}
{loading}
/>