umami/hooks/useReport.js

21 lines
497 B
JavaScript
Raw Normal View History

2023-05-20 18:02:08 +02:00
import useStore, { createReport } from 'store/reports';
import { useCallback, useEffect, useState } from 'react';
export function useReport(reportId, defaultParameters) {
2023-05-20 18:02:08 +02:00
const [id, setId] = useState(reportId);
const selector = useCallback(state => state[id], [id]);
const report = useStore(selector);
useEffect(() => {
if (!report) {
const newReport = createReport(defaultParameters);
setId(newReport.id);
2023-05-20 18:02:08 +02:00
}
}, []);
return report;
}
export default useReport;