import { useRef, useLayoutEffect, ReactNode, FC, useState, useCallback } from 'react' import { IconProps, Notepad, Prohibit } from 'phosphor-react' import useStayScrolled from 'react-stay-scrolled' import NextLink from 'next/link' import Container from './Container' import LogText from './LogText' import state, { ILog } from '../state' import { Pre, Link, Heading, Button, Text, Flex, Box } from '.' import regexifyString from 'regexify-string' import { useSnapshot } from 'valtio' import { AccountDialog } from './Accounts' interface ILogBox { title: string clearLog?: () => void logs: ILog[] renderNav?: () => ReactNode enhanced?: boolean Icon?: FC } const LogBox: FC = ({ title, clearLog, logs, children, renderNav, enhanced, Icon = Notepad }) => { const logRef = useRef(null) const { stayScrolled /*, scrollBottom*/ } = useStayScrolled(logRef) useLayoutEffect(() => { stayScrolled() }, [stayScrolled, logs]) return ( {title} {renderNav?.()} {clearLog && ( )} {logs?.map((log, index) => ( ))} {children} ) } export const Log: FC = ({ type, timestring, message: _message, link, linkText, defaultCollapsed, jsonData: _jsonData }) => { const [expanded, setExpanded] = useState(!defaultCollapsed) const { accounts } = useSnapshot(state) const [dialogAccount, setDialogAccount] = useState(null) const enrichAccounts = useCallback( (str?: string): ReactNode => { if (!str || !accounts.length) return str const pattern = `(${accounts.map(acc => acc.address).join('|')})` const res = regexifyString({ pattern: new RegExp(pattern, 'gim'), decorator: (match, idx) => { const name = accounts.find(acc => acc.address === match)?.name return ( setDialogAccount(match)} title={match} highlighted > {name || match} ) }, input: str }) return <>{res} }, [accounts] ) let message: ReactNode if (typeof _message === 'string') { _message = _message.trim().replace(/\n /gi, '\n') if (_message) message = enrichAccounts(_message) else message = {'""'} } else { message = _message } const jsonData = enrichAccounts(_jsonData) return ( <> {timestring && ( {timestring}{' '} )}
{message} 
{link && ( {linkText} )} {jsonData && ( setExpanded(!expanded)} as="a"> {expanded ? 'Collapse' : 'Expand'} )} {expanded && jsonData &&
{jsonData}
}

) } export default LogBox