1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
import React, { useCallback, useEffect, useState } from "react";
import styled from "styled-components";
import {
StyledTooltip,
defaultBorderRadius,
lightGray,
secondaryDark,
vscForeground,
} from ".";
import {
PaperAirplaneIcon,
SparklesIcon,
XMarkIcon,
} from "@heroicons/react/24/outline";
import { useSelector } from "react-redux";
import { RootStore } from "../redux/store";
import HeaderButtonWithText from "./HeaderButtonWithText";
import { getFontSize } from "../util";
import { usePostHog } from "posthog-js/react";
const Div = styled.div<{ isDisabled: boolean }>`
border-radius: ${defaultBorderRadius};
cursor: ${(props) => (props.isDisabled ? "not-allowed" : "pointer")};
padding: 8px 8px;
background-color: ${secondaryDark};
border: 1px solid transparent;
display: flex;
justify-content: space-between;
align-items: center;
color: ${(props) => (props.isDisabled ? lightGray : vscForeground)};
&:hover {
border: ${(props) =>
props.isDisabled ? "1px solid transparent" : `1px solid ${lightGray}`};
}
`;
const P = styled.p<{ fontSize: number }>`
font-size: ${(props) => props.fontSize}px;
margin: 0;
`;
interface SuggestionsDivProps {
title: string;
description: string;
textInput: string;
onClick?: () => void;
disabled: boolean;
}
function SuggestionsDiv(props: SuggestionsDivProps) {
const [isHovered, setIsHovered] = useState(false);
return (
<>
<Div
data-tooltip-id={`suggestion-disabled-${props.textInput.replace(
" ",
""
)}`}
onClick={props.onClick}
onMouseEnter={() => {
if (props.disabled) return;
setIsHovered(true);
}}
onMouseLeave={() => setIsHovered(false)}
isDisabled={props.disabled}
>
<P fontSize={getFontSize()}>{props.description}</P>
<PaperAirplaneIcon
width="1.6em"
height="1.6em"
style={{
opacity: isHovered ? 1 : 0,
backgroundColor: secondaryDark,
boxShadow: `1px 1px 10px ${secondaryDark}`,
borderRadius: defaultBorderRadius,
}}
/>
</Div>
<StyledTooltip
id={`suggestion-disabled-${props.textInput.replace(" ", "")}`}
place="bottom"
hidden={!props.disabled}
>
Must highlight code first
</StyledTooltip>
</>
);
}
const stageDescriptions = [
<p>Ask a question</p>,
<p>
1. Highlight code in the editor
<br />
2. Press cmd+M to select the code
<br />
3. Ask a question
</p>,
<p>
1. Highlight code in the editor
<br />
2. Press cmd+shift+M to select the code
<br />
3. Request an edit
</p>,
];
const suggestionsStages: any[][] = [
// [
// {
// title: stageDescriptions[0],
// description: "How does merge sort work?",
// textInput: "How does merge sort work?",
// },
// {
// title: stageDescriptions[0],
// description: "How do I sum over a column in SQL?",
// textInput: "How do I sum over a column in SQL?",
// },
// ],
[
{
title: stageDescriptions[1],
description: "Is there any way to make this code more efficient?",
textInput: "Is there any way to make this code more efficient?",
},
{
title: stageDescriptions[1],
description: "What does this function do?",
textInput: "What does this function do?",
},
],
[
{
title: stageDescriptions[2],
description: "/edit write comments for this code",
textInput: "/edit write comments for this code",
},
{
title: stageDescriptions[2],
description: "/edit make this code more efficient",
textInput: "/edit make this code more efficient",
},
],
];
const NUM_STAGES = suggestionsStages.length;
const TutorialDiv = styled.div`
margin: 4px;
margin-left: 8px;
margin-right: 8px;
position: relative;
background-color: #ff02;
border-radius: ${defaultBorderRadius};
padding: 8px 4px;
`;
function SuggestionsArea(props: { onClick: (textInput: string) => void }) {
const posthog = usePostHog();
const [stage, setStage] = useState(
parseInt(localStorage.getItem("stage") || "0")
);
const timeline = useSelector(
(state: RootStore) => state.serverState.history.timeline
);
const sessionId = useSelector(
(state: RootStore) => state.serverState.session_info?.session_id
);
const codeIsHighlighted = useSelector((state: RootStore) =>
state.serverState.selected_context_items.some(
(item) => item.description.id.provider_title === "code"
)
);
const [hide, setHide] = useState(false);
useEffect(() => {
setHide(false);
}, [sessionId]);
const [numTutorialInputs, setNumTutorialInputs] = useState(0);
const inputsAreOnlyTutorial = useCallback(() => {
const inputs = timeline.filter(
(node) => !node.step.hide && node.step.name === "User Input"
);
return inputs.length - numTutorialInputs <= 0;
}, [timeline, numTutorialInputs]);
return (
<>
{hide || stage > NUM_STAGES - 1 || !inputsAreOnlyTutorial() || (
<TutorialDiv>
<div className="flex">
<SparklesIcon width="1.3em" height="1.3em" color="yellow" />
<b className="ml-1">
Tutorial ({stage + 1}/{NUM_STAGES})
</b>
</div>
<p style={{ color: vscForeground, paddingLeft: "4px" }}>
{stage < suggestionsStages.length &&
suggestionsStages[stage][0]?.title}
</p>
<HeaderButtonWithText
className="absolute right-1 top-1 cursor-pointer"
text="Close Tutorial"
onClick={() => {
setHide(true);
const tutorialClosedCount = parseInt(
localStorage.getItem("tutorialClosedCount") || "0"
);
localStorage.setItem(
"tutorialClosedCount",
(tutorialClosedCount + 1).toString()
);
posthog?.capture("tutorial_closed", {
stage,
tutorialClosedCount,
});
}}
>
<XMarkIcon width="1.2em" height="1.2em" />
</HeaderButtonWithText>
<div className="grid grid-cols-2 gap-2 mt-2">
{suggestionsStages[stage]?.map((suggestion) => (
<SuggestionsDiv
disabled={!codeIsHighlighted}
{...suggestion}
onClick={() => {
if (!codeIsHighlighted) return;
props.onClick(suggestion.textInput);
posthog?.capture("tutorial_stage_complete", { stage });
setStage(stage + 1);
localStorage.setItem("stage", (stage + 1).toString());
setHide(true);
setNumTutorialInputs((prev) => prev + 1);
}}
/>
))}
</div>
</TutorialDiv>
)}
</>
);
}
export default SuggestionsArea;
|