start view-component
This commit is contained in:
@@ -8,33 +8,19 @@ import {
|
||||
ComboboxOption,
|
||||
ComboboxOptions,
|
||||
} from "@headlessui/react"
|
||||
import {
|
||||
connectInjectedExtension,
|
||||
getInjectedExtensions,
|
||||
InjectedPolkadotAccount,
|
||||
} from "polkadot-api/pjs-signer"
|
||||
import {
|
||||
getSs58AddressInfo,
|
||||
SS58String,
|
||||
} from "@polkadot-api/substrate-bindings"
|
||||
import { state, useStateObservable } from "@react-rxjs/core"
|
||||
import { combineKeys } from "@react-rxjs/utils"
|
||||
import { useStateObservable } from "@react-rxjs/core"
|
||||
import { FC, useState } from "react"
|
||||
import {
|
||||
concat,
|
||||
filter,
|
||||
finalize,
|
||||
from,
|
||||
fromEventPattern,
|
||||
interval,
|
||||
map,
|
||||
retry,
|
||||
startWith,
|
||||
switchMap,
|
||||
take,
|
||||
timer,
|
||||
} from "rxjs"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import {
|
||||
accountDetail$,
|
||||
accounts$,
|
||||
getAccountMapKey,
|
||||
getPublicKey,
|
||||
} from "../common/accounts.state"
|
||||
|
||||
export const CAccountId: EditAccountId = ({ value, onValueChanged }) => {
|
||||
const [localInput, setLocalInput] = useSynchronizeInput(
|
||||
@@ -81,7 +67,7 @@ export const CAccountId: EditAccountId = ({ value, onValueChanged }) => {
|
||||
className="bg-transparent outline-none w-full"
|
||||
aria-label="AccountId"
|
||||
displayValue={(address: string) =>
|
||||
accounts.get(address)?.name ?? address
|
||||
accounts.get(getAccountMapKey(address))?.name ?? address
|
||||
}
|
||||
onChange={(event) => setQuery(event.target.value)}
|
||||
/>
|
||||
@@ -138,11 +124,6 @@ const AccountOption: FC<{ account: string; publicKey: Uint8Array | null }> = ({
|
||||
)
|
||||
}
|
||||
|
||||
const getPublicKey = (address: string) => {
|
||||
const info = getSs58AddressInfo(address)
|
||||
return info.isValid ? info.publicKey : null
|
||||
}
|
||||
|
||||
const getValidationError = (value: string) => {
|
||||
if (value === "") return null
|
||||
const { isValid } = getSs58AddressInfo(value)
|
||||
@@ -153,56 +134,3 @@ const parseValue = (value: string): SS58String | NOTIN => {
|
||||
if (getValidationError(value)) return NOTIN
|
||||
return value
|
||||
}
|
||||
|
||||
const extensions$ = state(
|
||||
concat(
|
||||
timer(0, 100).pipe(
|
||||
map(getInjectedExtensions),
|
||||
filter((v) => v.length > 0),
|
||||
take(1),
|
||||
),
|
||||
interval(2000).pipe(map(getInjectedExtensions)),
|
||||
),
|
||||
[],
|
||||
)
|
||||
const extensionAccounts$ = combineKeys(extensions$, (extension) =>
|
||||
from(connectInjectedExtension(extension)).pipe(
|
||||
switchMap((extension) =>
|
||||
fromEventPattern<InjectedPolkadotAccount[]>(
|
||||
extension.subscribe,
|
||||
(_, fn) => fn(),
|
||||
).pipe(
|
||||
startWith(extension.getAccounts()),
|
||||
finalize(extension.disconnect),
|
||||
),
|
||||
),
|
||||
retry({
|
||||
delay: 100,
|
||||
}),
|
||||
),
|
||||
)
|
||||
const accounts$ = state(
|
||||
extensionAccounts$.pipe(
|
||||
map(
|
||||
(extensionAccounts) =>
|
||||
new Map(
|
||||
Array.from(extensionAccounts.entries()).flatMap(
|
||||
([extension, accounts]) =>
|
||||
accounts.map((account) => [
|
||||
account.address,
|
||||
{
|
||||
...account,
|
||||
extension,
|
||||
},
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
new Map<string, InjectedPolkadotAccount & { extension: string }>(),
|
||||
)
|
||||
|
||||
const accountDetail$ = state(
|
||||
(account: string) => accounts$.pipe(map((v) => v.get(account) ?? null)),
|
||||
null,
|
||||
)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { EditSequence, NOTIN } from "@codec-components"
|
||||
import { CirclePlus } from "lucide-react"
|
||||
import { twMerge as clsx } from "tailwind-merge"
|
||||
import { ListItem } from "../common/ListItem"
|
||||
import { useSubtreeFocus } from "../common/SubtreeFocus"
|
||||
import { ListItem } from "./ListItem"
|
||||
|
||||
export const CSequence: EditSequence = ({
|
||||
innerComponents,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { withDefault } from "@/utils/default"
|
||||
import { EditStr } from "@codec-components"
|
||||
|
||||
// TODO
|
||||
export const CStr: EditStr = ({ value, onValueChanged }) => {
|
||||
return (
|
||||
<div>
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import { ExpandBtn } from "@/components/Expand"
|
||||
import { useStateObservable } from "@react-rxjs/core"
|
||||
import { Trash2 } from "lucide-react"
|
||||
import { twMerge as clsx } from "tailwind-merge"
|
||||
import { Marker } from "../common/Markers"
|
||||
import { isCollapsed$, toggleCollapsed } from "../common/paths.state"
|
||||
|
||||
export const ListItem: React.FC<{
|
||||
idx: number
|
||||
children: React.ReactNode
|
||||
path: string[]
|
||||
onDelete?: () => void
|
||||
}> = ({ idx, onDelete, children, path }) => {
|
||||
const pathStr = path.join(".")
|
||||
const isCollapsed = useStateObservable(isCollapsed$(pathStr))
|
||||
|
||||
return (
|
||||
<li className="flex flex-col mb-1">
|
||||
<div className="flex items-stretch">
|
||||
<Marker id={path} />
|
||||
<span
|
||||
className="cursor-pointer flex items-center py-1 gap-1"
|
||||
onClick={() => toggleCollapsed(pathStr)}
|
||||
>
|
||||
<ExpandBtn expanded={!isCollapsed} />
|
||||
Item {idx + 1}.
|
||||
</span>
|
||||
{onDelete ? (
|
||||
<button
|
||||
className="cursor-pointer text-gray-200 ml-2 hover:text-polkadot-400"
|
||||
onClick={() => onDelete()}
|
||||
>
|
||||
<Trash2 size={16} />
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
<div
|
||||
className={clsx(
|
||||
"flex-row p-2 items-center border border-polkadot-700 rounded ",
|
||||
isCollapsed ? "hidden" : "",
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
@@ -90,7 +90,10 @@ export const CEnum: EditEnum = ({
|
||||
)
|
||||
}
|
||||
|
||||
const useAppendTitle = (container: HTMLElement | null, className: string) => {
|
||||
export const useAppendTitle = (
|
||||
container: HTMLElement | null,
|
||||
className: string,
|
||||
) => {
|
||||
const [element, setElement] = useState<HTMLElement | null>(null)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
|
||||
@@ -11,8 +11,8 @@ import {
|
||||
} from "@codec-components"
|
||||
import { FC, ReactNode, useState } from "react"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { ListItem } from "../common/ListItem"
|
||||
import { useSubtreeFocus } from "../common/SubtreeFocus"
|
||||
import { ListItem } from "./ListItem"
|
||||
|
||||
export const CBool: EditBool = ({ value, onValueChanged, path }) => {
|
||||
return (
|
||||
@@ -45,6 +45,7 @@ export const CBool: EditBool = ({ value, onValueChanged, path }) => {
|
||||
|
||||
export const CVoid: EditVoid = () => null
|
||||
|
||||
// TODO
|
||||
export const CEthAccount: EditEthAccount = ({ value }) => (
|
||||
<span>{withDefault(value, "")}</span>
|
||||
)
|
||||
@@ -87,6 +88,7 @@ export const COption: EditOption = ({ value, inner, onValueChanged }) => {
|
||||
)
|
||||
}
|
||||
|
||||
// TODO
|
||||
export const CResult: EditResult = ({ value, inner, type }) => {
|
||||
return type === "blank" ? null : (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user