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
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
1 year ago
by
Niklas P
Browse files
Options
Downloads
Patches
Plain Diff
update init logic
parent
1da91308
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide 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
{
useEffect
,
useState
}
from
'
react
'
;
import
{
useIsMounted
}
from
'
./use-is-mounted
'
;
import
{
InjectedAccountWithMeta
,
InjectedExtension
,
}
from
"
@polkadot/extension-inject/types
"
;
import
{
useEffect
,
useState
}
from
"
react
"
;
import
{
useIsMounted
}
from
"
./use-is-mounted
"
;
import
{
documentReadyPromise
}
from
"
./utils
"
;
interface
checkEnabledReturnType
{
accounts
:
InjectedAccountWithMeta
[]
|
null
;
error
:
Error
|
null
;
accounts
:
InjectedAccountWithMeta
[]
|
null
;
error
:
Error
|
null
;
}
export
const
checkEnabled
:
(
extensionName
:
string
)
=>
Promise
<
checkEnabledReturnType
>
=
async
(
extensionName
:
string
=
'
polkadot-extension
'
,
)
=>
{
const
extensionDapp
=
await
import
(
'
@polkadot/extension-dapp
'
);
const
{
web3Accounts
,
web3Enable
}
=
extensionDapp
;
try
{
const
enabledApps
=
await
web3Enable
(
extensionName
);
const
w3Enabled
=
enabledApps
.
length
>
0
;
let
accounts
=
null
;
if
(
w3Enabled
)
{
accounts
=
await
web3Accounts
();
return
{
accounts
,
error
:
null
};
}
export
const
extensionSetup
=
async
()
=>
{
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
;
}
};
return
{
accounts
:
null
,
error
:
new
Error
(
'
please allow your extension to access this dApp and refresh the page or install a substrate wallet
'
)
}
}
catch
(
error
:
any
)
{
return
{
accounts
:
null
,
error
};
export
const
checkEnabled
:
(
extensionName
:
string
)
=>
Promise
<
checkEnabledReturnType
>
=
async
(
extensionName
:
string
=
"
polkadot-extension
"
)
=>
{
const
extensionDapp
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
{
web3Accounts
,
web3Enable
}
=
extensionDapp
;
try
{
const
enabledApps
=
await
web3Enable
(
extensionName
);
console
.
log
(
"
enabled Apps
"
,
enabledApps
);
const
w3Enabled
=
enabledApps
.
length
>
0
;
let
accounts
=
null
;
if
(
w3Enabled
)
{
accounts
=
await
web3Accounts
();
console
.
log
(
"
accounts
"
,
accounts
);
return
{
accounts
,
error
:
null
};
}
};
export
interface
UsePolkadotExtensionReturnType
{
isReady
:
boolean
accounts
:
InjectedAccountWithMeta
[]
|
null
error
:
Error
|
null
injector
:
InjectedExtension
|
null
actingAccount
:
InjectedAccountWithMeta
|
null
setActingAccountIdx
:
(
idx
:
number
)
=>
void
return
{
accounts
:
null
,
error
:
new
Error
(
"
please allow your extension to access this dApp and refresh the page or install a substrate wallet
"
),
};
}
catch
(
error
:
any
)
{
return
{
accounts
:
null
,
error
};
}
};
export
const
usePolkadotExtension
=
():
UsePolkadotExtensionReturnType
=>
{
const
isMounted
=
useIsMounted
()
const
[
isReady
,
setIsReady
]
=
useState
(
false
)
const
[
accounts
,
setAccounts
]
=
useState
<
InjectedAccountWithMeta
[]
|
null
>
(
null
)
const
[
actingAccountIdx
,
setActingAccountIdx
]
=
useState
<
number
>
(
0
)
const
[
error
,
setError
]
=
useState
<
Error
|
null
>
(
null
)
const
[
injector
,
setInjector
]
=
useState
<
InjectedExtension
|
null
>
(
null
)
const
[
extensions
,
setExtensions
]
=
useState
<
InjectedExtension
[]
|
null
>
(
null
)
const
actingAccount
=
accounts
&&
accounts
[
actingAccountIdx
]
useEffect
(()
=>
{
const
setup
=
async
()
=>
{
const
extensionDapp
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
{
web3AccountsSubscribe
,
web3Enable
,
web3Accounts
}
=
extensionDapp
;
// const enabledApps = await web3Enable("polkadot-extension");
const
injectedPromise
=
await
web3Enable
(
"
polkadot-extension
"
);
const
_extensions
=
await
injectedPromise
;
setExtensions
(
_extensions
);
if
(
_extensions
.
length
===
0
)
{
console
.
log
(
"
no extension
"
);
return
;
}
if
(
accounts
)
{
setIsReady
(
true
);
}
else
{
let
unsubscribe
:
()
=>
void
;
// we subscribe to any account change and log the new list.
// note that `web3AccountsSubscribe` returns the function to unsubscribe
unsubscribe
=
await
web3AccountsSubscribe
((
injectedAccounts
)
=>
{
console
.
log
(
'
receivedAccounts
'
,
injectedAccounts
)
setAccounts
(
injectedAccounts
);
});
return
()
=>
unsubscribe
&&
unsubscribe
();
}
};
if
(
!
isReady
)
{
setup
();
export
interface
UsePolkadotExtensionReturnType
{
isReady
:
boolean
;
accounts
:
InjectedAccountWithMeta
[]
|
null
;
error
:
Error
|
null
;
injector
:
InjectedExtension
|
null
;
actingAccount
:
InjectedAccountWithMeta
|
null
;
setActingAccountIdx
:
(
idx
:
number
)
=>
void
;
}
export
const
usePolkadotExtension
=
():
UsePolkadotExtensionReturnType
=>
{
const
isMounted
=
useIsMounted
();
const
[
isReady
,
setIsReady
]
=
useState
(
false
);
const
[
accounts
,
setAccounts
]
=
useState
<
InjectedAccountWithMeta
[]
|
null
>
(
null
);
const
[
extensions
,
setExtensions
]
=
useState
<
InjectedExtension
[]
|
null
>
(
null
);
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
(()
=>
{
const
setup
=
async
()
=>
{
const
extensionDapp
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
{
web3AccountsSubscribe
,
web3Enable
,
web3Accounts
}
=
extensionDapp
;
// const enabledApps = await web3Enable("polkadot-extension");
const
injectedPromise
=
documentReadyPromise
(()
=>
web3Enable
(
"
polkadot-extension
"
)
);
// const injectedPromise = await web3Enable("polkadot-extension");
const
extensions
=
await
injectedPromise
;
setExtensions
(
extensions
);
if
(
extensions
.
length
===
0
)
{
console
.
log
(
"
no extension
"
);
return
;
}
},
[]);
// 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
(()
=>
{
if
(
isMounted
)
{
const
getInjector
=
async
()
=>
{
const
{
web3FromSource
}
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
actingAccount
=
accounts
&&
actingAccountIdx
!==
undefined
?
accounts
[
actingAccountIdx
]
:
undefined
if
(
actingAccount
?.
meta
.
source
)
{
try
{
const
injector
=
await
web3FromSource
(
actingAccount
?.
meta
.
source
);
setInjector
(
injector
)
}
catch
(
e
:
any
)
{
setError
(
e
)
}
}
}
getInjector
()
// const accounts = await web3Accounts();
if
(
accounts
)
{
setIsReady
(
true
);
}
else
{
let
unsubscribe
:
()
=>
void
;
// we subscribe to any account change
// note that `web3AccountsSubscribe` returns the function to unsubscribe
unsubscribe
=
await
web3AccountsSubscribe
((
injectedAccounts
)
=>
{
setAccounts
(
injectedAccounts
);
});
return
()
=>
unsubscribe
&&
unsubscribe
();
}
};
if
(
!
isReady
)
{
setup
();
}
},
[
extensions
,
isReady
]);
useEffect
(()
=>
{
const
getInjector
=
async
()
=>
{
const
{
web3FromSource
}
=
await
import
(
"
@polkadot/extension-dapp
"
);
const
actingAccount
=
accounts
&&
actingAccountIdx
!==
undefined
?
accounts
[
actingAccountIdx
]
:
undefined
;
if
(
actingAccount
?.
meta
.
source
)
{
try
{
const
injector
=
await
web3FromSource
(
actingAccount
?.
meta
.
source
);
setInjector
(
injector
);
}
catch
(
e
:
any
)
{
setError
(
e
);
}
},
[
actingAccountIdx
,
accounts
]
)
return
{
accounts
,
actingAccount
,
setActingAccountIdx
,
isReady
,
error
,
injector
};
}
\ No newline at end of file
}
};
getInjector
();
},
[
actingAccountIdx
,
accounts
]);
return
{
accounts
,
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