-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathview-context-menu-builder.ts
More file actions
171 lines (154 loc) · 7 KB
/
view-context-menu-builder.ts
File metadata and controls
171 lines (154 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as _ from 'lodash';
import { runInAction } from 'mobx';
import { CollectedEvent, HttpExchangeView, WebSocketStream } from '../../types';
import { copyToClipboard } from '../../util/ui';
import { AccountStore } from '../../model/account/account-store';
import { UiStore } from '../../model/ui/ui-store';
import { HttpExchange } from '../../model/http/http-exchange';
import {
exportHar,
generateCodeSnippet,
getCodeSnippetFormatKey,
getCodeSnippetFormatName,
getCodeSnippetOptionFromKey,
snippetExportOptions
} from '../../model/ui/export';
import { ContextMenuItem } from '../../model/ui/context-menu';
export class ViewEventContextMenuBuilder {
constructor(
private accountStore: AccountStore,
private uiStore: UiStore,
private onPin: (event: CollectedEvent) => void,
private onDelete: (event: CollectedEvent) => void,
private onBuildRuleFromExchange: (exchange: HttpExchangeView) => void,
private onPrepareToResendRequest?: (exchange: HttpExchangeView) => void,
private onHeaderColumnOptionChange?: (visibleViewColumns: Map<string, boolean>, columnName: string, show: boolean) => void,
) {
}
private readonly BaseOptions = {
Pin: {
type: 'option',
label: 'Toggle Pinning',
callback: this.onPin
},
Delete: {
type: 'option',
label: 'Delete',
callback: this.onDelete
}
} as const;
getContextMenuCallback(event: CollectedEvent) {
return (mouseEvent: React.MouseEvent) => {
const { isPaidUser } = this.accountStore;
const preferredExportFormat = this.uiStore.exportSnippetFormat
? getCodeSnippetOptionFromKey(this.uiStore.exportSnippetFormat)
: undefined;
if (event.isHttp()) {
const menuOptions = [
this.BaseOptions.Pin,
{
type: 'option',
label: 'Copy Request URL',
callback: (data: HttpExchangeView) => copyToClipboard(data.request.url)
},
...(!isPaidUser ? [
{ type: 'separator' },
{ type: 'option', label: 'With Pro:', enabled: false, callback: () => {} }
] as const : []),
...(this.onPrepareToResendRequest ? [
{
type: 'option',
enabled: isPaidUser,
label: 'Resend Request',
callback: (data: HttpExchangeView) => this.onPrepareToResendRequest!(data)
}
] as const : []),
{
type: 'option',
enabled: isPaidUser,
label: `Create Matching Modify Rule`,
callback: this.onBuildRuleFromExchange
},
{
type: 'option',
enabled: isPaidUser,
label: `Export Exchange as HAR`,
callback: exportHar
},
// If you have a preferred default format, we show that option at the top level:
...(preferredExportFormat && isPaidUser ? [{
type: 'option',
label: `Copy as ${getCodeSnippetFormatName(preferredExportFormat)} Snippet`,
callback: async (data: HttpExchange) => {
copyToClipboard(
await generateCodeSnippet(data, preferredExportFormat, {
waitForBodyDecoding: true
})
);
}
}] as const : []),
{
type: 'submenu',
enabled: isPaidUser,
label: `Copy as Code Snippet`,
items: Object.keys(snippetExportOptions).map((snippetGroupName) => ({
type: 'submenu' as const,
label: snippetGroupName,
items: snippetExportOptions[snippetGroupName].map((snippetOption) => ({
type: 'option' as const,
label: getCodeSnippetFormatName(snippetOption),
callback: async (data: HttpExchange) => {
// When you pick an option here, it updates your preferred default option
runInAction(() => {
this.uiStore.exportSnippetFormat = getCodeSnippetFormatKey(snippetOption);
});
copyToClipboard(
await generateCodeSnippet(data, snippetOption, {
waitForBodyDecoding: true
})
);
}
}))
}))
},
this.BaseOptions.Delete
];
const sortedOptions = _.sortBy(menuOptions, (o: ContextMenuItem<any>) =>
o.type === 'separator' || !(o.enabled ?? true)
) as Array<ContextMenuItem<HttpExchange | WebSocketStream>>;
this.uiStore.handleContextMenuEvent(
mouseEvent,
sortedOptions,
event
)
} else {
// For non-HTTP events, we just show the super-basic globally supported options:
this.uiStore.handleContextMenuEvent(mouseEvent, [
this.BaseOptions.Pin,
this.BaseOptions.Delete
], event);
}
};
}
getHeaderToggleContextMenu(enabledColumns: Map<string, boolean>) {
let menuOptions: ContextMenuItem<void>[] = [];
enabledColumns.forEach((enabled, columnName) => {
menuOptions.push({
type: 'option',
label: (!enabled ? "Show " : "Hide ") + columnName,
callback: () => {
this.onHeaderColumnOptionChange ? this.onHeaderColumnOptionChange(enabledColumns, columnName, !enabled) : console.log('onHeaderColumnOptionChange callback not set');
}
});
});
return (mouseEvent: React.MouseEvent) => {
const sortedOptions = _.sortBy(menuOptions, (o: ContextMenuItem<void>) =>
o.type === 'separator' || !(o.enabled ?? true)
) as Array<ContextMenuItem<void>>;
this.uiStore.handleContextMenuEvent(
mouseEvent,
sortedOptions
)
};
}
}