2020-08-27 16:19:37 +03:00
|
|
|
import { Action } from 'redux';
|
|
|
|
import {
|
|
|
|
UPDATE_SCROLL_TOP,
|
|
|
|
START_CONSECUTIVE_TOGGLE,
|
|
|
|
LogMonitorAction,
|
|
|
|
} from './actions';
|
|
|
|
import { LogMonitorProps } from './LogMonitor';
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
function initialScrollTop<S, A extends Action<string>>(
|
2020-08-27 16:19:37 +03:00
|
|
|
props: LogMonitorProps<S, A>,
|
|
|
|
state = 0,
|
2023-07-12 21:03:20 +03:00
|
|
|
action: LogMonitorAction,
|
2020-08-27 16:19:37 +03:00
|
|
|
) {
|
|
|
|
if (!props.preserveScrollTop) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return action.type === UPDATE_SCROLL_TOP ? action.scrollTop : state;
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
function startConsecutiveToggle<S, A extends Action<string>>(
|
2020-08-27 16:19:37 +03:00
|
|
|
props: LogMonitorProps<S, A>,
|
|
|
|
state: number | null | undefined,
|
2023-07-12 21:03:20 +03:00
|
|
|
action: LogMonitorAction,
|
2020-08-27 16:19:37 +03:00
|
|
|
) {
|
|
|
|
return action.type === START_CONSECUTIVE_TOGGLE ? action.id : state;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LogMonitorState {
|
|
|
|
initialScrollTop: number;
|
|
|
|
consecutiveToggleStartId: number | null | undefined;
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
export default function reducer<S, A extends Action<string>>(
|
2020-08-27 16:19:37 +03:00
|
|
|
props: LogMonitorProps<S, A>,
|
|
|
|
state: Partial<LogMonitorState> = {},
|
2023-07-12 21:03:20 +03:00
|
|
|
action: LogMonitorAction,
|
2020-08-27 16:19:37 +03:00
|
|
|
): LogMonitorState {
|
|
|
|
return {
|
|
|
|
initialScrollTop: initialScrollTop(props, state.initialScrollTop, action),
|
|
|
|
consecutiveToggleStartId: startConsecutiveToggle(
|
|
|
|
props,
|
|
|
|
state.consecutiveToggleStartId,
|
2023-07-12 21:03:20 +03:00
|
|
|
action,
|
2020-08-27 16:19:37 +03:00
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|