Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions static/app/components/searchQueryBuilder/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import useOrganization from 'sentry/utils/useOrganization';

interface SearchQueryBuilderContextData {
actionBarRef: React.RefObject<HTMLDivElement | null>;
autoSubmitSeer: boolean;
committedQuery: string;
currentInputValue: string;
disabled: boolean;
disallowFreeText: boolean;
disallowWildcard: boolean;
Expand All @@ -49,6 +51,8 @@ interface SearchQueryBuilderContextData {
parsedQuery: ParseResult | null;
query: string;
searchSource: string;
setAutoSubmitSeer: (enabled: boolean) => void;
setCurrentInputValue: (value: string) => void;
setDisplaySeerResults: (enabled: boolean) => void;
size: 'small' | 'normal';
wrapperRef: React.RefObject<HTMLDivElement | null>;
Expand Down Expand Up @@ -108,6 +112,8 @@ export function SearchQueryBuilderProvider({
const {setupAcknowledgement} = useOrganizationSeerSetup({enabled: enableAISearch});

const [displaySeerResults, setDisplaySeerResults] = useState(false);
const [autoSubmitSeer, setAutoSubmitSeer] = useState(false);
const [currentInputValue, setCurrentInputValue] = useState('');

const {state, dispatch} = useQueryBuilderState({
initialQuery,
Expand Down Expand Up @@ -191,16 +197,21 @@ export function SearchQueryBuilderProvider({
portalTarget,
displaySeerResults,
setDisplaySeerResults,
autoSubmitSeer,
setAutoSubmitSeer,
replaceRawSearchKeys,
filterKeyAliases,
gaveSeerConsent: setupAcknowledgement.orgHasAcknowledged,
currentInputValue,
setCurrentInputValue,
};
}, [
disabled,
disallowFreeText,
disallowWildcard,
dispatch,
displaySeerResults,
autoSubmitSeer,
enableAISearch,
filterKeyAliases,
filterKeyMenuWidth,
Expand All @@ -220,6 +231,8 @@ export function SearchQueryBuilderProvider({
stableFilterKeys,
stableGetSuggestedFilterKey,
state,
currentInputValue,
setCurrentInputValue,
]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ export function useFilterKeyListBox({filterValue}: {filterValue: string}) {
filterKeys,
getFieldDefinition,
setDisplaySeerResults,
setAutoSubmitSeer,
enableAISearch,
gaveSeerConsent,
currentInputValue,
} = useSearchQueryBuilder();
const {sectionedItems} = useFilterKeyItems();
const recentFilters = useRecentSearchFilters();
Expand Down Expand Up @@ -391,6 +393,12 @@ export function useFilterKeyListBox({filterValue}: {filterValue: string}) {
action: 'opened',
});
setDisplaySeerResults(true);

if (currentInputValue?.trim()) {
setAutoSubmitSeer(true);
} else {
setAutoSubmitSeer(false);
}
return;
}

Expand All @@ -403,7 +411,13 @@ export function useFilterKeyListBox({filterValue}: {filterValue: string}) {
return;
}
},
[organization, seerAcknowledgeMutate, setDisplaySeerResults]
[
organization,
seerAcknowledgeMutate,
setDisplaySeerResults,
setAutoSubmitSeer,
currentInputValue,
]
);

return {
Expand Down
13 changes: 8 additions & 5 deletions static/app/components/searchQueryBuilder/tokens/freeText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,6 @@ function SearchQueryBuilderInputInternal({
setSelectionIndex(inputRef.current?.selectionStart ?? 0);
}, []);

const resetInputValue = useCallback(() => {
setInputValue(trimmedTokenValue);
updateSelectionIndex();
}, [trimmedTokenValue, updateSelectionIndex]);

const filterValue = getWordAtCursorPosition(inputValue, selectionIndex);

const {
Expand All @@ -270,8 +265,15 @@ function SearchQueryBuilderInputInternal({
placeholder,
searchSource,
recentSearches,
setCurrentInputValue,
} = useSearchQueryBuilder();

const resetInputValue = useCallback(() => {
setInputValue(trimmedTokenValue);
setCurrentInputValue(trimmedTokenValue);
updateSelectionIndex();
}, [trimmedTokenValue, updateSelectionIndex, setCurrentInputValue]);

const {customMenu, sectionItems, maxOptions, onKeyDownCapture, handleOptionSelected} =
useFilterKeyListBox({
filterValue,
Expand Down Expand Up @@ -617,6 +619,7 @@ function SearchQueryBuilderInputInternal({
}

setInputValue(e.target.value);
setCurrentInputValue(e.target.value);
setSelectionIndex(e.target.selectionStart ?? 0);
}}
onKeyDown={onKeyDown}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ function isNoneOfTheseItem(item: SeerSearchItems): item is NoneOfTheseItem {
type SeerSearchItems = SeerSearchItem<string> | NoneOfTheseItem;

export function SeerComboBox({initialQuery, ...props}: SeerComboBoxProps) {
// console.log('initialQuery', initialQuery);
// console.log('props', props);
const buttonRef = useRef<HTMLButtonElement>(null);
const listBoxRef = useRef<HTMLUListElement>(null);
const popoverRef = useRef<HTMLDivElement>(null);
Expand All @@ -106,7 +108,8 @@ export function SeerComboBox({initialQuery, ...props}: SeerComboBoxProps) {
);

const openForm = useFeedbackForm();
const {setDisplaySeerResults} = useSearchQueryBuilder();
const {setDisplaySeerResults, autoSubmitSeer, setAutoSubmitSeer} =
useSearchQueryBuilder();
const {rawResult, submitQuery, isPending} = useSeerSearch();
const applySeerSearchQuery = useApplySeerSearchQuery();
const organization = useOrganization();
Expand Down Expand Up @@ -321,6 +324,19 @@ export function SeerComboBox({initialQuery, ...props}: SeerComboBoxProps) {
}
}, [state]);

// Auto-submit search when autoSubmitSeer flag is true
useLayoutEffect(() => {
if (autoSubmitSeer && searchQuery.trim()) {
trackAnalytics('trace.explorer.ai_query_submitted', {
organization,
natural_language_query: searchQuery.trim(),
});
submitQuery(searchQuery.trim());
// Reset the flag after submitting
setAutoSubmitSeer(false);
}
}, [autoSubmitSeer, searchQuery, organization, submitQuery, setAutoSubmitSeer]);

return (
<Wrapper ref={containerRef} isDropdownOpen={state.isOpen}>
<PositionedSearchIconContainer>
Expand Down
17 changes: 15 additions & 2 deletions static/app/views/explore/spans/spansTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,23 @@ function SpansSearchBar({
}: {
eapSpanSearchQueryBuilderProps: EAPSpanSearchQueryBuilderProps;
}) {
const {displaySeerResults, query} = useSearchQueryBuilder();
const {displaySeerResults, query, currentInputValue} = useSearchQueryBuilder();

const initialSeerQuery = (() => {
const committedQuery = query.trim();
const inputValue = currentInputValue.trim();

if (!inputValue) return committedQuery;

if (!committedQuery) return inputValue;

if (committedQuery.includes(inputValue)) return committedQuery;

return `${committedQuery} ${inputValue}`;
})();

return displaySeerResults ? (
<SeerComboBox initialQuery={query} />
<SeerComboBox initialQuery={initialSeerQuery} />
) : (
<EAPSpanSearchQueryBuilder autoFocus {...eapSpanSearchQueryBuilderProps} />
);
Expand Down
16 changes: 12 additions & 4 deletions static/app/views/explore/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,26 @@ export function formatQueryToNaturalLanguage(query: string): string {
return formattedTokens.reduce((result, token, index) => {
if (index === 0) return token;

const prevToken = formattedTokens[index - 1];
if (!prevToken) return `${result}, ${token}`;
const currentOriginalToken = tokens[index] || '';
const prevOriginalToken = tokens[index - 1] || '';

const isLogicalOp = token.toUpperCase() === 'AND' || token.toUpperCase() === 'OR';
const prevIsLogicalOp =
prevToken.toUpperCase() === 'AND' || prevToken.toUpperCase() === 'OR';
formattedTokens[index - 1]?.toUpperCase() === 'AND' ||
formattedTokens[index - 1]?.toUpperCase() === 'OR';

if (isLogicalOp || prevIsLogicalOp) {
return `${result} ${token}`;
}

return `${result}, ${token}`;
const isCurrentFilter = /[:>=<!]/.test(currentOriginalToken);
const isPrevFilter = /[:>=<!]/.test(prevOriginalToken);

if (isCurrentFilter && isPrevFilter) {
return `${result}, ${token}`;
}

return `${result} ${token}`;
}, '');
}

Expand Down
Loading