-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathuseTagsAggregation.ts
More file actions
82 lines (71 loc) · 1.98 KB
/
useTagsAggregation.ts
File metadata and controls
82 lines (71 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { useMemo } from "react";
import { useParams } from "react-router-dom";
import useResourceQuery, {
featureViewListPath,
featureServiceListPath,
} from "../queries/useResourceQuery";
const buildTagCollection = <T>(
array: T[],
recordExtractor: (unknownFCO: T) => Record<string, string> | undefined,
): Record<string, Record<string, T[]>> => {
const tagCollection = array.reduce(
(memo: Record<string, Record<string, T[]>>, fco: T) => {
const tags = recordExtractor(fco);
if (tags) {
Object.entries(tags).forEach(([tagKey, tagValue]) => {
if (!memo[tagKey]) {
memo[tagKey] = {
[tagValue]: [fco],
};
} else {
if (!memo[tagKey][tagValue]) {
memo[tagKey][tagValue] = [fco];
} else {
memo[tagKey][tagValue].push(fco);
}
}
});
}
return memo;
},
{},
);
return tagCollection;
};
const useFeatureViewTagsAggregation = () => {
const { projectName } = useParams();
const query = useResourceQuery<any[]>({
resourceType: "tags-fvs",
project: projectName,
restPath: featureViewListPath(projectName),
restSelect: (d) => d.featureViews,
});
const data = useMemo(() => {
return query.data
? buildTagCollection<any>(query.data, (fv) => fv.spec?.tags)
: undefined;
}, [query.data]);
return {
...query,
data,
};
};
const useFeatureServiceTagsAggregation = () => {
const { projectName } = useParams();
const query = useResourceQuery<any[]>({
resourceType: "tags-fss",
project: projectName,
restPath: featureServiceListPath(projectName),
restSelect: (d) => d.featureServices,
});
const data = useMemo(() => {
return query.data
? buildTagCollection<any>(query.data, (fs) => fs.spec?.tags)
: undefined;
}, [query.data]);
return {
...query,
data,
};
};
export { useFeatureViewTagsAggregation, useFeatureServiceTagsAggregation };