mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-06 05:57:35 +03:00
Only refetch changed resources when receive a "refreshResource" event
This commit is contained in:
parent
8a56584aed
commit
8383527aab
6 changed files with 235 additions and 39 deletions
|
@ -1,23 +1,36 @@
|
|||
import { useSelector } from 'react-redux'
|
||||
import { useState } from 'react'
|
||||
import { useRefresh } from 'react-admin'
|
||||
import { useRefresh, useDataProvider } from 'react-admin'
|
||||
|
||||
export const useResourceRefresh = (...resources) => {
|
||||
export const useResourceRefresh = (...visibleResources) => {
|
||||
const [lastTime, setLastTime] = useState(Date.now())
|
||||
const refreshData = useSelector(
|
||||
(state) => state.activity?.refresh || { lastTime }
|
||||
)
|
||||
const refresh = useRefresh()
|
||||
const dataProvider = useDataProvider()
|
||||
const refreshData = useSelector(
|
||||
(state) => state.activity?.refresh || { lastReceived: lastTime }
|
||||
)
|
||||
const { resources, lastReceived } = refreshData
|
||||
|
||||
const resource = refreshData.resource
|
||||
if (refreshData.lastTime > lastTime) {
|
||||
if (
|
||||
resource === '' ||
|
||||
resources.length === 0 ||
|
||||
resources.includes(resource)
|
||||
) {
|
||||
refresh()
|
||||
}
|
||||
setLastTime(refreshData.lastTime)
|
||||
if (lastReceived <= lastTime) {
|
||||
return
|
||||
}
|
||||
setLastTime(lastReceived)
|
||||
|
||||
if (
|
||||
resources &&
|
||||
(resources['*'] === '*' ||
|
||||
Object.values(resources).find((v) => v.find((v2) => v2 === '*')))
|
||||
) {
|
||||
refresh()
|
||||
return
|
||||
}
|
||||
if (resources) {
|
||||
Object.keys(resources).forEach((r) => {
|
||||
if (visibleResources.length === 0 || visibleResources?.includes(r)) {
|
||||
resources[r]?.forEach((id) => {
|
||||
dataProvider.getOne(r, { id })
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue