11import {
22 FormEvent ,
3+ FormEventHandler ,
34 useCallback ,
45 useEffect ,
56 useMemo ,
@@ -964,14 +965,18 @@ function DBSearchPage() {
964965
965966 const { data : aliasMap } = useAliasMapFromChartConfig ( dbSqlRowTableConfig ) ;
966967
967- const aliasWith = Object . entries ( aliasMap ?? { } ) . map ( ( [ key , value ] ) => ( {
968- name : key ,
969- sql : {
970- sql : value ,
971- params : { } ,
972- } ,
973- isSubquery : false ,
974- } ) ) ;
968+ const aliasWith = useMemo (
969+ ( ) =>
970+ Object . entries ( aliasMap ?? { } ) . map ( ( [ key , value ] ) => ( {
971+ name : key ,
972+ sql : {
973+ sql : value ,
974+ params : { } ,
975+ } ,
976+ isSubquery : false ,
977+ } ) ) ,
978+ [ aliasMap ] ,
979+ ) ;
975980
976981 const histogramTimeChartConfig = useMemo ( ( ) => {
977982 if ( chartConfig == null ) {
@@ -1006,6 +1011,60 @@ function DBSearchPage() {
10061011 } ;
10071012 } , [ chartConfig , searchedSource , aliasWith , searchedTimeRange ] ) ;
10081013
1014+ const onFormSubmit = useCallback < FormEventHandler < HTMLFormElement > > (
1015+ e => {
1016+ e . preventDefault ( ) ;
1017+ onSubmit ( ) ;
1018+ return false ;
1019+ } ,
1020+ [ onSubmit ] ,
1021+ ) ;
1022+
1023+ const handleTimeRangeSelect = useCallback (
1024+ ( d1 : Date , d2 : Date ) => {
1025+ onTimeRangeSelect ( d1 , d2 ) ;
1026+ setIsLive ( false ) ;
1027+ } ,
1028+ [ onTimeRangeSelect ] ,
1029+ ) ;
1030+
1031+ const onTimeChartError = useCallback (
1032+ ( error : Error | ClickHouseQueryError ) =>
1033+ setQueryErrors ( prev => ( {
1034+ ...prev ,
1035+ DBTimeChart : error ,
1036+ } ) ) ,
1037+ [ setQueryErrors ] ,
1038+ ) ;
1039+
1040+ const filtersChartConfig = useMemo < ChartConfigWithDateRange > ( ( ) => {
1041+ const overrides = {
1042+ orderBy : undefined ,
1043+ dateRange : searchedTimeRange ,
1044+ with : aliasWith ,
1045+ } as const ;
1046+ return chartConfig
1047+ ? {
1048+ ...chartConfig ,
1049+ ...overrides ,
1050+ }
1051+ : {
1052+ timestampValueExpression : '' ,
1053+ connection : '' ,
1054+ from : {
1055+ databaseName : '' ,
1056+ tableName : '' ,
1057+ } ,
1058+ where : '' ,
1059+ select : '' ,
1060+ ...overrides ,
1061+ } ;
1062+ } , [ chartConfig , searchedTimeRange , aliasWith ] ) ;
1063+
1064+ const openNewSourceModal = useCallback ( ( ) => {
1065+ setNewSourceModalOpened ( true ) ;
1066+ } , [ ] ) ;
1067+
10091068 return (
10101069 < Flex direction = "column" h = "100vh" style = { { overflow : 'hidden' } } >
10111070 { ! IS_LOCAL_MODE && isAlertModalOpen && (
@@ -1017,13 +1076,7 @@ function DBSearchPage() {
10171076 />
10181077 ) }
10191078 < OnboardingModal />
1020- < form
1021- onSubmit = { e => {
1022- e . preventDefault ( ) ;
1023- onSubmit ( ) ;
1024- return false ;
1025- } }
1026- >
1079+ < form onSubmit = { onFormSubmit } >
10271080 { /* <DevTool control={control} /> */ }
10281081 < Flex gap = "sm" px = "sm" pt = "sm" wrap = "nowrap" >
10291082 < Group gap = "4px" wrap = "nowrap" >
@@ -1032,9 +1085,7 @@ function DBSearchPage() {
10321085 size = "xs"
10331086 control = { control }
10341087 name = "source"
1035- onCreate = { ( ) => {
1036- setNewSourceModalOpened ( true ) ;
1037- } }
1088+ onCreate = { openNewSourceModal }
10381089 />
10391090 < ActionIcon
10401091 variant = "subtle"
@@ -1297,12 +1348,7 @@ function DBSearchPage() {
12971348 isLive = { isLive }
12981349 analysisMode = { analysisMode }
12991350 setAnalysisMode = { setAnalysisMode }
1300- chartConfig = { {
1301- ...chartConfig ,
1302- orderBy : undefined ,
1303- dateRange : searchedTimeRange ,
1304- with : aliasWith ,
1305- } }
1351+ chartConfig = { filtersChartConfig }
13061352 sourceId = { inputSourceObj ?. id }
13071353 showDelta = { ! ! searchedSource ?. durationExpression }
13081354 { ...searchFilters }
@@ -1344,16 +1390,8 @@ function DBSearchPage() {
13441390 enabled = { isReady }
13451391 showDisplaySwitcher = { false }
13461392 queryKeyPrefix = { QUERY_KEY_PREFIX }
1347- onTimeRangeSelect = { ( d1 , d2 ) => {
1348- onTimeRangeSelect ( d1 , d2 ) ;
1349- setIsLive ( false ) ;
1350- } }
1351- onError = { error =>
1352- setQueryErrors ( prev => ( {
1353- ...prev ,
1354- DBTimeChart : error ,
1355- } ) )
1356- }
1393+ onTimeRangeSelect = { handleTimeRangeSelect }
1394+ onError = { onTimeChartError }
13571395 />
13581396 </ Box >
13591397 ) }
@@ -1464,16 +1502,8 @@ function DBSearchPage() {
14641502 enabled = { isReady }
14651503 showDisplaySwitcher = { false }
14661504 queryKeyPrefix = { QUERY_KEY_PREFIX }
1467- onTimeRangeSelect = { ( d1 , d2 ) => {
1468- onTimeRangeSelect ( d1 , d2 ) ;
1469- setIsLive ( false ) ;
1470- } }
1471- onError = { error =>
1472- setQueryErrors ( prev => ( {
1473- ...prev ,
1474- DBTimeChart : error ,
1475- } ) )
1476- }
1505+ onTimeRangeSelect = { handleTimeRangeSelect }
1506+ onError = { onTimeChartError }
14771507 />
14781508 </ Box >
14791509 ) }
0 commit comments