|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + EuiFlexGroup, |
| 10 | + EuiFlexItem, |
| 11 | + EuiText, |
| 12 | + EuiDragDropContext, |
| 13 | + EuiDroppable, |
| 14 | + EuiDraggable, |
| 15 | + EuiPanel, |
| 16 | + EuiButtonEmpty, |
| 17 | +} from '@elastic/eui'; |
| 18 | +import { i18n } from '@kbn/i18n'; |
| 19 | +import { WiredStreamGetResponse } from '@kbn/streams-schema'; |
| 20 | +import { css } from '@emotion/css'; |
| 21 | +import { cloneDeep } from 'lodash'; |
| 22 | +import React from 'react'; |
| 23 | +import { EMPTY_EQUALS_CONDITION } from '../../util/condition'; |
| 24 | +import { NestedView } from '../nested_view'; |
| 25 | +import { useRoutingState } from './hooks/routing_state'; |
| 26 | +import { CurrentStreamEntry } from './current_stream_entry'; |
| 27 | +import { NewRoutingStreamEntry } from './new_routing_stream_entry'; |
| 28 | +import { RoutingStreamEntry } from './routing_stream_entry'; |
| 29 | + |
| 30 | +export function ChildStreamList({ |
| 31 | + definition, |
| 32 | + availableStreams, |
| 33 | + routingAppState: { |
| 34 | + childUnderEdit, |
| 35 | + selectChildUnderEdit, |
| 36 | + childStreams, |
| 37 | + onChildStreamDragEnd, |
| 38 | + onChildStreamDragStart, |
| 39 | + draggingChildStream, |
| 40 | + }, |
| 41 | +}: { |
| 42 | + definition: WiredStreamGetResponse; |
| 43 | + routingAppState: ReturnType<typeof useRoutingState>; |
| 44 | + availableStreams: string[]; |
| 45 | +}) { |
| 46 | + return ( |
| 47 | + <EuiFlexGroup |
| 48 | + direction="column" |
| 49 | + gutterSize="s" |
| 50 | + className={css` |
| 51 | + overflow: auto; |
| 52 | + `} |
| 53 | + > |
| 54 | + <EuiFlexItem grow={false}> |
| 55 | + <EuiText |
| 56 | + size="s" |
| 57 | + className={css` |
| 58 | + height: 40px; |
| 59 | + align-content: center; |
| 60 | + font-weight: bold; |
| 61 | + `} |
| 62 | + > |
| 63 | + {i18n.translate('xpack.streams.streamDetailRouting.rules.header', { |
| 64 | + defaultMessage: 'Routing rules', |
| 65 | + })} |
| 66 | + </EuiText> |
| 67 | + </EuiFlexItem> |
| 68 | + <EuiFlexGroup |
| 69 | + direction="column" |
| 70 | + gutterSize="xs" |
| 71 | + className={css` |
| 72 | + overflow: auto; |
| 73 | + `} |
| 74 | + > |
| 75 | + <CurrentStreamEntry definition={definition} /> |
| 76 | + <EuiDragDropContext onDragEnd={onChildStreamDragEnd} onDragStart={onChildStreamDragStart}> |
| 77 | + <EuiDroppable droppableId="routing_children_reordering" spacing="none"> |
| 78 | + <EuiFlexGroup direction="column" gutterSize="xs"> |
| 79 | + {childStreams.map((child, i) => ( |
| 80 | + <EuiFlexItem key={`${child.destination}-${i}-flex-item`} grow={false}> |
| 81 | + <EuiDraggable |
| 82 | + key={child.destination} |
| 83 | + index={i} |
| 84 | + draggableId={child.destination} |
| 85 | + hasInteractiveChildren={true} |
| 86 | + customDragHandle={true} |
| 87 | + spacing="none" |
| 88 | + > |
| 89 | + {(provided) => ( |
| 90 | + <NestedView |
| 91 | + key={i} |
| 92 | + isBeingDragged={draggingChildStream === child.destination} |
| 93 | + > |
| 94 | + <RoutingStreamEntry |
| 95 | + draggableProvided={provided} |
| 96 | + child={ |
| 97 | + !childUnderEdit?.isNew && |
| 98 | + child.destination === childUnderEdit?.child.destination |
| 99 | + ? childUnderEdit.child |
| 100 | + : child |
| 101 | + } |
| 102 | + edit={ |
| 103 | + !childUnderEdit?.isNew && |
| 104 | + child.destination === childUnderEdit?.child.destination |
| 105 | + } |
| 106 | + onEditStateChange={() => { |
| 107 | + if (child.destination === childUnderEdit?.child.destination) { |
| 108 | + selectChildUnderEdit(undefined); |
| 109 | + } else { |
| 110 | + selectChildUnderEdit({ isNew: false, child }); |
| 111 | + } |
| 112 | + }} |
| 113 | + onChildChange={(newChild) => { |
| 114 | + selectChildUnderEdit({ |
| 115 | + isNew: false, |
| 116 | + child: newChild, |
| 117 | + }); |
| 118 | + }} |
| 119 | + availableStreams={availableStreams} |
| 120 | + /> |
| 121 | + </NestedView> |
| 122 | + )} |
| 123 | + </EuiDraggable> |
| 124 | + </EuiFlexItem> |
| 125 | + ))} |
| 126 | + </EuiFlexGroup> |
| 127 | + </EuiDroppable> |
| 128 | + </EuiDragDropContext> |
| 129 | + {childUnderEdit?.isNew ? ( |
| 130 | + <NestedView last> |
| 131 | + <NewRoutingStreamEntry |
| 132 | + child={childUnderEdit.child} |
| 133 | + onChildChange={(newChild) => { |
| 134 | + if (!newChild) { |
| 135 | + selectChildUnderEdit(undefined); |
| 136 | + return; |
| 137 | + } |
| 138 | + selectChildUnderEdit({ |
| 139 | + isNew: true, |
| 140 | + child: newChild, |
| 141 | + }); |
| 142 | + }} |
| 143 | + /> |
| 144 | + </NestedView> |
| 145 | + ) : ( |
| 146 | + <NestedView last> |
| 147 | + <EuiPanel hasShadow={false} hasBorder paddingSize="none"> |
| 148 | + <EuiButtonEmpty |
| 149 | + iconType="plus" |
| 150 | + data-test-subj="streamsAppStreamDetailRoutingAddRuleButton" |
| 151 | + onClick={() => { |
| 152 | + selectChildUnderEdit({ |
| 153 | + isNew: true, |
| 154 | + child: { |
| 155 | + destination: `${definition.stream.name}.child`, |
| 156 | + if: cloneDeep(EMPTY_EQUALS_CONDITION), |
| 157 | + }, |
| 158 | + }); |
| 159 | + }} |
| 160 | + > |
| 161 | + {i18n.translate('xpack.streams.streamDetailRouting.addRule', { |
| 162 | + defaultMessage: 'Create a new child stream', |
| 163 | + })} |
| 164 | + </EuiButtonEmpty> |
| 165 | + </EuiPanel> |
| 166 | + </NestedView> |
| 167 | + )} |
| 168 | + </EuiFlexGroup> |
| 169 | + </EuiFlexGroup> |
| 170 | + ); |
| 171 | +} |
0 commit comments