@@ -7,31 +7,32 @@ import { ChartFiltersList } from "@/components/chart-filters-list";
77import Flex from "@/components/flex" ;
88import { Select } from "@/components/form" ;
99import { Loading } from "@/components/hint" ;
10+ import SelectTree from "@/components/select-tree" ;
1011import {
1112 ChartConfig ,
1213 DataSource ,
1314 InteractiveFiltersDataConfig ,
1415 OptionGroup ,
1516 Option ,
1617} from "@/configurator" ;
17- import { TimeInput } from "@/configurator/components/field" ;
18+ import { hierarchyToOptions , TimeInput } from "@/configurator/components/field" ;
1819import {
1920 getTimeIntervalFormattedSelectOptions ,
2021 getTimeIntervalWithProps ,
2122} from "@/configurator/components/ui-helpers" ;
22- import useHierarchyParents from "@/configurator/components/use-hierarchy-parents" ;
2323import { FIELD_VALUE_NONE } from "@/configurator/constants" ;
2424import { isTemporalDimension } from "@/domain/data" ;
2525import { useTimeFormatLocale } from "@/formatters" ;
2626import {
2727 Dimension ,
28+ HierarchyValue ,
2829 TemporalDimension ,
2930 TimeUnit ,
31+ useDimensionHierarchyQuery ,
3032 useDimensionValuesQuery ,
3133} from "@/graphql/query-hooks" ;
3234import { Icon } from "@/icons" ;
3335import { useLocale } from "@/locales/use-locale" ;
34- import { makeOptionGroups } from "@/utils/hierarchy" ;
3536
3637export const ChartDataFilters = ( {
3738 dataSet,
@@ -150,19 +151,25 @@ const DataFilter = ({
150151 } ,
151152 } ) ;
152153
153- const { data : hierarchyParents } = useHierarchyParents ( {
154- datasetIri : dataSetIri ,
155- dataSource,
156- dimension : data ?. dataCubeByIri ?. dimensionByIri ! ,
157- locale,
158- pause : ! data ?. dataCubeByIri ?. dimensionByIri ,
154+ const dimension = data ?. dataCubeByIri ?. dimensionByIri ;
155+
156+ const [ hierarchyResp ] = useDimensionHierarchyQuery ( {
157+ variables : {
158+ cubeIri : dataSetIri ,
159+ dimensionIri : dimension ?. iri ! ,
160+ sourceType : dataSource . type ,
161+ sourceUrl : dataSource . url ,
162+ locale : locale ,
163+ } ,
164+ pause : ! dimension ,
159165 } ) ;
160166
161- const optionGroups = React . useMemo ( ( ) => {
162- return makeOptionGroups ( hierarchyParents ) ;
163- } , [ hierarchyParents ] ) ;
167+ const hierarchy =
168+ hierarchyResp . data ?. dataCubeByIri ?. dimensionByIri ?. hierarchy ;
164169
165- const setDataFilter = ( e : SelectChangeEvent < unknown > ) => {
170+ const setDataFilter = (
171+ e : SelectChangeEvent < unknown > | { target : { value : string } }
172+ ) => {
166173 dispatch ( {
167174 type : "UPDATE_DATA_FILTER" ,
168175 value : { dimensionIri, dimensionValueIri : e . target . value as string } ,
@@ -195,33 +202,40 @@ const DataFilter = ({
195202 " > div" : { width : "100%" } ,
196203 } }
197204 >
198- { ! isTemporalDimension ( dimension ) ? (
199- < DataFilterBaseDimension
205+ { isTemporalDimension ( dimension ) ? (
206+ dimension . timeUnit === TimeUnit . Year ? (
207+ < DataFilterTemporalDimension
208+ value = { value as string }
209+ dimension = { dimension }
210+ onChange = { setDataFilter }
211+ />
212+ ) : null
213+ ) : hierarchy ? (
214+ < DataFilterHierarchyDimension
200215 dimension = { dimension }
201- optionGroups = { optionGroups }
202216 onChange = { setDataFilter }
217+ hierarchy = { hierarchy }
203218 value = { value as string }
204219 />
205- ) : dimension . timeUnit === TimeUnit . Year ? (
206- < DataFilterTemporalDimension
207- value = { value as string }
220+ ) : (
221+ < DataFilterGenericDimension
208222 dimension = { dimension }
209223 onChange = { setDataFilter }
224+ value = { value as string }
210225 />
211- ) : null }
226+ ) }
212227 </ Flex >
213228 ) ;
214229 } else {
215230 return < Loading /> ;
216231 }
217232} ;
218233
219- const DataFilterBaseDimension = ( {
234+ const DataFilterGenericDimension = ( {
220235 dimension,
221236 value,
222237 onChange,
223238 options : propOptions ,
224- optionGroups,
225239} : {
226240 dimension : Dimension ;
227241 value : string ;
@@ -255,14 +269,63 @@ const DataFilterBaseDimension = ({
255269 id = "dataFilterBaseDimension"
256270 label = { label }
257271 options = { allOptions }
258- optionGroups = { optionGroups }
259272 value = { value }
260273 tooltipText = { tooltipText || undefined }
261274 onChange = { onChange }
262275 />
263276 ) ;
264277} ;
265278
279+ const DataFilterHierarchyDimension = ( {
280+ dimension,
281+ value,
282+ onChange,
283+ hierarchy,
284+ } : {
285+ dimension : Dimension ;
286+ value : string ;
287+ onChange : ( e : { target : { value : string } } ) => void ;
288+ hierarchy ?: HierarchyValue [ ] ;
289+ } ) => {
290+ const noneLabel = t ( {
291+ id : "controls.dimensionvalue.none" ,
292+ message : `No Filter` ,
293+ } ) ;
294+
295+ const {
296+ label,
297+ isKeyDimension,
298+ description : tooltipText ,
299+ values : dimensionValues ,
300+ } = dimension ;
301+ const options = React . useMemo ( ( ) => {
302+ let opts = [ ] as { label : string ; value : string ; isNoneValue ?: boolean } [ ] ;
303+ if ( hierarchy ) {
304+ opts = hierarchyToOptions ( hierarchy ) ;
305+ } else {
306+ opts = dimensionValues ;
307+ }
308+ if ( ! isKeyDimension ) {
309+ opts . unshift ( {
310+ value : FIELD_VALUE_NONE ,
311+ label : noneLabel ,
312+ isNoneValue : true ,
313+ } ) ;
314+ }
315+ return opts ;
316+ } , [ hierarchy , isKeyDimension , dimensionValues , noneLabel ] ) ;
317+
318+ return (
319+ < SelectTree
320+ value = { value }
321+ options = { options }
322+ onChange = { onChange }
323+ label = { label }
324+ tooltipText = { tooltipText || undefined }
325+ />
326+ ) ;
327+ } ;
328+
266329const DataFilterTemporalDimension = ( {
267330 dimension,
268331 value,
@@ -300,7 +363,7 @@ const DataFilterTemporalDimension = ({
300363
301364 if ( timeIntervalWithProps . range < 100 ) {
302365 return (
303- < DataFilterBaseDimension
366+ < DataFilterGenericDimension
304367 dimension = { dimension }
305368 options = { timeIntervalOptions }
306369 value = { value }
0 commit comments