From a94e0f4570b0bfbd6642d6cb36380b5eaf4b2e52 Mon Sep 17 00:00:00 2001 From: Stef Lewandowski Date: Mon, 14 Oct 2024 12:09:14 +0100 Subject: [PATCH] fix: findLast is not available for all browsers (WIP) (#211) Co-authored-by: Adam Howard <91115+codeincontext@users.noreply.github.com> --- .../AppComponents/Chat/chat-quick-buttons.tsx | 4 +++- .../nextjs/src/lib/hooks/use-enter-submit.tsx | 24 +++++++++++-------- .../helpers/chat/getLastAssistantMessage.ts | 6 +++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/nextjs/src/components/AppComponents/Chat/chat-quick-buttons.tsx b/apps/nextjs/src/components/AppComponents/Chat/chat-quick-buttons.tsx index 5558c322d..3b8a35b52 100644 --- a/apps/nextjs/src/components/AppComponents/Chat/chat-quick-buttons.tsx +++ b/apps/nextjs/src/components/AppComponents/Chat/chat-quick-buttons.tsx @@ -1,5 +1,7 @@ import { useCallback } from "react"; +import { findLast } from "remeda"; + import { useLessonChat } from "@/components/ContextProviders/ChatProvider"; import { Icon } from "@/components/Icon"; import { useLessonPlanTracking } from "@/lib/analytics/lessonPlanTrackingContext"; @@ -61,7 +63,7 @@ const QuickActionButtons = ({ isEmptyScreen }: QuickActionButtonsProps) => { const handleRegenerate = useCallback(() => { trackEvent("chat:regenerate", { id: id }); const lastUserMessage = - messages.findLast((m) => m.role === "user")?.content || ""; + findLast(messages, (m) => m.role === "user")?.content || ""; lessonPlanTracking.onClickRetry(lastUserMessage); queueUserAction("regenerate"); }, [queueUserAction, lessonPlanTracking, messages, trackEvent, id]); diff --git a/apps/nextjs/src/lib/hooks/use-enter-submit.tsx b/apps/nextjs/src/lib/hooks/use-enter-submit.tsx index d66b2d325..9ea937c9e 100644 --- a/apps/nextjs/src/lib/hooks/use-enter-submit.tsx +++ b/apps/nextjs/src/lib/hooks/use-enter-submit.tsx @@ -1,23 +1,27 @@ -import { useRef, type RefObject } from 'react' +import { useRef, type RefObject } from "react"; export function useEnterSubmit(): { - formRef: RefObject - onKeyDown: (event: React.KeyboardEvent) => void + formRef: RefObject; + onKeyDown: (event: React.KeyboardEvent) => void; } { - const formRef = useRef(null) + const formRef = useRef(null); const handleKeyDown = ( - event: React.KeyboardEvent + event: React.KeyboardEvent, ): void => { if ( - event.key === 'Enter' && + event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing ) { - formRef.current?.requestSubmit() - event.preventDefault() + try { + formRef.current?.requestSubmit(); + } catch (error) { + console.error("Failed to submit form:", error); + } + event.preventDefault(); } - } + }; - return { formRef, onKeyDown: handleKeyDown } + return { formRef, onKeyDown: handleKeyDown }; } diff --git a/packages/aila/src/helpers/chat/getLastAssistantMessage.ts b/packages/aila/src/helpers/chat/getLastAssistantMessage.ts index 8f1065f94..1608243f7 100644 --- a/packages/aila/src/helpers/chat/getLastAssistantMessage.ts +++ b/packages/aila/src/helpers/chat/getLastAssistantMessage.ts @@ -1,4 +1,5 @@ import type { Message as AiMessage } from "ai"; +import { findLast } from "remeda"; import type { Message as AilaMessage } from "../../core/chat/types"; @@ -12,9 +13,10 @@ interface AssistantMessage extends AilaMessage { export function getLastAssistantMessage( messages: AiMessage[], ): AssistantMessage | undefined { - const lastAssistantMessage = messages.findLast( + const lastAssistantMessage = findLast( + messages, (m): m is AssistantMessage => m.role === "assistant", - ); + ) as AssistantMessage | undefined; return lastAssistantMessage; }