metacubexd/src/pages/Logs.tsx

126 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-08-24 04:20:53 +08:00
import { createEventSignal } from '@solid-primitives/event-listener'
2023-09-02 00:15:19 +08:00
import { useI18n } from '@solid-primitives/i18n'
2023-08-24 04:20:53 +08:00
import { createReconnectingWS } from '@solid-primitives/websocket'
2023-09-01 22:07:02 +08:00
import {
ColumnDef,
createSolidTable,
flexRender,
getCoreRowModel,
} from '@tanstack/solid-table'
2023-08-24 04:20:53 +08:00
import { For, createEffect, createSignal } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import { secret, tableSize, wsEndpointURL } from '~/signals'
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-01 22:07:02 +08:00
let seq = 0
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 ws = createReconnectingWS(`${wsEndpointURL()}/logs?token=${secret()}`)
2023-08-24 04:20:53 +08:00
const messageEvent = createEventSignal<{
message: WebSocketEventMap['message']
}>(ws, 'message')
createEffect(() => {
const data = messageEvent()?.data
if (!data) {
return
}
setLogs((logs) =>
2023-09-01 22:07:02 +08:00
[{ ...(JSON.parse(data) as Log), seq }, ...logs].slice(0, 100),
2023-08-24 04:20:53 +08:00
)
2023-09-01 22:07:02 +08:00
seq++
})
const columns: ColumnDef<LogWithSeq>[] = [
{
accessorKey: 'Sequence',
accessorFn: (row) => row.seq,
},
{
accessorKey: 'Type',
accessorFn: (row) => row.type,
},
{
accessorKey: 'Payload',
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 (
2023-09-05 00:20:19 +08:00
<div class="flex h-full flex-col gap-4 overflow-y-auto p-1">
2023-08-24 04:20:53 +08:00
<input
2023-09-05 00:20:19 +08:00
class="input input-primary flex-shrink-0"
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">
<table class={twMerge(tableSize(), 'table relative rounded-none')}>
2023-09-05 00:20:19 +08:00
<thead class="sticky top-0 z-10">
2023-09-01 22:07:02 +08:00
<For each={table.getHeaderGroups()}>
{(headerGroup) => (
<tr>
<For each={headerGroup.headers}>
{(header) => (
<th class="bg-base-300">
<div>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</div>
</th>
)}
</For>
</tr>
)}
</For>
</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>
)
}