• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

electron / fiddle / ca977947-d226-4d89-8edc-6f8573fedf8b

24 Aug 2023 10:18PM UTC coverage: 88.325% (-2.8%) from 91.157%
ca977947-d226-4d89-8edc-6f8573fedf8b

push

circleci

web-flow
ci: re-enable Coveralls (#1301)

967 of 1183 branches covered (0.0%)

Branch coverage included in aggregate %.

3648 of 4042 relevant lines covered (90.25%)

32.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.22
/src/renderer/components/dialog-add-version.tsx
1
import * as React from 'react';
2✔
2

3
import {
2✔
4
  Button,
5
  Callout,
6
  Dialog,
7
  FileInput,
8
  InputGroup,
9
  Intent,
10
} from '@blueprintjs/core';
11
import { observer } from 'mobx-react';
2✔
12
import * as semver from 'semver';
2✔
13

14
import { Version } from '../../interfaces';
15
import { AppState } from '../state';
16
import { getElectronNameForPlatform } from '../utils/electron-name';
2✔
17
import { getLocalVersionForPath } from '../versions';
2✔
18

19
interface AddVersionDialogProps {
20
  appState: AppState;
21
}
22

23
interface AddVersionDialogState {
24
  isValidElectron: boolean;
25
  isValidVersion: boolean;
26
  existingLocalVersion?: Version;
27
  folderPath?: string;
28
  localName?: string;
29
  version: string;
30
}
31

32
/**
33
 * The "add version" dialog allows users to add custom builds of Electron.
34
 *
35
 * @class AddVersionDialog
36
 * @extends {React.Component<AddVersionDialogProps, AddVersionDialogState>}
37
 */
38
export const AddVersionDialog = observer(
2✔
39
  class AddVersionDialog extends React.Component<
40
    AddVersionDialogProps,
41
    AddVersionDialogState
42
  > {
43
    constructor(props: AddVersionDialogProps) {
44
      super(props);
8✔
45

46
      this.state = {
8✔
47
        isValidVersion: false,
48
        isValidElectron: false,
49
        version: '',
50
      };
51

52
      this.onSubmit = this.onSubmit.bind(this);
8✔
53
      this.onClose = this.onClose.bind(this);
8✔
54
      this.onChangeVersion = this.onChangeVersion.bind(this);
8✔
55
    }
56

57
    /**
58
     * Show dialog to select a local version and update state
59
     */
60
    public async selectLocalVersion(): Promise<void> {
61
      const selected = await window.ElectronFiddle.selectLocalVersion();
2✔
62
      if (selected) {
2✔
63
        const { folderPath, isValidElectron, localName } = selected;
1✔
64
        const existingLocalVersion = getLocalVersionForPath(folderPath);
1✔
65

66
        this.setState({
1✔
67
          existingLocalVersion,
68
          folderPath,
69
          isValidElectron,
70
          localName,
71
        });
72
      }
73
    }
74

75
    /**
76
     * Handles a change of the file input
77
     *
78
     * @param {React.ChangeEvent<HTMLInputElement>} event
79
     */
80
    public onChangeVersion(event: React.ChangeEvent<HTMLInputElement>) {
81
      const version = event.target.value || '';
3✔
82
      const isValidVersion = !!semver.valid(version);
3✔
83

84
      this.setState({
3✔
85
        version,
86
        isValidVersion,
87
      });
88
    }
89

90
    /**
91
     * Handles the submission of the dialog
92
     *
93
     * @returns {Promise<void>}
94
     */
95
    public async onSubmit(): Promise<void> {
96
      const {
97
        folderPath,
98
        version,
99
        isValidElectron,
100
        existingLocalVersion,
101
        localName,
102
      } = this.state;
2✔
103

104
      if (!folderPath) return;
2✔
105

106
      const toAdd: Version = {
1✔
107
        localPath: folderPath,
108
        version,
109
        name: localName,
110
      };
111

112
      // swap to old local electron version if the user adds a new one with the same path
113
      if (isValidElectron && existingLocalVersion?.localPath) {
1!
114
        // set previous version as active version
115
        this.props.appState.setVersion(existingLocalVersion.version);
×
116
      } else {
117
        this.props.appState.addLocalVersion(toAdd);
1✔
118
      }
119
      this.onClose();
1✔
120
    }
121

122
    /**
123
     * Closes the dialog
124
     */
125
    public onClose() {
126
      this.props.appState.isAddVersionDialogShowing = false;
1✔
127
      this.reset();
1✔
128
    }
129

130
    get buttons() {
131
      const { isValidElectron, isValidVersion, existingLocalVersion } =
132
        this.state;
18✔
133
      const canAdd = isValidElectron && isValidVersion && !existingLocalVersion;
18✔
134
      const canSwitch = isValidElectron && existingLocalVersion;
18✔
135

136
      return [
18✔
137
        <Button
138
          icon="add"
139
          key="submit"
140
          disabled={!canAdd && !canSwitch}
35✔
141
          onClick={this.onSubmit}
142
          text={canSwitch ? 'Switch' : 'Add'}
18✔
143
        />,
144
        <Button
145
          icon="cross"
146
          key="cancel"
147
          onClick={this.onClose}
148
          text="Cancel"
149
        />,
150
      ];
151
    }
152

153
    public render() {
154
      const { isAddVersionDialogShowing } = this.props.appState;
18✔
155
      const inputProps = {
18✔
156
        onClick: async (e: React.MouseEvent<HTMLInputElement, MouseEvent>) => {
157
          e.preventDefault();
1✔
158
          await this.selectLocalVersion();
1✔
159
        },
160
      };
161
      const { folderPath } = this.state;
18✔
162

163
      const text =
164
        folderPath ||
18✔
165
        `Select the folder containing ${getElectronNameForPlatform()}...`;
166

167
      return (
18✔
168
        <Dialog
169
          isOpen={isAddVersionDialogShowing}
170
          onClose={this.onClose}
171
          title="Add local Electron build"
172
          className="dialog-add-version"
173
        >
174
          <div className="bp3-dialog-body">
175
            <FileInput
176
              id="custom-electron-version"
177
              inputProps={inputProps}
178
              text={text}
179
            />
180
            <br />
181
            {this.renderPath()}
182
          </div>
183
          <div className="bp3-dialog-footer">
184
            <div className="bp3-dialog-footer-actions">{this.buttons}</div>
185
          </div>
186
        </Dialog>
187
      );
188
    }
189

190
    private renderPath(): JSX.Element | null {
191
      const { isValidElectron, folderPath, existingLocalVersion } = this.state;
18✔
192
      const canSwitch = isValidElectron && existingLocalVersion;
18✔
193

194
      if (!folderPath) return null;
18✔
195
      return (
6✔
196
        <Callout>
197
          {this.buildDialogText()}
198
          {!canSwitch && this.renderVersionInput()}
10✔
199
        </Callout>
200
      );
201
    }
202

203
    private buildDialogText(): string {
204
      const { isValidElectron, existingLocalVersion } = this.state;
6✔
205
      const canSwitch = isValidElectron && existingLocalVersion;
6✔
206

207
      if (canSwitch)
6✔
208
        return `This folder is already in use as version "${
2✔
209
          existingLocalVersion!.version
210
        }". Would you like to switch to that version now?`;
211

212
      if (isValidElectron)
4✔
213
        return `We found an ${getElectronNameForPlatform()} in this folder.`;
3✔
214

215
      return `We did not find a ${getElectronNameForPlatform()} in this folder...`;
1✔
216
    }
217

218
    private renderVersionInput(): JSX.Element | null {
219
      const { isValidElectron, isValidVersion, version } = this.state;
4✔
220
      if (!isValidElectron) return null;
4✔
221

222
      return (
3✔
223
        <>
224
          <p>
225
            Please specify a version, used for typings and the name. Must be{' '}
226
            <code>semver</code> compliant.
227
          </p>
228
          <InputGroup
229
            intent={isValidVersion ? undefined : Intent.DANGER}
3✔
230
            value={version}
231
            onChange={this.onChangeVersion}
232
            placeholder="4.0.0"
233
          />
234
        </>
235
      );
236
    }
237

238
    /**
239
     * Reset this component's state
240
     */
241
    private reset(): void {
242
      this.setState({
1✔
243
        isValidElectron: false,
244
        isValidVersion: false,
245
        version: '',
246
        folderPath: undefined,
247
        localName: undefined,
248
      });
249
    }
250
  },
251
);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc