From 7e65f7fa4d31deb04842bcc51227d95bab2e6fc1 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 3 Mar 2025 20:31:35 -0800 Subject: [PATCH] fix: chat event handler not being registered on sign in --- .../components/layout/Sidebar/UserMenu.svelte | 5 ++++- src/lib/i18n/index.ts | 2 +- src/routes/+layout.svelte | 17 +++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/lib/components/layout/Sidebar/UserMenu.svelte b/src/lib/components/layout/Sidebar/UserMenu.svelte index cb61e050b..3680a4daa 100644 --- a/src/lib/components/layout/Sidebar/UserMenu.svelte +++ b/src/lib/components/layout/Sidebar/UserMenu.svelte @@ -5,7 +5,7 @@ import { flyAndScale } from '$lib/utils/transitions'; import { goto } from '$app/navigation'; import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte'; - import { showSettings, activeUserIds, USAGE_POOL, mobile, showSidebar } from '$lib/stores'; + import { showSettings, activeUserIds, USAGE_POOL, mobile, showSidebar, user } from '$lib/stores'; import { fade, slide } from 'svelte/transition'; import Tooltip from '$lib/components/common/Tooltip.svelte'; import { userSignOut } from '$lib/apis/auths'; @@ -157,8 +157,11 @@ class="flex rounded-md py-2 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition" on:click={async () => { await userSignOut(); + user.set(null); + localStorage.removeItem('token'); location.href = '/auth'; + show = false; }} > diff --git a/src/lib/i18n/index.ts b/src/lib/i18n/index.ts index e5e7fdef7..a5a83d093 100644 --- a/src/lib/i18n/index.ts +++ b/src/lib/i18n/index.ts @@ -37,7 +37,7 @@ const createIsLoadingStore = (i18n: i18nType) => { return isLoading; }; -export const initI18n = (defaultLocale: string | undefined) => { +export const initI18n = (defaultLocale?: string | undefined) => { let detectionOrder = defaultLocale ? ['querystring', 'localStorage'] : ['querystring', 'localStorage', 'navigator']; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index ae32a25c9..4aa41d17b 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -220,6 +220,8 @@ const type = event?.data?.type ?? null; const data = event?.data?.data ?? null; + console.log('chatEventHandler', event); + if ((event.chat_id !== $chatId && !$temporaryChatEnabled) || isFocused) { if (type === 'chat:completion') { const { done, content, title } = data; @@ -443,6 +445,7 @@ theme.set(localStorage.theme); mobile.set(window.innerWidth < BREAKPOINT); + const onResize = () => { if (window.innerWidth < BREAKPOINT) { mobile.set(true); @@ -450,9 +453,18 @@ mobile.set(false); } }; - window.addEventListener('resize', onResize); + user.subscribe((value) => { + if (value) { + $socket?.off('chat-events', chatEventHandler); + $socket?.off('channel-events', channelEventHandler); + + $socket?.on('chat-events', chatEventHandler); + $socket?.on('channel-events', channelEventHandler); + } + }); + let backendConfig = null; try { backendConfig = await getBackendConfig(); @@ -494,9 +506,6 @@ // Save Session User to Store $socket.emit('user-join', { auth: { token: sessionUser.token } }); - $socket?.on('chat-events', chatEventHandler); - $socket?.on('channel-events', channelEventHandler); - await user.set(sessionUser); await config.set(await getBackendConfig()); } else {