Skip to content

Commit 4d78b9e

Browse files
authored
Use Redux for drag and drop state (#1944)
* WIP add redux * WIP add redux * Fix sections and zoomedInPlot * Fix storybook * Fix lint errors * Fix some tests * Fix tests * Revert demo changed files * Revert demo changed files * Revert demo changed files * Use Redux state for drag and drop * e Redux for drag and drop state * Remove clear data action * Revert changes to experiments * Revert change to experiments * Fix comparison table stories * Fix storybook
1 parent e7c1562 commit 4d78b9e

File tree

8 files changed

+61
-38
lines changed

8 files changed

+61
-38
lines changed

webview/src/plots/components/Plots.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { setZoomedInPlot } from './webviewSlice'
1010
import { EmptyState } from '../../shared/components/emptyState/EmptyState'
1111
import { Modal } from '../../shared/components/modal/Modal'
1212
import { WebviewWrapper } from '../../shared/components/webviewWrapper/WebviewWrapper'
13-
import { DragDropProvider } from '../../shared/components/dragDrop/DragDropContext'
1413
import { GetStarted } from '../../shared/components/getStarted/GetStarted'
1514
import { RootState } from '../store'
1615

@@ -56,11 +55,9 @@ const PlotsContent = () => {
5655
return (
5756
<>
5857
<Ribbon />
59-
<DragDropProvider>
60-
{hasTemplateData && <TemplatePlotsWrapper />}
61-
{hasComparisonData && <ComparisonTableWrapper />}
62-
{hasCheckpointData && <CheckpointPlotsWrapper />}
63-
</DragDropProvider>
58+
{hasTemplateData && <TemplatePlotsWrapper />}
59+
{hasComparisonData && <ComparisonTableWrapper />}
60+
{hasCheckpointData && <CheckpointPlotsWrapper />}
6461

6562
{zoomedInPlot?.plot && (
6663
<Modal

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
} from '../../../test/dragDrop'
2727
import { vsCodeApi } from '../../../shared/api'
2828
import { DragEnterDirection } from '../../../shared/components/dragDrop/util'
29-
import { DragDropProvider } from '../../../shared/components/dragDrop/DragDropContext'
3029
import { storeReducers } from '../../store'
3130
import { webviewInitialState } from '../webviewSlice'
3231

@@ -74,9 +73,7 @@ describe('ComparisonTable', () => {
7473
reducer: storeReducers
7574
})}
7675
>
77-
<DragDropProvider>
78-
<ComparisonTable />
79-
</DragDropProvider>
76+
<ComparisonTable />
8077
</Provider>
8178
) || {}
8279
)

webview/src/plots/components/ribbon/Ribbon.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { RibbonBlock } from './RibbonBlock'
77
import { sendMessage } from '../../../shared/vscode'
88
import { IconButton } from '../../../shared/components/button/IconButton'
99
import { performOrderedUpdate } from '../../../util/objects'
10-
1110
import { RootState } from '../../store'
1211
import { Lines, Refresh } from '../../../shared/components/icons'
1312

webview/src/plots/components/templatePlots/AddedSection.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React, { DragEvent, useContext } from 'react'
1+
import React, { DragEvent } from 'react'
2+
import { useSelector } from 'react-redux'
23
import cx from 'classnames'
34
import { TemplatePlotSection } from 'dvc/src/plots/webview/contract'
45
import styles from '../styles.module.scss'
56
import { getIDWithoutIndex } from '../../../util/ids'
7+
import { RootState } from '../../store'
68
import { Icon } from '../../../shared/components/Icon'
7-
import { DragDropContext } from '../../../shared/components/dragDrop/DragDropContext'
89
import { GraphLine } from '../../../shared/components/icons'
910

1011
interface AddedSectionProps {
@@ -24,7 +25,7 @@ export const AddedSection: React.FC<AddedSectionProps> = ({
2425
closestSection,
2526
acceptedGroups
2627
}) => {
27-
const { draggedRef } = useContext(DragDropContext)
28+
const { draggedRef } = useSelector((state: RootState) => state.dragAndDrop)
2829
const handleDragLeave = () => {
2930
setHoveredSection('')
3031
}

webview/src/plots/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import checkpointPlotsReducer from './components/checkpointPlots/checkpointPlots
33
import comparisonTableReducer from './components/comparisonTable/comparisonTableSlice'
44
import templatePlotsReducer from './components/templatePlots/templatePlotsSlice'
55
import webviewReducer from './components/webviewSlice'
6+
import dragAndDropReducer from '../shared/components/dragDrop/dragDropSlice'
67

78
export const storeReducers = {
89
checkpoint: checkpointPlotsReducer,
910
comparison: comparisonTableReducer,
11+
dragAndDrop: dragAndDropReducer,
1012
template: templatePlotsReducer,
1113
webview: webviewReducer
1214
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import React, {
33
useEffect,
44
useState,
55
useRef,
6-
useContext,
76
DragEventHandler
87
} from 'react'
8+
import { useDispatch, useSelector } from 'react-redux'
99
import { DragEnterDirection, getDragEnterDirection } from './util'
10-
import { DragDropContext, DragDropContextValue } from './DragDropContext'
10+
import { changeRef } from './dragDropSlice'
1111
import { getIDIndex, getIDWithoutIndex } from '../../../util/ids'
1212
import { Any } from '../../../util/objects'
13+
import { RootState } from '../../../plots/store'
1314

1415
const orderIdxTune = (direction: DragEnterDirection, isAfter: boolean) => {
1516
if (direction === DragEnterDirection.RIGHT) {
@@ -84,9 +85,9 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
8485
const [draggedOverId, setDraggedOverId] = useState('')
8586
const [draggedId, setDraggedId] = useState('')
8687
const [direction, setDirection] = useState(DragEnterDirection.LEFT)
87-
const { draggedRef, setDraggedRef } =
88-
useContext<DragDropContextValue>(DragDropContext)
88+
const { draggedRef } = useSelector((state: RootState) => state.dragAndDrop)
8989
const draggedOverIdTimeout = useRef<number>(0)
90+
const dispatch = useDispatch()
9091

9192
const cleanup = () => {
9293
setDraggedOverId('')
@@ -119,11 +120,13 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
119120
e.dataTransfer.setData('group', group)
120121
e.dataTransfer.effectAllowed = 'move'
121122
e.dataTransfer.dropEffect = 'move'
122-
setDraggedRef?.({
123-
group,
124-
itemId: id,
125-
itemIndex
126-
})
123+
dispatch(
124+
changeRef({
125+
group,
126+
itemId: id,
127+
itemIndex
128+
})
129+
)
127130
draggedOverIdTimeout.current = window.setTimeout(() => {
128131
setDraggedId(id)
129132
setDraggedOverId(order[toIdx])
@@ -143,7 +146,7 @@ export const DragDropContainer: React.FC<DragDropContainerProps> = ({
143146
newOrder.splice(droppedIndex, 0, dragged)
144147

145148
setOrder(newOrder)
146-
setDraggedRef?.(undefined)
149+
dispatch(changeRef(undefined))
147150

148151
onDrop?.(oldDraggedId, e.dataTransfer.getData('group'), group, droppedIndex)
149152
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
2+
3+
export type DraggedInfo =
4+
| {
5+
itemIndex: string
6+
itemId: string
7+
group?: string
8+
}
9+
| undefined
10+
export interface DragDropState {
11+
draggedRef: DraggedInfo
12+
}
13+
14+
export const dragDropInitialState: DragDropState = {
15+
draggedRef: undefined
16+
}
17+
18+
export const dragDropSlice = createSlice({
19+
initialState: dragDropInitialState,
20+
name: 'dragAndDrop',
21+
reducers: {
22+
changeRef: (state, action: PayloadAction<DraggedInfo>) => {
23+
return {
24+
...state,
25+
draggedRef: action.payload
26+
}
27+
}
28+
}
29+
})
30+
31+
export const { changeRef } = dragDropSlice.actions
32+
33+
export default dragDropSlice.reducer

webview/src/stories/ComparisonTable.stories.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ import {
1313
import comparisonTableFixture from 'dvc/src/test/fixtures/plotsDiff/comparison'
1414
import { ComparisonTable } from '../plots/components/comparisonTable/ComparisonTable'
1515
import { WebviewWrapper } from '../shared/components/webviewWrapper/WebviewWrapper'
16-
import { DragDropProvider } from '../shared/components/dragDrop/DragDropContext'
17-
import comparisonTableReducer, {
18-
update
19-
} from '../plots/components/comparisonTable/comparisonTableSlice'
20-
import webviewReducer, {
21-
updateSelectedRevisions
22-
} from '../plots/components/webviewSlice'
16+
import { update } from '../plots/components/comparisonTable/comparisonTableSlice'
17+
import { updateSelectedRevisions } from '../plots/components/webviewSlice'
18+
import { storeReducers } from '../plots/store'
2319

2420
const MockedState: React.FC<{
2521
data: PlotsComparisonData
@@ -40,10 +36,7 @@ export default {
4036

4137
const Template: Story = ({ plots, revisions }) => {
4238
const store = configureStore({
43-
reducer: {
44-
comparison: comparisonTableReducer,
45-
webview: webviewReducer
46-
}
39+
reducer: storeReducers
4740
})
4841
return (
4942
<Provider store={store}>
@@ -55,9 +48,7 @@ const Template: Story = ({ plots, revisions }) => {
5548
selectedRevisions={revisions}
5649
>
5750
<WebviewWrapper>
58-
<DragDropProvider>
59-
<ComparisonTable />
60-
</DragDropProvider>
51+
<ComparisonTable />
6152
</WebviewWrapper>
6253
</MockedState>
6354
</Provider>

0 commit comments

Comments
 (0)