Skip to content

Commit 1597fc0

Browse files
authored
Standardize use of send message across setup, experiments and plots (#4077)
1 parent 52093ce commit 1597fc0

32 files changed

+168
-128
lines changed

webview/src/experiments/components/AddStage.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
21
import React from 'react'
32
import styles from './table/styles.module.scss'
43
import { IconButton } from '../../shared/components/button/IconButton'
54
import { Add } from '../../shared/components/icons'
6-
import { sendMessage } from '../../shared/vscode'
5+
import { addConfiguration } from '../util/messages'
76

87
interface AddStageProps {
98
hasValidDvcYaml: boolean
@@ -14,10 +13,7 @@ export const AddStage: React.FC<AddStageProps> = ({ hasValidDvcYaml }) => (
1413
<p>Easily and efficiently reproduce your experiments </p>
1514
<IconButton
1615
icon={Add}
17-
onClick={() =>
18-
hasValidDvcYaml &&
19-
sendMessage({ type: MessageFromWebviewType.ADD_CONFIGURATION })
20-
}
16+
onClick={() => hasValidDvcYaml && addConfiguration()}
2117
text="Add a Pipeline Stage"
2218
disabled={!hasValidDvcYaml}
2319
/>

webview/src/experiments/components/Experiments.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
getExpandedRowModel,
2222
ColumnSizingState
2323
} from '@tanstack/react-table'
24-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
2524
import { Table } from './table/Table'
2625
import styles from './table/styles.module.scss'
2726
import { ErrorState } from './emptyState/ErrorState'
@@ -31,11 +30,11 @@ import { CellValue } from './table/content/Cell'
3130
import { AddStage } from './AddStage'
3231
import { ExperimentCell } from './table/content/ExperimentCell'
3332
import { buildColumns, columnHelper } from '../util/buildColumns'
34-
import { sendMessage } from '../../shared/vscode'
3533
import { WebviewWrapper } from '../../shared/components/webviewWrapper/WebviewWrapper'
3634
import { EmptyState } from '../../shared/components/emptyState/EmptyState'
3735
import { ExperimentsState } from '../store'
3836
import { EXPERIMENT_COLUMN_ID } from '../util/columns'
37+
import { resizeColumn } from '../util/messages'
3938

4039
const DEFAULT_COLUMN_WIDTH = 90
4140
const MINIMUM_COLUMN_WIDTH = 90
@@ -109,10 +108,7 @@ const reportResizedColumn = (
109108
if (width !== columnWidths[id]) {
110109
window.clearTimeout(debounceTimer.current)
111110
debounceTimer.current = window.setTimeout(() => {
112-
sendMessage({
113-
payload: { id, width },
114-
type: MessageFromWebviewType.RESIZE_COLUMN
115-
})
111+
resizeColumn(id, width)
116112
}, 500)
117113
}
118114
}

webview/src/experiments/components/table/header/TableHead.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import { Experiment } from 'dvc/src/experiments/webview/contract'
22
import React, { DragEvent, useRef, useEffect } from 'react'
33
import { useSelector, useDispatch } from 'react-redux'
44
import { Table, Header, ColumnOrderState } from '@tanstack/react-table'
5-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
65
import { MergedHeaderGroups } from './MergeHeaderGroups'
76
import { setDropTarget } from '../../../state/headerDropTargetSlice'
87
import { ExperimentsState } from '../../../store'
9-
import { sendMessage } from '../../../../shared/vscode'
108
import {
119
leafColumnIds,
1210
reorderColumnIds,
1311
isExperimentColumn
1412
} from '../../../util/columns'
1513
import { DragFunction } from '../../../../shared/components/dragDrop/Draggable'
1614
import styles from '../styles.module.scss'
15+
import { reorderColumns } from '../../../util/messages'
1716

1817
interface TableHeadProps {
1918
instance: Table<Experiment>
@@ -109,10 +108,7 @@ export const TableHead = ({
109108
newOrder = reorderColumnIds(fullOrder, displacer, leafs)
110109

111110
setColumnOrder(newOrder)
112-
sendMessage({
113-
payload: newOrder,
114-
type: MessageFromWebviewType.REORDER_COLUMNS
115-
})
111+
reorderColumns(newOrder)
116112
onDragEnd()
117113
onOrderChange(newOrder)
118114
}

webview/src/experiments/util/messages.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ export const addStarredFilter = () =>
1515
type: MessageFromWebviewType.ADD_STARRED_EXPERIMENT_FILTER
1616
})
1717

18+
export const addConfiguration = () =>
19+
sendMessage({ type: MessageFromWebviewType.ADD_CONFIGURATION })
20+
21+
export const reorderColumns = (newOrder: string[]) =>
22+
sendMessage({
23+
payload: newOrder,
24+
type: MessageFromWebviewType.REORDER_COLUMNS
25+
})
26+
27+
export const resizeColumn = (id: string, width: number) =>
28+
sendMessage({
29+
payload: { id, width },
30+
type: MessageFromWebviewType.RESIZE_COLUMN
31+
})
32+
1833
export const selectColumns = () =>
1934
sendMessage({ type: MessageFromWebviewType.SELECT_COLUMNS })
2035

webview/src/plots/components/PlotsContainer.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import React, {
88
import { AnyAction } from '@reduxjs/toolkit'
99
import { useDispatch, useSelector } from 'react-redux'
1010
import { PlotHeight, PlotsSection } from 'dvc/src/plots/webview/contract'
11-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
1211
import { PlotsPicker, PlotsPickerProps } from './PlotsPicker'
1312
import styles from './styles.module.scss'
1413
import { IconMenuItemProps } from '../../shared/components/iconMenu/IconMenuItem'
15-
import { sendMessage } from '../../shared/vscode'
1614
import { ListFilter, Add, Trash } from '../../shared/components/icons'
1715
import { Slider } from '../../shared/components/slider/Slider'
1816
import { PlotsState } from '../store'
1917
import { SectionContainer } from '../../shared/components/sectionContainer/SectionContainer'
18+
import { resizePlots, togglePlotsSection } from '../util/messages'
2019

2120
interface PlotsContainerProps {
2221
sectionCollapsed: boolean
@@ -96,25 +95,12 @@ export const PlotsContainer: React.FC<PlotsContainerProps> = ({
9695
nbItemsPerRowOrWidth: positiveNbItems
9796
})
9897
)
99-
sendMessage({
100-
payload: {
101-
height: newHeight,
102-
nbItemsPerRow: positiveNbItems,
103-
section: sectionKey
104-
},
105-
type: MessageFromWebviewType.RESIZE_PLOTS
106-
})
98+
resizePlots(newHeight, positiveNbItems, sectionKey)
10799
},
108100
[dispatch, changeSize, sectionKey]
109101
)
110102

111-
const toggleSection = () =>
112-
sendMessage({
113-
payload: {
114-
[sectionKey]: !sectionCollapsed
115-
},
116-
type: MessageFromWebviewType.TOGGLE_PLOTS_SECTION
117-
})
103+
const toggleSection = () => togglePlotsSection(sectionKey, sectionCollapsed)
118104

119105
const plotHeights = Object.values(PlotHeight).filter(
120106
value => typeof value !== 'string'

webview/src/plots/components/ZoomablePlot.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import VegaLite, { VegaLiteProps } from 'react-vega/lib/VegaLite'
77
import { setZoomedInPlot } from './webviewSlice'
88
import styles from './styles.module.scss'
99
import { config } from './constants'
10-
import { zoomPlot } from './messages'
10+
import { zoomPlot } from '../util/messages'
1111
import { useGetPlot } from '../hooks/useGetPlot'
1212
import { GripIcon } from '../../shared/components/dragDrop/GripIcon'
1313

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import { ComparisonPlots, Revision } from 'dvc/src/plots/webview/contract'
22
import { reorderObjectList } from 'dvc/src/util/array'
33
import React, { useState, useRef, useEffect, useCallback } from 'react'
44
import { useSelector } from 'react-redux'
5-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
65
import {
76
ComparisonTableColumn,
87
ComparisonTableHead
98
} from './ComparisonTableHead'
109
import { ComparisionTableRows } from './ComparisonTableRows'
1110
import plotsStyles from '../styles.module.scss'
1211
import { withScale, withVariant } from '../../../util/styles'
13-
import { sendMessage } from '../../../shared/vscode'
1412
import { PlotsState } from '../../store'
1513
import { EmptyState } from '../../../shared/components/emptyState/EmptyState'
14+
import { reorderComparisonPlots } from '../../util/messages'
1615

1716
export const ComparisonTable: React.FC = () => {
1817
const { revisions, plots, width } = useSelector(
@@ -61,10 +60,7 @@ export const ComparisonTable: React.FC = () => {
6160
const setColumnsOrder = (order: string[]) => {
6261
const newOrder = reorderObjectList<Revision>(order, columns, 'id')
6362
setColumns(newOrder)
64-
sendMessage({
65-
payload: newOrder.map(({ id }) => id),
66-
type: MessageFromWebviewType.REORDER_PLOTS_COMPARISON
67-
})
63+
reorderComparisonPlots(newOrder)
6864
}
6965

7066
const changePinnedColumn = (column: string) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { ComparisonPlot } from 'dvc/src/plots/webview/contract'
33
import styles from './styles.module.scss'
44
import { RefreshButton } from '../../../shared/components/button/RefreshButton'
5-
import { refreshRevisions, zoomPlot } from '../messages'
5+
import { refreshRevisions, zoomPlot } from '../../util/messages'
66
import { ErrorIcon } from '../../../shared/components/errorIcon/ErrorIcon'
77

88
type ComparisonTableCellProps = {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { ComparisonPlots } from 'dvc/src/plots/webview/contract'
2-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
32
import React, { createRef, useEffect, useState } from 'react'
43
import { useDispatch } from 'react-redux'
54
import { ComparisonTableColumn } from './ComparisonTableHead'
65
import { ComparisonTableRow } from './ComparisonTableRow'
76
import { changeRowHeight, DEFAULT_ROW_HEIGHT } from './comparisonTableSlice'
87
import { RowDropTarget } from './RowDropTarget'
98
import { DragDropContainer } from '../../../shared/components/dragDrop/DragDropContainer'
10-
import { sendMessage } from '../../../shared/vscode'
9+
import { reorderComparisonRows } from '../../util/messages'
1110

1211
interface ComparisonTableRowsProps {
1312
plots: ComparisonPlots
@@ -57,10 +56,7 @@ export const ComparisionTableRows: React.FC<ComparisonTableRowsProps> = ({
5756

5857
const changeRowsOrder = (order: string[]) => {
5958
setRowsOrder(order)
60-
sendMessage({
61-
payload: order,
62-
type: MessageFromWebviewType.REORDER_PLOTS_COMPARISON_ROWS
63-
})
59+
reorderComparisonRows(order)
6460
}
6561

6662
return (

webview/src/plots/components/customPlots/CustomPlots.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { DragEvent, useEffect, useState } from 'react'
22
import { useSelector } from 'react-redux'
33
import cx from 'classnames'
4-
import { MessageFromWebviewType } from 'dvc/src/webview/contract'
54
import { CustomPlot } from './CustomPlot'
65
import styles from '../styles.module.scss'
76
import { EmptyState } from '../../../shared/components/emptyState/EmptyState'
@@ -13,9 +12,9 @@ import { DropTarget } from '../DropTarget'
1312
import { VirtualizedGrid } from '../../../shared/components/virtualizedGrid/VirtualizedGrid'
1413
import { shouldUseVirtualizedGrid } from '../util'
1514
import { PlotsState } from '../../store'
16-
import { sendMessage } from '../../../shared/vscode'
1715
import { changeOrderWithDraggedInfo } from '../../../util/array'
1816
import { LoadingSection, sectionIsLoading } from '../LoadingSection'
17+
import { reorderCustomPlots } from '../../util/messages'
1918

2019
interface CustomPlotsProps {
2120
plotsIds: string[]
@@ -40,10 +39,7 @@ export const CustomPlots: React.FC<CustomPlotsProps> = ({ plotsIds }) => {
4039

4140
const setPlotsIdsOrder = (order: string[]): void => {
4241
setOrder(order)
43-
sendMessage({
44-
payload: order,
45-
type: MessageFromWebviewType.REORDER_PLOTS_CUSTOM
46-
})
42+
reorderCustomPlots(order)
4743
}
4844

4945
if (sectionIsLoading(selectedRevisions, hasItems)) {

0 commit comments

Comments
 (0)