Elffuss BraidHub

kikocis/continue public

e9ecc1af12bb · 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, 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 [contentElement, setContentElement] = useState<HTMLDivElement | null>(
126 null,
127 );
128 const [contentDims, setContentDims] = useState({ width: 0, height: 0 });
129
130 useEffect(() => {
131 // Only update hidden state from props if initiallyHidden isn't explicitly set to false
132 if (props.initiallyHidden !== false) {
133 setHidden(calculatedInitiallyHidden);
134 }
135 }, [calculatedInitiallyHidden, props.initiallyHidden]);
136
137 useEffect(() => {
138 if (!contentElement) return;
139
140 const resizeObserver = new ResizeObserver(() => {
141 setContentDims({
142 width: contentElement.scrollWidth,
143 height: contentElement.scrollHeight,
144 });
145 });
146
147 resizeObserver.observe(contentElement);
148
149 return () => {
150 resizeObserver.disconnect();
151 };
152 }, [contentElement]);
153
154 return (
155 <PreviewDiv
156 spellCheck={false}
157 borderColor={
158 props.isSelected ? vscBadgeBackground : vscCommandCenterInactiveBorder
159 }
160 className="find-widget-skip !my-0"
161 contentEditable={false}
162 >
163 <div
164 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"
165 style={{
166 fontSize: getFontSize() - 3,
167 }}
168 onClick={() => {
169 setHidden(!hidden);
170 }}
171 >
172 <div
173 className={`flex items-center gap-1 text-[11px] ${!!props.onTitleClick ? "hover:underline" : ""}`}
174 onClick={(e) => {
175 e.stopPropagation();
176 if (props.onTitleClick) {
177 props.onTitleClick(e);
178 }
179 }}
180 >
181 {props.icon}
182 {props.title}
183 </div>
184 <div className="flex items-center gap-1">
185 <HeaderButtonWithToolTip text={hidden ? "Show" : "Hide"}>
186 {!!props.children &&
187 (hidden ? (
188 <EyeIcon className="h-2.5 w-2.5" />
189 ) : (
190 <EyeSlashIcon className="h-2.5 w-2.5" />
191 ))}
192 </HeaderButtonWithToolTip>
193 {props.onDelete && (
194 <HeaderButtonWithToolTip
195 text="Delete"
196 onClick={(e) => {
197 e.stopPropagation();
198 props.onDelete?.();
199 }}
200 >
201 <XMarkIcon className="h-2.5 w-2.5" />
202 </HeaderButtonWithToolTip>
203 )}
204 </div>
205 </div>
206
207 {!hidden && !!props.children && (
208 <ContentContainer expanded={isExpanded}>
209 <ScrollableContent
210 ref={setContentElement}
211 shouldShowChevron={contentDims.height > MAX_PREVIEW_HEIGHT}
212 >
213 {props.children}
214 </ScrollableContent>
215
216 {contentDims.height > MAX_PREVIEW_HEIGHT && (
217 <ChevronContainer>
218 <HeaderButtonWithToolTip
219 text={isExpanded ? "Collapse" : "Expand"}
220 >
221 <ChevronDownIcon
222 className="h-3 w-3 transition-all"
223 style={{
224 transform: isExpanded ? "rotate(180deg)" : "",
225 }}
226 onClick={() => setIsExpanded((v) => !v)}
227 />
228 </HeaderButtonWithToolTip>
229 </ChevronContainer>
230 )}
231 </ContentContainer>
232 )}
233 </PreviewDiv>
234 );
235}