@@ -7,13 +7,23 @@ import * as HarFormat from 'har-format';
77
88import { RawHeaders } from '../../types' ;
99
10+ import { AccountStore } from '../../model/account/account-store' ;
1011import { RulesStore } from '../../model/rules/rules-store' ;
1112import { UiStore } from '../../model/ui/ui-store' ;
1213import { RequestInput } from '../../model/send/send-request-model' ;
1314import { EditableContentType } from '../../model/events/content-types' ;
15+ import { ContextMenuItem } from '../../model/ui/context-menu' ;
16+ import {
17+ generateCodeSnippetFromRequestInput ,
18+ getCodeSnippetFormatKey ,
19+ getCodeSnippetFormatName ,
20+ getCodeSnippetOptionFromKey ,
21+ snippetExportOptions ,
22+ SnippetOption
23+ } from '../../model/ui/export' ;
1424
1525import { ContainerSizedEditor } from '../editor/base-editor' ;
16- import { useHotkeys } from '../../util/ui' ;
26+ import { useHotkeys , copyToClipboard } from '../../util/ui' ;
1727
1828import { SendCardContainer } from './send-card-section' ;
1929import { SendRequestLine } from './send-request-line' ;
@@ -49,10 +59,12 @@ const RequestPaneKeyboardShortcuts = (props: {
4959
5060@inject ( 'rulesStore' )
5161@inject ( 'uiStore' )
62+ @inject ( 'accountStore' )
5263@observer
5364export class RequestPane extends React . Component < {
5465 rulesStore ?: RulesStore ,
5566 uiStore ?: UiStore ,
67+ accountStore ?: AccountStore ,
5668
5769 editorNode : portals . HtmlPortalNode < typeof ContainerSizedEditor > ,
5870
@@ -106,6 +118,7 @@ export class RequestPane extends React.Component<{
106118 isSending = { isSending }
107119 sendRequest = { sendRequest }
108120 updateFromHar = { this . props . updateFromHar }
121+ showCopyAsSnippetMenu = { this . showCopyAsSnippetMenu }
109122 />
110123 < SendRequestHeadersCard
111124 { ...this . cardProps . requestHeaders }
@@ -152,4 +165,57 @@ export class RequestPane extends React.Component<{
152165 requestInput . rawBody . updateDecodedBody ( input ) ;
153166 }
154167
168+ private copyRequestAsSnippet = async ( snippetOption : SnippetOption ) => {
169+ const { requestInput } = this . props ;
170+
171+ try {
172+ const snippet = generateCodeSnippetFromRequestInput ( requestInput , snippetOption ) ;
173+ await copyToClipboard ( snippet ) ;
174+ } catch ( e : any ) {
175+ console . log ( e ) ;
176+ alert ( `Could not copy this request as a code snippet:\n\n${ e . message || e } ` ) ;
177+ }
178+ } ;
179+
180+ private showCopyAsSnippetMenu = ( event : React . MouseEvent ) => {
181+ const uiStore = this . props . uiStore ! ;
182+ const isPaidUser = this . props . accountStore ! . user . isPaidUser ( ) ;
183+
184+ const preferredFormat = uiStore . exportSnippetFormat
185+ ? getCodeSnippetOptionFromKey ( uiStore . exportSnippetFormat )
186+ : undefined ;
187+
188+ const menuItems : Array < ContextMenuItem < void > > = [
189+ ...( ! isPaidUser ? [
190+ { type : 'option' , label : 'With Pro:' , enabled : false , callback : ( ) => { } }
191+ ] as const : [ ] ) ,
192+ // If you have a preferred default format, we show that option at the top level:
193+ ...( preferredFormat && isPaidUser ? [ {
194+ type : 'option' as const ,
195+ label : `Copy as ${ getCodeSnippetFormatName ( preferredFormat ) } Snippet` ,
196+ callback : ( ) => this . copyRequestAsSnippet ( preferredFormat )
197+ } ] : [ ] ) ,
198+ {
199+ type : 'submenu' ,
200+ enabled : isPaidUser ,
201+ label : `Copy as Code Snippet` ,
202+ items : Object . keys ( snippetExportOptions ) . map ( ( snippetGroupName ) => ( {
203+ type : 'submenu' as const ,
204+ label : snippetGroupName ,
205+ items : snippetExportOptions [ snippetGroupName ] . map ( ( snippetOption ) => ( {
206+ type : 'option' as const ,
207+ label : getCodeSnippetFormatName ( snippetOption ) ,
208+ callback : action ( ( ) => {
209+ // When you pick an option here, it updates your preferred default option
210+ uiStore . exportSnippetFormat = getCodeSnippetFormatKey ( snippetOption ) ;
211+ this . copyRequestAsSnippet ( snippetOption ) ;
212+ } )
213+ } ) )
214+ } ) )
215+ }
216+ ] ;
217+
218+ uiStore . handleContextMenuEvent ( event , menuItems ) ;
219+ } ;
220+
155221}
0 commit comments