1
+ import { useEffect , useRef } from 'react' ;
1
2
import objectHash from 'object-hash' ;
2
3
import {
3
4
ColumnMeta ,
@@ -20,6 +21,25 @@ import api from '@/api';
20
21
import { getMetadata } from '@/metadata' ;
21
22
import { toArray } from '@/utils' ;
22
23
24
+ // Hook to get metadata with proper settings applied
25
+ // TODO: replace all getMetadata calls with useMetadataWithSettings
26
+ export function useMetadataWithSettings ( ) {
27
+ const metadata = getMetadata ( ) ;
28
+ const { data : me } = api . useMe ( ) ;
29
+ const settingsApplied = useRef ( false ) ;
30
+
31
+ useEffect ( ( ) => {
32
+ if ( me ?. team ?. metadataMaxRowsToRead && ! settingsApplied . current ) {
33
+ metadata . setClickHouseSettings ( {
34
+ max_rows_to_read : me . team . metadataMaxRowsToRead ,
35
+ } ) ;
36
+ settingsApplied . current = true ;
37
+ }
38
+ } , [ me ?. team ?. metadataMaxRowsToRead , metadata ] ) ;
39
+
40
+ return metadata ;
41
+ }
42
+
23
43
export function useColumns (
24
44
{
25
45
databaseName,
@@ -32,10 +52,10 @@ export function useColumns(
32
52
} ,
33
53
options ?: Partial < UseQueryOptions < ColumnMeta [ ] > > ,
34
54
) {
55
+ const metadata = useMetadataWithSettings ( ) ;
35
56
return useQuery < ColumnMeta [ ] > ( {
36
57
queryKey : [ 'useMetadata.useColumns' , { databaseName, tableName } ] ,
37
58
queryFn : async ( ) => {
38
- const metadata = getMetadata ( ) ;
39
59
return metadata . getColumns ( {
40
60
databaseName,
41
61
tableName,
@@ -51,10 +71,10 @@ export function useJsonColumns(
51
71
{ databaseName, tableName, connectionId } : TableConnection ,
52
72
options ?: Partial < UseQueryOptions < string [ ] > > ,
53
73
) {
74
+ const metadata = useMetadataWithSettings ( ) ;
54
75
return useQuery < string [ ] > ( {
55
76
queryKey : [ 'useMetadata.useJsonColumns' , { databaseName, tableName } ] ,
56
77
queryFn : async ( ) => {
57
- const metadata = getMetadata ( ) ;
58
78
const columns = await metadata . getColumns ( {
59
79
databaseName,
60
80
tableName,
@@ -78,7 +98,7 @@ export function useAllFields(
78
98
const tableConnections = Array . isArray ( _tableConnections )
79
99
? _tableConnections
80
100
: [ _tableConnections ] ;
81
- const metadata = getMetadata ( ) ;
101
+ const metadata = useMetadataWithSettings ( ) ;
82
102
const { data : me , isFetched } = api . useMe ( ) ;
83
103
return useQuery < Field [ ] > ( {
84
104
queryKey : [
@@ -91,13 +111,6 @@ export function useAllFields(
91
111
return [ ] ;
92
112
}
93
113
94
- // TODO: set the settings at the top level so that it doesn't have to be set for each useQuery
95
- if ( team ?. metadataMaxRowsToRead ) {
96
- metadata . setClickHouseSettings ( {
97
- max_rows_to_read : team . metadataMaxRowsToRead ,
98
- } ) ;
99
- }
100
-
101
114
const fields2d = await Promise . all (
102
115
tableConnections . map ( tc => metadata . getAllFields ( tc ) ) ,
103
116
) ;
@@ -129,7 +142,7 @@ export function useTableMetadata(
129
142
} ,
130
143
options ?: Omit < UseQueryOptions < any , Error > , 'queryKey' > ,
131
144
) {
132
- const metadata = getMetadata ( ) ;
145
+ const metadata = useMetadataWithSettings ( ) ;
133
146
return useQuery < TableMetadata > ( {
134
147
queryKey : [ 'useMetadata.useTableMetadata' , { databaseName, tableName } ] ,
135
148
queryFn : async ( ) => {
@@ -159,9 +172,8 @@ export function useGetKeyValues(
159
172
} ,
160
173
options ?: Omit < UseQueryOptions < any , Error > , 'queryKey' > ,
161
174
) {
162
- const metadata = getMetadata ( ) ;
175
+ const metadata = useMetadataWithSettings ( ) ;
163
176
const chartConfigsArr = toArray ( chartConfigs ) ;
164
- const { data : me , isFetched } = api . useMe ( ) ;
165
177
return useQuery < { key : string ; value : string [ ] } [ ] > ( {
166
178
queryKey : [
167
179
'useMetadata.useGetKeyValues' ,
@@ -170,14 +182,6 @@ export function useGetKeyValues(
170
182
disableRowLimit ,
171
183
] ,
172
184
queryFn : async ( ) => {
173
- const team = me ?. team ;
174
-
175
- // TODO: set the settings at the top level so that it doesn't have to be set for each useQuery
176
- if ( team ?. metadataMaxRowsToRead ) {
177
- metadata . setClickHouseSettings ( {
178
- max_rows_to_read : team . metadataMaxRowsToRead ,
179
- } ) ;
180
- }
181
185
return (
182
186
await Promise . all (
183
187
chartConfigsArr . map ( chartConfig =>
@@ -192,7 +196,7 @@ export function useGetKeyValues(
192
196
) . flatMap ( v => v ) ;
193
197
} ,
194
198
staleTime : 1000 * 60 * 5 , // Cache every 5 min
195
- enabled : ! ! keys . length && isFetched ,
199
+ enabled : ! ! keys . length ,
196
200
placeholderData : keepPreviousData ,
197
201
...options ,
198
202
} ) ;
0 commit comments