Elffuss BraidHub

kikocis/continue public

627d7d0d1f85 · 3.2 KiBRawPermalink
1/**
2 * Provides context for controlling the Lump component's visibility state
3 */
4import {
5 createContext,
6 ReactNode,
7 useCallback,
8 useContext,
9 useEffect,
10 useState,
11} from "react";
12import { useAppSelector } from "../../../redux/hooks";
13
14interface LumpContextType {
15 isLumpVisible: boolean;
16 selectedSection: string | null;
17 displayedSection: string | null;
18 isToolbarExpanded: boolean;
19 hideLump: () => void;
20 setSelectedSection: (section: string | null) => void;
21 toggleToolbar: () => void;
22}
23
24const LumpContext = createContext<LumpContextType | undefined>(undefined);
25
26interface LumpProviderProps {
27 children: ReactNode;
28}
29
30/**
31 * Provider component that makes Lump state available to any child component
32 */
33export function LumpProvider({ children }: LumpProviderProps) {
34 const [selectedSection, setSelectedSection] = useState<string | null>(null);
35 const [displayedSection, setDisplayedSection] = useState<string | null>(null);
36 const [isVisible, setIsVisible] = useState(false);
37 const [isToolbarExpanded, setIsToolbarExpanded] = useState(true);
38 const isStreaming = useAppSelector((state) => state.session.isStreaming);
39
40 const configError = useAppSelector((store) => store.config.configError);
41
42 // Handle keyboard escape
43 const handleKeyDown = useCallback(
44 (event: KeyboardEvent) => {
45 if (event.key === "Escape" && selectedSection) {
46 setSelectedSection(null);
47 }
48 },
49 [selectedSection],
50 );
51
52 // Function to hide the lump
53 const hideLump = useCallback(() => {
54 setSelectedSection(null);
55 }, []);
56
57 // Function to toggle toolbar expanded state
58 const toggleToolbar = useCallback(() => {
59 setIsToolbarExpanded((prev) => !prev);
60 }, []);
61
62 // Update displayedSection and visibility when selectedSection changes
63 useEffect(() => {
64 if (selectedSection) {
65 setDisplayedSection(selectedSection);
66 setIsVisible(true);
67 } else {
68 setIsVisible(false);
69 const timeout = setTimeout(() => {
70 setDisplayedSection(null);
71 }, 300);
72 return () => clearTimeout(timeout);
73 }
74 }, [selectedSection]);
75
76 // Don't allow selection error section if there are no errors
77 useEffect(() => {
78 if (selectedSection === "error" && !configError) {
79 setSelectedSection(null);
80 }
81 }, [selectedSection, configError]);
82
83 // Reset when streaming starts
84 useEffect(() => {
85 if (isStreaming && selectedSection) {
86 setSelectedSection(null);
87 }
88 }, [isStreaming, selectedSection]);
89
90 // Set up keyboard listener
91 useEffect(() => {
92 if (selectedSection) {
93 document.addEventListener("keydown", handleKeyDown);
94 return () => document.removeEventListener("keydown", handleKeyDown);
95 }
96 }, [selectedSection, handleKeyDown]);
97
98 return (
99 <LumpContext.Provider
100 value={{
101 isLumpVisible: isVisible,
102 selectedSection,
103 displayedSection,
104 isToolbarExpanded,
105 hideLump,
106 setSelectedSection,
107 toggleToolbar,
108 }}
109 >
110 {children}
111 </LumpContext.Provider>
112 );
113}
114
115/**
116 * Hook that lets any component access the Lump context
117 */
118export function useLump() {
119 const context = useContext(LumpContext);
120 if (context === undefined) {
121 throw new Error("useLump must be used within a LumpProvider");
122 }
123 return context;
124}