Remove follow and show recent logs first

This commit is contained in:
ekzyis 2024-06-24 14:36:13 +02:00
parent 61be80446d
commit fd08356d37
1 changed files with 4 additions and 57 deletions

View File

@ -1,8 +1,5 @@
import { useRouter } from 'next/router'
import LogMessage from './log-message' import LogMessage from './log-message'
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react' import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
import { Checkbox, Form } from './form'
import { useField } from 'formik'
import styles from '@/styles/log.module.css' import styles from '@/styles/log.module.css'
import { Button } from 'react-bootstrap' import { Button } from 'react-bootstrap'
import { useToast } from './toast' import { useToast } from './toast'
@ -12,71 +9,21 @@ import { getWalletByName } from './wallet'
import { gql, useMutation, useQuery } from '@apollo/client' import { gql, useMutation, useQuery } from '@apollo/client'
import { useMe } from './me' import { useMe } from './me'
const FollowCheckbox = ({ value, ...props }) => {
const [,, helpers] = useField(props.name)
useEffect(() => {
helpers.setValue(value)
}, [value])
return (
<Checkbox {...props} />
)
}
export function WalletLogs ({ wallet, embedded }) { export function WalletLogs ({ wallet, embedded }) {
const logs = useWalletLogs(wallet) const logs = useWalletLogs(wallet)
const router = useRouter()
const { follow: defaultFollow } = router.query
const [follow, setFollow] = useState(defaultFollow ?? true)
const tableRef = useRef() const tableRef = useRef()
const scrollY = useRef()
const showModal = useShowModal() const showModal = useShowModal()
useEffect(() => {
if (follow) {
tableRef.current?.scroll({ top: tableRef.current.scrollHeight, behavior: 'smooth' })
}
}, [logs, follow])
useEffect(() => {
function onScroll (e) {
const y = e.target.scrollTop
const down = y - scrollY.current >= -1
if (!!scrollY.current && !down) {
setFollow(false)
}
const maxY = e.target.scrollHeight - e.target.clientHeight
const dY = maxY - y
const isBottom = dY >= -1 && dY <= 1
if (isBottom) {
setFollow(true)
}
scrollY.current = y
}
tableRef.current?.addEventListener('scroll', onScroll)
return () => tableRef.current?.removeEventListener('scroll', onScroll)
}, [])
return ( return (
<> <>
<div className='d-flex w-100 align-items-center mb-3'> <div className='d-flex w-100 align-items-center mb-3'>
<Form initial={{ follow: true }}>
<FollowCheckbox
label='follow logs' name='follow' value={follow}
handleChange={setFollow} groupClassName='mb-0'
/>
</Form>
<span <span
style={{ cursor: 'pointer' }} style={{ cursor: 'pointer' }}
className='text-muted fw-bold nav-link' onClick={() => { className='text-muted fw-bold nav-link ms-auto' onClick={() => {
showModal(onClose => <DeleteWalletLogsObstacle wallet={wallet} onClose={onClose} />) showModal(onClose => <DeleteWalletLogsObstacle wallet={wallet} onClose={onClose} />)
}} }}
>clear >clear logs
</span> </span>
</div> </div>
<div ref={tableRef} className={`${styles.logTable} ${embedded ? styles.embedded : ''}`}> <div ref={tableRef} className={`${styles.logTable} ${embedded ? styles.embedded : ''}`}>
@ -236,7 +183,7 @@ export const WalletLoggerProvider = ({ children }) => {
logs = logs.filter(({ id }) => !existingIds.includes(id)) logs = logs.filter(({ id }) => !existingIds.includes(id))
} }
// sort oldest first to keep same order as logs are appended // sort oldest first to keep same order as logs are appended
return [...prevLogs, ...logs].sort((a, b) => a.ts - b.ts) return [...prevLogs, ...logs].sort((a, b) => b.ts - a.ts)
}) })
} }
@ -255,7 +202,7 @@ export const WalletLoggerProvider = ({ children }) => {
const appendLog = useCallback((wallet, level, message) => { const appendLog = useCallback((wallet, level, message) => {
const log = { wallet: wallet.name, level, message, ts: +new Date() } const log = { wallet: wallet.name, level, message, ts: +new Date() }
saveLog(log) saveLog(log)
setLogs((prevLogs) => [...prevLogs, log]) setLogs((prevLogs) => [log, ...prevLogs])
}, [saveLog]) }, [saveLog])
const deleteLogs = useCallback(async (wallet) => { const deleteLogs = useCallback(async (wallet) => {