|
| 1 | +import React, { |
| 2 | + type ChangeEvent, |
| 3 | + useCallback, |
| 4 | + useEffect, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | +} from 'react'; |
| 9 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 10 | +import { type IconDefinition } from '@fortawesome/fontawesome-svg-core'; |
| 11 | +import { EMPTY_ARRAY } from '@deephaven/utils'; |
| 12 | +import { Button } from '../Button'; |
| 13 | +import SearchInput from '../SearchInput'; |
| 14 | +import type { NavTabItem } from './NavTabList'; |
| 15 | +import './DashboardList.scss'; |
| 16 | +import { GLOBAL_SHORTCUTS } from '../shortcuts'; |
| 17 | + |
| 18 | +export interface DashboardListProps { |
| 19 | + onSelect: (tab: NavTabItem) => void; |
| 20 | + tabs?: NavTabItem[]; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Display a search field and a list of dashboard tabs |
| 25 | + * @param props The tabs and handlers to use for this list |
| 26 | + * @returns A JSX element for the list of dashboard tabs, along with search |
| 27 | + */ |
| 28 | +export function DashboardList(props: DashboardListProps): JSX.Element { |
| 29 | + const { onSelect, tabs = EMPTY_ARRAY } = props; |
| 30 | + const [searchText, setSearchText] = useState(''); |
| 31 | + const [focusedIndex, setFocusedIndex] = useState(0); |
| 32 | + const searchField = useRef<SearchInput>(null); |
| 33 | + const listRef = useRef<HTMLUListElement>(null); |
| 34 | + const itemRefs = useRef<(HTMLLIElement | null)[]>([]); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + searchField.current?.focus(); |
| 38 | + }, []); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + setFocusedIndex(0); |
| 42 | + }, [searchText]); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (focusedIndex >= 0 && itemRefs.current[focusedIndex]) { |
| 46 | + itemRefs.current[focusedIndex]?.scrollIntoView({ |
| 47 | + behavior: 'auto', |
| 48 | + block: 'nearest', |
| 49 | + }); |
| 50 | + } |
| 51 | + }, [focusedIndex]); |
| 52 | + |
| 53 | + const handleSearchChange = useCallback((e: ChangeEvent<HTMLInputElement>) => { |
| 54 | + setSearchText(e.target.value); |
| 55 | + }, []); |
| 56 | + |
| 57 | + const handleTabSelect = useCallback( |
| 58 | + (tab: NavTabItem) => { |
| 59 | + onSelect(tab); |
| 60 | + }, |
| 61 | + [onSelect] |
| 62 | + ); |
| 63 | + |
| 64 | + const handleMouseDown = useCallback((event: React.MouseEvent) => { |
| 65 | + // Prevent mousedown from taking focus away from the search input |
| 66 | + event.preventDefault(); |
| 67 | + }, []); |
| 68 | + |
| 69 | + const filteredTabs = useMemo( |
| 70 | + () => |
| 71 | + tabs.filter(tab => |
| 72 | + tab.title.toLowerCase().includes(searchText.toLowerCase()) |
| 73 | + ), |
| 74 | + [searchText, tabs] |
| 75 | + ).sort((a, b) => a.title.localeCompare(b.title) ?? 0); |
| 76 | + |
| 77 | + const handleSearchKeyDown = useCallback( |
| 78 | + (event: React.KeyboardEvent) => { |
| 79 | + if (event.key === 'ArrowDown' && filteredTabs.length > 0) { |
| 80 | + event.preventDefault(); |
| 81 | + setFocusedIndex(prev => |
| 82 | + prev === -1 ? 0 : (prev + 1) % filteredTabs.length |
| 83 | + ); |
| 84 | + } else if (event.key === 'ArrowUp' && filteredTabs.length > 0) { |
| 85 | + event.preventDefault(); |
| 86 | + setFocusedIndex(prev => { |
| 87 | + if (prev === -1) return filteredTabs.length - 1; |
| 88 | + return (prev - 1 + filteredTabs.length) % filteredTabs.length; |
| 89 | + }); |
| 90 | + } else if (event.key === 'Enter' && filteredTabs.length > 0) { |
| 91 | + event.preventDefault(); |
| 92 | + const selectedIndex = focusedIndex >= 0 ? focusedIndex : 0; |
| 93 | + handleTabSelect(filteredTabs[selectedIndex]); |
| 94 | + } else if (event.key === 'Tab') { |
| 95 | + event.preventDefault(); |
| 96 | + } |
| 97 | + }, |
| 98 | + [filteredTabs, focusedIndex, handleTabSelect] |
| 99 | + ); |
| 100 | + |
| 101 | + const tabElements = useMemo( |
| 102 | + () => |
| 103 | + filteredTabs.map((tab, index) => ( |
| 104 | + <li |
| 105 | + key={tab.key} |
| 106 | + ref={(el: HTMLLIElement | null) => { |
| 107 | + itemRefs.current[index] = el; |
| 108 | + }} |
| 109 | + onMouseDown={handleMouseDown} |
| 110 | + > |
| 111 | + <Button |
| 112 | + kind="ghost" |
| 113 | + data-testid={`dashboard-list-item-${tab.key ?? ''}-button`} |
| 114 | + onClick={() => handleTabSelect(tab)} |
| 115 | + className={focusedIndex === index ? 'focused' : ''} |
| 116 | + style={{ transition: 'none' }} |
| 117 | + > |
| 118 | + {tab.icon ? ( |
| 119 | + <span className="dashboard-list-item-icon"> |
| 120 | + {React.isValidElement(tab.icon) ? ( |
| 121 | + tab.icon |
| 122 | + ) : ( |
| 123 | + <FontAwesomeIcon icon={tab.icon as IconDefinition} /> |
| 124 | + )} |
| 125 | + </span> |
| 126 | + ) : null} |
| 127 | + {tab.title} |
| 128 | + </Button> |
| 129 | + </li> |
| 130 | + )), |
| 131 | + [filteredTabs, handleTabSelect, focusedIndex, handleMouseDown] |
| 132 | + ); |
| 133 | + |
| 134 | + const errorElement = useMemo( |
| 135 | + () => |
| 136 | + tabElements.length === 0 ? <span>No open dashboard found.</span> : null, |
| 137 | + [tabElements] |
| 138 | + ); |
| 139 | + |
| 140 | + return ( |
| 141 | + <div className="dashboard-list-container d-flex flex-column"> |
| 142 | + <div className="dashboard-list-header"> |
| 143 | + <SearchInput |
| 144 | + value={searchText} |
| 145 | + placeholder="Find open dashboard" |
| 146 | + endPlaceholder={GLOBAL_SHORTCUTS.OPEN_DASHBOARD_LIST.getDisplayText()} |
| 147 | + onChange={handleSearchChange} |
| 148 | + onKeyDown={handleSearchKeyDown} |
| 149 | + ref={searchField} |
| 150 | + /> |
| 151 | + </div> |
| 152 | + <ul className="dashboard-list flex-grow-1" ref={listRef}> |
| 153 | + {errorElement && ( |
| 154 | + <li className="dashboard-list-message">{errorElement}</li> |
| 155 | + )} |
| 156 | + {!errorElement && tabElements} |
| 157 | + </ul> |
| 158 | + </div> |
| 159 | + ); |
| 160 | +} |
| 161 | + |
| 162 | +export default DashboardList; |
0 commit comments