Skip to content

Commit

Permalink
Expose centerCursor from logEntriesState
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández Gómez committed Mar 18, 2020
1 parent a806e3a commit 5269c24
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
17 changes: 14 additions & 3 deletions x-pack/plugins/infra/public/containers/logs/log_entries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface LogEntriesStateParams {
entries: LogEntriesResponse['data']['entries'];
topCursor: LogEntriesResponse['data']['topCursor'] | null;
bottomCursor: LogEntriesResponse['data']['bottomCursor'] | null;
centerCursor: TimeKey | null;
isReloading: boolean;
isLoadingMore: boolean;
lastLoadedTime: Date | null;
Expand All @@ -88,6 +89,7 @@ export const logEntriesInitialState: LogEntriesStateParams = {
entries: [],
topCursor: null,
bottomCursor: null,
centerCursor: null,
isReloading: true,
isLoadingMore: false,
lastLoadedTime: null,
Expand Down Expand Up @@ -348,7 +350,7 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action
return {
...prevState,
...action.payload,
entries: action.payload.entries,
centerCursor: getCenterCursor(action.payload.entries),
lastLoadedTime: new Date(),
isReloading: false,

Expand All @@ -360,13 +362,15 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action
case Action.ReceiveEntriesBefore: {
const newEntries = action.payload.entries;
const prevEntries = cleanDuplicateItems(prevState.entries, newEntries);
const entries = [...newEntries, ...prevEntries];

const update = {
entries: [...newEntries, ...prevEntries],
entries,
isLoadingMore: false,
hasMoreBeforeStart: newEntries.length > 0,
// Keep the previous cursor if request comes empty, to easily extend the range.
topCursor: newEntries.length > 0 ? action.payload.topCursor : prevState.topCursor,
centerCursor: getCenterCursor(entries),
lastLoadedTime: new Date(),
};

Expand All @@ -375,13 +379,15 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action
case Action.ReceiveEntriesAfter: {
const newEntries = action.payload.entries;
const prevEntries = cleanDuplicateItems(prevState.entries, newEntries);
const entries = [...prevEntries, ...newEntries];

const update = {
entries: [...prevEntries, ...newEntries],
entries,
isLoadingMore: false,
hasMoreAfterEnd: newEntries.length > 0,
// Keep the previous cursor if request comes empty, to easily extend the range.
bottomCursor: newEntries.length > 0 ? action.payload.bottomCursor : prevState.bottomCursor,
centerCursor: getCenterCursor(entries),
lastLoadedTime: new Date(),
};

Expand All @@ -394,6 +400,7 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action
entries: [],
topCursor: null,
bottomCursor: null,
centerCursor: null,
hasMoreBeforeStart: true,
hasMoreAfterEnd: true,
};
Expand All @@ -419,4 +426,8 @@ const logEntriesStateReducer = (prevState: LogEntriesStateParams, action: Action
}
};

function getCenterCursor(entries: LogEntry[]): TimeKey | null {
return entries.length > 0 ? entries[Math.floor(entries.length / 2)].cursor : null;
}

export const LogEntriesState = createContainer(useLogEntriesState);
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const FETCH_THROTTLE_INTERVAL = 3000;
interface UseLogHighlightsStateProps {
sourceId: string;
sourceVersion: string | undefined;
centerPoint: TimeKey | null;
centerCursor: TimeKey | null;
size: number;
filterQuery: string | null;
}

export const useLogHighlightsState = ({
sourceId,
sourceVersion,
centerPoint,
centerCursor,
size,
filterQuery,
}: UseLogHighlightsStateProps) => {
Expand All @@ -47,7 +47,7 @@ export const useLogHighlightsState = ({
sourceVersion,
throttledStartTimestamp,
throttledEndTimestamp,
centerPoint,
centerCursor,
size,
filterQuery,
highlightTerms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,15 @@ const LogEntriesStateProvider: React.FC = ({ children }) => {

const LogHighlightsStateProvider: React.FC = ({ children }) => {
const { sourceId, version } = useContext(Source.Context);
const [{ topCursor, bottomCursor, entries }] = useContext(LogEntriesState.Context);
const [{ topCursor, bottomCursor, centerCursor, entries }] = useContext(LogEntriesState.Context);
const { filterQuery } = useContext(LogFilterState.Context);

const centerPoint = entries.length > 0 ? entries[Math.floor(entries.length / 2)].cursor : null;

const highlightsProps = {
sourceId,
sourceVersion: version,
entriesStart: topCursor,
entriesEnd: bottomCursor,
centerPoint,
centerCursor,
size: entries.length,
filterQuery,
};
Expand Down

0 comments on commit 5269c24

Please sign in to comment.