Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xx-polkadot-js-tokengated-website
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rothbardian
xx-polkadot-js-tokengated-website
Commits
a8823276
Commit
a8823276
authored
May 9, 2023
by
Niklas P
Browse files
Options
Downloads
Patches
Plain Diff
update init logic
parent
1da91308
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
hooks/use-polkadot-extension.ts
+137
-117
137 additions, 117 deletions
hooks/use-polkadot-extension.ts
hooks/utils.ts
+10
-0
10 additions, 0 deletions
hooks/utils.ts
with
147 additions
and
117 deletions
hooks/use-polkadot-extension.ts
+
137
−
117
View file @
a8823276
import
{
InjectedAccountWithMeta
,
InjectedExtension
}
from
'
@polkadot/extension-inject/types
'
;
import
{
import
{
useEffect
,
useState
}
from
'
react
'
;
InjectedAccountWithMeta
,
import
{
useIsMounted
}
from
'
./use-is-mounted
'
;
InjectedExtension
,
}
from
"
@polkadot/extension-inject/types
"
;
import
{
useEffect
,
useState
}
from
"
react
"
;
import
{
useIsMounted
}
from
"
./use-is-mounted
"
;
import
{
documentReadyPromise
}
from
"
./utils
"
;
interface
checkEnabledReturnType
{
interface
checkEnabledReturnType
{
accounts
:
InjectedAccountWithMeta
[]
|
null
;
accounts
:
InjectedAccountWithMeta
[]
|
null
;
error
:
Error
|
null
;
error
:
Error
|
null
;
}
}
export
const
checkEnabled
:
(
extensionName
:
string
)
=>
Promise
<
checkEnabledReturnType
>
=
async
(
export
const
extensionSetup
=
async
()
=>
{
extensionName
:
string
=
'
polkadot-extension
'
,
const
extensionDapp
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
{
web3Accounts
,
web3Enable
,
web3AccountsSubscribe
}
=
extensionDapp
;
const
enabledApps
=
await
web3Enable
(
"
polkadot-extension
"
);
console
.
log
(
"
enabled Apps
"
,
enabledApps
);
if
(
enabledApps
.
length
===
0
)
{
console
.
log
(
"
no extension
"
);
return
;
}
};
export
const
checkEnabled
:
(
extensionName
:
string
)
=>
Promise
<
checkEnabledReturnType
>
=
async
(
extensionName
:
string
=
"
polkadot-extension
"
)
=>
{
)
=>
{
const
extensionDapp
=
await
import
(
'
@polkadot/extension-dapp
'
);
const
extensionDapp
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
{
web3Accounts
,
web3Enable
}
=
extensionDapp
;
const
{
web3Accounts
,
web3Enable
}
=
extensionDapp
;
try
{
try
{
const
enabledApps
=
await
web3Enable
(
extensionName
);
const
enabledApps
=
await
web3Enable
(
extensionName
);
console
.
log
(
"
enabled Apps
"
,
enabledApps
);
const
w3Enabled
=
enabledApps
.
length
>
0
;
const
w3Enabled
=
enabledApps
.
length
>
0
;
let
accounts
=
null
;
let
accounts
=
null
;
if
(
w3Enabled
)
{
if
(
w3Enabled
)
{
accounts
=
await
web3Accounts
();
accounts
=
await
web3Accounts
();
console
.
log
(
"
accounts
"
,
accounts
);
return
{
accounts
,
error
:
null
};
return
{
accounts
,
error
:
null
};
}
}
return
{
return
{
accounts
:
null
,
accounts
:
null
,
error
:
new
Error
(
'
please allow your extension to access this dApp and refresh the page or install a substrate wallet
'
)
error
:
new
Error
(
}
"
please allow your extension to access this dApp and refresh the page or install a substrate wallet
"
),
};
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
return
{
accounts
:
null
,
error
};
return
{
accounts
:
null
,
error
};
}
}
};
};
export
interface
UsePolkadotExtensionReturnType
{
export
interface
UsePolkadotExtensionReturnType
{
isReady
:
boolean
isReady
:
boolean
;
accounts
:
InjectedAccountWithMeta
[]
|
null
accounts
:
InjectedAccountWithMeta
[]
|
null
;
error
:
Error
|
null
error
:
Error
|
null
;
injector
:
InjectedExtension
|
null
injector
:
InjectedExtension
|
null
;
actingAccount
:
InjectedAccountWithMeta
|
null
actingAccount
:
InjectedAccountWithMeta
|
null
;
setActingAccountIdx
:
(
idx
:
number
)
=>
void
setActingAccountIdx
:
(
idx
:
number
)
=>
void
;
}
}
export
const
usePolkadotExtension
=
():
UsePolkadotExtensionReturnType
=>
{
export
const
usePolkadotExtension
=
():
UsePolkadotExtensionReturnType
=>
{
const
isMounted
=
useIsMounted
()
const
isMounted
=
useIsMounted
();
const
[
isReady
,
setIsReady
]
=
useState
(
false
)
const
[
isReady
,
setIsReady
]
=
useState
(
false
);
const
[
accounts
,
setAccounts
]
=
useState
<
InjectedAccountWithMeta
[]
|
null
>
(
null
)
const
[
accounts
,
setAccounts
]
=
useState
<
InjectedAccountWithMeta
[]
|
null
>
(
const
[
actingAccountIdx
,
setActingAccountIdx
]
=
useState
<
number
>
(
0
)
null
const
[
error
,
setError
]
=
useState
<
Error
|
null
>
(
null
)
);
const
[
injector
,
setInjector
]
=
useState
<
InjectedExtension
|
null
>
(
null
)
const
[
extensions
,
setExtensions
]
=
useState
<
InjectedExtension
[]
|
null
>
(
const
[
extensions
,
setExtensions
]
=
useState
<
InjectedExtension
[]
|
null
>
(
null
)
null
);
const
actingAccount
=
accounts
&&
accounts
[
actingAccountIdx
]
const
[
actingAccountIdx
,
setActingAccountIdx
]
=
useState
<
number
>
(
0
);
const
[
error
,
setError
]
=
useState
<
Error
|
null
>
(
null
);
const
[
injector
,
setInjector
]
=
useState
<
InjectedExtension
|
null
>
(
null
);
const
actingAccount
=
accounts
&&
accounts
[
actingAccountIdx
];
useEffect
(()
=>
{
useEffect
(()
=>
{
const
setup
=
async
()
=>
{
const
setup
=
async
()
=>
{
...
@@ -57,25 +82,29 @@ export const checkEnabled: (extensionName: string) => Promise<checkEnabledReturn
...
@@ -57,25 +82,29 @@ export const checkEnabled: (extensionName: string) => Promise<checkEnabledReturn
const
{
web3AccountsSubscribe
,
web3Enable
,
web3Accounts
}
=
extensionDapp
;
const
{
web3AccountsSubscribe
,
web3Enable
,
web3Accounts
}
=
extensionDapp
;
// const enabledApps = await web3Enable("polkadot-extension");
// const enabledApps = await web3Enable("polkadot-extension");
const
injectedPromise
=
await
web3Enable
(
"
polkadot-extension
"
);
const
injectedPromise
=
documentReadyPromise
(()
=>
const
_extensions
=
await
injectedPromise
;
web3Enable
(
"
polkadot-extension
"
)
);
// const injectedPromise = await web3Enable("polkadot-extension");
const
extensions
=
await
injectedPromise
;
setExtensions
(
_
extensions
);
setExtensions
(
extensions
);
if
(
_
extensions
.
length
===
0
)
{
if
(
extensions
.
length
===
0
)
{
console
.
log
(
"
no extension
"
);
console
.
log
(
"
no extension
"
);
return
;
return
;
}
}
// const accounts = await web3Accounts();
if
(
accounts
)
{
if
(
accounts
)
{
setIsReady
(
true
);
setIsReady
(
true
);
}
else
{
}
else
{
let
unsubscribe
:
()
=>
void
;
let
unsubscribe
:
()
=>
void
;
// we subscribe to any account change
and log the new list.
// we subscribe to any account change
// note that `web3AccountsSubscribe` returns the function to unsubscribe
// note that `web3AccountsSubscribe` returns the function to unsubscribe
unsubscribe
=
await
web3AccountsSubscribe
((
injectedAccounts
)
=>
{
unsubscribe
=
await
web3AccountsSubscribe
((
injectedAccounts
)
=>
{
console
.
log
(
'
receivedAccounts
'
,
injectedAccounts
)
setAccounts
(
injectedAccounts
);
setAccounts
(
injectedAccounts
);
});
});
...
@@ -86,43 +115,34 @@ export const checkEnabled: (extensionName: string) => Promise<checkEnabledReturn
...
@@ -86,43 +115,34 @@ export const checkEnabled: (extensionName: string) => Promise<checkEnabledReturn
if
(
!
isReady
)
{
if
(
!
isReady
)
{
setup
();
setup
();
}
}
},
[]);
},
[
extensions
,
isReady
]);
// useEffect(() => {
// const maybeEnable = async () => {
// console.log( 'here at maybeEnable')
// if (isMounted) {
// const enablePromise = checkEnabled('polkadot-extension')
// const enableResult = await enablePromise
// const { accounts, error } = enableResult
// setError(error)
// setAccounts(accounts)
// }
// }
// maybeEnable()
// }, []);
useEffect
(()
=>
{
useEffect
(()
=>
{
if
(
isMounted
)
{
const
getInjector
=
async
()
=>
{
const
getInjector
=
async
()
=>
{
const
{
web3FromSource
}
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
{
web3FromSource
}
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
actingAccount
=
accounts
&&
actingAccountIdx
!==
undefined
?
const
actingAccount
=
accounts
[
actingAccountIdx
]
:
undefined
accounts
&&
actingAccountIdx
!==
undefined
?
accounts
[
actingAccountIdx
]
:
undefined
;
if
(
actingAccount
?.
meta
.
source
)
{
if
(
actingAccount
?.
meta
.
source
)
{
try
{
try
{
const
injector
=
await
web3FromSource
(
actingAccount
?.
meta
.
source
);
const
injector
=
await
web3FromSource
(
actingAccount
?.
meta
.
source
);
setInjector
(
injector
)
setInjector
(
injector
)
;
}
catch
(
e
:
any
)
{
}
catch
(
e
:
any
)
{
setError
(
e
)
setError
(
e
);
}
}
}
}
}
};
getInjector
()
getInjector
();
}
},
[
actingAccountIdx
,
accounts
]);
},
[
actingAccountIdx
,
accounts
]
)
return
{
accounts
,
actingAccount
,
setActingAccountIdx
,
isReady
,
error
,
injector
};
return
{
}
accounts
,
\ No newline at end of file
actingAccount
,
setActingAccountIdx
,
isReady
,
error
,
injector
,
};
};
This diff is collapsed.
Click to expand it.
hooks/utils.ts
0 → 100644
+
10
−
0
View file @
a8823276
export
function
documentReadyPromise
<
T
>
(
creator
:
()
=>
Promise
<
T
>
):
Promise
<
T
>
{
return
new
Promise
((
resolve
):
void
=>
{
if
(
document
.
readyState
===
"
complete
"
)
{
resolve
(
creator
());
}
else
{
window
.
addEventListener
(
"
load
"
,
()
=>
resolve
(
creator
()));
}
});
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment