refactor: migrate accounts to polkahub

This commit is contained in:
Victor Oliva
2025-11-05 19:56:34 +01:00
parent b3214684bf
commit 7adfb9e3aa
33 changed files with 2450 additions and 2059 deletions
+18 -9
View File
@@ -1,14 +1,15 @@
import { PolkadotIdenticon } from "@/components/PolkadotIdenticon"
import { accountDetail$, getPublicKey } from "@/state/extension-accounts.state"
import { identity$, isVerified } from "@/state/identity.state"
import { accountDetail$ } from "@/state/polkahub"
import { EthIdenticon, PolkadotIdenticon } from "@polkadot-api/react-components"
import { useStateObservable } from "@react-rxjs/core"
import { CheckCircle } from "lucide-react"
import { SS58String } from "polkadot-api"
import { getSs58AddressInfo } from "polkadot-api"
import { AccountAddress } from "polkahub"
import { FC } from "react"
import { twMerge } from "tailwind-merge"
export const AccountIdDisplay: FC<{
value: SS58String
value: AccountAddress
className?: string
}> = ({ value, className }) => {
const details = useStateObservable(accountDetail$(value))
@@ -18,11 +19,15 @@ export const AccountIdDisplay: FC<{
return (
<div className={twMerge("flex items-center gap-2", className)}>
<PolkadotIdenticon
className="shrink-0"
publicKey={getPublicKey(value)}
size={28}
/>
{value.startsWith("0x") ? (
<EthIdenticon address={value} size={28} className="rounded" />
) : (
<PolkadotIdenticon
className="shrink-0"
publicKey={getPublicKey(value)}
size={28}
/>
)}
<div className="flex flex-col justify-center text-foreground leading-tight overflow-hidden">
{name && (
<span className="inline-flex items-center gap-1">
@@ -42,3 +47,7 @@ export const AccountIdDisplay: FC<{
</div>
)
}
const getPublicKey = (address: string) => {
const info = getSs58AddressInfo(address)
return info.isValid ? info.publicKey : null
}
+8 -10
View File
@@ -13,10 +13,8 @@ import {
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import { groupBy } from "@/lib/groupBy"
import { accounts$ } from "@/state/accounts.state"
import { getPublicKey } from "@/state/extension-accounts.state"
import { identity$, isVerified } from "@/state/identity.state"
import { addrToAccount$, getPublicKey } from "@/state/polkahub"
import { cn } from "@/utils/cn"
import {
getSs58AddressInfo,
@@ -29,13 +27,15 @@ import { Check, ChevronsUpDown } from "lucide-react"
import { FC, useState } from "react"
import { filter, map, switchMap, take } from "rxjs"
const acconutMap$ = accounts$.pipeState(
map((accounts) => groupBy(accounts, (acc) => acc.accountId)),
const acconutMap$ = addrToAccount$.pipeState(
map((groups) =>
Object.fromEntries(
Object.entries(groups)
.filter(([accountId]) => !accountId.startsWith("0x"))
.map(([accountId, [groupRep]]) => [accountId, groupRep]),
.map(([accountId, account]) => [
toHex(getPublicKey(accountId)),
account,
]),
),
),
)
@@ -49,9 +49,9 @@ const hintedAccounts$ = state(
filter((v) => !!v),
take(1),
switchMap((details) =>
identity$(details.accountId).pipe(
identity$(details.address).pipe(
map((identity) => ({
address: details.accountId,
address: details.address,
name: identity?.displayName ?? details.name,
isVerified: isVerified(identity),
})),
@@ -113,7 +113,6 @@ export const AccountIdInput: FC<{
"flex w-64 justify-between overflow-hidden px-2 border border-border bg-input",
className,
)}
forceSvgSize={false}
>
{value !== null ? (
<AccountIdDisplay value={value} className="overflow-hidden" />
@@ -193,7 +192,6 @@ const AccountOption: FC<{
value={account + "_" + name}
onSelect={onSelect}
className="flex flex-row items-center gap-2 p-1"
forceSvgSize={false}
>
<AccountIdDisplay value={account} className="overflow-hidden" />
<Check
+2 -2
View File
@@ -1,9 +1,9 @@
import { accountDetail$ } from "@/state/extension-accounts.state"
import { accountDetail$ } from "@/state/polkahub"
import { EthIdenticon } from "@polkadot-api/react-components"
import { useStateObservable } from "@react-rxjs/core"
import { HexString } from "polkadot-api"
import { FC } from "react"
import { twMerge } from "tailwind-merge"
import { EthIdenticon } from "./EthIdenticon"
export const EthAccountDisplay: FC<{
value: HexString
-10
View File
@@ -1,10 +0,0 @@
import { HexString } from "polkadot-api"
import { FC } from "react"
import { blo } from "blo"
export const EthIdenticon: FC<{ address: HexString; size?: number }> = ({
address,
size,
}) => {
return <img className="rounded-full" src={blo(address as "0x", size)} />
}
-154
View File
@@ -1,154 +0,0 @@
// Based from https://github.com/paritytech/oo7/blob/251ba2b7c45503b68eab4320c270b5afa9bccb60/packages/polkadot-identicon/src/index.jsx
import { blake2b } from "@noble/hashes/blake2.js"
import { FC, SVGAttributes } from "react"
export const PolkadotIdenticon: FC<
Omit<SVGAttributes<SVGElement>, "viewBox"> & {
sixPoint?: boolean
publicKey: Uint8Array | null
size?: number
}
> = ({ sixPoint, publicKey, ...props }) => {
const svgProps = {
...props,
width: props.size ?? props.width ?? SIZE,
height: props.size ?? props.height ?? SIZE,
viewBox: `0 0 ${SIZE} ${SIZE}`,
}
if (!publicKey) {
return (
<svg {...svgProps}>
<circle cx={SIZE / 2} cy={SIZE / 2} r={SIZE / 2} fill="#eee" />
</svg>
)
}
const colors = getColors(publicKey)
const circles = getPositions(sixPoint)
return (
<svg {...svgProps}>
<circle cx={SIZE / 2} cy={SIZE / 2} r={SIZE / 2} fill="#eee" />
{circles.map(({ x, y }, i) => (
<circle key={i} cx={x} cy={y} r={z} fill={colors[i]} />
))}
</svg>
)
}
const SIZE = 64
const CENTER = SIZE / 2
const z = (SIZE / 64) * 5
type Schema = { freq: number; colors: number[] }
const schema: Record<string, Schema> = {
target: {
freq: 1,
colors: [0, 28, 0, 0, 28, 0, 0, 28, 0, 0, 28, 0, 0, 28, 0, 0, 28, 0, 1],
},
cube: {
freq: 20,
colors: [0, 1, 3, 2, 4, 3, 0, 1, 3, 2, 4, 3, 0, 1, 3, 2, 4, 3, 5],
},
quazar: {
freq: 16,
colors: [1, 2, 3, 1, 2, 4, 5, 5, 4, 1, 2, 3, 1, 2, 4, 5, 5, 4, 0],
},
flower: {
freq: 32,
colors: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 3],
},
cyclic: {
freq: 32,
colors: [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6],
},
vmirror: {
freq: 128,
colors: [0, 1, 2, 3, 4, 5, 3, 4, 2, 0, 1, 6, 7, 8, 9, 7, 8, 6, 10],
},
hmirror: {
freq: 128,
colors: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 8, 6, 7, 5, 3, 4, 2, 11],
},
}
const totalFreq = Object.keys(schema)
.map((k) => schema[k].freq)
.reduce((a, b) => a + b)
const findScheme = (d: number) => {
let cum = 0
for (const scheme of Object.values(schema)) {
cum += scheme.freq
if (d < cum) {
return scheme
}
}
throw "Unreachable"
}
const blake2512 = (input: Uint8Array) => blake2b(input, { dkLen: 64 })
const zero = blake2512(
new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
]),
)
const getColors = (publicKey: Uint8Array) => {
const id = Array.from(blake2512(publicKey)).map(
(x, i) => (x + 256 - zero[i]) % 256,
)
const sat = (Math.floor((id[29] * 70) / 256 + 26) % 80) + 30
const d = Math.floor((id[30] + id[31] * 256) % totalFreq)
const scheme = findScheme(d)
const palette = Array.from(id).map((x, i) => {
const b = (x + (i % 28) * 58) % 256
if (b == 0) {
return "#444"
}
if (b == 255) {
return "transparent"
}
const h = Math.floor(((b % 64) * 360) / 64)
const l = [53, 15, 35, 75][Math.floor(b / 64)]
return `hsl(${h}, ${sat}%, ${l}%)`
})
const rot = (id[28] % 6) * 3
return scheme.colors.map(
(_, i) => palette[scheme.colors[i < 18 ? (i + rot) % 18 : 18]],
)
}
const getPositions = (sixPoint = false) => {
const r = sixPoint ? (SIZE / 2 / 8) * 5 : (SIZE / 2 / 4) * 3
const rroot3o2 = (r * Math.sqrt(3)) / 2
const ro2 = r / 2
const rroot3o4 = (r * Math.sqrt(3)) / 4
const ro4 = r / 4
const r3o4 = (r * 3) / 4
return [
{ x: CENTER, y: CENTER - r },
{ x: CENTER, y: CENTER - ro2 },
{ x: CENTER - rroot3o4, y: CENTER - r3o4 },
{ x: CENTER - rroot3o2, y: CENTER - ro2 },
{ x: CENTER - rroot3o4, y: CENTER - ro4 },
{ x: CENTER - rroot3o2, y: CENTER },
{ x: CENTER - rroot3o2, y: CENTER + ro2 },
{ x: CENTER - rroot3o4, y: CENTER + ro4 },
{ x: CENTER - rroot3o4, y: CENTER + r3o4 },
{ x: CENTER, y: CENTER + r },
{ x: CENTER, y: CENTER + ro2 },
{ x: CENTER + rroot3o4, y: CENTER + r3o4 },
{ x: CENTER + rroot3o2, y: CENTER + ro2 },
{ x: CENTER + rroot3o4, y: CENTER + ro4 },
{ x: CENTER + rroot3o2, y: CENTER },
{ x: CENTER + rroot3o2, y: CENTER - ro2 },
{ x: CENTER + rroot3o4, y: CENTER - ro4 },
{ x: CENTER + rroot3o4, y: CENTER - r3o4 },
{ x: CENTER, y: CENTER },
]
}
+1 -71
View File
@@ -1,71 +1 @@
import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const buttonVariants = cva(
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"border bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
},
)
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean
forceSvgSize?: boolean
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
(
{
className,
variant,
size,
asChild = false,
forceSvgSize = true,
...props
},
ref,
) => {
const Comp = asChild ? Slot : "button"
return (
<Comp
className={cn(
buttonVariants({ variant, size, className }),
forceSvgSize &&
"[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
)}
ref={ref}
{...props}
/>
)
},
)
Button.displayName = "Button"
export { Button, buttonVariants }
export { Button } from "@polkahub/ui-components"
+3 -152
View File
@@ -1,157 +1,8 @@
import * as React from "react"
import { type DialogProps } from "@radix-ui/react-dialog"
import { Command as CommandPrimitive } from "cmdk"
import { Search } from "lucide-react"
import { cn } from "@/lib/utils"
import { Dialog, DialogContent } from "@/components/ui/dialog"
const Command = React.forwardRef<
React.ElementRef<typeof CommandPrimitive>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive>
>(({ className, ...props }, ref) => (
<CommandPrimitive
ref={ref}
className={cn(
"flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground",
className,
)}
{...props}
/>
))
Command.displayName = CommandPrimitive.displayName
interface CommandDialogProps extends DialogProps {}
const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
return (
<Dialog {...props}>
<DialogContent className="overflow-hidden p-0 shadow-lg">
<Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
{children}
</Command>
</DialogContent>
</Dialog>
)
}
const CommandInput = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Input>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
>(({ className, ...props }, ref) => (
<div className="flex items-center border-b px-3" cmdk-input-wrapper="">
<Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
<CommandPrimitive.Input
ref={ref}
className={cn(
"flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-hidden placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
{...props}
/>
</div>
))
CommandInput.displayName = CommandPrimitive.Input.displayName
const CommandList = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.List>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.List>
>(({ className, ...props }, ref) => (
<CommandPrimitive.List
ref={ref}
className={cn("max-h-[300px] overflow-y-auto overflow-x-hidden", className)}
{...props}
/>
))
CommandList.displayName = CommandPrimitive.List.displayName
const CommandEmpty = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Empty>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
>((props, ref) => (
<CommandPrimitive.Empty
ref={ref}
className="py-6 text-center text-sm"
{...props}
/>
))
CommandEmpty.displayName = CommandPrimitive.Empty.displayName
const CommandGroup = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Group>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
>(({ className, ...props }, ref) => (
<CommandPrimitive.Group
ref={ref}
className={cn(
"overflow-hidden p-1 text-foreground [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground",
className,
)}
{...props}
/>
))
CommandGroup.displayName = CommandPrimitive.Group.displayName
const CommandSeparator = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
>(({ className, ...props }, ref) => (
<CommandPrimitive.Separator
ref={ref}
className={cn("-mx-1 h-px bg-border", className)}
{...props}
/>
))
CommandSeparator.displayName = CommandPrimitive.Separator.displayName
const CommandItem = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item> & {
forceSvgSize?: boolean
}
>(({ className, forceSvgSize = true, ...props }, ref) => (
<CommandPrimitive.Item
ref={ref}
className={cn(
"relative flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-hidden data-[disabled=true]:pointer-events-none data-[selected='true']:bg-accent data-[selected=true]:text-accent-foreground data-[disabled=true]:opacity-50",
forceSvgSize &&
"[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
className,
)}
{...props}
/>
))
CommandItem.displayName = CommandPrimitive.Item.displayName
const CommandShortcut = ({
className,
...props
}: React.HTMLAttributes<HTMLSpanElement>) => {
return (
<span
className={cn(
"ml-auto text-xs tracking-widest text-muted-foreground",
className,
)}
{...props}
/>
)
}
CommandShortcut.displayName = "CommandShortcut"
export {
Command,
CommandDialog,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandEmpty,
CommandGroup,
CommandItem,
CommandShortcut,
CommandSeparator,
}
} from "@polkahub/ui-components"
+13 -113
View File
@@ -1,120 +1,20 @@
import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"
import { cn } from "@/lib/utils"
const Dialog = DialogPrimitive.Root
const DialogTrigger = DialogPrimitive.Trigger
const DialogPortal = DialogPrimitive.Portal
const DialogClose = DialogPrimitive.Close
const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className,
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-top-[20%] data-[state=open]:slide-in-from-top-[20%] sm:rounded-lg",
className,
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-hidden focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName
const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className,
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"
const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className,
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"
const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className,
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName
const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName
export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
DialogTrigger,
} from "@polkahub/ui-components"
export function DialogBody({
children,
id,
className,
}: React.PropsWithChildren<{ id?: string; className?: string }>) {
return (
<div className={cn("overflow-auto px-6 py-2 last:pb-6", className)} id={id}>
{children}
</div>
)
}
+5 -29
View File
@@ -1,29 +1,5 @@
import * as React from "react"
import * as PopoverPrimitive from "@radix-ui/react-popover"
import { cn } from "@/lib/utils"
const Popover = PopoverPrimitive.Root
const PopoverTrigger = PopoverPrimitive.Trigger
const PopoverContent = React.forwardRef<
React.ElementRef<typeof PopoverPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
>(({ className, align = "center", sideOffset = 4, ...props }, ref) => (
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
ref={ref}
align={align}
sideOffset={sideOffset}
className={cn(
"z-50 w-72 rounded-md border border-neutral-200 bg-white p-4 text-neutral-950 shadow-md outline-hidden data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 dark:border-neutral-800 dark:bg-neutral-950 dark:text-neutral-50",
className,
)}
{...props}
/>
</PopoverPrimitive.Portal>
))
PopoverContent.displayName = PopoverPrimitive.Content.displayName
export { Popover, PopoverTrigger, PopoverContent }
export {
Popover,
PopoverContent,
PopoverTrigger,
} from "@polkahub/ui-components"