Skip to content

Commit 6dc6989

Browse files
authored
feat: Save last source as default source on search page load (#790)
fixes HDX-1680
1 parent 5254a6f commit 6dc6989

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

.changeset/happy-hornets-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/app": patch
3+
---
4+
5+
feat: Automatically use last used source when loading search page

packages/app/src/DBSearchPage.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ import {
9292
useSources,
9393
} from '@/source';
9494
import { parseTimeQuery, useNewTimeQuery } from '@/timeQuery';
95-
import { QUERY_LOCAL_STORAGE, usePrevious } from '@/utils';
95+
import { QUERY_LOCAL_STORAGE, useLocalStorage, usePrevious } from '@/utils';
9696

9797
import { SQLPreview } from './components/ChartSQLPreview';
9898
import PatternTable from './components/PatternTable';
@@ -472,6 +472,10 @@ function DBSearchPage() {
472472
);
473473

474474
const { data: sources } = useSources();
475+
const [lastSelectedSourceId, setLastSelectedSourceId] = useLocalStorage(
476+
'hdx-last-selected-source-id',
477+
'',
478+
);
475479
const { data: searchedSource } = useSource({
476480
id: searchedConfig.source,
477481
});
@@ -514,7 +518,13 @@ function DBSearchPage() {
514518
select: searchedConfig.select || '',
515519
where: searchedConfig.where || '',
516520
whereLanguage: searchedConfig.whereLanguage ?? 'lucene',
517-
source: searchedConfig.source ?? sources?.[0]?.id ?? '',
521+
source:
522+
searchedConfig.source ??
523+
(lastSelectedSourceId &&
524+
sources?.some(s => s.id === lastSelectedSourceId)
525+
? lastSelectedSourceId
526+
: sources?.[0]?.id) ??
527+
'',
518528
filters: searchedConfig.filters ?? [],
519529
orderBy: searchedConfig.orderBy ?? '',
520530
},
@@ -623,6 +633,8 @@ function DBSearchPage() {
623633
setSearchedConfig,
624634
savedSearchId,
625635
inputSource,
636+
lastSelectedSourceId,
637+
sources,
626638
]);
627639

628640
const [_queryErrors, setQueryErrors] = useState<{
@@ -676,6 +688,9 @@ function DBSearchPage() {
676688
s => s.id === data.source,
677689
);
678690
if (newInputSourceObj != null) {
691+
// Save the selected source ID to localStorage
692+
setLastSelectedSourceId(newInputSourceObj.id);
693+
679694
setValue(
680695
'select',
681696
newInputSourceObj?.defaultTableSelectExpression ?? '',
@@ -692,7 +707,14 @@ function DBSearchPage() {
692707
}
693708
});
694709
return () => unsubscribe();
695-
}, [watch, inputSourceObj, setValue, inputSourceObjs, searchFilters]);
710+
}, [
711+
watch,
712+
inputSourceObj,
713+
setValue,
714+
inputSourceObjs,
715+
searchFilters,
716+
setLastSelectedSourceId,
717+
]);
696718

697719
const onTableScroll = useCallback(
698720
(scrollTop: number) => {

0 commit comments

Comments
 (0)