-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hook useAccountName * Support inputChildren of Input * Export api from TransferProvider * Enhance account name display * Enable notify pr comment for preview
- Loading branch information
1 parent
af26dec
commit 0c3de02
Showing
9 changed files
with
152 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { DeriveAccountRegistration } from "@polkadot/api-derive/accounts/types"; | ||
import type { AccountId } from "@polkadot/types/interfaces"; | ||
import { TypeRegistry } from "@polkadot/types/create"; | ||
import { stringToU8a } from "@polkadot/util"; | ||
import { from, Subscription } from "rxjs"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { ApiPromise } from "@polkadot/api"; | ||
|
||
const registry = new TypeRegistry(); | ||
const PAD = 32; | ||
const KNOWN: [AccountId, string][] = [ | ||
[registry.createType("AccountId", stringToU8a("modlpy/socie".padEnd(PAD, "\0"))), "Society"], | ||
[registry.createType("AccountId", stringToU8a("modlpy/trsry".padEnd(PAD, "\0"))), "Treasury"], | ||
]; | ||
|
||
const extractName = (accountAddress: string, defaultName?: string) => { | ||
const known = KNOWN.find(([address]) => address.eq(accountAddress)); | ||
if (known) { | ||
return known[1]; | ||
} | ||
return defaultName || accountAddress; | ||
}; | ||
|
||
const extractIdentity = (cacheAddress: string, identity: DeriveAccountRegistration) => { | ||
const judgements = identity.judgements.filter(([, judgement]) => !judgement.isFeePaid); | ||
const isGood = judgements.some(([, judgement]) => judgement.isKnownGood || judgement.isReasonable); | ||
// const isBad = judgements.some(([, judgement]) => judgement.isErroneous || judgement.isLowQuality); | ||
const displayName = isGood ? identity.display : (identity.display || "").replace(/[^\x20-\x7E]/g, ""); | ||
const displayParent = | ||
identity.displayParent && (isGood ? identity.displayParent : identity.displayParent.replace(/[^\x20-\x7E]/g, "")); | ||
return displayParent || displayName || cacheAddress; | ||
}; | ||
|
||
export const useAccountName = (address: string, api: ApiPromise | undefined) => { | ||
const [name, setName] = useState(address); | ||
|
||
useEffect(() => { | ||
let sub$$: Subscription | undefined; | ||
|
||
if (api) { | ||
sub$$ = from(api.derive.accounts.info(address)).subscribe({ | ||
next: ({ nickname, identity }) => { | ||
if (typeof api.query.identity?.identityOf === "function") { | ||
setName(identity.display ? extractIdentity(address, identity) : extractName(address)); | ||
} else if (nickname) { | ||
setName(nickname); | ||
} else { | ||
setName(extractName(address)); | ||
} | ||
}, | ||
error: console.error, | ||
}); | ||
} | ||
|
||
return () => { | ||
sub$$?.unsubscribe(); | ||
}; | ||
}, [address, api]); | ||
|
||
return name; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import { InputHTMLAttributes, forwardRef } from "react"; | ||
|
||
interface Props {} | ||
interface Props { | ||
inputChildren?: JSX.Element; | ||
} | ||
|
||
export default forwardRef<HTMLInputElement, InputHTMLAttributes<HTMLInputElement> & Props>(function Input( | ||
{ className, ...rest }, | ||
{ className, inputChildren, ...rest }, | ||
ref, | ||
) { | ||
return ( | ||
<input className={`focus-visible:outline-none disabled:cursor-not-allowed ${className}`} ref={ref} {...rest} /> | ||
<div className="relative w-full"> | ||
<input | ||
className={`focus-visible:outline-none disabled:cursor-not-allowed ${ | ||
inputChildren ? "text-transparent" : "" | ||
} ${className}`} | ||
ref={ref} | ||
{...rest} | ||
/> | ||
{inputChildren && <div className="absolute bottom-0 left-0 right-0 top-0 truncate">{inputChildren}</div>} | ||
</div> | ||
); | ||
}); |