11import { TemplatePlotGroup } from 'dvc/src/plots/webview/contract'
2- import React , {
3- DragEvent ,
4- useState ,
5- useEffect ,
6- useRef ,
7- useCallback
8- } from 'react'
2+ import React , { DragEvent , useState , useCallback } from 'react'
93import cx from 'classnames'
104import { useDispatch , useSelector } from 'react-redux'
115import { MessageFromWebviewType } from 'dvc/src/webview/contract'
126import { AddedSection } from './AddedSection'
137import { TemplatePlotsGrid } from './TemplatePlotsGrid'
14- import { PlotGroup } from './templatePlotsSlice'
8+ import { PlotGroup , updateSections } from './templatePlotsSlice'
159import { removeFromPreviousAndAddToNewSection } from './util'
1610import { sendMessage } from '../../../shared/vscode'
1711import { createIDWithIndex , getIDIndex } from '../../../util/ids'
@@ -30,9 +24,7 @@ export enum NewSectionBlock {
3024}
3125
3226export const TemplatePlots : React . FC = ( ) => {
33- const { plotSections, size } = useSelector (
34- ( state : PlotsState ) => state . template
35- )
27+ const { size, sections } = useSelector ( ( state : PlotsState ) => state . template )
3628 const draggedOverGroup = useSelector (
3729 ( state : PlotsState ) => state . dragAndDrop . draggedOverGroup
3830 )
@@ -43,29 +35,30 @@ export const TemplatePlots: React.FC = () => {
4335 ( state : PlotsState ) => state . webview . selectedRevisions
4436 )
4537
46- const [ sections , setSections ] = useState < PlotGroup [ ] > ( plotSections )
4738 const [ hoveredSection , setHoveredSection ] = useState ( '' )
4839 const nbItemsPerRow = size
49- const shouldSendMessage = useRef ( true )
5040 const dispatch = useDispatch ( )
5141
52- useEffect ( ( ) => {
53- shouldSendMessage . current = false
54- setSections ( plotSections )
55- } , [ plotSections , setSections ] )
56-
57- useEffect ( ( ) => {
58- if ( sections && shouldSendMessage . current ) {
59- sendMessage ( {
60- payload : sections . map ( section => ( {
61- group : section . group ,
62- paths : section . entries
63- } ) ) ,
64- type : MessageFromWebviewType . REORDER_PLOTS_TEMPLATES
65- } )
66- }
67- shouldSendMessage . current = true
68- } , [ sections ] )
42+ const sendReorderMessage = useCallback ( ( sections : PlotGroup [ ] ) => {
43+ sendMessage ( {
44+ payload : sections . map ( section => ( {
45+ group : section . group ,
46+ paths : section . entries
47+ } ) ) ,
48+ type : MessageFromWebviewType . REORDER_PLOTS_TEMPLATES
49+ } )
50+ } , [ ] )
51+
52+ const setSections = useCallback (
53+ ( sections : PlotGroup [ ] ) => {
54+ /* Although the following dispatch duplicates the work the reducer will do when the state returns
55+ from the extension, this is necessary to not see any flickering in the order as the returned state
56+ sometimes takes a while to come back */
57+ dispatch ( updateSections ( sections ) )
58+ sendReorderMessage ( sections )
59+ } ,
60+ [ dispatch , sendReorderMessage ]
61+ )
6962
7063 const handleDropInSection = useCallback (
7164 (
@@ -94,16 +87,14 @@ export const TemplatePlots: React.FC = () => {
9487
9588 const setSectionEntries = useCallback (
9689 ( index : number , entries : string [ ] ) => {
97- setSections ( sections => {
98- const updatedSections = [ ...sections ]
99- updatedSections [ index ] = {
100- ...sections [ index ] ,
101- entries
102- }
103- return updatedSections
104- } )
90+ const updatedSections = [ ...sections ]
91+ updatedSections [ index ] = {
92+ ...updatedSections [ index ] ,
93+ entries
94+ }
95+ setSections ( updatedSections )
10596 } ,
106- [ setSections ]
97+ [ setSections , sections ]
10798 )
10899
109100 if ( sectionIsLoading ( selectedRevisions ) ) {
@@ -235,7 +226,6 @@ export const TemplatePlots: React.FC = () => {
235226 useVirtualizedGrid = { useVirtualizedGrid }
236227 nbItemsPerRow = { nbItemsPerRow }
237228 parentDraggedOver = { draggedOverGroup === groupId }
238- entries = { section . entries }
239229 />
240230 </ div >
241231 )
0 commit comments