Skip to content

added presisted time range #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions public/app/core/components/TimePicker/TimePickerWithHistory.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { merge } from 'lodash';
import React, { CSSProperties } from 'react';
import { useSelector } from 'react-redux';
// eslint-disable-next-line no-restricted-imports
import { useDispatch, useSelector } from 'react-redux';

import { TimeRange, isDateTime, toUtc } from '@grafana/data';
import { TimeRangePickerProps, TimeRangePicker, useTheme2 } from '@grafana/ui';
import { FnGlobalState } from 'app/core/reducers/fn-slice';
import { FnGlobalState, updatePartialFnStates } from 'app/core/reducers/fn-slice';
import { StoreState } from 'app/types';

import { LocalStorageValueProvider } from '../LocalStorageValueProvider';
Expand All @@ -22,14 +24,22 @@ const FnText: React.FC = () => {
};

export const TimePickerWithHistory: React.FC<Props> = (props) => {
const { fnGlobalTimeRange } = useSelector<StoreState, FnGlobalState>(({ fnGlobalState }) => fnGlobalState);
const dispatch = useDispatch();

return (
<LocalStorageValueProvider<TimeRange[]> storageKey={LOCAL_STORAGE_KEY} defaultValue={[]}>
{(values, onSaveToStore) => {
return (
<TimeRangePicker
{...props}
{...merge({}, props, { value: fnGlobalTimeRange || props.value })}
history={convertIfJson(values)}
onChange={(value) => {
dispatch(
updatePartialFnStates({
fnGlobalTimeRange: value,
})
);
onAppendToHistory(value, values, onSaveToStore);
props.onChange(value);
}}
Expand Down
4 changes: 3 additions & 1 deletion public/app/core/reducers/fn-slice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSlice, PayloadAction, SliceCaseReducers } from '@reduxjs/toolkit';

import { GrafanaThemeType } from '@grafana/data';
import { GrafanaThemeType, TimeRange } from '@grafana/data';

import { AnyObject } from '../../fn-app/types';

Expand All @@ -13,6 +13,7 @@ export interface FnGlobalState {
pageTitle: string;
queryParams: AnyObject;
hiddenVariables: readonly string[];
fnGlobalTimeRange: TimeRange | null;
}

export type UpdateFNGlobalStateAction = PayloadAction<Partial<FnGlobalState>>;
Expand Down Expand Up @@ -55,6 +56,7 @@ export const INITIAL_FN_STATE: FnGlobalState = {
pageTitle: '',
queryParams: {},
hiddenVariables: [],
fnGlobalTimeRange: null,
} as const;

const reducers: SliceCaseReducers<FnGlobalState> = {
Expand Down