Skip to content

Commit 965e502

Browse files
committed
refactor: update minute logic, will auto switch date unit after switch
1 parent 5a5579a commit 965e502

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/client/components/insights/ChartRender.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { trpc } from '@/api/trpc';
22
import { useCurrentWorkspaceId } from '@/store/user';
33
import dayjs from 'dayjs';
44
import React, { useMemo, useState } from 'react';
5-
import { TimeEventChart } from '../chart/TimeEventChart';
5+
import { TimeEventChart, TimeEventChartType } from '../chart/TimeEventChart';
66
import { useInsightsStore } from '@/store/insights';
77
import { pickColorWithNum } from '@/utils/color';
88
import { DateRangeSelection } from './DateRangeSelection';
@@ -44,7 +44,19 @@ export const ChartRender: React.FC<ChartRenderProps> = React.memo((props) => {
4444
dayjs().subtract(30, 'day').startOf('day').toDate(),
4545
dayjs().endOf('day').toDate(),
4646
]);
47+
const allowMinute = useMemo(() => {
48+
const start = dayjs(dateRange[0]);
49+
const end = dayjs(dateRange[1]);
50+
return end.diff(start, 'day') <= 1;
51+
}, [dateRange]);
4752
const [dateUnit, setDateUnit] = useState<DateUnit>('day');
53+
const [chartType, setChartType] = useState<TimeEventChartType>('area');
54+
55+
useWatch([allowMinute, dateUnit], () => {
56+
if (!allowMinute && dateUnit === 'minute') {
57+
setDateUnit('day');
58+
}
59+
});
4860

4961
const time = useMemo(
5062
() => ({

src/client/components/insights/DateUnitSelection.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ import { DateUnit } from '@tianji/shared';
1010
import { useTranslation } from '@i18next-toolkit/react';
1111

1212
interface DateRangeSelectionProps {
13+
allowMinute: boolean;
1314
value: DateUnit;
1415
onChange: (val: DateUnit) => void;
1516
}
1617
export const DateUnitSelection: React.FC<DateRangeSelectionProps> = React.memo(
1718
(props) => {
18-
const { value, onChange } = props;
19+
const { allowMinute, value, onChange } = props;
1920
const { t } = useTranslation();
2021

2122
return (
2223
<Select value={value} onValueChange={onChange}>
2324
<SelectTrigger className="w-[120px]">
2425
<SelectValue placeholder="Day" />
2526
</SelectTrigger>
26-
<SelectContent>
27-
<SelectItem value="minute">{t('Minute')}</SelectItem>
27+
<SelectContent align="end">
28+
<SelectItem value="minute" disabled={!allowMinute}>
29+
{t('Minute')}
30+
</SelectItem>
2831
<SelectItem value="hour">{t('Hour')}</SelectItem>
2932
<SelectItem value="day">{t('Day')}</SelectItem>
3033
<SelectItem value="month">{t('Month')}</SelectItem>

0 commit comments

Comments
 (0)