metacubexd/src/pages/Logs.tsx

156 lines
4.1 KiB
TypeScript
Raw Normal View History

2023-09-02 00:15:19 +08:00
import { useI18n } from '@solid-primitives/i18n'
2023-09-01 22:07:02 +08:00
import {
ColumnDef,
createSolidTable,
flexRender,
getCoreRowModel,
} from '@tanstack/solid-table'
2023-09-10 19:03:26 +08:00
import { For, Index, createEffect, createSignal } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import { LOG_LEVEL } from '~/constants'
2023-09-06 19:33:07 +08:00
import { tableSize, tableSizeClassName, useWsRequest } from '~/signals'
import { logLevel, logMaxRows } from '~/signals/config'
2023-09-01 22:07:02 +08:00
import { Log } from '~/types'
type LogWithSeq = Log & { seq: number }
2023-08-24 04:20:53 +08:00
export default () => {
2023-09-02 00:15:19 +08:00
const [t] = useI18n()
2023-09-10 19:03:26 +08:00
let seq = 1
2023-08-24 04:20:53 +08:00
const [search, setSearch] = createSignal('')
2023-09-01 22:07:02 +08:00
const [logs, setLogs] = createSignal<LogWithSeq[]>([])
2023-08-24 04:20:53 +08:00
const logsData = useWsRequest<Log>('logs', { level: logLevel() })
const maxRows = logMaxRows()
2023-08-24 04:20:53 +08:00
createEffect(() => {
2023-09-06 19:33:07 +08:00
const data = logsData()
2023-08-24 04:20:53 +08:00
if (!data) {
return
}
setLogs((logs) => [{ ...data, seq }, ...logs].slice(0, maxRows))
2023-09-01 22:07:02 +08:00
seq++
})
const columns: ColumnDef<LogWithSeq>[] = [
{
header: t('sequence'),
2023-09-01 22:07:02 +08:00
accessorFn: (row) => row.seq,
},
{
header: t('type'),
2023-09-10 01:10:56 +08:00
cell: ({ row }) => {
const type = row.original.type as LOG_LEVEL
2023-09-10 01:10:56 +08:00
let className = ''
switch (type) {
case LOG_LEVEL.Error:
2023-09-10 01:10:56 +08:00
className = 'text-error'
break
case LOG_LEVEL.Warning:
2023-09-10 01:10:56 +08:00
className = 'text-warning'
break
case LOG_LEVEL.Info:
2023-09-10 01:10:56 +08:00
className = 'text-info'
break
case LOG_LEVEL.Debug:
className = 'text-success'
break
2023-09-10 01:10:56 +08:00
}
return <span class={className}>{`[${row.original.type}]`}</span>
},
2023-09-01 22:07:02 +08:00
},
{
header: t('payload'),
2023-09-01 22:07:02 +08:00
accessorFn: (row) => row.payload,
},
]
const table = createSolidTable({
get data() {
return search()
? logs().filter((log) =>
log.payload.toLowerCase().includes(search().toLowerCase()),
)
: logs()
},
columns,
getCoreRowModel: getCoreRowModel(),
2023-08-24 04:20:53 +08:00
})
return (
<div class="flex h-full flex-col gap-4 p-1">
2023-08-24 04:20:53 +08:00
<input
2023-09-10 16:42:00 +08:00
type="search"
2023-09-12 20:50:52 +08:00
class="input input-primary input-sm flex-shrink-0 sm:input-md"
2023-09-02 00:15:19 +08:00
placeholder={t('search')}
2023-08-24 04:20:53 +08:00
onInput={(e) => setSearch(e.target.value)}
/>
2023-09-05 00:20:19 +08:00
<div class="overflow-x-auto whitespace-nowrap rounded-md bg-base-300">
2023-09-06 03:08:18 +08:00
<table
class={twMerge(
tableSizeClassName(tableSize()),
'table relative rounded-none',
)}
2023-09-06 03:08:18 +08:00
>
2023-09-05 00:20:19 +08:00
<thead class="sticky top-0 z-10">
2023-09-10 19:03:26 +08:00
<Index each={table.getHeaderGroups()}>
{(keyedHeaderGroup) => {
const headerGroup = keyedHeaderGroup()
return (
<tr>
<Index each={headerGroup.headers}>
{(keyedHeader) => {
const header = keyedHeader()
return (
<th class="bg-base-300">
<div>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</div>
</th>
)
}}
</Index>
</tr>
)
}}
</Index>
2023-09-01 22:07:02 +08:00
</thead>
<tbody>
<For each={table.getRowModel().rows}>
{(row) => (
2023-09-05 00:20:19 +08:00
<tr class="hover">
2023-09-01 22:07:02 +08:00
<For each={row.getVisibleCells()}>
{(cell) => (
<td>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</td>
)}
</For>
</tr>
)}
</For>
</tbody>
</table>
2023-08-24 04:20:53 +08:00
</div>
</div>
)
}