11import { useRef , useState } from 'react'
2+ import Highlighter from 'react-highlight-words'
23import { fetchComments } from './api'
34import { Reply } from './reply'
45import { extractVideoId , formatDateTime } from './utils'
56
67import './App.css'
78
9+ let keyword = ''
10+
811export default function App ( ) {
912 const dialogRef = useRef < HTMLDialogElement > ( null )
1013 const [ comments , setComments ] = useState < Reply [ ] > ( )
@@ -22,10 +25,11 @@ export default function App() {
2225 if ( e . key === 'Enter' && promiseRef . current ) {
2326 e . preventDefault ( )
2427 // `e.currentTarget` will be null after `await`
25- const keyword = e . currentTarget . value
28+ const _keyword = e . currentTarget . value
29+ keyword = _keyword
2630 promiseRef . current
2731 . then ( ( allComments ) => {
28- const comments = searchComments ( allComments , keyword )
32+ const comments = searchComments ( allComments , _keyword )
2933 setComments ( comments )
3034 } )
3135 . catch ( ( e ) => {
@@ -87,9 +91,13 @@ const CommentTree: React.FC<CommentProps> = ({ comments, sub = false }) => {
8791 < span className = 'comment-item-username' >
8892 { comment . member . uname }
8993 </ span >
90- < span className = 'comment-item-message' >
91- { comment . content . message }
92- </ span >
94+ < Highlighter
95+ className = 'comment-item-message'
96+ highlightClassName = 'comment-item-highlight'
97+ searchWords = { [ keyword ] }
98+ autoEscape = { true }
99+ textToHighlight = { comment . content . message }
100+ />
93101 < div className = 'comment-item-time' >
94102 { formatDateTime ( comment . ctime ) }
95103 </ div >
0 commit comments