Skip to content

Commit 1d4d856

Browse files
mattseddonsroy3
andauthored
Handle missing items in drag and drop container (#5054)
Co-authored-by: Stephanie Roy <[email protected]>
1 parent 30e3653 commit 1d4d856

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

webview/src/plots/components/comparisonTable/ComparisonTable.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,25 @@ describe('ComparisonTable', () => {
388388
expect(aHeader.style.top).not.toBe('')
389389
})
390390

391+
it('should not throw an error when an image is removed from the data', () => {
392+
const { rerender } = renderTable()
393+
394+
const plotsWithMissingImage = comparisonTableFixture.plots.slice(1)
395+
expect(plotsWithMissingImage.length).toStrictEqual(
396+
comparisonTableFixture.plots.length - 1
397+
)
398+
399+
expect(() =>
400+
renderTable(
401+
{
402+
...comparisonTableFixture,
403+
plots: plotsWithMissingImage
404+
},
405+
rerender
406+
)
407+
).not.toThrow()
408+
})
409+
391410
describe('Columns drag and drop', () => {
392411
const pinSecondColumn = () => {
393412
const secondColumn = getPin(screen.getByText(revisions[1]))

webview/src/plots/components/comparisonTable/ComparisonTableRows.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,33 @@ export const ComparisonTableRows: React.FC<ComparisonTableRowsProps> = ({
3737
dispatch(changeRowHeight(firstRowHeight))
3838
}
3939

40-
const rows = rowsOrder.map((path, i) => {
41-
const plot = plots.find(p => p.path === path)
42-
if (!plot) {
43-
return
44-
}
45-
const revs = plot.revisions
46-
return (
47-
<tbody
48-
data-testid="comparison-table-body"
49-
key={path}
50-
id={path}
51-
ref={i === 0 ? firstRowRef : undefined}
52-
>
53-
<ComparisonTableRow
54-
path={path}
55-
plots={columns.map(column => ({
56-
id: column.id,
57-
imgs: revs[column.id]?.imgs
58-
}))}
59-
nbColumns={columns.length}
60-
pinnedColumn={pinnedColumn}
61-
/>
62-
</tbody>
63-
)
64-
})
40+
const rows = rowsOrder
41+
.map((path, i) => {
42+
const plot = plots.find(p => p.path === path)
43+
if (!plot) {
44+
return
45+
}
46+
const revs = plot.revisions
47+
return (
48+
<tbody
49+
data-testid="comparison-table-body"
50+
key={path}
51+
id={path}
52+
ref={i === 0 ? firstRowRef : undefined}
53+
>
54+
<ComparisonTableRow
55+
path={path}
56+
plots={columns.map(column => ({
57+
id: column.id,
58+
imgs: revs[column.id]?.imgs
59+
}))}
60+
nbColumns={columns.length}
61+
pinnedColumn={pinnedColumn}
62+
/>
63+
</tbody>
64+
)
65+
})
66+
.filter(Boolean) as JSX.Element[]
6567

6668
const changeRowsOrder = (order: string[]) => {
6769
setRowsOrder(order)
@@ -70,7 +72,7 @@ export const ComparisonTableRows: React.FC<ComparisonTableRowsProps> = ({
7072

7173
return (
7274
<DragDropContainer
73-
items={rows as JSX.Element[]}
75+
items={rows}
7476
order={rowsOrder}
7577
setOrder={changeRowsOrder}
7678
group="comparison-table"

webview/src/shared/components/dragDrop/DragDropContainer.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
297297

298298
const wrappedItems = items
299299
.map(draggable => {
300+
if (!draggable) {
301+
return
302+
}
300303
const id = draggable.props.id
301304
const isDraggedOver =
302305
id === draggedOverId && (hoveringSomething || !parentDraggedOver)

0 commit comments

Comments
 (0)