fcdd30ba8f
* feat: create vite project * feat: it's alive! * feat: `make dev` working! * feat: replace custom serviceWorker with vite plugin * test: replace Jest with Vitest * fix: run prettier * fix: skip eslint for now. * chore: remove ui.old folder * refactor: replace lodash.pick with simple destructuring * fix: eslint errors (wip) * fix: eslint errors (wip) * fix: display-name eslint errors (wip) * fix: no-console eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: build * fix: pwa manifest * refactor: pwa manifest * refactor: simplify PORT configuration * refactor: rename simple JS files * test: cover playlistUtils * fix: react-image-lightbox * feat(ui): add sourcemaps to help debug issues
45 lines
1.1 KiB
React
45 lines
1.1 KiB
React
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { useDataProvider, useNotify } from 'react-admin'
|
|
import subsonic from '../subsonic'
|
|
|
|
export const useToggleLove = (resource, record = {}) => {
|
|
const [loading, setLoading] = useState(false)
|
|
const notify = useNotify()
|
|
|
|
const mountedRef = useRef(false)
|
|
useEffect(() => {
|
|
mountedRef.current = true
|
|
return () => {
|
|
mountedRef.current = false
|
|
}
|
|
}, [])
|
|
|
|
const dataProvider = useDataProvider()
|
|
|
|
const refreshRecord = useCallback(() => {
|
|
dataProvider.getOne(resource, { id: record.id }).then(() => {
|
|
if (mountedRef.current) {
|
|
setLoading(false)
|
|
}
|
|
})
|
|
}, [dataProvider, record.id, resource])
|
|
|
|
const toggleLove = () => {
|
|
const toggle = record.starred ? subsonic.unstar : subsonic.star
|
|
|
|
setLoading(true)
|
|
toggle(record.id)
|
|
.then(refreshRecord)
|
|
.catch((e) => {
|
|
// eslint-disable-next-line no-console
|
|
console.log('Error toggling love: ', e)
|
|
notify('ra.page.error', 'warning')
|
|
if (mountedRef.current) {
|
|
setLoading(false)
|
|
}
|
|
})
|
|
}
|
|
|
|
return [toggleLove, loading]
|
|
}
|