Skip to content

Commit 65e19db

Browse files
authored
Add button which resets the number of commits to show for a branch to experiments table (#4355)
1 parent 8620e6f commit 65e19db

File tree

12 files changed

+145
-23
lines changed

12 files changed

+145
-23
lines changed

extension/src/experiments/model/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ export class ExperimentsModel extends ModelWithPersistence {
478478
this.persistNbOfCommitsToShow()
479479
}
480480

481+
public resetNbfCommitsToShow(branch: string) {
482+
delete this.numberOfCommitsToShow[branch]
483+
this.persistNbOfCommitsToShow()
484+
}
485+
481486
public getNbOfCommitsToShow(branch: string) {
482487
return (
483488
this.numberOfCommitsToShow[branch] ||

extension/src/experiments/webview/messages.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ export class WebviewMessages {
190190

191191
case MessageFromWebviewType.SHOW_MORE_COMMITS:
192192
return this.changeCommitsToShow(1, message.payload)
193-
194193
case MessageFromWebviewType.SHOW_LESS_COMMITS:
195194
return this.changeCommitsToShow(-1, message.payload)
195+
case MessageFromWebviewType.RESET_COMMITS:
196+
return this.resetCommitsToShow(message.payload)
197+
196198
case MessageFromWebviewType.SELECT_BRANCHES:
197199
return this.addAndRemoveBranches()
198200

@@ -221,19 +223,29 @@ export class WebviewMessages {
221223
}
222224

223225
private async changeCommitsToShow(change: 1 | -1, branch: string) {
226+
this.experiments.setNbfCommitsToShow(
227+
this.experiments.getNbOfCommitsToShow(branch) +
228+
NUM_OF_COMMITS_TO_INCREASE * change,
229+
branch
230+
)
231+
await this.update()
224232
sendTelemetryEvent(
225233
change === 1
226234
? EventName.VIEWS_EXPERIMENTS_TABLE_SHOW_MORE_COMMITS
227235
: EventName.VIEWS_EXPERIMENTS_TABLE_SHOW_LESS_COMMITS,
228236
undefined,
229237
undefined
230238
)
231-
this.experiments.setNbfCommitsToShow(
232-
this.experiments.getNbOfCommitsToShow(branch) +
233-
NUM_OF_COMMITS_TO_INCREASE * change,
234-
branch
235-
)
239+
}
240+
241+
private async resetCommitsToShow(branch: string) {
242+
this.experiments.resetNbfCommitsToShow(branch)
236243
await this.update()
244+
sendTelemetryEvent(
245+
EventName.VIEWS_EXPERIMENTS_TABLE_RESET_COMMITS,
246+
undefined,
247+
undefined
248+
)
237249
}
238250

239251
private refreshData() {

extension/src/telemetry/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export const EventName = Object.assign(
5151
VIEWS_EXPERIMENTS_TABLE_REFRESH: 'views.experimentsTable.refresh',
5252
VIEWS_EXPERIMENTS_TABLE_REMOVE_COLUMN_SORT:
5353
'views.experimentsTable.columnSortRemoved',
54+
VIEWS_EXPERIMENTS_TABLE_RESET_COMMITS:
55+
'views.experimentsTable.resetCommits',
5456
VIEWS_EXPERIMENTS_TABLE_RESIZE_COLUMN:
5557
'views.experimentsTable.columnResized',
5658
VIEWS_EXPERIMENTS_TABLE_SELECT_BRANCHES:
@@ -246,6 +248,7 @@ export interface IEventNamePropertyMapping {
246248
[EventName.VIEWS_EXPERIMENTS_TABLE_REMOVE_COLUMN_SORT]: {
247249
path: string
248250
}
251+
[EventName.VIEWS_EXPERIMENTS_TABLE_RESET_COMMITS]: undefined
249252
[EventName.VIEWS_EXPERIMENTS_TABLE_CREATED]: undefined
250253
[EventName.VIEWS_EXPERIMENTS_TABLE_FOCUS_CHANGED]: WebviewFocusChangedProperties
251254
[EventName.VIEWS_EXPERIMENTS_TABLE_HIDE_COLUMN_PATH]: {

extension/src/test/suite/experiments/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,39 @@ suite('Experiments Test Suite', () => {
14801480
expect(setNbfCommitsToShowSpy).to.be.calledWith(3, 'main')
14811481
}).timeout(WEBVIEW_TEST_TIMEOUT)
14821482

1483+
it('should handle a message to reset the number of commits shown for a branch', async () => {
1484+
const {
1485+
experiments,
1486+
experimentsModel,
1487+
messageSpy,
1488+
mockUpdateExperimentsData
1489+
} = setupExperimentsAndMockCommands()
1490+
1491+
experimentsModel.setNbfCommitsToShow(100, 'main')
1492+
const getNumberOfCommitsToShowForMain = () =>
1493+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1494+
(experimentsModel as any).numberOfCommitsToShow.main
1495+
expect(getNumberOfCommitsToShowForMain()).to.equal(100)
1496+
1497+
const resetNbfCommitsToShowSpy = spy(
1498+
experimentsModel,
1499+
'resetNbfCommitsToShow'
1500+
)
1501+
1502+
const webview = await experiments.showWebview()
1503+
messageSpy.resetHistory()
1504+
const mockMessageReceived = getMessageReceivedEmitter(webview)
1505+
1506+
mockMessageReceived.fire({
1507+
payload: 'main',
1508+
type: MessageFromWebviewType.RESET_COMMITS
1509+
})
1510+
1511+
expect(mockUpdateExperimentsData).to.be.calledOnce
1512+
expect(resetNbfCommitsToShowSpy).to.be.calledWithExactly('main')
1513+
expect(getNumberOfCommitsToShowForMain()).to.be.undefined
1514+
}).timeout(WEBVIEW_TEST_TIMEOUT)
1515+
14831516
it('should handle a message to select branches', async () => {
14841517
const {
14851518
experiments,

extension/src/webview/contract.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export enum MessageFromWebviewType {
3838
REORDER_PLOTS_TEMPLATES = 'reorder-plots-templates',
3939
REFRESH_EXP_DATA = 'refresh-exp-data',
4040
REFRESH_REVISIONS = 'refresh-revisions',
41+
RESET_COMMITS = 'reset-commits',
4142
RESIZE_COLUMN = 'resize-column',
4243
RESIZE_PLOTS = 'resize-plots',
4344
SAVE_STUDIO_TOKEN = 'save-studio-token',
@@ -119,6 +120,10 @@ export type MessageFromWebview =
119120
type: MessageFromWebviewType.REORDER_COLUMNS
120121
payload: string[]
121122
}
123+
| {
124+
type: MessageFromWebviewType.RESET_COMMITS
125+
payload: string
126+
}
122127
| {
123128
type: MessageFromWebviewType.RESIZE_COLUMN
124129
payload: ColumnResizePayload

webview/icons/codicons.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const codicons = [
1010
'cloud-upload',
1111
'cloud',
1212
'copy',
13+
'discard',
1314
'ellipsis',
1415
'error',
1516
'extensions',

webview/src/experiments/components/App.test.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ describe('App', () => {
18681868
})
18691869
})
18701870

1871-
describe('Show more commits', () => {
1871+
describe('Change number of commits', () => {
18721872
it('should display a show more commits button', () => {
18731873
renderTable({ ...tableDataFixture, hasMoreCommits: { main: true } })
18741874

@@ -1933,6 +1933,25 @@ describe('App', () => {
19331933
type: MessageFromWebviewType.SHOW_LESS_COMMITS
19341934
})
19351935
})
1936+
1937+
it('should display a reset commits button', () => {
1938+
renderTable({ ...tableDataFixture, hasMoreCommits: { main: true } })
1939+
1940+
expect(
1941+
screen.getByLabelText('Reset Commits to Default')
1942+
).toBeInTheDocument()
1943+
})
1944+
1945+
it('should send a message to reset commits when the reset commits button is clicked', () => {
1946+
renderTable()
1947+
1948+
fireEvent.click(screen.getByLabelText('Reset Commits to Default'))
1949+
1950+
expect(mockPostMessage).toHaveBeenCalledWith({
1951+
payload: 'main',
1952+
type: MessageFromWebviewType.RESET_COMMITS
1953+
})
1954+
})
19361955
})
19371956

19381957
describe('Add / Remove branches', () => {

webview/src/experiments/components/table/body/commits/CommitsButton.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ import styles from './styles.module.scss'
33
import { Icon, IconValue } from '../../../../../shared/components/Icon'
44
import Tooltip from '../../../../../shared/components/tooltip/Tooltip'
55
export interface CommitsButtonProps {
6-
icon: IconValue
7-
moreOrLess: 'More' | 'Less'
86
action: () => void
97
disabled: boolean
8+
icon: IconValue
9+
tooltipContent: string
1010
}
1111

1212
export const CommitsButton: React.FC<CommitsButtonProps> = ({
13-
icon,
14-
moreOrLess,
1513
action,
16-
disabled
14+
disabled,
15+
icon,
16+
tooltipContent
1717
}) => {
18-
const text = `Show ${moreOrLess} Commits`
1918
return (
20-
<Tooltip content={<>{text}</>}>
19+
<Tooltip content={tooltipContent}>
2120
<button
2221
className={styles.commitsButton}
2322
onClick={action}
2423
disabled={disabled}
25-
aria-label={text}
24+
aria-label={tooltipContent}
2625
>
2726
<Icon icon={icon} className={styles.commitsIcon} />
2827
</button>

webview/src/experiments/components/table/body/commits/CommitsNavigation.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import React from 'react'
22
import { useSelector } from 'react-redux'
33
import styles from './styles.module.scss'
44
import { CommitsButton, CommitsButtonProps } from './CommitsButton'
5-
import { showLessCommits, showMoreCommits } from '../../../../util/messages'
5+
import {
6+
resetCommits,
7+
showLessCommits,
8+
showMoreCommits
9+
} from '../../../../util/messages'
610
import { ExperimentsState } from '../../../../store'
7-
import { Add, Remove } from '../../../../../shared/components/icons'
11+
import { Add, Discard, Remove } from '../../../../../shared/components/icons'
812
interface CommitsNavigationProps {
913
branch: string
1014
}
@@ -16,26 +20,41 @@ export const CommitsNavigation: React.FC<CommitsNavigationProps> = ({
1620
(state: ExperimentsState) => state.tableData
1721
)
1822

19-
const commitsButtons: CommitsButtonProps[] = [
23+
const getMoreOrLessValues = (
24+
moreOrLess: 'More' | 'Less'
25+
): { key: string; tooltipContent: string } => ({
26+
key: moreOrLess,
27+
tooltipContent: `Show ${moreOrLess} Commits`
28+
})
29+
30+
const commitsButtons: (CommitsButtonProps & { key: string })[] = [
2031
{
2132
action: () => showMoreCommits(branch),
2233
disabled: !hasMoreCommits[branch],
2334
icon: Add,
24-
moreOrLess: 'More'
35+
...getMoreOrLessValues('More')
2536
},
2637
{
2738
action: () => showLessCommits(branch),
2839
disabled: !isShowingMoreCommits[branch],
2940
icon: Remove,
30-
moreOrLess: 'Less'
41+
...getMoreOrLessValues('Less')
42+
},
43+
{
44+
action: () => resetCommits(branch),
45+
disabled: false,
46+
icon: Discard,
47+
key: 'Reset',
48+
tooltipContent: 'Reset Commits to Default'
3149
}
3250
]
3351

3452
return (
3553
<div className={styles.commitsNav}>
36-
{commitsButtons.map(commitButton => (
37-
<CommitsButton key={commitButton.moreOrLess} {...commitButton} />
38-
))}
54+
{commitsButtons.map(commitButton => {
55+
const { key, ...commitButtonProps } = commitButton
56+
return <CommitsButton key={key} {...commitButtonProps} />
57+
})}
3958
</div>
4059
)
4160
}

webview/src/experiments/util/messages.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ export const reorderColumns = (newOrder: string[]) =>
2727
type: MessageFromWebviewType.REORDER_COLUMNS
2828
})
2929

30+
export const resetCommits = (branch: string) =>
31+
sendMessage({
32+
payload: branch,
33+
type: MessageFromWebviewType.RESET_COMMITS
34+
})
35+
3036
export const resizeColumn = (id: string, width: number) =>
3137
sendMessage({
3238
payload: { id, width },

0 commit comments

Comments
 (0)