more lump provider centralization

This commit is contained in:
Patrick Erichsen 2025-04-04 14:07:03 -07:00
parent 8bdcb16140
commit 8a7d7acb03
5 changed files with 114 additions and 123 deletions

View file

@ -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,
)
}
/>

View file

@ -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<LumpContextType | undefined>(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<string | null>(null);
const [displayedSection, setDisplayedSection] = useState<string | null>(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 (
<LumpContext.Provider
value={{
isLumpVisible,
isLumpVisible: isVisible,
selectedSection,
displayedSection,
isToolbarExpanded,
hideLump,
setSelectedSection,

View file

@ -19,12 +19,7 @@ const StopButton = styled.div`
cursor: pointer;
`;
interface TopToolbarProps {
selectedSection: string | null;
setSelectedSection: (value: string | null) => 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 (
<BlockSettingsTopToolbar
selectedSection={props.selectedSection}
setSelectedSection={props.setSelectedSection}
/>
);
return <BlockSettingsTopToolbar />;
}

View file

@ -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<string | null>(null);
const [displayedSection, setDisplayedSection] = useState<string | null>(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 (
<LumpProvider
isLumpVisible={isVisible}
selectedSection={selectedSection}
isToolbarExpanded={isToolbarExpanded}
hideLump={hideLump}
setSelectedSection={setSelectedSection}
toggleToolbar={toggleToolbar}
>
<LumpDiv>
<div className="mt-0.5 px-2">
<LumpToolbar
selectedSection={selectedSection}
setSelectedSection={setSelectedSection}
/>
<ContentDiv
className="no-scrollbar pr-0.5"
hasSection={!!selectedSection}
isVisible={isVisible}
>
<SelectedSection selectedSection={displayedSection} />
</ContentDiv>
</div>
</LumpDiv>
<LumpProvider>
<LumpContent />
</LumpProvider>
);
}
/**
* Internal component that consumes the LumpContext
*/
function LumpContent() {
const { isLumpVisible, selectedSection } = useLump();
return (
<LumpDiv>
<div className="mt-0.5 px-2">
<LumpToolbar />
<ContentDiv
className="no-scrollbar pr-0.5"
hasSection={!!selectedSection}
isVisible={isLumpVisible}
>
<SelectedSection />
</ContentDiv>
</div>
</LumpDiv>
);
}

View file

@ -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 <ModelsSection />;
case "rules":