Skip to content

Commit

Permalink
feat: add realtime datarange which can visit data more easy
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Oct 4, 2024
1 parent 6da0e6f commit f3d8f55
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/client/components/DateFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export const DateFilter: React.FC<DateFilterProps> = React.memo((props) => {
setShowDropdown(false);
},
items: compact([
{
label: t('Realtime'),
onClick: () => {
useGlobalStateStore.setState({ dateRange: DateRange.Realtime });
},
},
{
type: 'divider',
},
{
label: t('Today'),
onClick: () => {
Expand Down
36 changes: 35 additions & 1 deletion src/client/hooks/useGlobalRangeDate.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs, { Dayjs } from 'dayjs';
import { useMemo, useReducer } from 'react';
import { useEffect, useMemo, useReducer } from 'react';
import { getMinimumUnit } from '@tianji/shared';
import { DateRange, useGlobalStateStore } from '../store/global';
import { DateUnit } from '../utils/date';
Expand Down Expand Up @@ -45,6 +45,15 @@ export function useGlobalRangeDate(): {
};
}

if (dateRange === DateRange.Realtime) {
return {
label: t('Realtime'),
startDate: dayjs().subtract(1, 'hour').startOf('minute'),
endDate: dayjs().endOf('minute'),
unit: 'minute' as const,
};
}

if (dateRange === DateRange.Today) {
return {
label: t('Today'),
Expand Down Expand Up @@ -126,5 +135,30 @@ export function useGlobalRangeDate(): {
};
}, [dateRange, globalStartDate, globalEndDate, updateInc]);

/**
* Auto refresh if is realtime
* NOTICE: Not cool yet
*/
useEffect(() => {
let timer: number | null = null;

if (dateRange === DateRange.Realtime) {
if (timer) {
window.clearInterval(timer);
}

timer = window.setInterval(() => {
refresh();
}, 60 * 1000);
}

return () => {
if (timer) {
window.clearInterval(timer);
timer = null;
}
};
}, [dateRange]);

return { label, startDate, endDate, unit, refresh };
}
1 change: 1 addition & 0 deletions src/client/store/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Dayjs } from 'dayjs';
import { create } from 'zustand';

export enum DateRange {
Realtime,
Last24Hours,
Today,
Yesterday,
Expand Down

0 comments on commit f3d8f55

Please sign in to comment.