Selaa lähdekoodia

fix: chat event handler not being registered on sign in

Timothy Jaeryang Baek 2 kuukautta sitten
vanhempi
commit
7e65f7fa4d

+ 4 - 1
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;
 				}}
 			>

+ 1 - 1
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'];

+ 13 - 4
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 {