Skip to content

Commit fc3be1b

Browse files
committed
Refactor global chrome to remove never-rendered config component
1 parent b837c63 commit fc3be1b

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed
Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,42 @@
1-
import * as React from 'react';
2-
import { observer } from 'mobx-react';
3-
import { observable } from 'mobx';
41
import dedent from 'dedent';
52

63
import { Interceptor } from '../../../model/interception/interceptors';
74

8-
@observer
9-
class ExistingBrowserConfig extends React.Component<{
5+
export async function onActivateExistingBrowser(
106
interceptor: Interceptor,
117
activateInterceptor: (
128
options: { closeConfirmed?: true },
139
shouldTrackEvent?: false
1410
) => Promise<void>,
1511
reportStarted: () => void,
1612
reportSuccess: (options?: { showRequests?: boolean }) => void,
17-
closeSelf: () => void
18-
}> {
19-
20-
async componentDidMount() {
21-
const { activateInterceptor, reportStarted, reportSuccess, closeSelf } = this.props;
22-
closeSelf(); // We immediately unmount, but continue activating:
23-
24-
try {
25-
// Try to activate, assuming the browser isn't currently open:
26-
await activateInterceptor({}, false);
27-
28-
// Only it runs without confirmation does this count as an activation
29-
reportStarted();
30-
} catch (error: any) {
31-
if (!error.metadata || error.metadata.closeConfirmRequired !== true) {
32-
// This is a real error, not a confirmation requirement.
33-
34-
reportStarted(); // Track that this started, before it fails
35-
throw error;
36-
}
37-
38-
// If the browser is open, confirm that we can kill & restart it first:
39-
const confirmed = confirm(dedent`
40-
Your browser is currently open, and needs to be
41-
restarted to enable interception. Restart it now?
42-
`.replace('\n', ' '));
43-
44-
// If cancelled, we silently do nothing
45-
if (!confirmed) return;
46-
47-
reportStarted();
48-
await activateInterceptor({ closeConfirmed: true });
13+
) {
14+
try {
15+
// Try to activate, assuming the browser isn't currently open:
16+
await activateInterceptor({}, false);
17+
18+
// Only it runs without confirmation does this count as an activation
19+
reportStarted();
20+
} catch (error: any) {
21+
if (!error.metadata || error.metadata.closeConfirmRequired !== true) {
22+
// This is a real error, not a confirmation requirement.
23+
24+
reportStarted(); // Track that this started, before it fails
25+
throw error;
4926
}
5027

51-
reportSuccess();
52-
}
28+
// If the browser is open, confirm that we can kill & restart it first:
29+
const confirmed = confirm(dedent`
30+
Your browser is currently open, and needs to be
31+
restarted to enable interception. Restart it now?
32+
`.replace('\n', ' '));
5333

54-
render() {
55-
return null; // This never actually displays - we just mount, confirm, and close
56-
}
34+
// If cancelled, we silently do nothing
35+
if (!confirmed) return;
5736

58-
}
37+
reportStarted();
38+
await activateInterceptor({ closeConfirmed: true });
39+
}
5940

60-
export const ExistingBrowserCustomUi = {
61-
columnWidth: 1,
62-
rowHeight: 1,
63-
configComponent: ExistingBrowserConfig
64-
};
41+
reportSuccess();
42+
}

src/components/intercept/intercept-option.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export interface InterceptorCustomUiConfig {
5151
customPill?: React.ComponentType<{}>;
5252
}
5353

54+
// A function with the same types as a custom config component, but none of the UI:
55+
export type CustomActivationFunction = (
56+
interceptor: Interceptor,
57+
activateInterceptor: (activationOptions?: any) => Promise<any>,
58+
reportStarted: (options?: { idSuffix?: string }) => void,
59+
reportSuccess: (options?: { showRequests?: boolean, idSuffix?: string }) => void
60+
) => Promise<void>;
61+
5462
const BackgroundIcons = styled.div`
5563
z-index: 0;
5664
@@ -341,6 +349,14 @@ export class InterceptOption extends React.Component<InterceptOptionProps> {
341349
behavior: 'smooth'
342350
});
343351
});
352+
} else if (interceptor.customActivation) {
353+
onActivationStarted();
354+
interceptor.customActivation(
355+
interceptor,
356+
this.activateInterceptor,
357+
onActivationStarted,
358+
onActivationSuccessful
359+
).catch((e) => logError(e));
344360
} else {
345361
onActivationStarted();
346362
activateInterceptor(interceptor.activationOptions)

src/model/interception/interceptors.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {
99
import { IconProps, SourceIcons } from "../../icons";
1010
import { AccountStore } from "../account/account-store";
1111

12-
import { InterceptorCustomUiConfig } from "../../components/intercept/intercept-option";
12+
import { CustomActivationFunction, InterceptorCustomUiConfig } from "../../components/intercept/intercept-option";
1313
import { ManualInterceptCustomUi } from "../../components/intercept/config/manual-intercept-config";
1414
import { ExistingTerminalCustomUi } from "../../components/intercept/config/existing-terminal-config";
1515
import { ElectronCustomUi } from '../../components/intercept/config/electron-config';
1616
import { AndroidDeviceCustomUi } from "../../components/intercept/config/android-device-config";
1717
import { AndroidAdbCustomUi } from "../../components/intercept/config/android-adb-config";
1818
import { FridaCustomUi } from "../../components/intercept/config/frida-config";
19-
import { ExistingBrowserCustomUi } from "../../components/intercept/config/existing-browser-config";
19+
import { onActivateExistingBrowser } from "../../components/intercept/config/existing-browser-config";
2020
import { JvmCustomUi } from "../../components/intercept/config/jvm-config";
2121
import { DockerAttachCustomUi } from "../../components/intercept/config/docker-attach-config";
2222
import { ManualIOSCustomUi } from "../../components/intercept/config/manual-ios-config";
@@ -34,6 +34,7 @@ interface InterceptorConfig {
3434
serverVersion?: string
3535
}) => boolean;
3636
uiConfig?: InterceptorCustomUiConfig;
37+
customActivation?: CustomActivationFunction,
3738
getActivationOptions?: (options: {
3839
accountStore: AccountStore,
3940
serverVersion?: string
@@ -91,7 +92,7 @@ const INTERCEPT_OPTIONS: _.Dictionary<InterceptorConfig> = {
9192
"Intercept your main Chrome profile globally",
9293
"This captures all default Chrome traffic, so may interfere with normal usage"
9394
],
94-
uiConfig: ExistingBrowserCustomUi,
95+
customActivation: onActivateExistingBrowser,
9596
iconProps: [
9697
SourceIcons.Chrome,
9798
{ icon: ['fas', 'globe'], color: '#fafafa', size: '2x' }

0 commit comments

Comments
 (0)