Skip to content

Commit

Permalink
Update searchpal.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinLiquid authored May 11, 2024
1 parent 6c95fb2 commit 0e542d0
Showing 1 changed file with 1 addition and 75 deletions.
76 changes: 1 addition & 75 deletions src/searchpal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,59 +229,6 @@ class SearchPalette {
})
}

/**
* Handle a cached track
*
* @private
* @param key The key of the cached track
* @memberof SearchPalette
*/
private async handleCachedTrack (key: string): Promise<void> {
const data: CachedData | null = await this.localForage.getItem(key)
if (data == null || (data.image == null || data.track == null || data.track.name == null || data.track.artists == null)) return

const item = new HTML('div').classOn('item').attr({ tabindex: '0' })
const icon = new HTML('img').classOn('image').attr({ src: data.image, alt: `${data.track.name} by ${data.track.artists.map(artist => artist.name).join(', ')}` })
const meta = new HTML('span').text(
`${data.track.name}\n${data.track.artists
.map(artist => artist.name)
.join(', ')}`
)
const icons = new HTML('div').classOn('icons')

const add = new HTML('button')
.classOn('material-symbols-sharp')
.text('playlist_add')
.appendTo(icons)

const remove = new HTML('button')
.classOn('material-symbols-sharp')
.text('delete')
.appendTo(icons)

remove.on('click', e => {
e.preventDefault()
e.stopPropagation()
this.localForage.removeItem(key).catch(console.error)
item.cleanup()
})

add.on('click', e => {
e.preventDefault()
e.stopPropagation()
this.queue.add(data.track)
})

item.appendMany(icon, meta, icons)
item.appendTo(this.container)

item.on('click', () => {
this.queue.load(data.track)
this.player.start().catch(console.error)
this.hide()
})
}

/**
* Render the search results
*
Expand Down Expand Up @@ -342,22 +289,6 @@ class SearchPalette {
})
}

/**
* Render the cached data
*
* @private
* @memberof SearchPalette
*/
private renderCached (): void {
this.container.html('')
this.container.append(new HTML('div').id('tracks'))
this.localForage.keys().then(keys => {
keys.forEach(key => {
this.handleCachedTrack(key).catch(console.error)
})
}).catch(console.error)
}

/**
* Register the events
*
Expand Down Expand Up @@ -398,18 +329,13 @@ class SearchPalette {
})
)
}

opened = false

/**
* Show the search palette
*
* @memberof SearchPalette
*/
show (): void {
if (!this.opened && window.isSafari === false) {
// Fill the search palette with cached data
this.localForage.ready(() => this.renderCached()).catch(console.error)
}
this.element.classOn('show')
}

Expand Down

0 comments on commit 0e542d0

Please sign in to comment.