mirror of
https://github.com/umami-software/umami.git
synced 2026-02-20 04:25:39 +01:00
wip: enforce max chart points when selecting unit
This commit is contained in:
parent
0948cb40c0
commit
d7ade2f80f
5 changed files with 39 additions and 16 deletions
|
|
@ -1,28 +1,46 @@
|
|||
import { DEFAULT_TIME_UNIT, TIME_UNIT_CONFIG } from 'lib/constants';
|
||||
import { TimeUnit } from 'lib/types';
|
||||
import { DEFAULT_TIME_UNIT, MAX_CHART_POINTS, TIME_UNIT_CONFIG, UNIT_TYPES } from 'lib/constants';
|
||||
import { getDateLength } from 'lib/date';
|
||||
import { DateRange, TimeUnit } from 'lib/types';
|
||||
import { getItem, setItem } from 'next-basics';
|
||||
import { useState } from 'react';
|
||||
import useStore, { setTimeUnit } from 'store/app';
|
||||
|
||||
const selector = (state: { timeUnit: string }) => state.timeUnit;
|
||||
|
||||
export function useTimeUnit() {
|
||||
export function useTimeUnit(dateRange?: DateRange) {
|
||||
const storeTimeUnit = useStore(selector) || getItem(TIME_UNIT_CONFIG) || DEFAULT_TIME_UNIT;
|
||||
const [tempTimeUnit, setTempTimeUnit] = useState<TimeUnit>(storeTimeUnit);
|
||||
const timeUnitOptions = ['hour', 'day', 'week', 'month', 'year'];
|
||||
const validTimeUnits = getValidTimeUnits(dateRange);
|
||||
const [tempTimeUnit, setTempTimeUnit] = useState<TimeUnit>(() => {
|
||||
return validTimeUnits.includes(storeTimeUnit) ? storeTimeUnit : validTimeUnits[0];
|
||||
});
|
||||
|
||||
function getValidTimeUnits(dateRange: DateRange) {
|
||||
if (!dateRange?.startDate || !dateRange?.endDate) {
|
||||
return UNIT_TYPES;
|
||||
}
|
||||
|
||||
return UNIT_TYPES.filter(unit => {
|
||||
const points = getDateLength(dateRange.startDate, dateRange.endDate, unit);
|
||||
return points <= MAX_CHART_POINTS;
|
||||
});
|
||||
}
|
||||
|
||||
function updateTimeUnit(value: TimeUnit) {
|
||||
setTempTimeUnit(value);
|
||||
if (validTimeUnits.includes(value)) {
|
||||
setTempTimeUnit(value);
|
||||
}
|
||||
}
|
||||
|
||||
function saveTimeUnit() {
|
||||
setTimeUnit(tempTimeUnit);
|
||||
setItem(TIME_UNIT_CONFIG, tempTimeUnit);
|
||||
if (validTimeUnits.includes(tempTimeUnit)) {
|
||||
setTimeUnit(tempTimeUnit);
|
||||
setItem(TIME_UNIT_CONFIG, tempTimeUnit);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
timeUnit: tempTimeUnit,
|
||||
timeUnitOptions,
|
||||
timeUnitOptions: validTimeUnits,
|
||||
updateTimeUnit,
|
||||
saveTimeUnit,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue