Skip to content

Commit 7390615

Browse files
authored
Create Modal class (mirror Toast) (#3294)
1 parent d8ae4a0 commit 7390615

File tree

7 files changed

+59
-38
lines changed

7 files changed

+59
-38
lines changed

extension/src/cli/dvc/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Args, Flag } from './constants'
22
import { Prompt } from './output'
33
import { MaybeConsoleError } from '../error'
4-
import { warnOfConsequences } from '../../vscode/modal'
4+
import { Modal } from '../../vscode/modal'
55
import { CommandId, InternalCommands } from '../../commands/internal'
66
import { Response } from '../../vscode/response'
77

@@ -15,7 +15,7 @@ const offerToForce = async (
1515
Prompt.TRY_FORCE,
1616
'\n\nWould you like to force this action?'
1717
)
18-
const response = await warnOfConsequences(text, Response.FORCE)
18+
const response = await Modal.warnOfConsequences(text, Response.FORCE)
1919
if (response !== Response.FORCE) {
2020
return
2121
}

extension/src/connect/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Title } from '../vscode/title'
1212
import { openUrl } from '../vscode/external'
1313
import { ContextKey, setContextValue } from '../vscode/context'
1414
import { RegisteredCommands } from '../commands/external'
15-
import { showInformation } from '../vscode/modal'
15+
import { Modal } from '../vscode/modal'
1616

1717
export class Connect extends BaseRepository<undefined> {
1818
public readonly viewKey = ViewKey.CONNECT
@@ -96,7 +96,7 @@ export class Connect extends BaseRepository<undefined> {
9696
const storedToken = await this.getStudioAccessToken()
9797
if (isStudioAccessToken(storedToken)) {
9898
if (this.deferred.state === 'resolved') {
99-
void showInformation(
99+
void Modal.showInformation(
100100
'Studio is now connected. Use the "Share to Studio" command from an experiment\'s context menu to share experiments.'
101101
)
102102
}

extension/src/repository/commands/index.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { join } from 'path'
22
import { Uri } from 'vscode'
33
import { getResourceCommand, getRootCommand, getSimpleResourceCommand } from '.'
4-
import { warnOfConsequences } from '../../vscode/modal'
4+
import { Modal } from '../../vscode/modal'
55
import { CommandId, InternalCommands } from '../../commands/internal'
66
import { OutputChannel } from '../../vscode/outputChannel'
77
import { WorkspaceRepositories } from '../workspace'
88

99
const mockedFunc = jest.fn()
10-
const mockedGetWarningResponse = jest.mocked(warnOfConsequences)
10+
const mockedModal = jest.mocked(Modal)
11+
const mockedGetWarningResponse = jest.fn()
12+
mockedModal.warnOfConsequences = mockedGetWarningResponse
1113
const mockedDvcRoot = join('some', 'path')
1214
const mockedRelPath = join('with', 'a', 'target')
1315
const mockedTarget = join(mockedDvcRoot, mockedRelPath)

extension/src/repository/commands/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
InternalCommands
88
} from '../../commands/internal'
99
import { relativeWithUri } from '../../fileSystem'
10-
import { warnOfConsequences } from '../../vscode/modal'
10+
import { Modal } from '../../vscode/modal'
1111
import { Response } from '../../vscode/response'
1212
import { WorkspaceRepositories } from '../workspace'
1313

@@ -135,7 +135,7 @@ export const getResetRootCommand =
135135
return
136136
}
137137

138-
const response = await warnOfConsequences(
138+
const response = await Modal.warnOfConsequences(
139139
'Are you sure you want to discard ALL workspace changes?\n' +
140140
'This is IRREVERSIBLE!\n' +
141141
'Your current working set will be FOREVER LOST if you proceed.',

extension/src/repository/model/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { sendViewOpenedTelemetryEvent } from '../../telemetry'
3535
import { EventName } from '../../telemetry/constants'
3636
import { getInput } from '../../vscode/inputBox'
3737
import { pickResources } from '../../vscode/resourcePicker'
38-
import { warnOfConsequences } from '../../vscode/modal'
38+
import { Modal } from '../../vscode/modal'
3939
import { Response } from '../../vscode/response'
4040
import { Title } from '../../vscode/title'
4141
import { Disposable } from '../../class/dispose'
@@ -229,7 +229,7 @@ export class RepositoriesTree
229229
async ({ resourceUri: destination }) => {
230230
const targets = await pickResources(Title.CHOOSE_RESOURCES)
231231
if (targets) {
232-
const response = await warnOfConsequences(
232+
const response = await Modal.warnOfConsequences(
233233
'Are you sure you want to move the selected data into this dataset?',
234234
Response.MOVE
235235
)

extension/src/vscode/modal.test.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { window } from 'vscode'
2-
import { warnOfConsequences } from './modal'
2+
import { Modal } from './modal'
33
import { Response } from './response'
44

55
const mockedWindow = jest.mocked(window)
@@ -14,28 +14,36 @@ beforeEach(() => {
1414
jest.resetAllMocks()
1515
})
1616

17-
describe('warnOfConsequences', () => {
18-
it('should return the text of the response provided by the user', async () => {
19-
const userSelection = Response.YES
20-
const options = [userSelection, Response.NO, Response.NEVER]
17+
describe('Modal', () => {
18+
describe('warnOfConsequences', () => {
19+
it('should return the text of the response provided by the user', async () => {
20+
const userSelection = Response.YES
21+
const options = [userSelection, Response.NO, Response.NEVER]
2122

22-
mockedShowWarningMessage.mockResolvedValueOnce(Response.YES)
23+
mockedShowWarningMessage.mockResolvedValueOnce(Response.YES)
2324

24-
const response = await warnOfConsequences('WHAT DO I DO?', ...options)
25+
const response = await Modal.warnOfConsequences(
26+
'WHAT DO I DO?',
27+
...options
28+
)
2529

26-
expect(response).toStrictEqual(userSelection)
27-
expect(mockedShowWarningMessage).toHaveBeenCalledTimes(1)
28-
})
30+
expect(response).toStrictEqual(userSelection)
31+
expect(mockedShowWarningMessage).toHaveBeenCalledTimes(1)
32+
})
2933

30-
it('should return undefined if the modal is cancelled', async () => {
31-
const modalCancelled = undefined
32-
const options = [Response.YES, Response.NO, Response.NEVER]
34+
it('should return undefined if the modal is cancelled', async () => {
35+
const modalCancelled = undefined
36+
const options = [Response.YES, Response.NO, Response.NEVER]
3337

34-
mockedShowWarningMessage.mockResolvedValueOnce(modalCancelled)
38+
mockedShowWarningMessage.mockResolvedValueOnce(modalCancelled)
3539

36-
const response = await warnOfConsequences('WHAT DO I DO?', ...options)
40+
const response = await Modal.warnOfConsequences(
41+
'WHAT DO I DO?',
42+
...options
43+
)
3744

38-
expect(response).toStrictEqual(modalCancelled)
39-
expect(mockedShowWarningMessage).toHaveBeenCalledTimes(1)
45+
expect(response).toStrictEqual(modalCancelled)
46+
expect(mockedShowWarningMessage).toHaveBeenCalledTimes(1)
47+
})
4048
})
4149
})

extension/src/vscode/modal.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { window } from 'vscode'
22
import { Response } from './response'
33

4-
export const warnOfConsequences = (
5-
text: string,
6-
...items: Response[]
7-
): Thenable<string | undefined> =>
8-
window.showWarningMessage(text, { modal: true }, ...items)
9-
10-
export const showInformation = (
11-
text: string,
12-
...items: Response[]
13-
): Thenable<string | undefined> =>
14-
window.showInformationMessage(text, { modal: true }, ...items)
4+
enum Level {
5+
INFORMATION = 'Information',
6+
WARNING = 'Warning'
7+
}
8+
9+
export class Modal {
10+
public static showInformation(text: string, ...items: Response[]) {
11+
return Modal.show(Level.INFORMATION, text, ...items)
12+
}
13+
14+
public static warnOfConsequences(text: string, ...items: Response[]) {
15+
return Modal.show(Level.WARNING, text, ...items)
16+
}
17+
18+
private static show(
19+
level: Level,
20+
message: string,
21+
...items: Response[]
22+
): Thenable<Response | undefined> {
23+
return window[`show${level}Message`](message, { modal: true }, ...items)
24+
}
25+
}

0 commit comments

Comments
 (0)