77 NavLink ,
88} from 'reactstrap' ;
99import ReactTooltip from 'react-tooltip' ;
10- import { useTable , useFilters } from 'react-table' ;
10+ import { useTable , useFilters , useSortBy } from 'react-table' ;
1111import { FaQuestionCircle } from 'react-icons/fa' ;
1212import { Event } from '../Shared/Tracking' ;
1313
@@ -17,49 +17,10 @@ import './styles.scss';
1717
1818const images = require . context ( '../../icons' , true ) ;
1919
20- function DefaultColumnFilter ( {
21- column : { filterValue, preFilteredRows, setFilter } ,
22- } ) {
23- const count = preFilteredRows . length ;
24-
25- return (
26- < input
27- value = { filterValue || '' }
28- onChange = { e => {
29- setFilter ( e . target . value || undefined ) ; // Set undefined to remove the filter entirely
30- } }
31- placeholder = { `Search ${ count } questions...` }
32- />
33- ) ;
34- }
35-
36- function SelectColumnFilter ( {
37- column : { filterValue, setFilter, preFilteredRows, id } ,
38- } ) {
39- const options = React . useMemo ( ( ) => {
40- const options = new Set ( ) ;
41- preFilteredRows . forEach ( row => {
42- options . add ( row . values [ id ] ) ;
43- } ) ;
44- return [ ...options . values ( ) ] ;
45- } , [ id , preFilteredRows ] ) ;
46-
47- return (
48- < select
49- value = { filterValue }
50- onChange = { e => {
51- setFilter ( e . target . value || undefined ) ;
52- } }
53- >
54- < option value = "" > All</ option >
55- { options . map ( ( option , i ) => (
56- < option key = { i } value = { option } >
57- { option }
58- </ option >
59- ) ) }
60- </ select >
61- ) ;
62- }
20+ const sortByObject = { Easy : 0 , Medium : 1 , Hard : 2 } ;
21+ questionList . sort (
22+ ( a , b ) => sortByObject [ a . difficulty ] - sortByObject [ b . difficulty ] ,
23+ ) ;
6324
6425const Table = ( ) => {
6526 const [ checked , setChecked ] = useState (
@@ -71,21 +32,21 @@ const Table = () => {
7132 window . localStorage . setItem ( 'checked' , JSON . stringify ( checked ) ) ;
7233 } , [ checked ] ) ;
7334
35+ const data = React . useMemo ( ( ) => questionList , [ ] ) ;
36+
7437 const defaultColumn = React . useMemo (
7538 ( ) => ( {
7639 Filter : DefaultColumnFilter ,
7740 minWidth : 30 ,
78- maxWidth : 10 ,
41+ maxWidth : 30 ,
7942 } ) ,
8043 [ ] ,
8144 ) ;
8245
83- const data = React . useMemo ( ( ) => questionList , [ ] ) ;
84-
8546 const columns = React . useMemo (
8647 ( ) => [
8748 {
88- Header : 'Sort questions by name or pattern! ' ,
49+ Header : 'Leetcode Patterns ' ,
8950 columns : [
9051 {
9152 id : 'Checkbox' ,
@@ -138,7 +99,6 @@ const Table = () => {
13899 {
139100 Header : 'Difficulty' ,
140101 accessor : 'difficulty' ,
141- Filter : SelectColumnFilter ,
142102 Cell : cellInfo => (
143103 < Badge
144104 className = { cellInfo . row . original . difficulty . toLowerCase ( ) }
@@ -147,6 +107,7 @@ const Table = () => {
147107 { cellInfo . row . original . difficulty }
148108 </ Badge >
149109 ) ,
110+ Filter : SelectColumnFilter ,
150111 } ,
151112 {
152113 Header : ( ) => {
@@ -184,6 +145,50 @@ const Table = () => {
184145 [ ] ,
185146 ) ;
186147
148+ function DefaultColumnFilter ( {
149+ column : { filterValue, preFilteredRows, setFilter } ,
150+ } ) {
151+ const count = preFilteredRows . length ;
152+
153+ return (
154+ < input
155+ value = { filterValue || '' }
156+ onChange = { e => {
157+ setFilter ( e . target . value || undefined ) ; // Set undefined to remove the filter entirely
158+ } }
159+ placeholder = { `Search ${ count } questions...` }
160+ />
161+ ) ;
162+ }
163+
164+ function SelectColumnFilter ( {
165+ column : { filterValue, setFilter, preFilteredRows, id } ,
166+ } ) {
167+ const options = React . useMemo ( ( ) => {
168+ const options = new Set ( ) ;
169+ preFilteredRows . forEach ( row => {
170+ options . add ( row . values [ id ] ) ;
171+ } ) ;
172+ return [ ...options . values ( ) ] ;
173+ } , [ id , preFilteredRows ] ) ;
174+
175+ return (
176+ < select
177+ value = { filterValue }
178+ onChange = { e => {
179+ setFilter ( e . target . value || undefined ) ;
180+ } }
181+ >
182+ < option value = "" > All</ option >
183+ { options . map ( ( option , i ) => (
184+ < option key = { i } value = { option } >
185+ { option }
186+ </ option >
187+ ) ) }
188+ </ select >
189+ ) ;
190+ }
191+
187192 const {
188193 getTableProps,
189194 getTableBodyProps,
@@ -195,8 +200,12 @@ const Table = () => {
195200 columns,
196201 data,
197202 defaultColumn,
203+ initialState : {
204+ sortBy : [ { id : 'pattern' } ] ,
205+ } ,
198206 } ,
199207 useFilters ,
208+ useSortBy ,
200209 ) ;
201210
202211 return (
0 commit comments