2023-08-19 02:52:43 +03:00
|
|
|
import React, { ReactNode, useCallback, useLayoutEffect, useRef } from 'react';
|
2020-08-31 00:49:06 +03:00
|
|
|
import { Action } from 'redux';
|
2020-12-21 17:08:08 +03:00
|
|
|
import { PerformAction } from '@redux-devtools/core';
|
2020-08-31 00:49:06 +03:00
|
|
|
import { StylingFunction } from 'react-base16-styling';
|
2023-08-19 02:52:43 +03:00
|
|
|
import {
|
|
|
|
closestCenter,
|
|
|
|
DndContext,
|
|
|
|
DragEndEvent,
|
|
|
|
KeyboardSensor,
|
|
|
|
PointerSensor,
|
|
|
|
useSensor,
|
|
|
|
useSensors,
|
|
|
|
} from '@dnd-kit/core';
|
|
|
|
import { restrictToFirstScrollableAncestor } from '@dnd-kit/modifiers';
|
|
|
|
import {
|
|
|
|
SortableContext,
|
|
|
|
sortableKeyboardCoordinates,
|
|
|
|
useSortable,
|
|
|
|
verticalListSortingStrategy,
|
|
|
|
} from '@dnd-kit/sortable';
|
|
|
|
import { CSS } from '@dnd-kit/utilities';
|
2018-12-22 03:10:49 +03:00
|
|
|
import ActionListRow from './ActionListRow';
|
|
|
|
import ActionListHeader from './ActionListHeader';
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
function getTimestamps<A extends Action<string>>(
|
2020-08-31 00:49:06 +03:00
|
|
|
actions: { [actionId: number]: PerformAction<A> },
|
|
|
|
actionIds: number[],
|
2023-07-12 21:03:20 +03:00
|
|
|
actionId: number,
|
2020-08-31 00:49:06 +03:00
|
|
|
) {
|
2018-12-22 03:10:49 +03:00
|
|
|
const idx = actionIds.indexOf(actionId);
|
|
|
|
const prevActionId = actionIds[idx - 1];
|
|
|
|
|
|
|
|
return {
|
|
|
|
current: actions[actionId].timestamp,
|
2020-08-08 23:26:39 +03:00
|
|
|
previous: idx ? actions[prevActionId].timestamp : 0,
|
2018-12-22 03:10:49 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-19 02:52:43 +03:00
|
|
|
function scrollToBottom(node: HTMLDivElement) {
|
|
|
|
node.scrollTop = node.scrollHeight;
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
interface Props<A extends Action<string>> {
|
2020-08-31 00:49:06 +03:00
|
|
|
actions: { [actionId: number]: PerformAction<A> };
|
|
|
|
actionIds: number[];
|
|
|
|
isWideLayout: boolean;
|
|
|
|
searchValue: string | undefined;
|
|
|
|
selectedActionId: number | null;
|
|
|
|
startActionId: number | null;
|
|
|
|
skippedActionIds: number[];
|
|
|
|
draggableActions: boolean;
|
|
|
|
hideMainButtons: boolean | undefined;
|
|
|
|
hideActionButtons: boolean | undefined;
|
|
|
|
styling: StylingFunction;
|
|
|
|
onSearch: (value: string) => void;
|
|
|
|
onSelect: (e: React.MouseEvent<HTMLDivElement>, actionId: number) => void;
|
|
|
|
onToggleAction: (actionId: number) => void;
|
|
|
|
onJumpToState: (actionId: number) => void;
|
|
|
|
onCommit: () => void;
|
|
|
|
onSweep: () => void;
|
|
|
|
onReorderAction: (actionId: number, beforeActionId: number) => void;
|
|
|
|
currentActionId: number;
|
|
|
|
lastActionId: number;
|
|
|
|
}
|
|
|
|
|
2023-11-05 00:04:23 +03:00
|
|
|
export default function ActionList<A extends Action<string>>({
|
2023-08-19 02:52:43 +03:00
|
|
|
styling,
|
|
|
|
actions,
|
|
|
|
actionIds,
|
|
|
|
isWideLayout,
|
|
|
|
onToggleAction,
|
|
|
|
skippedActionIds,
|
|
|
|
selectedActionId,
|
|
|
|
startActionId,
|
|
|
|
onSelect,
|
|
|
|
onSearch,
|
|
|
|
searchValue,
|
|
|
|
currentActionId,
|
|
|
|
hideMainButtons,
|
|
|
|
hideActionButtons,
|
|
|
|
onCommit,
|
|
|
|
onSweep,
|
|
|
|
onJumpToState,
|
|
|
|
lastActionId,
|
|
|
|
onReorderAction,
|
|
|
|
}: Props<A>) {
|
|
|
|
const nodeRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
const prevLastActionId = useRef<number | undefined>();
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (nodeRef.current && prevLastActionId.current !== lastActionId) {
|
|
|
|
const { scrollTop, offsetHeight, scrollHeight } = nodeRef.current;
|
|
|
|
if (Math.abs(scrollHeight - (scrollTop + offsetHeight)) < 50) {
|
|
|
|
scrollToBottom(nodeRef.current);
|
2018-12-22 03:10:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 02:52:43 +03:00
|
|
|
prevLastActionId.current = lastActionId;
|
|
|
|
}, [lastActionId]);
|
|
|
|
|
|
|
|
const setNodeRef = useCallback((node: HTMLDivElement | null) => {
|
|
|
|
if (node && !nodeRef.current) {
|
|
|
|
scrollToBottom(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeRef.current = node;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const sensors = useSensors(
|
2023-08-19 05:34:49 +03:00
|
|
|
useSensor(PointerSensor, {
|
|
|
|
activationConstraint: {
|
|
|
|
distance: 8,
|
|
|
|
},
|
|
|
|
}),
|
2023-08-19 02:52:43 +03:00
|
|
|
useSensor(KeyboardSensor, {
|
|
|
|
coordinateGetter: sortableKeyboardCoordinates,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleDragEnd = useCallback(
|
|
|
|
({ active, over }: DragEndEvent) => {
|
|
|
|
if (over && active.id !== over.id) {
|
|
|
|
const activeIndex = actionIds.indexOf(active.id as number);
|
|
|
|
const overIndex = actionIds.indexOf(over.id as number);
|
|
|
|
|
|
|
|
const beforeActionId =
|
|
|
|
overIndex < activeIndex
|
|
|
|
? (over.id as number)
|
|
|
|
: overIndex < actionIds.length - 1
|
2023-11-14 01:41:31 +03:00
|
|
|
? actionIds[overIndex + 1]
|
|
|
|
: actionIds.length;
|
2018-12-22 03:10:49 +03:00
|
|
|
|
2023-08-19 02:52:43 +03:00
|
|
|
onReorderAction(active.id as number, beforeActionId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[actionIds, onReorderAction],
|
|
|
|
);
|
|
|
|
|
|
|
|
const lowerSearchValue = searchValue && searchValue.toLowerCase();
|
|
|
|
const filteredActionIds = searchValue
|
|
|
|
? actionIds.filter(
|
|
|
|
(id) =>
|
2023-11-05 00:04:23 +03:00
|
|
|
actions[id].action.type
|
2023-08-19 02:52:43 +03:00
|
|
|
.toLowerCase()
|
|
|
|
.indexOf(lowerSearchValue as string) !== -1,
|
|
|
|
)
|
|
|
|
: actionIds;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key="actionList"
|
|
|
|
data-testid="actionList"
|
|
|
|
{...styling(
|
|
|
|
['actionList', isWideLayout && 'actionListWide'],
|
|
|
|
isWideLayout,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<ActionListHeader
|
|
|
|
styling={styling}
|
|
|
|
onSearch={onSearch}
|
|
|
|
onCommit={onCommit}
|
|
|
|
onSweep={onSweep}
|
|
|
|
hideMainButtons={hideMainButtons}
|
|
|
|
hasSkippedActions={skippedActionIds.length > 0}
|
|
|
|
hasStagedActions={actionIds.length > 1}
|
|
|
|
searchValue={searchValue}
|
|
|
|
/>
|
2019-01-10 21:51:14 +03:00
|
|
|
<div
|
2023-08-19 02:52:43 +03:00
|
|
|
data-testid="actionListRows"
|
|
|
|
{...styling('actionListRows')}
|
|
|
|
ref={setNodeRef}
|
2019-01-10 21:51:14 +03:00
|
|
|
>
|
2023-08-19 02:52:43 +03:00
|
|
|
<DndContext
|
|
|
|
sensors={sensors}
|
|
|
|
collisionDetection={closestCenter}
|
|
|
|
onDragEnd={handleDragEnd}
|
|
|
|
modifiers={[restrictToFirstScrollableAncestor]}
|
2021-10-22 17:49:53 +03:00
|
|
|
>
|
2023-08-19 02:52:43 +03:00
|
|
|
<SortableContext
|
|
|
|
items={filteredActionIds}
|
|
|
|
strategy={verticalListSortingStrategy}
|
|
|
|
>
|
|
|
|
{filteredActionIds.map((actionId) => (
|
|
|
|
<SortableItem key={actionId} actionId={actionId}>
|
|
|
|
<ActionListRow
|
|
|
|
styling={styling}
|
|
|
|
actionId={actionId}
|
|
|
|
isInitAction={!actionId}
|
|
|
|
isSelected={
|
|
|
|
(startActionId !== null &&
|
|
|
|
actionId >= startActionId &&
|
|
|
|
actionId <= (selectedActionId as number)) ||
|
|
|
|
actionId === selectedActionId
|
|
|
|
}
|
|
|
|
isInFuture={
|
|
|
|
actionIds.indexOf(actionId) >
|
|
|
|
actionIds.indexOf(currentActionId)
|
|
|
|
}
|
|
|
|
onSelect={(e: React.MouseEvent<HTMLDivElement>) =>
|
|
|
|
onSelect(e, actionId)
|
|
|
|
}
|
|
|
|
timestamps={getTimestamps(actions, actionIds, actionId)}
|
|
|
|
action={actions[actionId].action}
|
|
|
|
onToggleClick={() => onToggleAction(actionId)}
|
|
|
|
onJumpClick={() => onJumpToState(actionId)}
|
|
|
|
onCommitClick={() => onCommit()}
|
|
|
|
hideActionButtons={hideActionButtons}
|
|
|
|
isSkipped={skippedActionIds.indexOf(actionId) !== -1}
|
|
|
|
/>
|
|
|
|
</SortableItem>
|
|
|
|
))}
|
|
|
|
</SortableContext>
|
|
|
|
</DndContext>
|
2018-12-22 03:10:49 +03:00
|
|
|
</div>
|
2023-08-19 02:52:43 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SortableItemProps {
|
|
|
|
readonly children: ReactNode;
|
|
|
|
readonly actionId: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
function SortableItem({ children, actionId }: SortableItemProps) {
|
|
|
|
const { attributes, listeners, setNodeRef, transform, transition } =
|
|
|
|
useSortable({ id: actionId });
|
|
|
|
|
|
|
|
const style = {
|
|
|
|
transform: CSS.Transform.toString(transform),
|
|
|
|
transition,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={setNodeRef} style={style} {...attributes} {...listeners}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
2018-12-22 03:10:49 +03:00
|
|
|
}
|