|
1 | 1 | import fuzzy from 'fuzzysort'
|
2 |
| -import React, { ChangeEvent, Dispatch, FC, KeyboardEvent as ReactKeyboardEvent, memo, RefObject, SetStateAction, useEffect, useRef, useState } from 'react' |
| 2 | +import React, { ChangeEvent, Dispatch, FC, KeyboardEvent as ReactKeyboardEvent, memo, RefObject, SetStateAction, useEffect, useRef, useState, useCallback, useMemo } from 'react' |
3 | 3 | import { getAllLinks, LinkItem, SearchTarget } from '../links'
|
4 | 4 | import { AppMode, setMode } from '../stores/currentModeStore'
|
5 | 5 | import { useHiddenLinks } from '../stores/hiddenLinksStore'
|
@@ -27,121 +27,136 @@ export const Search: FC<SearchProps> = memo(({ latestKeypress }) => {
|
27 | 27 | }
|
28 | 28 | })
|
29 | 29 |
|
30 |
| - function handleInputChange (event: ChangeEvent<HTMLInputElement>): void { |
31 |
| - setSearchTerm(event.currentTarget.value) |
32 |
| - setKeyboardIndex(0) |
33 |
| - } |
34 |
| - |
35 |
| - function handleInputKeyDown (event: ReactKeyboardEvent<HTMLInputElement>): void { |
36 |
| - switch (event.key) { |
37 |
| - case 'Backspace': { |
38 |
| - if (searchTerm !== '') { return } |
| 30 | + const handleInputChange = useCallback( |
| 31 | + (event: ChangeEvent<HTMLInputElement>): void => { |
| 32 | + setSearchTerm(event.currentTarget.value) |
| 33 | + setKeyboardIndex(0) |
| 34 | + }, |
| 35 | + [setKeyboardIndex, setSearchTerm] |
| 36 | + ) |
39 | 37 |
|
40 |
| - if (searchTarget !== null) { |
41 |
| - setSearchTarget(null) |
42 |
| - } else { |
43 |
| - setMode(AppMode.default) |
| 38 | + const handleInputKeyDown = useCallback( |
| 39 | + (event: ReactKeyboardEvent<HTMLInputElement>): void => { |
| 40 | + switch (event.key) { |
| 41 | + case 'Backspace': { |
| 42 | + if (searchTerm !== '') { return } |
| 43 | + |
| 44 | + if (searchTarget !== null) { |
| 45 | + setSearchTarget(null) |
| 46 | + } else { |
| 47 | + setMode(AppMode.default) |
| 48 | + } |
| 49 | + break |
44 | 50 | }
|
45 |
| - break |
46 |
| - } |
47 | 51 |
|
48 |
| - case 'Tab': { |
49 |
| - event.preventDefault() |
| 52 | + case 'Tab': { |
| 53 | + event.preventDefault() |
50 | 54 |
|
51 |
| - if (searchTarget !== null) { return } |
52 |
| - if (focusedResult === null) { return } |
53 |
| - if (focusedResult.obj.searchUrl === undefined) { return } |
| 55 | + if (searchTarget !== null) { return } |
| 56 | + if (focusedResult === null) { return } |
| 57 | + if (focusedResult.obj.searchUrl === undefined) { return } |
54 | 58 |
|
55 |
| - setSearchTarget(focusedResult.obj as SearchTarget) |
56 |
| - setSearchTerm('') |
57 |
| - break |
58 |
| - } |
| 59 | + setSearchTarget(focusedResult.obj as SearchTarget) |
| 60 | + setSearchTerm('') |
| 61 | + break |
| 62 | + } |
| 63 | + |
| 64 | + case 'Enter': { |
| 65 | + const url = getUrl(focusedResult?.obj ?? null, searchTarget, searchTerm) |
59 | 66 |
|
60 |
| - case 'Enter': { |
61 |
| - const url = getUrl(focusedResult?.obj ?? null, searchTarget, searchTerm) |
| 67 | + if (url === null) { return } |
62 | 68 |
|
63 |
| - if (url === null) { return } |
| 69 | + if (event.ctrlKey) { |
| 70 | + window.open(url, '', 'alwaysRaised=on') |
| 71 | + } else { |
| 72 | + window.location.href = url |
| 73 | + } |
64 | 74 |
|
65 |
| - if (event.ctrlKey) { |
66 |
| - window.open(url, '', 'alwaysRaised=on') |
67 |
| - } else { |
68 |
| - window.location.href = url |
| 75 | + if (event.ctrlKey || event.shiftKey) { setMode(AppMode.default) } |
| 76 | + break |
69 | 77 | }
|
70 | 78 |
|
71 |
| - if (event.ctrlKey || event.shiftKey) { setMode(AppMode.default) } |
72 |
| - break |
73 |
| - } |
| 79 | + case 'ArrowUp': { |
| 80 | + if (results === null) { return } |
| 81 | + event.preventDefault() |
| 82 | + setKeyboardIndex(Math.max(0, keyboardIndex - 1)) |
| 83 | + break |
| 84 | + } |
74 | 85 |
|
75 |
| - case 'ArrowUp': { |
76 |
| - if (results === null) { return } |
77 |
| - event.preventDefault() |
78 |
| - setKeyboardIndex(Math.max(0, keyboardIndex - 1)) |
79 |
| - break |
| 86 | + case 'ArrowDown': { |
| 87 | + if (results === null) { return } |
| 88 | + event.preventDefault() |
| 89 | + setKeyboardIndex(Math.min(results.total - 1, keyboardIndex + 1)) |
| 90 | + } |
80 | 91 | }
|
| 92 | + }, |
| 93 | + [focusedResult, keyboardIndex, results, searchTarget, searchTerm, setKeyboardIndex, setSearchTarget, setSearchTerm] |
| 94 | + ) |
81 | 95 |
|
82 |
| - case 'ArrowDown': { |
83 |
| - if (results === null) { return } |
84 |
| - event.preventDefault() |
85 |
| - setKeyboardIndex(Math.min(results.total - 1, keyboardIndex + 1)) |
86 |
| - } |
87 |
| - } |
88 |
| - } |
| 96 | + const handleGlobalKeyDown = useCallback( |
| 97 | + (event: KeyboardEvent): void => { |
| 98 | + if (event.key === 'Escape') { |
| 99 | + if (searchTarget !== null) { |
| 100 | + setSearchTarget(null) |
| 101 | + setSearchTerm('') |
| 102 | + return |
| 103 | + } |
89 | 104 |
|
90 |
| - function handleGlobalKeyDown (event: KeyboardEvent): void { |
91 |
| - if (event.key === 'Escape') { |
92 |
| - if (searchTarget !== null) { |
93 |
| - setSearchTarget(null) |
94 |
| - setSearchTerm('') |
95 |
| - return |
| 105 | + setMode(AppMode.default) |
96 | 106 | }
|
| 107 | + }, |
| 108 | + [searchTarget, setSearchTarget, setSearchTerm] |
| 109 | + ) |
97 | 110 |
|
98 |
| - setMode(AppMode.default) |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - const hints = <> |
103 |
| - <div className="search__results-hint"> |
| 111 | + const hints = useMemo( |
| 112 | + () => <> |
| 113 | + <div className="search__results-hint"> |
104 | 114 | Type ahead to filter links.
|
105 |
| - </div> |
106 |
| - <div className="search__results-hint"> |
107 |
| - <kbd>Return</kbd> |
108 |
| - <div className="search__results-hint-description"> |
| 115 | + </div> |
| 116 | + <div className="search__results-hint"> |
| 117 | + <kbd>Return</kbd> |
| 118 | + <div className="search__results-hint-description"> |
109 | 119 | Open link
|
| 120 | + </div> |
110 | 121 | </div>
|
111 |
| - </div> |
112 |
| - <div className="search__results-hint"> |
113 |
| - <kbd>Ctrl</kbd> + <kbd>Return</kbd> |
114 |
| - <div className="search__results-hint-description"> |
| 122 | + <div className="search__results-hint"> |
| 123 | + <kbd>Ctrl</kbd> + <kbd>Return</kbd> |
| 124 | + <div className="search__results-hint-description"> |
115 | 125 | Open link in a new tab (background)
|
| 126 | + </div> |
116 | 127 | </div>
|
117 |
| - </div> |
118 |
| - <div className="search__results-hint"> |
119 |
| - <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Return</kbd> |
120 |
| - <div className="search__results-hint-description"> |
| 128 | + <div className="search__results-hint"> |
| 129 | + <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Return</kbd> |
| 130 | + <div className="search__results-hint-description"> |
121 | 131 | Open link in a new tab (foreground)
|
| 132 | + </div> |
122 | 133 | </div>
|
123 |
| - </div> |
124 |
| - </> |
125 |
| - |
126 |
| - const resultElements = <> |
127 |
| - {results !== null && results.total > 0 ? ( |
128 |
| - results.map(link => ( |
129 |
| - <Link |
130 |
| - key={link.obj.url} |
131 |
| - title={link.obj.title} |
132 |
| - url={link.obj.url} |
133 |
| - icon={link.obj.icon} |
134 |
| - searchable={link.obj.searchUrl !== undefined} |
135 |
| - color={link.obj.color} |
136 |
| - customize={false} |
137 |
| - visible={true} |
138 |
| - focus={link === focusedResult} |
139 |
| - /> |
140 |
| - )) |
141 |
| - ) : ( |
142 |
| - <div className="search__results-hint">No results found...</div> |
143 |
| - )} |
144 |
| - </> |
| 134 | + </>, |
| 135 | + [] |
| 136 | + ) |
| 137 | + |
| 138 | + const resultElements = useMemo( |
| 139 | + () => <> |
| 140 | + {results !== null && results.total > 0 ? ( |
| 141 | + results.map(link => ( |
| 142 | + <Link |
| 143 | + key={link.obj.url} |
| 144 | + title={link.obj.title} |
| 145 | + url={link.obj.url} |
| 146 | + icon={link.obj.icon} |
| 147 | + searchable={link.obj.searchUrl !== undefined} |
| 148 | + color={link.obj.color} |
| 149 | + customize={false} |
| 150 | + visible={true} |
| 151 | + focus={link === focusedResult} |
| 152 | + /> |
| 153 | + )) |
| 154 | + ) : ( |
| 155 | + <div className="search__results-hint">No results found...</div> |
| 156 | + )} |
| 157 | + </>, |
| 158 | + [focusedResult, results] |
| 159 | + ) |
145 | 160 |
|
146 | 161 | return (
|
147 | 162 | <div className="search">
|
@@ -193,19 +208,26 @@ function useSearch (latestKeypress: string): UseSearch {
|
193 | 208 | const { links } = useHiddenLinks()
|
194 | 209 | const visibleLinks = getAllLinks().filter(link => !links.includes(link.url))
|
195 | 210 |
|
196 |
| - const fuzzyOptions: Fuzzysort.KeyOptions = { |
197 |
| - key: 'title', allowTypo: false, limit: 6 |
198 |
| - } |
| 211 | + const fuzzyOptions: Fuzzysort.KeyOptions = useMemo( |
| 212 | + () => ({ key: 'title', allowTypo: false, limit: 6 }), |
| 213 | + [] |
| 214 | + ) |
199 | 215 |
|
200 |
| - const results = searchTerm !== '' && searchTarget === null |
201 |
| - ? fuzzy.go(searchTerm, visibleLinks, fuzzyOptions) |
202 |
| - : null |
| 216 | + const results = useMemo( |
| 217 | + () => searchTerm !== '' && searchTarget === null |
| 218 | + ? fuzzy.go(searchTerm, visibleLinks, fuzzyOptions) |
| 219 | + : null, |
| 220 | + [fuzzyOptions, searchTarget, searchTerm, visibleLinks] |
| 221 | + ) |
203 | 222 |
|
204 |
| - const focusedResult = results?.[keyboardIndex] ?? null |
| 223 | + const focusedResult = useMemo( |
| 224 | + () => results?.[keyboardIndex] ?? null, |
| 225 | + [keyboardIndex, results] |
| 226 | + ) |
205 | 227 |
|
206 | 228 | useEffect(() => {
|
207 |
| - inputElement.current?.focus() |
208 | 229 | setSearchTerm(latestKeypress)
|
| 230 | + setTimeout(() => { inputElement.current?.focus() }, 0) |
209 | 231 | }, [latestKeypress])
|
210 | 232 |
|
211 | 233 | return {
|
|
0 commit comments