1- import { useState } from 'react' ;
1+ import { useState , useEffect } from 'react' ;
22import { createPortal } from 'react-dom' ;
3+ import { db } from '../db' ;
34
45interface Props {
56 onSelect : ( exerciseName : string ) => void ;
@@ -8,16 +9,52 @@ interface Props {
89
910export function ExercisePicker ( { onSelect, onClose } : Props ) {
1011 const [ name , setName ] = useState ( '' ) ;
12+ const [ recentExercises , setRecentExercises ] = useState < string [ ] > ( [ ] ) ;
13+ const [ filteredExercises , setFilteredExercises ] = useState < string [ ] > ( [ ] ) ;
14+
15+ useEffect ( ( ) => {
16+ // Load recent unique exercise names
17+ async function loadRecent ( ) {
18+ const allExercises = await db . exercises . toArray ( ) ;
19+ const uniqueNames = Array . from ( new Set ( allExercises . map ( e => e . name ) ) )
20+ . filter ( Boolean )
21+ . sort ( ) ;
22+ setRecentExercises ( uniqueNames ) ;
23+ setFilteredExercises ( uniqueNames ) ;
24+ }
25+ loadRecent ( ) ;
26+ } , [ ] ) ;
27+
28+ useEffect ( ( ) => {
29+ // Filter exercises based on input
30+ if ( ! name . trim ( ) ) {
31+ setFilteredExercises ( recentExercises ) ;
32+ } else {
33+ const filtered = recentExercises . filter ( ex =>
34+ ex . toLowerCase ( ) . includes ( name . toLowerCase ( ) )
35+ ) ;
36+ setFilteredExercises ( filtered ) ;
37+ }
38+ } , [ name , recentExercises ] ) ;
1139
1240 function handleSubmit ( ) {
1341 if ( name . trim ( ) ) {
1442 onSelect ( name . trim ( ) ) ;
1543 }
1644 }
1745
46+ function handleSelect ( exerciseName : string ) {
47+ onSelect ( exerciseName ) ;
48+ }
49+
1850 function handleKeyDown ( e : React . KeyboardEvent ) {
1951 if ( e . key === 'Enter' ) {
20- handleSubmit ( ) ;
52+ if ( filteredExercises . length > 0 && ! name . trim ( ) ) {
53+ // If no input and there are suggestions, select first one
54+ handleSelect ( filteredExercises [ 0 ] ) ;
55+ } else {
56+ handleSubmit ( ) ;
57+ }
2158 }
2259 }
2360
@@ -33,19 +70,34 @@ export function ExercisePicker({ onSelect, onClose }: Props) {
3370 < div className = "form-group" >
3471 < input
3572 type = "text"
36- placeholder = "Exercise name..."
73+ placeholder = "Exercise name or search ..."
3774 value = { name }
3875 onChange = { ( e ) => setName ( e . target . value ) }
3976 onKeyDown = { handleKeyDown }
4077 autoFocus
4178 />
4279 </ div >
80+
81+ { filteredExercises . length > 0 && (
82+ < div className = "exercise-suggestions" >
83+ < div className = "suggestions-label" > Recent exercises:</ div >
84+ { filteredExercises . map ( ( ex ) => (
85+ < div
86+ key = { ex }
87+ className = "suggestion-item"
88+ onClick = { ( ) => handleSelect ( ex ) }
89+ >
90+ { ex }
91+ </ div >
92+ ) ) }
93+ </ div >
94+ ) }
4395 </ div >
4496
4597 < div className = "modal-actions" >
4698 < button className = "btn" onClick = { onClose } > Cancel</ button >
4799 < button className = "btn btn-primary" onClick = { handleSubmit } disabled = { ! name . trim ( ) } >
48- Add
100+ Add New
49101 </ button >
50102 </ div >
51103 </ div >
0 commit comments