|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { useState, useEffect } from 'react'; |
| 7 | +import { debounce } from 'lodash'; |
| 8 | +import { |
| 9 | + EuiInMemoryTable, |
| 10 | + Direction, |
| 11 | + EuiFlexGroup, |
| 12 | + EuiFlexItem, |
| 13 | + EuiFieldSearch, |
| 14 | + EuiFilterSelectItem, |
| 15 | +} from '@elastic/eui'; |
| 16 | +import { WORKFLOW_STATE, WorkflowLaunch } from '../../../../common'; |
| 17 | +import { columns } from './columns'; |
| 18 | +import { MultiSelectFilter } from '../../../general_components'; |
| 19 | +import { getStateOptions } from '../../../utils'; |
| 20 | + |
| 21 | +interface LaunchListProps {} |
| 22 | + |
| 23 | +/** |
| 24 | + * The searchable list of launches for this particular workflow. |
| 25 | + */ |
| 26 | +export function LaunchList(props: LaunchListProps) { |
| 27 | + // TODO: finalize how we persist launches for a particular workflow. |
| 28 | + // We may just add UI metadata tags to group workflows under a single, overall "workflow" |
| 29 | + // const { workflows } = useSelector((state: AppState) => state.workflows); |
| 30 | + const workflowLaunches = [ |
| 31 | + { |
| 32 | + id: 'Launch_1', |
| 33 | + state: WORKFLOW_STATE.IN_PROGRESS, |
| 34 | + lastUpdated: 12345678, |
| 35 | + }, |
| 36 | + { |
| 37 | + id: 'Launch_2', |
| 38 | + state: WORKFLOW_STATE.FAILED, |
| 39 | + lastUpdated: 12345677, |
| 40 | + }, |
| 41 | + ] as WorkflowLaunch[]; |
| 42 | + |
| 43 | + // search bar state |
| 44 | + const [searchQuery, setSearchQuery] = useState<string>(''); |
| 45 | + const debounceSearchQuery = debounce((query: string) => { |
| 46 | + setSearchQuery(query); |
| 47 | + }, 100); |
| 48 | + |
| 49 | + // filters state |
| 50 | + const [selectedStates, setSelectedStates] = useState<EuiFilterSelectItem[]>( |
| 51 | + getStateOptions() |
| 52 | + ); |
| 53 | + const [filteredLaunches, setFilteredLaunches] = useState<WorkflowLaunch[]>( |
| 54 | + workflowLaunches |
| 55 | + ); |
| 56 | + |
| 57 | + // When a filter selection or search query changes, update the filtered launches |
| 58 | + useEffect(() => { |
| 59 | + setFilteredLaunches( |
| 60 | + fetchFilteredLaunches(workflowLaunches, selectedStates, searchQuery) |
| 61 | + ); |
| 62 | + }, [selectedStates, searchQuery]); |
| 63 | + |
| 64 | + const sorting = { |
| 65 | + sort: { |
| 66 | + field: 'id', |
| 67 | + direction: 'asc' as Direction, |
| 68 | + }, |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <EuiFlexGroup direction="column"> |
| 73 | + <EuiFlexItem> |
| 74 | + <EuiFlexGroup direction="row" gutterSize="m"> |
| 75 | + <EuiFlexItem grow={true}> |
| 76 | + <EuiFieldSearch |
| 77 | + fullWidth={true} |
| 78 | + placeholder="Search launches..." |
| 79 | + onChange={(e) => debounceSearchQuery(e.target.value)} |
| 80 | + /> |
| 81 | + </EuiFlexItem> |
| 82 | + <MultiSelectFilter |
| 83 | + filters={getStateOptions()} |
| 84 | + title="Status" |
| 85 | + setSelectedFilters={setSelectedStates} |
| 86 | + /> |
| 87 | + </EuiFlexGroup> |
| 88 | + </EuiFlexItem> |
| 89 | + <EuiFlexItem> |
| 90 | + <EuiInMemoryTable<WorkflowLaunch> |
| 91 | + items={filteredLaunches} |
| 92 | + rowHeader="id" |
| 93 | + columns={columns} |
| 94 | + sorting={sorting} |
| 95 | + pagination={true} |
| 96 | + message={'No existing launches found'} |
| 97 | + /> |
| 98 | + </EuiFlexItem> |
| 99 | + </EuiFlexGroup> |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +// Collect the final launch list after applying all filters |
| 104 | +function fetchFilteredLaunches( |
| 105 | + allLaunches: WorkflowLaunch[], |
| 106 | + stateFilters: EuiFilterSelectItem[], |
| 107 | + searchQuery: string |
| 108 | +): WorkflowLaunch[] { |
| 109 | + // @ts-ignore |
| 110 | + const stateFilterStrings = stateFilters.map((filter) => filter.name); |
| 111 | + const filteredLaunches = allLaunches.filter((launch) => |
| 112 | + stateFilterStrings.includes(launch.state) |
| 113 | + ); |
| 114 | + return searchQuery.length === 0 |
| 115 | + ? filteredLaunches |
| 116 | + : filteredLaunches.filter((launch) => |
| 117 | + launch.id.toLowerCase().includes(searchQuery.toLowerCase()) |
| 118 | + ); |
| 119 | +} |
0 commit comments