mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-30 07:51:17 -07:00
18 lines
507 B
JavaScript
18 lines
507 B
JavaScript
import { useCallback, useState } from "react";
|
|
|
|
export default function useDataPoints(pointsLimit, initialPoint) {
|
|
const [dataPoints, setDataPoints] = useState(() => new Array(pointsLimit).fill(initialPoint));
|
|
|
|
const addDataPoint = useCallback(
|
|
(dataPoint) => {
|
|
setDataPoints((currentDataPoints) => {
|
|
if (pointsLimit <= 0) return [];
|
|
return [...currentDataPoints, dataPoint].slice(-pointsLimit);
|
|
});
|
|
},
|
|
[pointsLimit],
|
|
);
|
|
|
|
return [dataPoints, addDataPoint];
|
|
}
|