feat: logs pause (#855)

This commit is contained in:
YetAnotherZephyruso 2024-07-24 11:51:25 +08:00 committed by GitHub
parent d6ec561b81
commit 776d8f8daf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 29 deletions

View File

@ -1,5 +1,7 @@
import { makePersisted } from '@solid-primitives/storage'
import {
IconPlayerPause,
IconPlayerPlay,
IconSettings,
IconSortAscending,
IconSortDescending,
@ -19,16 +21,9 @@ import { twMerge } from 'tailwind-merge'
import { Button, LogsSettingsModal } from '~/components'
import { LOG_LEVEL } from '~/constants'
import { useI18n } from '~/i18n'
import {
endpoint,
logsTableSize,
tableSizeClassName,
useWsRequest,
} from '~/signals'
import { logLevel, logMaxRows } from '~/signals/config'
import { Log } from '~/types'
type LogWithSeq = Log & { seq: number }
import { endpoint, logsTableSize, tableSizeClassName } from '~/signals'
import { useLogs } from '~/signals/logs'
import { LogWithSeq } from '~/types'
const fuzzyFilter: FilterFn<LogWithSeq> = (row, columnId, value, addMeta) => {
// Rank the item
@ -53,27 +48,9 @@ export default () => {
}
let logsSettingsModalRef: HTMLDialogElement | undefined
const [t] = useI18n()
let seq = 1
const [logs, setLogs] = createSignal<LogWithSeq[]>([])
const logsData = useWsRequest<Log>('logs', { level: logLevel() })
createEffect(() => {
const data = logsData()
if (!data) {
return
}
setLogs((logs) => [{ ...data, seq }, ...logs].slice(0, logMaxRows()))
seq++
})
const [globalFilter, setGlobalFilter] = createSignal('')
const { logs, paused, setPaused } = useLogs()
const [sorting, setSorting] = makePersisted(createSignal<SortingState>([]), {
name: 'logsTableSorting',
@ -152,6 +129,11 @@ export default () => {
onInput={(e) => setGlobalFilter(e.target.value)}
/>
<Button
class="join-item btn-sm sm:btn-md"
onClick={() => setPaused((paused) => !paused)}
icon={paused() ? <IconPlayerPlay /> : <IconPlayerPause />}
/>
<Button
class="join-item btn-sm sm:btn-md"
onClick={() => logsSettingsModalRef?.showModal()}

28
src/signals/logs.ts Normal file
View File

@ -0,0 +1,28 @@
import { useWsRequest } from '~/signals'
import { logLevel, logMaxRows } from '~/signals/config'
import { Log, LogWithSeq } from '~/types'
let seq = 1
const [logs, setLogs] = createSignal<LogWithSeq[]>([])
const logsData = useWsRequest<Log>('logs', { level: logLevel() })
const [paused, setPaused] = createSignal(false)
createEffect(() => {
const data = logsData()
if (!data || paused()) {
return
}
setLogs((logs) => [{ ...data, seq }, ...logs].slice(0, logMaxRows()))
seq++
})
export function useLogs() {
return {
logs,
paused,
setPaused,
}
}

View File

@ -113,6 +113,8 @@ export type Log = {
payload: string
}
export type LogWithSeq = Log & { seq: number }
export type Config = {
mode: 'global' | 'rule' | 'direct'
port: number