11import { useMemo } from "react" ;
22
33import { toDataPoints } from "../components/graph" ;
4- import type { DateRange , Dimension , DimensionTableRow , Metric , ProjectResponse } from "./types" ;
4+ import type { DateRange , Dimension , DimensionFilter , DimensionTableRow , Metric , ProjectResponse } from "./types" ;
55
66import { api } from "." ;
77import { queryClient , useQuery } from "./query" ;
@@ -56,10 +56,12 @@ export const useDimension = ({
5656 dimension,
5757 metric,
5858 range,
59+ filters,
5960} : {
6061 project : ProjectResponse ;
6162 dimension : Dimension ;
6263 metric : Metric ;
64+ filters : DimensionFilter [ ] ;
6365 range : DateRange ;
6466} ) : {
6567 data : DimensionTableRow [ ] | undefined ;
@@ -70,13 +72,14 @@ export const useDimension = ({
7072} => {
7173 const { data, isLoading, error } = useQuery ( {
7274 placeholderData : ( prev ) => prev ,
73- queryKey : [ "dimension" , project . id , dimension , metric , range ] ,
75+ queryKey : [ "dimension" , project . id , dimension , metric , range , filters ] ,
7476 queryFn : ( ) =>
7577 api [ "/api/dashboard/project/{project_id}/dimension" ]
7678 . post ( {
7779 params : { project_id : project . id } ,
7880 json : {
7981 dimension,
82+ filters,
8083 metric,
8184 range,
8285 } ,
@@ -94,10 +97,12 @@ export const useProjectData = ({
9497 project,
9598 metric,
9699 rangeName = "last7Days" ,
100+ filters = [ ] ,
97101} : {
98102 project ?: ProjectResponse ;
99103 metric : Metric ;
100104 rangeName ?: RangeName ;
105+ filters ?: DimensionFilter [ ] ;
101106} ) => {
102107 const { range, graphRange, dataPoints } = useMemo ( ( ) => resolveRange ( rangeName ) , [ rangeName ] ) ;
103108
@@ -115,17 +120,16 @@ export const useProjectData = ({
115120 } = useQuery ( {
116121 refetchInterval,
117122 staleTime,
118- queryKey : [ "project_stats" , project ?. id , range ] ,
123+ queryKey : [ "project_stats" , project ?. id , range , metric , filters ] ,
119124
120125 enabled : project !== undefined ,
121126 queryFn : ( ) =>
122127 api [ "/api/dashboard/project/{project_id}/stats" ]
123- . post ( { json : { range } , params : { project_id : project ?. id ?? "" } } )
128+ . post ( { json : { range, filters } , params : { project_id : project ?. id ?? "" } } )
124129 . json ( ) ,
125130 placeholderData : ( prev ) => prev ,
126131 } ) ;
127132
128- const json = { range, metric, dataPoints } ;
129133 const {
130134 data : graph ,
131135 isError : isErrorGraph ,
@@ -136,7 +140,9 @@ export const useProjectData = ({
136140 enabled : project !== undefined ,
137141 queryKey : [ "project_graph" , project ?. id , range , graphRange , metric , dataPoints ] ,
138142 queryFn : ( ) =>
139- api [ "/api/dashboard/project/{project_id}/graph" ] . post ( { json, params : { project_id : project ?. id ?? "" } } ) . json ( ) ,
143+ api [ "/api/dashboard/project/{project_id}/graph" ]
144+ . post ( { json : { range, metric, dataPoints, filters } , params : { project_id : project ?. id ?? "" } } )
145+ . json ( ) ,
140146 placeholderData : ( prev ) => prev ,
141147 } ) ;
142148
@@ -157,6 +163,9 @@ export const useProjectData = ({
157163 } ;
158164} ;
159165
166+ export type ProjectDataGraph = ReturnType < typeof useProjectData > [ "graph" ] ;
167+ export type ProjectDataStats = ReturnType < typeof useProjectData > [ "stats" ] ;
168+
160169export const invalidateProjects = ( ) => queryClient . invalidateQueries ( { queryKey : [ "projects" ] } ) ;
161170export const invalidateEntities = ( ) => queryClient . invalidateQueries ( { queryKey : [ "entities" ] } ) ;
162171export const invalidateUsers = ( ) => queryClient . invalidateQueries ( { queryKey : [ "users" ] } ) ;
0 commit comments