import type { RenderableProps } from 'preact'; import React from 'preact/compat'; import { useLayoutEffect } from 'preact/hooks'; import { useMemo } from 'preact/hooks'; import { useRef } from 'preact/hooks'; import { useEffect } from 'preact/hooks'; import { useState } from 'preact/hooks'; import type { Account, GetAccounts } from '../LedgerWallet.js'; import { getLangText } from './lang.js'; export type SelectAccountProps = RenderableProps<{ accounts: Account[]; selectedIndex: number; onCancel: () => void; onConfirm: (account: Account) => void; getAccounts: GetAccounts; }>; export function SelectAccount(props: SelectAccountProps) { const [index, setIndex] = useState(0); const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(false); const loadBtnRef = useRef(null); const langText = useMemo(() => getLangText(), []); useEffect(() => { setAccounts([...props.accounts]); }, [props.accounts]); useEffect(() => { setIndex(props.selectedIndex); }, [props.selectedIndex]); function onInput(e: any) { setIndex(+e.target.value); } function onConfirm() { const selected = accounts.find((item) => item.index === index); props.onConfirm(selected || accounts?.[0]); } function onCancel() { props.onCancel(); } async function onLoadMore() { setLoading(true); const last = accounts[accounts.length - 1] || { index: -1 }; const from = last.index + 1; const to = last.index + 6; try { const result = await props.getAccounts(from, to); setAccounts((accounts) => [...accounts, ...result]); } finally { setLoading(false); } } useLayoutEffect(() => { loadBtnRef.current?.scrollIntoView?.(); }, [accounts]); return (
{langText.selectTip}
    {accounts.map((account, idx) => { return (
  • ); })}
); }