diff --git a/src/routes/s/[id]/+page.svelte b/src/routes/s/[id]/+page.svelte index 032c78e728..40f7c4ea86 100644 --- a/src/routes/s/[id]/+page.svelte +++ b/src/routes/s/[id]/+page.svelte @@ -48,30 +48,33 @@ $sessionUser && ($sessionUser.role === 'admin' || ($sessionUser.permissions?.chat?.import ?? true)); + // Reading history here would rerun this on every history change. $: if ($page.params.id) { - (async () => { - if (await loadSharedChat()) { - loaded = true; - await tick(); - const target = $page.url.searchParams.get('message'); - if (target && history.messages[target]) { - autoScroll = false; - history.currentId = getDeepestChildId(history, target); - await tick(); - await messagesComponent?.scrollToMessage(target); - } else messagesComponent?.scrollToBottom(); - } else if (localStorage.token) { - await goto('/'); - } else { - await goto(`/auth?redirect=${encodeURIComponent($page.url.pathname)}`); - } - })(); + loadAndScroll(); } ////////////////////////// // Web functions ////////////////////////// + const loadAndScroll = async () => { + if (await loadSharedChat()) { + loaded = true; + await tick(); + const target = $page.url.searchParams.get('message'); + if (target && history.messages[target]) { + autoScroll = false; + history.currentId = getDeepestChildId(history, target); + await tick(); + await messagesComponent?.scrollToMessage(target); + } else messagesComponent?.scrollToBottom(); + } else if (localStorage.token) { + await goto('/'); + } else { + await goto(`/auth?redirect=${encodeURIComponent($page.url.pathname)}`); + } + }; + const loadSharedChat = async () => { const token = localStorage.token ?? ''; const shareId = $page.params.id;