diff --git a/apps/web/src/features/legal/screen.tsx b/apps/web/src/features/legal/screen.tsx index b2d1a9c..d7fbb8e 100644 --- a/apps/web/src/features/legal/screen.tsx +++ b/apps/web/src/features/legal/screen.tsx @@ -51,10 +51,14 @@ export default function Screen(props: { children: React.ReactNode }) { useEffect(() => { let active = true; - setLoading(true); + const shouldBlockRender = userId === undefined || userId === 0; + + if (shouldBlockRender) { + setLoading(true); + } + setError(""); setErrorDescription(""); - setHasContinued(false); void load("user_id").then(async (id) => { if (!active) { @@ -64,7 +68,9 @@ export default function Screen(props: { children: React.ReactNode }) { setUserId(id); if (id === 0) { - setLoading(false); + if (shouldBlockRender) { + setLoading(false); + } return; } @@ -134,7 +140,9 @@ export default function Screen(props: { children: React.ReactNode }) { acceptTOS(false); } - setLoading(false); + if (shouldBlockRender) { + setLoading(false); + } }); return () => { diff --git a/packages/chat/src/components/input.tsx b/packages/chat/src/components/input.tsx index d95d2e1..897962d 100644 --- a/packages/chat/src/components/input.tsx +++ b/packages/chat/src/components/input.tsx @@ -41,6 +41,17 @@ export default function InputComponent() { const time = Date.now(); const currentValue = value; const currentUserId = userId(); + const currentSharedSecret = sharedSecret(); + + if (!Number.isSafeInteger(currentUserId) || currentUserId <= 0) { + toast("error", "No conversation selected"); + return; + } + + if (!currentSharedSecret) { + toast("error", "Conversation key not ready yet"); + return; + } addLiveMessage({ height: 0, @@ -50,7 +61,7 @@ export default function InputComponent() { sent_by_self: true, }); - const encryptedContext = await encrypt(sharedSecret(), currentValue); + const encryptedContext = await encrypt(currentSharedSecret, currentValue); send("message_send", { height: 0, diff --git a/packages/chat/src/screen.tsx b/packages/chat/src/screen.tsx index 807949c..76fd5d7 100644 --- a/packages/chat/src/screen.tsx +++ b/packages/chat/src/screen.tsx @@ -26,11 +26,13 @@ export default function Screen() { } | null>(null); const chatUserId = userId(); + const hasValidChatUser = Number.isSafeInteger(chatUserId) && chatUserId > 0; const messagesQuery = useInfiniteQuery({ queryKey: ["chat-messages", String(chatUserId)], initialPageParam: 0, queryFn: ({ pageParam }) => getMessages(PAGE_SIZE, Number(pageParam)), + enabled: hasValidChatUser, getNextPageParam: (lastPage, allPages) => { if (lastPage.length < PAGE_SIZE) { return undefined; @@ -175,6 +177,14 @@ export default function Screen() { void onScroll(); }, [onScroll]); + if (!hasValidChatUser) { + return ( +
+ Select a conversation to start chatting. +
+ ); + } + return (