Elffuss BraidHub

kikocis/continue public

b68bd43d7a56 · 6.9 KiBRawPermalink
1import {
2 ChevronDownIcon,
3 EyeSlashIcon,
4 XMarkIcon,
5} from "@heroicons/react/24/outline";
6import { EyeIcon } from "@heroicons/react/24/solid";
7import { useEffect, useMemo, useRef, useState } from "react";
8import styled from "styled-components";
9import {
10 defaultBorderRadius,
11 lightGray,
12 vscBadgeBackground,
13 vscCommandCenterInactiveBorder,
14 vscEditorBackground,
15} from "../../..";
16import { useAppSelector } from "../../../../redux/hooks";
17import { getFontSize } from "../../../../util";
18import HeaderButtonWithToolTip from "../../../gui/HeaderButtonWithToolTip";
19
20const MAX_PREVIEW_HEIGHT = 100;
21const MAX_EXPANED_PREVIEW_HEIGHT = MAX_PREVIEW_HEIGHT * 3;
22
23const PreviewDiv = styled.div<{
24 borderColor?: string;
25}>`
26 background-color: ${vscEditorBackground};
27 border-radius: ${defaultBorderRadius};
28 border: 0.5px solid ${(props) => props.borderColor || lightGray};
29 margin-top: 4px;
30 margin-bottom: 4px;
31 overflow: hidden;
32 position: relative;
33
34 & div {
35 background-color: ${vscEditorBackground};
36 }
37`;
38
39const ContentContainer = styled.div<{ expanded: boolean }>`
40 position: relative;
41 max-height: ${(props) =>
42 props.expanded
43 ? `${MAX_EXPANED_PREVIEW_HEIGHT}px`
44 : `${MAX_PREVIEW_HEIGHT}px`};
45 overflow: hidden;
46 display: flex;
47 flex-direction: column;
48`;
49
50const ScrollableContent = styled.div<{ shouldShowChevron: boolean }>`
51 overflow-y: auto;
52 padding-bottom: ${(props) => (props.shouldShowChevron ? "24px" : "0px")};
53`;
54
55const ChevronContainer = styled.div`
56 position: sticky;
57 bottom: 0;
58 left: 0;
59 right: 0;
60 display: flex;
61 justify-content: center;
62 background: ${vscEditorBackground};
63 padding: 4px 0;
64 z-index: 10;
65`;
66
67/**
68 * Props for the ExpandableToolbarPreview component
69 */
70interface ExpandableToolbarPreviewProps {
71 /** Title to display in the header */
72 title: string;
73 /** Optional icon to display next to the title */
74 icon?: React.ReactNode;
75 /** ID of the input this preview is associated with (for toolbar hiding logic) */
76 inputId?: string;
77 /** ID of the item being displayed (for toolbar hiding logic) */
78 itemId?: string;
79 /** Whether the preview is initially hidden (overridden if inputId and itemId are provided) */
80 initiallyHidden?: boolean;
81 /** Callback when the delete button is clicked */
82 onDelete?: () => void;
83 /** Callback when the title is clicked */
84 onTitleClick?: (e: React.MouseEvent) => void;
85 /** Border color of the preview */
86 borderColor?: string;
87 /* Whether the preview is selected */
88 isSelected?: boolean;
89 /** Content to display in the preview */
90 children: React.ReactNode;
91}
92
93/**
94 * A generalized expandable preview component for displaying content with a header and expandable body
95 */
96export function ExpandableToolbarPreview(props: ExpandableToolbarPreviewProps) {
97 // Get toolbar visibility state from Redux if inputId and itemId are provided
98 const newestCodeblockForInputId = useAppSelector((store) =>
99 props.inputId
100 ? store.session.newestToolbarPreviewForInput[props.inputId]
101 : undefined,
102 );
103
104 const calculatedInitiallyHidden = useMemo(() => {
105 // If initiallyHidden is explicitly set to false, always start visible
106 if (props.initiallyHidden === false) {
107 return false;
108 }
109
110 // Otherwise, use the inputId/itemId logic
111 if (props.inputId && props.itemId) {
112 return newestCodeblockForInputId !== props.itemId;
113 }
114 return props.initiallyHidden ?? false;
115 }, [
116 newestCodeblockForInputId,
117 props.inputId,
118 props.itemId,
119 props.initiallyHidden,
120 ]);
121
122 const [hidden, setHidden] = useState(calculatedInitiallyHidden);
123 const [isExpanded, setIsExpanded] = useState(false);
124
125 const contentRef = useRef<HTMLDivElement>(null);
126 const [contentDims, setContentDims] = useState({ width: 0, height: 0 });
127
128 useEffect(() => {
129 // Only update hidden state from props if initiallyHidden isn't explicitly set to false
130 if (props.initiallyHidden !== false) {
131 setHidden(calculatedInitiallyHidden);
132 }
133 }, [calculatedInitiallyHidden, props.initiallyHidden]);
134
135 useEffect(() => {
136 const resizeObserver = new ResizeObserver(() => {
137 setContentDims((prevValue) => ({
138 width: contentRef.current?.scrollWidth ?? prevValue.width,
139 height: contentRef.current?.scrollHeight ?? prevValue.height,
140 }));
141 });
142
143 if (contentRef.current) {
144 resizeObserver.observe(contentRef.current);
145 }
146
147 return () => {
148 resizeObserver.disconnect();
149 };
150 }, [contentRef]);
151
152 return (
153 <PreviewDiv
154 spellCheck={false}
155 borderColor={
156 props.isSelected ? vscBadgeBackground : vscCommandCenterInactiveBorder
157 }
158 className="find-widget-skip !my-0"
159 contentEditable={false}
160 >
161 <div
162 className="border-b-command-border m-0 flex cursor-pointer items-center justify-between break-all border-0 border-b-[1px] border-solid px-[5px] py-1.5 hover:opacity-90"
163 style={{
164 fontSize: getFontSize() - 3,
165 }}
166 onClick={() => {
167 setHidden(!hidden);
168 }}
169 >
170 <div
171 className={`flex items-center gap-1 text-[11px] ${!!props.onTitleClick ? "hover:underline" : ""}`}
172 onClick={(e) => {
173 e.stopPropagation();
174 if (props.onTitleClick) {
175 props.onTitleClick(e);
176 }
177 }}
178 >
179 {props.icon}
180 {props.title}
181 </div>
182 <div className="flex items-center gap-1">
183 <HeaderButtonWithToolTip text={hidden ? "Show" : "Hide"}>
184 {!!props.children &&
185 (hidden ? (
186 <EyeIcon className="h-2.5 w-2.5" />
187 ) : (
188 <EyeSlashIcon className="h-2.5 w-2.5" />
189 ))}
190 </HeaderButtonWithToolTip>
191 {props.onDelete && (
192 <HeaderButtonWithToolTip
193 text="Delete"
194 onClick={(e) => {
195 e.stopPropagation();
196 props.onDelete?.();
197 }}
198 >
199 <XMarkIcon className="h-2.5 w-2.5" />
200 </HeaderButtonWithToolTip>
201 )}
202 </div>
203 </div>
204
205 {!hidden && !!props.children && (
206 <ContentContainer expanded={isExpanded}>
207 <ScrollableContent
208 ref={contentRef}
209 shouldShowChevron={contentDims.height > MAX_PREVIEW_HEIGHT}
210 >
211 {props.children}
212 </ScrollableContent>
213
214 {contentDims.height > MAX_PREVIEW_HEIGHT && (
215 <ChevronContainer>
216 <HeaderButtonWithToolTip
217 text={isExpanded ? "Collapse" : "Expand"}
218 >
219 <ChevronDownIcon
220 className="h-3 w-3 transition-all"
221 style={{
222 transform: isExpanded ? "rotate(180deg)" : "",
223 }}
224 onClick={() => setIsExpanded((v) => !v)}
225 />
226 </HeaderButtonWithToolTip>
227 </ChevronContainer>
228 )}
229 </ContentContainer>
230 )}
231 </PreviewDiv>
232 );
233}