feat: add a better scroll behave on playground's chat (#3680)

* feat: Add useUtilityStore hook to IOModal for setting playground scroll behavior

* feat: Add useUtilityStore hook to IOModal for setting playground scroll behavior

* feat: Add setPlaygroundScrollBehaves function to useUtilityStore hook

* feat: Update codeTabsPropsType to allow undefined for the 'open' property

* feat: Add `playgroundScrollBehaves` and `setPlaygroundScrollBehaves` to `UtilityStoreType`

The `UtilityStoreType` now includes the `playgroundScrollBehaves` property of type `ScrollBehavior` and the `setPlaygroundScrollBehaves` function to update it. This allows for managing the scroll behavior of the playground in the application.

* refactor: Remove unused 'open' prop from CodeTabsComponent
This commit is contained in:
Cristhian Zanforlin Lousa 2024-09-06 14:49:50 -03:00 • committed by GitHub
commit 835559a05d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 4 deletions

View file

@ -1,3 +1,4 @@
import { useUtilityStore } from "@/stores/utilityStore";
import Convert from "ansi-to-html";
import { useEffect, useMemo, useRef, useState } from "react";
import Markdown from "react-markdown";
@ -41,6 +42,13 @@ export default function ChatMessage({
const setErrorData = useAlertStore((state) => state.setErrorData);
const chatMessageRef = useRef(chatMessage);
const playgroundScrollBehaves = useUtilityStore(
(state) => state.playgroundScrollBehaves,
);
const setPlaygroundScrollBehaves = useUtilityStore(
(state) => state.setPlaygroundScrollBehaves,
);
// Sync ref with state
useEffect(() => {
chatMessageRef.current = chatMessage;
@ -107,9 +115,14 @@ export default function ChatMessage({
useEffect(() => {
const element = document.getElementById("last-chat-message");
if (element) {
setTimeout(() => {
element.scrollIntoView({ behavior: "smooth" });
}, 200);
if (playgroundScrollBehaves === "instant") {
element.scrollIntoView({ behavior: playgroundScrollBehaves });
setPlaygroundScrollBehaves("smooth");
} else {
setTimeout(() => {
element.scrollIntoView({ behavior: playgroundScrollBehaves });
}, 200);
}
}
}, [lastMessage]);

View file

@ -2,6 +2,7 @@ import {
useDeleteMessages,
useGetMessagesQuery,
} from "@/controllers/API/queries/messages";
import { useUtilityStore } from "@/stores/utilityStore";
import { useEffect, useState } from "react";
import AccordionComponent from "../../components/accordionComponent";
import IconComponent from "../../components/genericIconComponent";
@ -172,6 +173,16 @@ export default function IOModal({
sessions;
}, [messages]);
const setPlaygroundScrollBehaves = useUtilityStore(
(state) => state.setPlaygroundScrollBehaves,
);
useEffect(() => {
if (open) {
setPlaygroundScrollBehaves("instant");
}
}, [open]);
return (
<BaseModal
size={"md-thin"}

View file

@ -15,4 +15,7 @@ export const useUtilityStore = create<UtilityStoreType>((set, get) => ({
healthCheckTimeout: null,
setHealthCheckTimeout: (timeout: string | null) =>
set({ healthCheckTimeout: timeout }),
playgroundScrollBehaves: "instant",
setPlaygroundScrollBehaves: (behaves: ScrollBehavior) =>
set({ playgroundScrollBehaves: behaves }),
}));

View file

@ -775,7 +775,7 @@ export type tabsArrayType = {
};
export type codeTabsPropsType = {
open: boolean;
open?: boolean;
tabs: Array<tabsArrayType>;
activeTab: string;
setActiveTab: (value: string) => void;

View file

@ -3,4 +3,6 @@ export type UtilityStoreType = {
setSelectedItems: (itemId: any) => void;
healthCheckTimeout: string | null;
setHealthCheckTimeout: (timeout: string | null) => void;
playgroundScrollBehaves: ScrollBehavior;
setPlaygroundScrollBehaves: (behaves: ScrollBehavior) => void;
};