-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-datasets-by-sub-topic.ts
95 lines (86 loc) · 2.56 KB
/
use-datasets-by-sub-topic.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
import { useGetDatasets } from "@/types/generated/dataset";
import { DatasetLayersDataItem, MetadataItemComponent } from "@/types/generated/strapi.schemas";
type DatasetsBySubTopic = {
subTopic: string;
datasets: {
id: number;
name: string;
shortDescription?: string;
defaultLayerId: number | undefined;
layers: DatasetLayersDataItem[];
metadata?: MetadataItemComponent;
}[];
};
export default function useDatasetsBySubTopic(
topicSlug: string,
sort = "sub_topic.name,name",
layersFields = ["name"],
includeMetadata = false,
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-error
const { data, isLoading } = useGetDatasets<DatasetsBySubTopic[]>(
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-error
fields: ["name", "short_description"],
populate: {
sub_topic: {
fields: ["name"],
},
default_layer: {
fields: ["id"],
},
layers: {
fields: layersFields,
sort: "name",
},
metadata: includeMetadata,
},
filters: {
topic: {
slug: {
$eq: topicSlug,
},
},
},
sort,
},
{
query: {
placeholderData: { data: [] },
select: (data) => {
const res: DatasetsBySubTopic[] = [];
if (!data?.data) {
return res;
}
let currentIndex = -1;
let currentSubTopic = null;
for (const item of data.data) {
const subTopic = item.attributes!.sub_topic!.data!.attributes!.name! as string;
const dataset = item.attributes!.name;
const shortDescription = item.attributes!.short_description;
const defaultLayerId = item.attributes!.default_layer!.data?.id;
const layers = item.attributes!.layers!.data!;
const metadata = item.attributes!.metadata;
if (currentSubTopic === null || currentSubTopic !== subTopic) {
currentSubTopic = subTopic;
currentIndex++;
res[currentIndex] = { subTopic: currentSubTopic, datasets: [] };
}
res[currentIndex].datasets.push({
id: item.id!,
name: dataset,
shortDescription,
defaultLayerId,
layers,
...(includeMetadata ? { metadata: metadata ?? undefined } : {}),
});
}
return res;
},
},
},
);
return { data, isLoading };
}