Skip to content

Commit

Permalink
fix: findLast is not available for all browsers (WIP) (#211)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Howard <[email protected]>
  • Loading branch information
stefl and codeincontext authored Oct 14, 2024
1 parent e538d70 commit a94e0f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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]);
Expand Down
24 changes: 14 additions & 10 deletions apps/nextjs/src/lib/hooks/use-enter-submit.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { useRef, type RefObject } from 'react'
import { useRef, type RefObject } from "react";

export function useEnterSubmit(): {
formRef: RefObject<HTMLFormElement>
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void
formRef: RefObject<HTMLFormElement>;
onKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void;
} {
const formRef = useRef<HTMLFormElement>(null)
const formRef = useRef<HTMLFormElement>(null);

const handleKeyDown = (
event: React.KeyboardEvent<HTMLTextAreaElement>
event: React.KeyboardEvent<HTMLTextAreaElement>,
): 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 };
}
6 changes: 4 additions & 2 deletions packages/aila/src/helpers/chat/getLastAssistantMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Message as AiMessage } from "ai";
import { findLast } from "remeda";

import type { Message as AilaMessage } from "../../core/chat/types";

Expand All @@ -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;
}

0 comments on commit a94e0f4

Please sign in to comment.