React 18 and Vite (#4510)

This commit is contained in:
Matthew Penner 2022-11-25 13:25:03 -07:00 committed by GitHub
parent 1bb1b13f6d
commit 21613fa602
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
244 changed files with 4547 additions and 8933 deletions

View file

@ -71,13 +71,14 @@ const options: ChartOptions<'line'> = {
};
function getOptions(opts?: DeepPartial<ChartOptions<'line'>> | undefined): ChartOptions<'line'> {
return deepmerge(options, opts || {});
// @ts-expect-error go away
return deepmerge(options, opts ?? {});
}
type ChartDatasetCallback = (value: ChartDataset<'line'>, index: number) => ChartDataset<'line'>;
function getEmptyData(label: string, sets = 1, callback?: ChartDatasetCallback | undefined): ChartData<'line'> {
const next = callback || ((value) => value);
const next = callback || (value => value);
return {
labels: Array(20)
@ -94,8 +95,8 @@ function getEmptyData(label: string, sets = 1, callback?: ChartDatasetCallback |
borderColor: theme('colors.cyan.400'),
backgroundColor: hexToRgba(theme('colors.cyan.700'), 0.5),
},
index
)
index,
),
),
};
}
@ -110,30 +111,31 @@ interface UseChartOptions {
function useChart(label: string, opts?: UseChartOptions) {
const options = getOptions(
typeof opts?.options === 'number' ? { scales: { y: { min: 0, suggestedMax: opts.options } } } : opts?.options
typeof opts?.options === 'number' ? { scales: { y: { min: 0, suggestedMax: opts.options } } } : opts?.options,
);
const [data, setData] = useState(getEmptyData(label, opts?.sets || 1, opts?.callback));
const push = (items: number | null | (number | null)[]) =>
setData((state) =>
setData(state =>
merge(state, {
datasets: (Array.isArray(items) ? items : [items]).map((item, index) => ({
...state.datasets[index],
data: state.datasets[index].data
.slice(1)
.concat(typeof item === 'number' ? Number(item.toFixed(2)) : item),
data:
state.datasets[index]?.data
?.slice(1)
?.concat(typeof item === 'number' ? Number(item.toFixed(2)) : item) ?? [],
})),
})
}),
);
const clear = () =>
setData((state) =>
setData(state =>
merge(state, {
datasets: state.datasets.map((value) => ({
datasets: state.datasets.map(value => ({
...value,
data: Array(20).fill(-5),
})),
})
}),
);
return { props: { data, options }, push, clear };