Skip to content

🎧 Your Personal Streaming Service

fix(ui): don't crash the playlist list when rows lose their record (#6250)

Deluan Quintão0e1893530b84
diff --git a/ui/src/playlist/PlaylistList.jsx b/ui/src/playlist/PlaylistList.jsx
index d2b17b108..14d819a4e 100644
--- a/ui/src/playlist/PlaylistList.jsx
+++ b/ui/src/playlist/PlaylistList.jsx
@@ -67,15 +67,15 @@ const PlaylistFilter = (props) => {
)
}
-const TogglePublicInput = ({ resource, source }) => {
+export const ToggleField = ({ resource, source }) => {
const record = useRecordContext()
const notify = useNotify()
- const [togglePublic] = useUpdate(
+ const [toggle] = useUpdate(
resource,
- record.id,
+ record?.id,
{
...record,
- public: !record.public,
+ [source]: !record?.[source],
},
{
undoable: false,
@@ -86,10 +86,12 @@ const TogglePublicInput = ({ resource, source }) => {
)
const handleClick = (e) => {
- togglePublic()
+ toggle()
e.stopPropagation()
}
+ if (!record) return null
+
return (
<Switch
checked={record[source]}
@@ -99,35 +101,9 @@ const TogglePublicInput = ({ resource, source }) => {
)
}
-const ToggleAutoImport = ({ resource, source }) => {
+export const ToggleAutoImport = (props) => {
const record = useRecordContext()
- const notify = useNotify()
- const [ToggleAutoImport] = useUpdate(
- resource,
- record.id,
- {
- ...record,
- sync: !record.sync,
- },
- {
- undoable: false,
- onFailure: (error) => {
- notify('ra.page.error', 'warning')
- },
- },
- )
- const handleClick = (e) => {
- ToggleAutoImport()
- e.stopPropagation()
- }
-
- return record.path ? (
- <Switch
- checked={record[source]}
- onClick={handleClick}
- disabled={!isWritable(record.ownerId)}
- />
- ) : null
+ return record?.path ? <ToggleField {...props} /> : null
}
const PlaylistListBulkActions = (props) => {
@@ -169,9 +145,7 @@ const PlaylistList = (props) => {
updatedAt: isDesktop && (
<DateField source="updatedAt" sortByOrder={'DESC'} />
),
- public: !isXsmall && (
- <TogglePublicInput source="public" sortByOrder={'DESC'} />
- ),
+ public: !isXsmall && <ToggleField source="public" sortByOrder={'DESC'} />,
comment: <TextField source="comment" />,
sync: !isXsmall && (
<ToggleAutoImport source="sync" sortByOrder={'DESC'} />
diff --git a/ui/src/playlist/PlaylistList.test.jsx b/ui/src/playlist/PlaylistList.test.jsx
index 4fbc6d516..6c714b827 100644
--- a/ui/src/playlist/PlaylistList.test.jsx
+++ b/ui/src/playlist/PlaylistList.test.jsx
@@ -1,7 +1,8 @@
import React from 'react'
import { render, screen } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
-import { PlaylistLove } from './PlaylistList'
+import { TestContext } from 'ra-test'
+import { PlaylistLove, ToggleField, ToggleAutoImport } from './PlaylistList'
vi.mock('../config', () => ({
default: { enableFavourites: true },
@@ -32,3 +33,25 @@ describe('<PlaylistLove />', () => {
})
})
})
+
+// react-admin evicts records older than 10 minutes while the list still holds
+// their ids, so rows can render with no record.
+describe('playlist toggles without a record', () => {
+ it('<ToggleField /> renders nothing', () => {
+ const { container } = render(
+ <TestContext>
+ <ToggleField resource="playlist" source="public" />
+ </TestContext>,
+ )
+ expect(container.innerHTML).toBe('')
+ })
+
+ it('<ToggleAutoImport /> renders nothing', () => {
+ const { container } = render(
+ <TestContext>
+ <ToggleAutoImport resource="playlist" source="sync" />
+ </TestContext>,
+ )
+ expect(container.innerHTML).toBe('')
+ })
+})