import { Action, AnyAction } from 'redux'; import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { QueryInfo, RtkQueryMonitorState, QueryFormValues, RtkQueryMonitorProps, QueryPreviewTabs, } from './types'; import { QueryComparators } from './utils/comparators'; import { QueryFilters } from './utils/filters'; const initialState: RtkQueryMonitorState = { queryForm: { values: { queryComparator: QueryComparators.fulfilledTimeStamp, isAscendingQueryComparatorOrder: false, searchValue: '', isRegexSearch: false, queryFilter: QueryFilters.queryKey, }, }, selectedPreviewTab: QueryPreviewTabs.queryinfo, selectedQueryKey: null, }; const monitorSlice = createSlice({ /** * `@@` prefix is mandatory. * @see lifedAction @ `packages/redux-devtools-app/src/actions/index.ts` */ name: '@@rtk-query-monitor', initialState, reducers: { changeQueryFormValues( state, action: PayloadAction>, ) { state.queryForm.values = { ...state.queryForm.values, ...action.payload }; }, selectQueryKey( state, action: PayloadAction>, ) { state.selectedQueryKey = { queryKey: action.payload.queryKey, reducerPath: action.payload.reducerPath, }; }, selectedPreviewTab(state, action: PayloadAction) { state.selectedPreviewTab = action.payload; }, }, }); export function reducer>( props: RtkQueryMonitorProps, state: RtkQueryMonitorState | undefined, action: AnyAction, ): RtkQueryMonitorState { return monitorSlice.reducer(state, action); } export const { selectQueryKey, changeQueryFormValues, selectedPreviewTab } = monitorSlice.actions;