implement SearchButton on playlist view

This commit is contained in:
SrGooglo 2023-06-01 18:01:41 +00:00
parent e875f7a090
commit ba8546d621
4 changed files with 134 additions and 29 deletions

View File

@ -6,6 +6,8 @@ import "./index.less"
export default (props) => { export default (props) => {
const searchBoxRef = React.useRef(null) const searchBoxRef = React.useRef(null)
const [value, setValue] = React.useState()
const [open, setOpen] = React.useState() const [open, setOpen] = React.useState()
const openSearchBox = (to) => { const openSearchBox = (to) => {
@ -17,16 +19,38 @@ export default (props) => {
} }
} }
const handleOnChange = (value) => {
setValue(value)
if (!value || value.length === 0 || value === "" || value === " ") {
if (typeof props.onEmpty === "function") {
props.onEmpty()
}
return false
}
if (typeof props.onChange === "function") {
props.onChange(value)
}
}
return <div return <div
className="searchButton"
onClick={() => openSearchBox(true)} onClick={() => openSearchBox(true)}
className="searchButton"> >
<SearchBar <SearchBar
ref={searchBoxRef} ref={searchBoxRef}
className={classnames("searchBox", { ["open"]: open })} className={classnames("searchBox", { ["open"]: open })}
onSearch={props.onSearch} onSearch={props.onSearch}
onChange={props.onChange} onChange={handleOnChange}
value={value}
onFocus={() => openSearchBox(true)} onFocus={() => openSearchBox(true)}
onBlur={() => openSearchBox(false)} onBlur={() => {
if (value.length === 0) {
openSearchBox(false)
}
}}
/> />
</div> </div>
} }

View File

@ -1,18 +1,38 @@
.searchButton { .searchButton {
.searchBox { .searchBox {
color: var(--text-color);
svg {
color: var(--text-color);
margin: 0;
}
.adm-search-bar-input-box {
color: var(--text-color);
background-color: var(--background-color-accent);
padding: 0 6px;
border-radius: 12px;
.adm-search-bar-input { .adm-search-bar-input {
transition: all 150ms ease-in-out; transition: all 150ms ease-in-out;
width: 0px; width: 0px;
padding: 0;
}
} }
&.open { &.open {
.adm-search-bar-input-box {
.adm-search-bar-input { .adm-search-bar-input {
width: 20vw; width: 20vw;
} max-width: 400px;
}
}
svg { padding-left: 10px;
margin: 0; }
}
}
} }
} }

View File

@ -4,6 +4,7 @@ import classnames from "classnames"
import ReactMarkdown from "react-markdown" import ReactMarkdown from "react-markdown"
import remarkGfm from "remark-gfm" import remarkGfm from "remark-gfm"
import moment from "moment" import moment from "moment"
import fuse from "fuse.js"
import { WithPlayerContext } from "contexts/WithPlayerContext" import { WithPlayerContext } from "contexts/WithPlayerContext"
@ -13,12 +14,17 @@ import { Icons } from "components/Icons"
import PlaylistsModel from "models/playlists" import PlaylistsModel from "models/playlists"
import MusicTrack from "components/MusicTrack" import MusicTrack from "components/MusicTrack"
import SearchButton from "components/SearchButton"
import "./index.less" import "./index.less"
export default (props) => { export default (props) => {
const play_id = props.params.play_id const play_id = props.params.play_id
const [playlist, setPlaylist] = React.useState(null) const [playlist, setPlaylist] = React.useState(null)
const [searchResults, setSearchResults] = React.useState(null)
let debounceSearch = null
const loadData = async () => { const loadData = async () => {
const response = await PlaylistsModel.getPlaylist(play_id).catch((err) => { const response = await PlaylistsModel.getPlaylist(play_id).catch((err) => {
@ -47,6 +53,48 @@ export default (props) => {
app.cores.player.startPlaylist(playlist.list, index) app.cores.player.startPlaylist(playlist.list, index)
} }
const makeSearch = (value) => {
const options = {
includeScore: true,
keys: [
"title",
"artist",
"album",
],
}
const fuseInstance = new fuse(playlist.list, options)
const results = fuseInstance.search(value)
setSearchResults(results.map((result) => {
return result.item
}))
}
const returnTracks = (list) => {
return list.map((item, index) => {
return <MusicTrack
order={index + 1}
track={item}
onClick={() => handleOnClickTrack(item)}
/>
})
}
const handleOnSearchChange = (value) => {
debounceSearch = setTimeout(() => {
makeSearch(value)
}, 500)
}
const handleOnSearchEmpty = () => {
if (debounceSearch) {
clearTimeout(debounceSearch)
}
setSearchResults(null)
}
React.useEffect(() => { React.useEffect(() => {
loadData() loadData()
}, []) }, [])
@ -108,19 +156,20 @@ export default (props) => {
</div> </div>
<div className="list"> <div className="list">
<div className="list_header">
<h1> <h1>
<Icons.MdPlaylistPlay /> Tracks <Icons.MdPlaylistPlay /> Tracks
</h1> </h1>
<SearchButton
onChange={handleOnSearchChange}
onEmpty={handleOnSearchEmpty}
/>
</div>
<WithPlayerContext> <WithPlayerContext>
{ {
playlist.list.map((item, index) => { returnTracks(searchResults ?? playlist.list)
return <MusicTrack
order={index + 1}
track={item}
onClick={() => handleOnClickTrack(item)}
/>
})
} }
</WithPlayerContext> </WithPlayerContext>
</div> </div>

View File

@ -152,5 +152,17 @@
padding: 20px 30px; padding: 20px 30px;
gap: 10px; gap: 10px;
h1 {
margin: 0;
}
.list_header {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
} }
} }