From 67345cf1e34eb309fa0a1ba5eb1bbfe441fc61fd Mon Sep 17 00:00:00 2001 From: kunish Date: Sun, 17 Sep 2023 12:37:10 +0800 Subject: [PATCH] feat(logs): fuzzy filter, sorting --- src/pages/Connections.tsx | 5 +- src/pages/Logs.tsx | 97 ++++++++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/src/pages/Connections.tsx b/src/pages/Connections.tsx index 1fdc328..b2415b5 100644 --- a/src/pages/Connections.tsx +++ b/src/pages/Connections.tsx @@ -261,9 +261,6 @@ export default () => { get globalFilter() { return globalFilter() }, - get columnFilters() { - return [] - }, }, get data() { return activeTab() === ActiveTab.activeConnections @@ -420,7 +417,7 @@ export default () => { {(row) => ( - + {(cell) => { return ( diff --git a/src/pages/Logs.tsx b/src/pages/Logs.tsx index 89361ed..4537298 100644 --- a/src/pages/Logs.tsx +++ b/src/pages/Logs.tsx @@ -1,12 +1,22 @@ import { useI18n } from '@solid-primitives/i18n' -import { IconSettings } from '@tabler/icons-solidjs' +import { makePersisted } from '@solid-primitives/storage' +import { + IconSettings, + IconSortAscending, + IconSortDescending, +} from '@tabler/icons-solidjs' +import { rankItem } from '@tanstack/match-sorter-utils' import { ColumnDef, + FilterFn, + SortingState, createSolidTable, flexRender, getCoreRowModel, + getFilteredRowModel, + getSortedRowModel, } from '@tanstack/solid-table' -import { For, Index, createEffect, createSignal } from 'solid-js' +import { For, Index, createEffect, createMemo, createSignal } from 'solid-js' import { twMerge } from 'tailwind-merge' import { Button, LogsSettingsModal } from '~/components' import { LOG_LEVEL, MODAL } from '~/constants' @@ -16,10 +26,22 @@ import { Log } from '~/types' type LogWithSeq = Log & { seq: number } +const fuzzyFilter: FilterFn = (row, columnId, value, addMeta) => { + // Rank the item + const itemRank = rankItem(row.getValue(columnId), value) + + // Store the itemRank info + addMeta({ + itemRank, + }) + + // Return if the item should be filtered in/out + return itemRank.passed +} + export default () => { const [t] = useI18n() let seq = 1 - const [search, setSearch] = createSignal('') const [logs, setLogs] = createSignal([]) const logsData = useWsRequest('logs', { level: logLevel() }) @@ -36,13 +58,21 @@ export default () => { seq++ }) - const columns: ColumnDef[] = [ + const [globalFilter, setGlobalFilter] = createSignal('') + + const [sorting, setSorting] = makePersisted(createSignal([]), { + name: 'logsTableSorting', + storage: localStorage, + }) + + const columns = createMemo[]>(() => [ { header: t('sequence'), accessorFn: (row) => row.seq, }, { header: t('type'), + accessorFn: (row) => row.type, cell: ({ row }) => { const type = row.original.type as LOG_LEVEL @@ -70,17 +100,30 @@ export default () => { header: t('payload'), accessorFn: (row) => row.payload, }, - ] + ]) const table = createSolidTable({ - get data() { - return search() - ? logs().filter((log) => - log.payload.toLowerCase().includes(search().toLowerCase()), - ) - : logs() + filterFns: { + fuzzy: fuzzyFilter, }, - columns, + state: { + get globalFilter() { + return globalFilter() + }, + get sorting() { + return sorting() + }, + }, + get data() { + return logs() + }, + sortDescFirst: true, + columns: columns(), + onGlobalFilterChange: setGlobalFilter, + onSortingChange: setSorting, + globalFilterFn: fuzzyFilter, + getFilteredRowModel: getFilteredRowModel(), + getSortedRowModel: getSortedRowModel(), getCoreRowModel: getCoreRowModel(), }) @@ -91,7 +134,7 @@ export default () => { type="search" class="input join-item input-primary input-sm flex-1 flex-shrink-0 sm:input-md" placeholder={t('search')} - onInput={(e) => setSearch(e.target.value)} + onInput={(e) => setGlobalFilter(e.target.value)} />