@@ -60,7 +60,7 @@ export type MetricMetaValue = {
6060 name : string ;
6161 version ?: number ;
6262 currentVersion ?: number ;
63- update_time : number ;
63+ update_time ? : number ;
6464 description ?: string ;
6565 tags ?: MetricMetaTag [ ] ;
6666 /**
@@ -85,8 +85,6 @@ export type MetricMetaValue = {
8585 [ key : string ] : unknown ;
8686} ;
8787
88- export type PostMetricMetaValue = Omit < MetricMetaValue , 'update_time' | 'currentVersion' > ;
89-
9088export type MetricMetaTag = {
9189 name ?: string ;
9290 description ?: string ;
@@ -109,23 +107,31 @@ export async function apiMetricVersionFetch(params: ApiMetricVersionGet, keyRequ
109107
110108export function getMetricOptions < T = ApiMetric > (
111109 metricName : string ,
112- version : string | null = null ,
110+ version : number | null = null ,
113111 enabled : boolean = true
114- ) : UndefinedInitialDataOptions < ApiMetric | undefined , ExtendedError , T , [ string , string , string | null ] > {
112+ ) : UndefinedInitialDataOptions < ApiMetric | undefined , ExtendedError , T , [ string , string , number | null ] > {
115113 return {
116- enabled : enabled && metricName !== '' ,
114+ enabled,
117115 queryKey : [ ApiMetricEndpoint , metricName , version ] ,
118- queryFn : async ( { signal } ) => {
116+ queryFn : async ( { signal, client } ) => {
119117 if ( ! metricName ) {
120118 throw new ExtendedError ( 'no metric name' ) ;
121119 }
122120 const { response, error } = await apiMetricFetch ( { [ GET_PARAMS . metricName ] : metricName } , signal ) ;
123121 if ( error ) {
124122 throw error ;
125123 }
124+ if ( response ) {
125+ response . data . metric . currentVersion = response . data . metric . version ;
126+ client . setQueryData ( [ ApiMetricEndpoint , metricName , response . data . metric . version ] , response ) ;
127+ }
126128 if ( response && version ) {
129+ client . setQueryData ( [ ApiMetricEndpoint , metricName , null ] , response ) ;
127130 const historyVersion = await apiMetricVersionFetch (
128- { [ GET_PARAMS . metricId ] : response . data . metric . metric_id . toString ( ) , [ GET_PARAMS . metricApiVersion ] : version } ,
131+ {
132+ [ GET_PARAMS . metricId ] : response . data . metric . metric_id . toString ( ) ,
133+ [ GET_PARAMS . metricApiVersion ] : version . toString ( ) ,
134+ } ,
129135 signal
130136 ) ;
131137
@@ -135,19 +141,14 @@ export function getMetricOptions<T = ApiMetric>(
135141 } ) ;
136142 }
137143 }
138- return (
139- response &&
140- produce ( response , ( res ) => {
141- res . data . metric . currentVersion = res . data . metric . version ;
142- } )
143- ) ;
144+ return response ;
144145 } ,
145146 } ;
146147}
147148
148149export async function apiMetric < T = ApiMetric > (
149150 metricName : string ,
150- version : string | null = null
151+ version : number | null = null
151152) : Promise < ApiFetchResponse < T > > {
152153 const result : ApiFetchResponse < T > = { ok : false , status : 0 } ;
153154
@@ -186,7 +187,7 @@ export async function loadMetricMeta(metricName: string) {
186187
187188export function useApiMetric < T = ApiMetric > (
188189 metricName : string ,
189- version : string | null = null ,
190+ version : number | null = null ,
190191 select ?: ( response ?: ApiMetric ) => T ,
191192 enabled : boolean = true
192193) : UseQueryResult < T , ExtendedError > {
@@ -208,8 +209,12 @@ export function getMetricMeta(metricName: string): MetricMeta | null {
208209 return null ;
209210}
210211
211- async function postMetricMeta ( metric : PostMetricMetaValue ) {
212- const { response, error } = await apiFetch < ApiMetric > ( { url : ApiMetricEndpoint , post : { metric } , method : 'POST' } ) ;
212+ async function postMetricMeta ( metric : MetricMetaValue ) {
213+ const { response, error } = await apiFetch < ApiMetric , undefined , ApiMetricPost > ( {
214+ url : ApiMetricEndpoint ,
215+ post : { metric } ,
216+ method : 'POST' ,
217+ } ) ;
213218 if ( error ) {
214219 throw error ;
215220 }
@@ -218,13 +223,10 @@ async function postMetricMeta(metric: PostMetricMetaValue) {
218223
219224export function useMutationMetricMeta ( ) {
220225 const queryClient = useQueryClient ( ) ;
221- return useMutation < Awaited < ReturnType < typeof postMetricMeta > > , ExtendedError , PostMetricMetaValue > ( {
222- mutationFn : async ( metric : PostMetricMetaValue , originalMetric ?: MetricMetaValue ) => {
223- originalMetric ??= queryClient . getQueryData < ApiMetric > ( [
224- ApiMetricEndpoint ,
225- metric . name ,
226- metric . version ?. toString ( ) ?? null ,
227- ] ) ?. data . metric ;
226+ return useMutation < Awaited < ReturnType < typeof postMetricMeta > > , ExtendedError , MetricMetaValue > ( {
227+ mutationFn : async ( metric : MetricMetaValue ) => {
228+ const originalMetric = queryClient . getQueryData < ApiMetric > ( [ ApiMetricEndpoint , metric . name , metric . version ] ) ?. data
229+ . metric ;
228230 return postMetricMeta (
229231 produce ( { ...originalMetric , ...metric } , ( p ) => {
230232 if ( p . currentVersion ) {
@@ -236,13 +238,14 @@ export function useMutationMetricMeta() {
236238 ) ;
237239 } ,
238240 onSuccess : ( data , variables ) => {
239- queryClient . invalidateQueries ( { queryKey : [ ApiMetricEndpoint , variables . name ] , type : 'all' } ) ;
240241 if ( data ) {
241242 const metric = data . data . metric ;
242243 const metricName = metric . name ;
243244 queryClient . setQueryData ( [ ApiMetricEndpoint , metricName , null ] , data ) ;
244- queryClient . setQueryData ( [ ApiMetricEndpoint , metricName , metric . version ?. toString ( ) ?? null ] , data ) ;
245+ queryClient . setQueryData ( [ ApiMetricEndpoint , metricName , metric . version ?? null ] , data ) ;
245246 queryClient . invalidateQueries ( { queryKey : [ API_HISTORY , metric . metric_id ] , type : 'all' } ) ;
247+ } else {
248+ queryClient . invalidateQueries ( { queryKey : [ ApiMetricEndpoint , variables . name ] , type : 'all' } ) ;
246249 }
247250 } ,
248251 onError : ( _error , variables ) => {
0 commit comments