Skip to content

Commit

Permalink
feat: add a better scroll behave on playground's chat (langflow-ai#3680)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Cristhianzl authored and diogocabral committed Nov 26, 2024
1 parent c010f15 commit c38208c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand Down
11 changes: 11 additions & 0 deletions src/frontend/src/modals/IOModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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"}
Expand Down
3 changes: 3 additions & 0 deletions src/frontend/src/stores/utilityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
}));
2 changes: 1 addition & 1 deletion src/frontend/src/types/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ export type tabsArrayType = {
};

export type codeTabsPropsType = {
open: boolean;
open?: boolean;
tabs: Array<tabsArrayType>;
activeTab: string;
setActiveTab: (value: string) => void;
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/types/zustand/utility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit c38208c

Please sign in to comment.