forked from smlxl/evm.codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilters.tsx
109 lines (91 loc) · 2.96 KB
/
Filters.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useState, useMemo, useEffect, useRef } from 'react'
import debounce from 'lodash.debounce'
import { useRouter } from 'next/router'
import Select, { OnChangeValue } from 'react-select'
import { Input } from 'components/ui'
const debounceTimeout = 100 // ms
type Props = {
onSetFilter: (columnId: string, value: string) => void
isPrecompiled?: boolean
}
const Filters = ({ onSetFilter, isPrecompiled = false }: Props) => {
const router = useRouter()
const [searchKeyword, setSearchKeyword] = useState('')
const [searchFilter, setSearchFilter] = useState({
value: 'name',
label: 'Name',
})
const filterByOptions = useMemo(
() => [
{
label: !isPrecompiled ? 'Opcode' : 'Address',
value: 'opcodeOrAddress',
},
{ label: 'Name', value: 'name' },
{ label: 'Description', value: 'description' },
],
[isPrecompiled],
)
const inputRef = useRef<HTMLInputElement>(null)
const handleKeywordChange = debounce(
(value: string) => onSetFilter(searchFilter.value, value),
debounceTimeout,
)
const handleSearchFilterChange = (option: OnChangeValue<any, any>) => {
// clear previous filter first
onSetFilter(searchFilter.value, '')
setSearchKeyword('')
setSearchFilter(option)
}
const handleAltK = (event: KeyboardEvent) => {
if (event.altKey && event.key.toLowerCase() === 'k') {
inputRef.current?.focus()
inputRef.current?.value && inputRef.current.select()
}
}
// Change filter and search opcode according to query param
useEffect(() => {
const query = router.query
if ('name' in query) {
// Change the filter type to Name
handleSearchFilterChange({ label: 'Name', value: 'name' })
setSearchKeyword(query.name as string)
handleKeywordChange(query.name as string)
router.push(router)
}
// Register and clean up Alt+K event listener
window.addEventListener('keydown', handleAltK)
return () => window.removeEventListener('keydown', handleAltK)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router.isReady])
return (
<div className="flex items-center md:justify-end">
<span className="hidden md:inline-block text-sm text-gray-400 mr-3">
Search by
</span>
<div className="bg-gray-100 dark:bg-black-500 rounded mr-3 pl-2 pr-1">
<Select
onChange={handleSearchFilterChange}
options={filterByOptions}
defaultValue={{ label: 'Name', value: 'name' }}
value={searchFilter}
isSearchable={false}
classNamePrefix="select"
menuPlacement="auto"
/>
</div>
<Input
ref={inputRef}
searchable
value={searchKeyword}
onChange={(e) => {
setSearchKeyword(e.target.value)
handleKeywordChange(e.target.value)
}}
placeholder={`Enter keyword...`}
className="bg-gray-100 dark:bg-black-500"
/>
</div>
)
}
export default Filters