diff --git a/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx b/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx index 9d8c79ccc..64a477cbb 100644 --- a/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx +++ b/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx @@ -80,17 +80,17 @@ function BlockSettingsToolbarIcon(props: BlockSettingsToolbarIcon) { ); } -interface BlockSettingsTopToolbarProps { - selectedSection: string | null; - setSelectedSection: (value: string | null) => void; -} - -export function BlockSettingsTopToolbar(props: BlockSettingsTopToolbarProps) { - const { isToolbarExpanded, toggleToolbar } = useLump(); +export function BlockSettingsTopToolbar() { + const { + isToolbarExpanded, + toggleToolbar, + selectedSection, + setSelectedSection, + } = useLump(); const handleEllipsisClick = () => { if (isToolbarExpanded) { - props.setSelectedSection(null); + setSelectedSection(null); } toggleToolbar(); }; @@ -115,10 +115,10 @@ export function BlockSettingsTopToolbar(props: BlockSettingsTopToolbarProps) { key={section.id} icon={section.icon} tooltip={section.tooltip} - isSelected={props.selectedSection === section.id} + isSelected={selectedSection === section.id} onClick={() => - props.setSelectedSection( - props.selectedSection === section.id ? null : section.id, + setSelectedSection( + selectedSection === section.id ? null : section.id, ) } /> diff --git a/gui/src/components/mainInput/Lump/LumpContext.tsx b/gui/src/components/mainInput/Lump/LumpContext.tsx index 8b6417f6f..92c5dbd6c 100644 --- a/gui/src/components/mainInput/Lump/LumpContext.tsx +++ b/gui/src/components/mainInput/Lump/LumpContext.tsx @@ -1,11 +1,20 @@ /** * Provides context for controlling the Lump component's visibility state */ -import { createContext, ReactNode, useContext } from "react"; +import { + createContext, + ReactNode, + useCallback, + useContext, + useEffect, + useState, +} from "react"; +import { useAppSelector } from "../../../redux/hooks"; interface LumpContextType { isLumpVisible: boolean; selectedSection: string | null; + displayedSection: string | null; isToolbarExpanded: boolean; hideLump: () => void; setSelectedSection: (section: string | null) => void; @@ -16,31 +25,73 @@ const LumpContext = createContext(undefined); interface LumpProviderProps { children: ReactNode; - isLumpVisible: boolean; - selectedSection: string | null; - isToolbarExpanded: boolean; - hideLump: () => void; - setSelectedSection: (section: string | null) => void; - toggleToolbar: () => void; } /** * Provider component that makes Lump state available to any child component */ -export function LumpProvider({ - children, - isLumpVisible, - selectedSection, - isToolbarExpanded, - hideLump, - setSelectedSection, - toggleToolbar, -}: LumpProviderProps) { +export function LumpProvider({ children }: LumpProviderProps) { + const [selectedSection, setSelectedSection] = useState(null); + const [displayedSection, setDisplayedSection] = useState(null); + const [isVisible, setIsVisible] = useState(false); + const [isToolbarExpanded, setIsToolbarExpanded] = useState(true); + const isStreaming = useAppSelector((state) => state.session.isStreaming); + + // Handle keyboard escape + const handleKeyDown = useCallback( + (event: KeyboardEvent) => { + if (event.key === "Escape" && selectedSection) { + setSelectedSection(null); + } + }, + [selectedSection], + ); + + // Function to hide the lump + const hideLump = useCallback(() => { + setSelectedSection(null); + }, []); + + // Function to toggle toolbar expanded state + const toggleToolbar = useCallback(() => { + setIsToolbarExpanded((prev) => !prev); + }, []); + + // Update displayedSection and visibility when selectedSection changes + useEffect(() => { + if (selectedSection) { + setDisplayedSection(selectedSection); + setIsVisible(true); + } else { + setIsVisible(false); + const timeout = setTimeout(() => { + setDisplayedSection(null); + }, 300); + return () => clearTimeout(timeout); + } + }, [selectedSection]); + + // Reset when streaming starts + useEffect(() => { + if (isStreaming && selectedSection) { + setSelectedSection(null); + } + }, [isStreaming, selectedSection]); + + // Set up keyboard listener + useEffect(() => { + if (selectedSection) { + document.addEventListener("keydown", handleKeyDown); + return () => document.removeEventListener("keydown", handleKeyDown); + } + }, [selectedSection, handleKeyDown]); + return ( void; -} - -export function LumpToolbar(props: TopToolbarProps) { +export function LumpToolbar() { const dispatch = useAppDispatch(); const ideMessenger = useContext(IdeMessengerContext); const ttsActive = useAppSelector((state) => state.ui.ttsActive); @@ -60,10 +55,5 @@ export function LumpToolbar(props: TopToolbarProps) { ); } - return ( - - ); + return ; } diff --git a/gui/src/components/mainInput/Lump/index.tsx b/gui/src/components/mainInput/Lump/index.tsx index b9df37108..de432f1d1 100644 --- a/gui/src/components/mainInput/Lump/index.tsx +++ b/gui/src/components/mainInput/Lump/index.tsx @@ -1,12 +1,10 @@ -import { useCallback, useState } from "react"; import styled from "styled-components"; import { defaultBorderRadius, vscCommandCenterInactiveBorder, vscInputBackground, } from "../.."; -import { useAppSelector } from "../../../redux/hooks"; -import { LumpProvider } from "./LumpContext"; +import { LumpProvider, useLump } from "./LumpContext"; import { LumpToolbar } from "./LumpToolbar"; import { SelectedSection } from "./sections/SelectedSection"; @@ -35,82 +33,32 @@ const ContentDiv = styled.div<{ hasSection: boolean; isVisible: boolean }>` * Main component that displays the toolbar and selected content section */ export function Lump() { - const [selectedSection, setSelectedSection] = useState(null); - const [displayedSection, setDisplayedSection] = useState(null); - const [isVisible, setIsVisible] = useState(false); - const [isToolbarExpanded, setIsToolbarExpanded] = useState(true); - const isStreaming = useAppSelector((state) => state.session.isStreaming); - - // Handle keyboard escape - const handleKeyDown = useCallback( - (event: KeyboardEvent) => { - if (event.key === "Escape" && selectedSection) { - setSelectedSection(null); - } - }, - [selectedSection], - ); - - // Function to hide the lump - const hideLump = useCallback(() => { - setSelectedSection(null); - }, []); - - // Function to toggle toolbar expanded state - const toggleToolbar = useCallback(() => { - setIsToolbarExpanded((prev) => !prev); - }, []); - - // Reset when streaming starts - if (isStreaming && selectedSection) { - setSelectedSection(null); - } - - // Update displayedSection and visibility when selectedSection changes - if (selectedSection) { - if (displayedSection !== selectedSection || !isVisible) { - setDisplayedSection(selectedSection); - setIsVisible(true); - } - } else if (isVisible) { - setIsVisible(false); - setTimeout(() => { - setDisplayedSection(null); - }, 300); - } - - // Set up keyboard listener - if (selectedSection) { - document.addEventListener("keydown", handleKeyDown); - } else { - document.removeEventListener("keydown", handleKeyDown); - } - return ( - - -
- - - - - -
-
+ + ); } + +/** + * Internal component that consumes the LumpContext + */ +function LumpContent() { + const { isLumpVisible, selectedSection } = useLump(); + + return ( + +
+ + + + + +
+
+ ); +} diff --git a/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx b/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx index 28dc37c28..e592d13ff 100644 --- a/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/SelectedSection.tsx @@ -1,3 +1,4 @@ +import { useLump } from "../LumpContext"; import { ContextSection } from "./ContextSection"; import DocsSection from "./docs/DocsSection"; import { MCPSection } from "./MCPSection"; @@ -6,12 +7,13 @@ import { PromptsSection } from "./PromptsSection"; import { RulesSection } from "./RulesSection"; import { ToolsSection } from "./ToolsSection"; -interface SelectedSectionProps { - selectedSection: string | null; -} +/** + * Renders the appropriate section based on the selected section in the Lump context + */ +export function SelectedSection() { + const { displayedSection } = useLump(); -export function SelectedSection(props: SelectedSectionProps) { - switch (props.selectedSection) { + switch (displayedSection) { case "models": return ; case "rules":