This repository was archived by the owner on Aug 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.hook.tsx
More file actions
160 lines (140 loc) · 5 KB
/
index.hook.tsx
File metadata and controls
160 lines (140 loc) · 5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Options, SeriesLineOptions } from "highcharts";
// eslint-disable-next-line import/no-named-as-default
import HighchartsReact from "highcharts-react-official";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
useGetPopulationsQueries,
GetPopulationQueryResultType,
CheckedPrefectureType,
} from "./useGetPopulationsQueries";
import { generateColors } from "@/utils/Color";
import { trpc } from "@/utils/trpc";
export const useIndex = () => {
const [checkedPrefCodes, setCheckedPrefCodes] = useState<Set<number>>(new Set());
const [targetDataIndex, setTargetDataIndex] = useState<number>(0);
const isInitialized = useRef(false);
const chartComponentRef = useRef<HighchartsReact.RefObject>(null);
const [prefecturesData] = trpc.getPrefectures.useSuspenseQuery();
const prefectures: CheckedPrefectureType[] = useMemo(
() =>
prefecturesData.result.map((prefecture) => ({
...prefecture,
checked: checkedPrefCodes.has(prefecture.prefCode),
})),
[checkedPrefCodes, prefecturesData.result],
);
const populationsData = useGetPopulationsQueries(prefectures);
const isFetching = useMemo(() => populationsData.some(({ isFetching }) => isFetching), [populationsData]);
const checkedPopulations = useMemo(() => {
const checkedPopulations: Exclude<GetPopulationQueryResultType["data"], undefined>[] = [];
populationsData.forEach(({ data }) => {
if (data === undefined) return;
if (data.prefecture.checked === false) return;
checkedPopulations.push(data);
});
return checkedPopulations;
}, [populationsData]);
const labels = useMemo(
() => checkedPopulations[0]?.result.data.map((d, index) => ({ index, name: d.label })) ?? [],
[checkedPopulations],
);
const targetLabel = useMemo(() => labels[targetDataIndex], [labels, targetDataIndex]);
const highchartsSeries: SeriesLineOptions[] = useMemo(
() =>
checkedPopulations.map((population) => {
const {
prefecture: { prefCode, prefName },
result,
} = population;
const series: SeriesLineOptions = {
type: "line",
id: `${prefCode}`,
name: prefName,
data: result.data[targetDataIndex]?.data.map((d) => [d.year, d.value]) ?? [],
color: generateColors(prefCode),
showInLegend: true,
};
return series;
}),
[checkedPopulations, targetDataIndex],
);
const highchartsOptions: Options = useMemo(() => {
const options: Options = {
title: { text: targetLabel?.name },
xAxis: { title: { text: "年" } },
yAxis: { title: { text: "人口" } },
series: highchartsSeries,
credits: { enabled: false },
lang: {
noData: "都道府県を選択してください",
loading: "データ取得中...",
},
loading: {
hideDuration: 1000,
showDuration: 1000,
},
};
return options;
}, [highchartsSeries, targetLabel]);
const handleSetCheckedPrefCodes = useCallback((args: { checked: boolean; prefCode: number }) => {
const { checked, prefCode } = args;
setCheckedPrefCodes((prevState) => {
const nextState = new Set(prevState);
if (checked) {
nextState.add(prefCode);
} else {
nextState.delete(prefCode);
}
return nextState;
});
}, []);
const handleChangeCheckedCode = useCallback(
async (event: React.ChangeEvent<HTMLInputElement>) => {
const {
target: { checked, value },
} = event;
const prefCode = Number(value);
handleSetCheckedPrefCodes({ checked, prefCode });
},
[handleSetCheckedPrefCodes],
);
const handleChangeTargetDataIndex = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const {
target: { value },
} = event;
const index = Number(value);
setTargetDataIndex(index);
}, []);
// 初回のみ都道府県のデータ取得時に最初と最後の都道府県のチェックをONにする
useEffect(() => {
if (isInitialized.current) return;
if (checkedPrefCodes.size > 0) return;
const firstPrefCode = prefecturesData.result[0]?.prefCode;
const lastPrefCode = prefecturesData.result[prefecturesData.result.length - 1]?.prefCode;
if (firstPrefCode !== undefined) {
handleSetCheckedPrefCodes({ prefCode: firstPrefCode, checked: true });
}
if (lastPrefCode !== undefined) {
handleSetCheckedPrefCodes({ prefCode: lastPrefCode, checked: true });
}
isInitialized.current = true;
}, [checkedPrefCodes.size, handleSetCheckedPrefCodes, prefecturesData.result]);
// データ取得中にローディングを表示する
useEffect(() => {
const chart = chartComponentRef.current?.chart;
if (isFetching) {
chart?.showLoading();
} else {
chart?.hideLoading();
}
}, [isFetching]);
return {
prefectures,
labels,
targetDataIndex,
highchartsOptions,
chartComponentRef,
handleChangeCheckedCode,
handleChangeTargetDataIndex,
};
};