1- import { Box , Container , Grid , Heading } from '@chakra-ui/react' ;
2- import React , { useEffect } from 'react' ;
1+ import { Box , Container , Flex , Grid , GridItem , Heading , Switch , Text } from '@chakra-ui/react' ;
2+ import React , { useCallback , useEffect } from 'react' ;
3+ import { useRouter } from 'next/router' ;
34
45import { EventListenersCard } from '../components/EventListeners/EventListenersCard' ;
56import { WIDTH_2XL } from '../components/Layout' ;
67import { MethodsSection } from '../components/MethodsSection/MethodsSection' ;
8+ import { RpcMethodCard } from '../components/RpcMethods/RpcMethodCard' ;
79import { connectionMethods } from '../components/RpcMethods/method/connectionMethods' ;
810import { ephemeralMethods } from '../components/RpcMethods/method/ephemeralMethods' ;
911import { experimentalMethods } from '../components/RpcMethods/method/experimentalMethods' ;
@@ -23,8 +25,58 @@ import { walletTxShortcutsMap } from '../components/RpcMethods/shortcut/walletTx
2325import { SDKConfig } from '../components/SDKConfig/SDKConfig' ;
2426import { useEIP1193Provider } from '../context/EIP1193ProviderContextProvider' ;
2527
28+ const COOP_QUERY_KEY = 'coop' ;
29+ const COOP_QUERY_VALUE = 'same-origin' ;
30+
31+ async function ensureCoopServiceWorkerReady ( basePath : string ) {
32+ if ( ! ( 'serviceWorker' in navigator ) ) {
33+ return ;
34+ }
35+
36+ await navigator . serviceWorker . register ( `${ basePath } /coop-service-worker.js` ) ;
37+ await navigator . serviceWorker . ready ;
38+
39+ if ( navigator . serviceWorker . controller ) {
40+ return ;
41+ }
42+
43+ await new Promise < void > ( ( resolve ) => {
44+ const timeout = window . setTimeout ( resolve , 1000 ) ;
45+ navigator . serviceWorker . addEventListener (
46+ 'controllerchange' ,
47+ ( ) => {
48+ window . clearTimeout ( timeout ) ;
49+ resolve ( ) ;
50+ } ,
51+ { once : true }
52+ ) ;
53+ } ) ;
54+ }
55+
2656export default function Home ( ) {
2757 const { provider } = useEIP1193Provider ( ) ;
58+ const router = useRouter ( ) ;
59+ const simulateCoop = router . query [ COOP_QUERY_KEY ] === COOP_QUERY_VALUE ;
60+
61+ const handleSimulateCoopToggle = useCallback (
62+ async ( event : React . ChangeEvent < HTMLInputElement > ) => {
63+ const url = new URL ( window . location . href ) ;
64+
65+ if ( event . target . checked ) {
66+ url . searchParams . set ( COOP_QUERY_KEY , COOP_QUERY_VALUE ) ;
67+ try {
68+ await ensureCoopServiceWorkerReady ( router . basePath ) ;
69+ } catch {
70+ // Still navigate so the URL reflects the requested simulation state.
71+ }
72+ } else {
73+ url . searchParams . delete ( COOP_QUERY_KEY ) ;
74+ }
75+
76+ window . location . assign ( url . toString ( ) ) ;
77+ } ,
78+ [ router . basePath ]
79+ ) ;
2880 const [ connected , setConnected ] = React . useState ( false ) ;
2981 const [ chainId , setChainId ] = React . useState < number | undefined > ( undefined ) ;
3082 // This is for Extension compatibility, Extension with SDK3.9 does not emit connect event
@@ -75,11 +127,46 @@ export default function Home() {
75127 < Box mt = { 4 } >
76128 < SDKConfig />
77129 </ Box >
78- < MethodsSection
79- title = "Wallet Connection"
80- methods = { connectionMethods }
81- shortcutsMap = { connectionMethodShortcutsMap }
82- />
130+ < Box mt = { 4 } >
131+ < Heading size = "md" > Wallet Connection</ Heading >
132+ < Grid
133+ mt = { 2 }
134+ templateColumns = { {
135+ base : '100%' ,
136+ md : 'repeat(2, 50%)' ,
137+ xl : 'repeat(3, 33%)' ,
138+ } }
139+ gap = { 2 }
140+ >
141+ < GridItem w = "100%" key = "eth_requestAccounts" >
142+ < RpcMethodCard
143+ method = "eth_requestAccounts"
144+ params = { [ ] }
145+ format = { undefined }
146+ shortcuts = { connectionMethodShortcutsMap . eth_requestAccounts }
147+ >
148+ < Flex align = "center" justify = "space-between" mt = { 4 } pt = { 3 } borderTopWidth = { 1 } >
149+ < Text fontSize = "sm" fontWeight = "medium" >
150+ Simulate COOP
151+ </ Text >
152+ < Switch isChecked = { simulateCoop } onChange = { handleSimulateCoopToggle } />
153+ </ Flex >
154+ </ RpcMethodCard >
155+ </ GridItem >
156+ { connectionMethods
157+ . filter ( ( rpc ) => rpc . method !== 'eth_requestAccounts' )
158+ . map ( ( rpc ) => (
159+ < GridItem w = "100%" key = { rpc . method } >
160+ < RpcMethodCard
161+ method = { rpc . method }
162+ params = { rpc . params }
163+ format = { rpc . format }
164+ shortcuts = { connectionMethodShortcutsMap [ rpc . method ] }
165+ />
166+ </ GridItem >
167+ ) ) }
168+ </ Grid >
169+ </ Box >
83170 < MethodsSection
84171 title = "Ephemeral Methods"
85172 methods = { ephemeralMethods }
0 commit comments