|
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import { actionType as T } from '../reducer'; |
| 3 | +import './searchPanel.css'; |
| 4 | + |
| 5 | +const SearchPanel = ({ superState, dispatcher }) => { |
| 6 | + const inputRef = useRef(); |
| 7 | + const { searchPanel, searchQuery, searchResults, searchIndex, curGraphInstance } = superState; |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + if (searchPanel && inputRef.current) inputRef.current.focus(); |
| 11 | + }, [searchPanel]); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + if (searchPanel && curGraphInstance && searchQuery) { |
| 15 | + const results = curGraphInstance.searchElements(searchQuery); |
| 16 | + dispatcher({ type: T.SET_SEARCH_RESULTS, payload: results }); |
| 17 | + dispatcher({ type: T.SET_SEARCH_INDEX, payload: 0 }); |
| 18 | + if (results.length > 0) curGraphInstance.flyToElement(results[0]); |
| 19 | + } |
| 20 | + }, [curGraphInstance]); |
| 21 | + |
| 22 | + const runSearch = (query) => { |
| 23 | + if (!curGraphInstance) return; |
| 24 | + const results = curGraphInstance.searchElements(query); |
| 25 | + dispatcher({ type: T.SET_SEARCH_RESULTS, payload: results }); |
| 26 | + dispatcher({ type: T.SET_SEARCH_INDEX, payload: 0 }); |
| 27 | + if (results.length > 0) curGraphInstance.flyToElement(results[0]); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleChange = (e) => { |
| 31 | + const q = e.target.value; |
| 32 | + dispatcher({ type: T.SET_SEARCH_QUERY, payload: q }); |
| 33 | + runSearch(q); |
| 34 | + }; |
| 35 | + |
| 36 | + const step = (dir) => { |
| 37 | + if (!searchResults.length) return; |
| 38 | + const next = (searchIndex + dir + searchResults.length) % searchResults.length; |
| 39 | + dispatcher({ type: T.SET_SEARCH_INDEX, payload: next }); |
| 40 | + curGraphInstance.flyToElement(searchResults[next]); |
| 41 | + }; |
| 42 | + |
| 43 | + const close = () => { |
| 44 | + if (curGraphInstance) curGraphInstance.clearSearch(); |
| 45 | + dispatcher({ type: T.SET_SEARCH_PANEL, payload: false }); |
| 46 | + dispatcher({ type: T.SET_SEARCH_QUERY, payload: '' }); |
| 47 | + dispatcher({ type: T.SET_SEARCH_RESULTS, payload: [] }); |
| 48 | + dispatcher({ type: T.SET_SEARCH_INDEX, payload: 0 }); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleKeyDown = (e) => { |
| 52 | + if (e.key === 'Escape') { close(); return; } |
| 53 | + if (e.key === 'Enter') { |
| 54 | + e.preventDefault(); |
| 55 | + step(e.shiftKey ? -1 : 1); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + if (!searchPanel) return null; |
| 60 | + |
| 61 | + const total = searchResults.length; |
| 62 | + const current = total > 0 ? searchIndex + 1 : 0; |
| 63 | + const hasQuery = searchQuery.trim().length > 0; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div className="search-panel"> |
| 67 | + <input |
| 68 | + ref={inputRef} |
| 69 | + type="text" |
| 70 | + placeholder="Find node / edge…" |
| 71 | + value={searchQuery} |
| 72 | + onChange={handleChange} |
| 73 | + onKeyDown={handleKeyDown} |
| 74 | + /> |
| 75 | + <span className="search-counter">{hasQuery ? (total > 0 ? `${current} of ${total}` : 'No results') : ''}</span> |
| 76 | + <button type="button" onClick={() => step(-1)} disabled={total < 2} title="Previous (Shift+Enter)">▲</button> |
| 77 | + <button type="button" onClick={() => step(1)} disabled={total < 2} title="Next (Enter)">▼</button> |
| 78 | + <button type="button" onClick={close} title="Close (Esc)">✕</button> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +export default SearchPanel; |
0 commit comments