Skip to content

Commit

Permalink
feat(connections): add sorting and close connection support
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish committed Aug 27, 2023
1 parent 5f72936 commit 45c9740
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions src/pages/Connections.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { createEventSignal } from '@solid-primitives/event-listener'
import { createReconnectingWS } from '@solid-primitives/websocket'
import {
IconSortAscending,
IconSortDescending,
IconX,
} from '@tabler/icons-solidjs'
import {
ColumnDef,
SortingState,
createSolidTable,
flexRender,
getCoreRowModel,
getSortedRowModel,
} from '@tanstack/solid-table'
import byteSize from 'byte-size'
import { isIPv6 } from 'is-ip'
import { For, createSignal } from 'solid-js'
import { secret, wsEndpointURL } from '~/signals'
import { twMerge } from 'tailwind-merge'
import { secret, useRequest, wsEndpointURL } from '~/signals'
import type { Connection } from '~/types'

export const Connections = () => {
const request = useRequest()
const [search, setSearch] = createSignal('')

const ws = createReconnectingWS(
Expand All @@ -35,7 +44,34 @@ export const Connections = () => {
).connections.slice(-100)
}

const onCloseConnection = (id: string) => request.delete(`connections/${id}`)

const [sorting, setSorting] = createSignal<SortingState>([])

const columns: ColumnDef<Connection>[] = [
{
id: 'close',
header: () => (
<div class="flex h-full items-center">
<button
class="btn btn-circle btn-outline btn-xs"
onClick={() => request.delete('connections')}
>
<IconX />
</button>
</div>
),
cell: ({ row }) => (
<div class="flex h-full items-center">
<button
class="btn btn-circle btn-outline btn-xs"
onClick={() => onCloseConnection(row.id)}
>
<IconX />
</button>
</div>
),
},
{
accessorKey: 'ID',
accessorFn: (row) => row.id,
Expand Down Expand Up @@ -93,6 +129,11 @@ export const Connections = () => {
]

const table = createSolidTable({
state: {
get sorting() {
return sorting()
},
},
get data() {
return search()
? connections().filter((connection) =>
Expand All @@ -105,6 +146,8 @@ export const Connections = () => {
: connections()
},
columns,
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
getCoreRowModel: getCoreRowModel(),
})

Expand All @@ -125,12 +168,26 @@ export const Connections = () => {
<For each={headerGroup.headers}>
{(header) => (
<th>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
<div
class={twMerge(
'flex items-center justify-between',
header.column.getCanSort() &&
'cursor-pointer select-none',
)}
onClick={header.column.getToggleSortingHandler()}
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}

{{
asc: <IconSortAscending />,
desc: <IconSortDescending />,
}[header.column.getIsSorted() as string] ?? null}
</div>
</th>
)}
</For>
Expand Down

0 comments on commit 45c9740

Please sign in to comment.