@@ -7,14 +7,7 @@ import {
7
7
visit ,
8
8
} from 'graphql' ;
9
9
import { VariableToType } from 'graphql-language-service' ;
10
- import {
11
- ReactNode ,
12
- useCallback ,
13
- useEffect ,
14
- useMemo ,
15
- useRef ,
16
- useState ,
17
- } from 'react' ;
10
+ import { ReactNode , useEffect , useRef , useState } from 'react' ;
18
11
19
12
import { useStorageContext } from '../storage' ;
20
13
import { createContextHook , createNullableContext } from '../utility/context' ;
@@ -329,7 +322,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
329
322
330
323
const [ tabState , setTabState ] = useState < TabsState > ( initialState . tabState ) ;
331
324
332
- const setShouldPersistHeaders = useCallback (
325
+ const setShouldPersistHeaders = // eslint-disable-line react-hooks/exhaustive-deps -- false positive, function is optimized by react-compiler, no need to wrap with useCallback
333
326
( persist : boolean ) => {
334
327
if ( persist ) {
335
328
storage ?. set ( STORAGE_KEY_HEADERS , headerEditor ?. getValue ( ) ?? '' ) ;
@@ -341,9 +334,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
341
334
}
342
335
setShouldPersistHeadersInternal ( persist ) ;
343
336
storage ?. set ( PERSIST_HEADERS_STORAGE_KEY , persist . toString ( ) ) ;
344
- } ,
345
- [ storage , tabState , headerEditor ] ,
346
- ) ;
337
+ } ;
347
338
348
339
const lastShouldPersistHeadersProp = useRef < boolean | undefined > ( ) ;
349
340
useEffect ( ( ) => {
@@ -369,7 +360,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
369
360
defaultHeaders,
370
361
} ) ;
371
362
372
- const addTab = useCallback < EditorContextType [ 'addTab' ] > ( ( ) => {
363
+ const addTab : EditorContextType [ 'addTab' ] = ( ) => {
373
364
setTabState ( current => {
374
365
// Make sure the current tab stores the latest values
375
366
const updatedValues = synchronizeActiveTabValues ( current ) ;
@@ -388,93 +379,71 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
388
379
onTabChange ?.( updated ) ;
389
380
return updated ;
390
381
} ) ;
391
- } , [
392
- defaultHeaders ,
393
- defaultQuery ,
394
- onTabChange ,
395
- setEditorValues ,
396
- storeTabs ,
397
- synchronizeActiveTabValues ,
398
- ] ) ;
399
-
400
- const changeTab = useCallback < EditorContextType [ 'changeTab' ] > (
401
- index => {
402
- setTabState ( current => {
403
- const updated = {
404
- ...current ,
405
- activeTabIndex : index ,
406
- } ;
407
- storeTabs ( updated ) ;
408
- setEditorValues ( updated . tabs [ updated . activeTabIndex ] ) ;
409
- onTabChange ?.( updated ) ;
410
- return updated ;
411
- } ) ;
412
- } ,
413
- [ onTabChange , setEditorValues , storeTabs ] ,
414
- ) ;
382
+ } ;
415
383
416
- const moveTab = useCallback < EditorContextType [ 'moveTab' ] > (
417
- newOrder => {
418
- setTabState ( current => {
419
- const activeTab = current . tabs [ current . activeTabIndex ] ;
420
- const updated = {
421
- tabs : newOrder ,
422
- activeTabIndex : newOrder . indexOf ( activeTab ) ,
423
- } ;
424
- storeTabs ( updated ) ;
425
- setEditorValues ( updated . tabs [ updated . activeTabIndex ] ) ;
426
- onTabChange ?.( updated ) ;
427
- return updated ;
428
- } ) ;
429
- } ,
430
- [ onTabChange , setEditorValues , storeTabs ] ,
431
- ) ;
384
+ const changeTab : EditorContextType [ 'changeTab' ] = index => {
385
+ setTabState ( current => {
386
+ const updated = {
387
+ ...current ,
388
+ activeTabIndex : index ,
389
+ } ;
390
+ storeTabs ( updated ) ;
391
+ setEditorValues ( updated . tabs [ updated . activeTabIndex ] ) ;
392
+ onTabChange ?.( updated ) ;
393
+ return updated ;
394
+ } ) ;
395
+ } ;
432
396
433
- const closeTab = useCallback < EditorContextType [ 'closeTab' ] > (
434
- index => {
435
- setTabState ( current => {
436
- const updated = {
437
- tabs : current . tabs . filter ( ( _tab , i ) => index !== i ) ,
438
- activeTabIndex : Math . max ( current . activeTabIndex - 1 , 0 ) ,
439
- } ;
440
- storeTabs ( updated ) ;
441
- setEditorValues ( updated . tabs [ updated . activeTabIndex ] ) ;
442
- onTabChange ?.( updated ) ;
443
- return updated ;
444
- } ) ;
445
- } ,
446
- [ onTabChange , setEditorValues , storeTabs ] ,
447
- ) ;
397
+ const moveTab : EditorContextType [ 'moveTab' ] = newOrder => {
398
+ setTabState ( current => {
399
+ const activeTab = current . tabs [ current . activeTabIndex ] ;
400
+ const updated = {
401
+ tabs : newOrder ,
402
+ activeTabIndex : newOrder . indexOf ( activeTab ) ,
403
+ } ;
404
+ storeTabs ( updated ) ;
405
+ setEditorValues ( updated . tabs [ updated . activeTabIndex ] ) ;
406
+ onTabChange ?.( updated ) ;
407
+ return updated ;
408
+ } ) ;
409
+ } ;
410
+
411
+ const closeTab : EditorContextType [ 'closeTab' ] = index => {
412
+ setTabState ( current => {
413
+ const updated = {
414
+ tabs : current . tabs . filter ( ( _tab , i ) => index !== i ) ,
415
+ activeTabIndex : Math . max ( current . activeTabIndex - 1 , 0 ) ,
416
+ } ;
417
+ storeTabs ( updated ) ;
418
+ setEditorValues ( updated . tabs [ updated . activeTabIndex ] ) ;
419
+ onTabChange ?.( updated ) ;
420
+ return updated ;
421
+ } ) ;
422
+ } ;
448
423
449
- const updateActiveTabValues = useCallback <
450
- EditorContextType [ 'updateActiveTabValues' ]
451
- > (
424
+ const updateActiveTabValues : EditorContextType [ 'updateActiveTabValues' ] =
452
425
partialTab => {
453
426
setTabState ( current => {
454
427
const updated = setPropertiesInActiveTab ( current , partialTab ) ;
455
428
storeTabs ( updated ) ;
456
429
onTabChange ?.( updated ) ;
457
430
return updated ;
458
431
} ) ;
459
- } ,
460
- [ onTabChange , storeTabs ] ,
461
- ) ;
432
+ } ;
462
433
463
434
const { onEditOperationName } = props ;
464
- const setOperationName = useCallback < EditorContextType [ 'setOperationName' ] > (
435
+ const setOperationName : EditorContextType [ 'setOperationName' ] =
465
436
operationName => {
466
437
if ( ! queryEditor ) {
467
438
return ;
468
439
}
469
440
470
- queryEditor . operationName = operationName ;
441
+ updateQueryEditor ( queryEditor , operationName ) ;
471
442
updateActiveTabValues ( { operationName } ) ;
472
443
onEditOperationName ?.( operationName ) ;
473
- } ,
474
- [ onEditOperationName , queryEditor , updateActiveTabValues ] ,
475
- ) ;
444
+ } ;
476
445
477
- const externalFragments = useMemo ( ( ) => {
446
+ const externalFragments = ( ( ) => {
478
447
const map = new Map < string , FragmentDefinitionNode > ( ) ;
479
448
if ( Array . isArray ( props . externalFragments ) ) {
480
449
for ( const fragment of props . externalFragments ) {
@@ -492,74 +461,54 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
492
461
) ;
493
462
}
494
463
return map ;
495
- } , [ props . externalFragments ] ) ;
464
+ } ) ( ) ;
496
465
497
- const validationRules = useMemo (
498
- ( ) => props . validationRules || [ ] ,
499
- [ props . validationRules ] ,
500
- ) ;
466
+ const validationRules = props . validationRules || [ ] ;
501
467
502
- const value = useMemo < EditorContextType > (
503
- ( ) => ( {
504
- ...tabState ,
505
- addTab,
506
- changeTab,
507
- moveTab,
508
- closeTab,
509
- updateActiveTabValues,
510
-
511
- headerEditor,
512
- queryEditor,
513
- responseEditor,
514
- variableEditor,
515
- setHeaderEditor,
516
- setQueryEditor,
517
- setResponseEditor,
518
- setVariableEditor,
519
-
520
- setOperationName,
521
-
522
- initialQuery : initialState . query ,
523
- initialVariables : initialState . variables ,
524
- initialHeaders : initialState . headers ,
525
- initialResponse : initialState . response ,
526
-
527
- externalFragments,
528
- validationRules,
468
+ const value : EditorContextType = {
469
+ ...tabState ,
470
+ addTab,
471
+ changeTab,
472
+ moveTab,
473
+ closeTab,
474
+ updateActiveTabValues,
529
475
530
- shouldPersistHeaders,
531
- setShouldPersistHeaders,
532
- } ) ,
533
- [
534
- tabState ,
535
- addTab ,
536
- changeTab ,
537
- moveTab ,
538
- closeTab ,
539
- updateActiveTabValues ,
540
-
541
- headerEditor ,
542
- queryEditor ,
543
- responseEditor ,
544
- variableEditor ,
476
+ headerEditor,
477
+ queryEditor,
478
+ responseEditor,
479
+ variableEditor,
480
+ setHeaderEditor,
481
+ setQueryEditor,
482
+ setResponseEditor,
483
+ setVariableEditor,
545
484
546
- setOperationName ,
485
+ setOperationName,
547
486
548
- initialState ,
487
+ initialQuery : initialState . query ,
488
+ initialVariables : initialState . variables ,
489
+ initialHeaders : initialState . headers ,
490
+ initialResponse : initialState . response ,
549
491
550
- externalFragments ,
551
- validationRules ,
492
+ externalFragments,
493
+ validationRules,
552
494
553
- shouldPersistHeaders ,
554
- setShouldPersistHeaders ,
555
- ] ,
556
- ) ;
495
+ shouldPersistHeaders,
496
+ setShouldPersistHeaders,
497
+ } ;
557
498
558
499
return (
559
500
< EditorContext . Provider value = { value } > { children } </ EditorContext . Provider >
560
501
) ;
561
502
}
562
503
504
+ // To make react-compiler happy, otherwise it fails due mutating props
505
+ function updateQueryEditor (
506
+ queryEditor : CodeMirrorEditorWithOperationFacts ,
507
+ operationName : string ,
508
+ ) {
509
+ queryEditor . operationName = operationName ;
510
+ }
511
+
563
512
export const useEditorContext = createContextHook ( EditorContext ) ;
564
513
565
514
const PERSIST_HEADERS_STORAGE_KEY = 'shouldPersistHeaders' ;
0 commit comments