Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generative-ai): update generative ai input to be resizable text area COMPASS-7940 #5869

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 86 additions & 36 deletions packages/compass-generative-ai/src/components/generative-ai-input.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import React, {
forwardRef,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react';
import {
Banner,
BannerVariant,
Button,
Icon,
IconButton,
SpinLoader,
TextInput,
TextArea,
css,
cx,
focusRing,
Expand All @@ -28,6 +35,7 @@ const containerStyles = css({
width: '100%',
flexDirection: 'column',
gap: spacing[25],
padding: `0px ${spacing[100]}px`,
marginBottom: spacing[300],
});

Expand Down Expand Up @@ -123,11 +131,17 @@ const inputContainerStyles = css({
position: 'relative',
});

const defaultTextAreaSize = spacing[400] + spacing[300];
const textInputStyles = css({
flexGrow: 1,
input: {
textarea: {
margin: 0,
padding: spacing[50] + spacing[25],
paddingLeft: spacing[800],
paddingRight: spacing[1600] * 2 + spacing[200],
height: defaultTextAreaSize, // Default height, overridden runtime.
minHeight: `${defaultTextAreaSize}px`,
maxHeight: spacing[1600] * 2,
},
});

Expand Down Expand Up @@ -216,6 +230,48 @@ const aiEntryContainerStyles = css({
display: 'flex',
});

function adjustHeight(elementRef: React.RefObject<HTMLTextAreaElement>) {
if (elementRef.current) {
// Dynamically set the text area height based on the scroll height.
// We first set it to 0 so the correct scroll height is used.
elementRef.current.style.height = '0px';
let adjustedHeight = elementRef.current.scrollHeight;
if (adjustedHeight > defaultTextAreaSize) {
// When it's greater than one line, we add pixels so that
// we don't show a scrollbar when it's still under the maxHeight.
adjustedHeight += spacing[50];
}
elementRef.current.style.height = `${adjustedHeight}px`;
}
}

const VerticallyResizingTextArea = forwardRef(
function VerticallyResizingTextArea(
{ value, ...props }: React.ComponentProps<typeof TextArea>,
forwardedRef: React.ForwardedRef<HTMLTextAreaElement>
) {
const ref = useRef<HTMLTextAreaElement | null>(null);

useLayoutEffect(() => adjustHeight(ref), []);
useEffect(() => adjustHeight(ref), [value]);

return (
<TextArea
ref={(node) => {
ref.current = node;
if (typeof forwardedRef === 'function') {
forwardedRef(node);
} else if (forwardedRef) {
forwardedRef.current = node;
}
}}
value={value}
{...props}
/>
);
}
);

const closeText = 'Close AI Helper';

const SubmitArrowSVG = ({ darkMode }: { darkMode?: boolean }) => (
Expand Down Expand Up @@ -278,10 +334,10 @@ function GenerativeAIInput({
isAggregationGeneratedFromQuery = false,
onResetIsAggregationGeneratedFromQuery,
}: GenerativeAIInputProps) {
const promptTextInputRef = useRef<HTMLInputElement>(null);
const promptTextInputRef = useRef<HTMLTextAreaElement>(null);
const [showSuccess, setShowSuccess] = useState(false);
const darkMode = useDarkMode();
const guideCueRef = useRef<HTMLInputElement>(null);
const guideCueRef = useRef<HTMLTextAreaElement>(null);

const handleSubmit = useCallback(
(aiPromptText: string) => {
Expand All @@ -292,8 +348,8 @@ function GenerativeAIInput({
);

const onTextInputKeyDown = useCallback(
(evt: React.KeyboardEvent<HTMLInputElement>) => {
if (evt.key === 'Enter') {
(evt: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (evt.key === 'Enter' && !evt.shiftKey) {
evt.preventDefault();
if (!aiPromptText) {
return;
Expand Down Expand Up @@ -341,24 +397,38 @@ function GenerativeAIInput({
<div className={inputBarContainerStyles}>
<div className={inputContainerStyles}>
<div className={isFetching ? gradientAnimationStyles : undefined}>
<div
className={contentWrapperStyles}
data-testid="ai-user-text-input-wrapper"
>
<TextInput
<div className={contentWrapperStyles}>
<button
className={closeAIButtonStyles}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by: moved this button up so that it's earlier in the tab order which is more inline with where it's rendered.

data-testid="close-ai-button"
aria-label={closeText}
title={closeText}
onClick={() => onClose()}
>
<AIGuideCue
showGuideCue={isAggregationGeneratedFromQuery}
onCloseGuideCue={() => {
onResetIsAggregationGeneratedFromQuery?.();
}}
refEl={guideCueRef}
title="Aggregation generated"
description="Your query requires stages from MongoDB's aggregation framework. Continue to work on it in our Aggregation Pipeline Builder"
/>
<span className={aiEntryContainerStyles} ref={guideCueRef}>
<Icon glyph="Sparkle" />
</span>
</button>
<VerticallyResizingTextArea
className={cx(
textInputStyles,
isFetching && isFetchingOverrideTextInputStyles
)}
ref={promptTextInputRef}
sizeVariant="small"
data-testid="ai-user-text-input"
aria-label="Enter a plain text query that the AI will translate into MongoDB query language."
placeholder={placeholder}
value={aiPromptText}
onChange={(evt: React.ChangeEvent<HTMLInputElement>) =>
onChangeAIPromptText(evt.currentTarget.value)
}
onChange={(evt) => onChangeAIPromptText(evt.target.value)}
onKeyDown={onTextInputKeyDown}
/>
<div className={floatingButtonsContainerStyles}>
Expand Down Expand Up @@ -424,26 +494,6 @@ function GenerativeAIInput({
)}
</Button>
</div>
<button
className={closeAIButtonStyles}
data-testid="close-ai-button"
aria-label={closeText}
title={closeText}
onClick={() => onClose()}
>
<AIGuideCue
showGuideCue={isAggregationGeneratedFromQuery}
onCloseGuideCue={() => {
onResetIsAggregationGeneratedFromQuery?.();
}}
refEl={guideCueRef}
title="Aggregation generated"
description="Your query requires stages from MongoDB's aggregation framework. Continue to work on it in our Aggregation Pipeline Builder"
/>
<span className={aiEntryContainerStyles} ref={guideCueRef}>
<Icon glyph="Sparkle" />
</span>
</button>
</div>
</div>
</div>
Expand Down
18 changes: 6 additions & 12 deletions packages/compass-query-bar/src/components/query-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ const queryOptionsContainerStyles = css({
gap: spacing[2],
});

const queryAIContainerStyles = css({
margin: `0px ${spacing[2]}px`,
});

const QueryOptionsToggle = connect(
(state: RootState) => {
return {
Expand Down Expand Up @@ -218,14 +214,12 @@ export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
data-apply-id={applyId}
>
{isAIFeatureEnabled && (
<div className={queryAIContainerStyles}>
<QueryAI
onClose={() => {
onHideAIInputClick?.();
}}
show={isAIInputVisible}
/>
</div>
<QueryAI
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by: align the query and aggregation ai prompt input margins.

onClose={() => {
onHideAIInputClick?.();
}}
show={isAIInputVisible}
/>
)}
<div className={queryBarFirstRowStyles}>
{enableSavedAggregationsQueries && <QueryHistoryButtonPopover />}
Expand Down
Loading