Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151<script lang="ts" module> /** * A column of a {@link SortableTable}. `value` is the row's sort key for this * column — the rendered cell comes from the caller's `cell` snippet, so the * two are kept in step by declaring the column once and using it for both. */ export type SortableColumn<Row> = { key: string label: string value: (row: Row) => string | number | undefined // right-align the column and start it descending (counts read best largest-first) numeric?: boolean }
export type SortState = { key: string; dir: 'asc' | 'desc' }
// The sort lives in the query string (`?sort=key` / `?sort=-key`) so a sorted // table can be linked, bookmarked and reloaded, and so the headers work as // plain links before hydration. function parse_sort(raw: string | null): SortState | undefined { if (!raw) return undefined return raw.startsWith('-') ? { key: raw.slice(1), dir: 'desc' } : { key: raw, dir: 'asc' } }
function format_sort(sort: SortState): string { return sort.dir === 'desc' ? `-${sort.key}` : sort.key }
// Missing values sort to the bottom whichever way the column points, so blanks // never crowd out the rows you're looking for. function compare( a: string | number | undefined, b: string | number | undefined, dir: number ): number { const a_empty = a === undefined || a === '' const b_empty = b === undefined || b === '' if (a_empty || b_empty) return a_empty && b_empty ? 0 : a_empty ? 1 : -1 const order = typeof a === 'number' && typeof b === 'number' ? a - b : String(a).localeCompare(String(b)) return order * dir }</script>
<script lang="ts" generics="Row"> import type { Snippet } from 'svelte' import { page } from '$app/state'
let { columns, rows, id, cell, default_sort, param = 'sort', label }: { columns: SortableColumn<Row>[] rows: Row[] id: (row: Row) => string cell: Snippet<[Row, SortableColumn<Row>]> // where the table starts, and the sort left out of the query string default_sort?: SortState // query parameter name, so two tables can share a page param?: string label: string } = $props()
const fallback = $derived<SortState>( default_sort ?? { key: columns[0]?.key ?? '', dir: 'asc' } )
// an unknown or misspelt key in the URL falls back rather than showing // arbitrarily ordered rows with no header marked as sorted const sort = $derived.by<SortState>(() => { const requested = parse_sort(page.url.searchParams.get(param)) if (!requested || !columns.some((c) => c.key === requested.key)) { return fallback } return requested })
const ordered = $derived.by(() => { const column = columns.find((c) => c.key === sort.key) if (!column) return rows const dir = sort.dir === 'asc' ? 1 : -1 return [...rows].sort((a, b) => compare(column.value(a), column.value(b), dir) ) })
// clicking the sorted column flips it; any other column starts fresh function next_sort(column: SortableColumn<Row>): SortState { if (sort.key === column.key) { return { key: sort.key, dir: sort.dir === 'asc' ? 'desc' : 'asc' } } return { key: column.key, dir: column.numeric ? 'desc' : 'asc' } }
function sort_href(column: SortableColumn<Row>): string { const next = next_sort(column) // eslint-disable-next-line svelte/prefer-svelte-reactivity -- throwaway copy used to build a string, never reactive state const params = new URLSearchParams(page.url.search) // the default sort is the bare URL, so links stay clean if (next.key === fallback.key && next.dir === fallback.dir) { params.delete(param) } else { params.set(param, format_sort(next)) } const query = params.toString() return query ? `?${query}` : page.url.pathname }</script>
<table aria-label={label}> <thead> <tr> {#each columns as column (column.key)} <th class:num={column.numeric} aria-sort={sort.key !== column.key ? 'none' : sort.dir === 'asc' ? 'ascending' : 'descending'} > <a class="sort" href={sort_href(column)} data-sveltekit-reset="false"> {column.label} <span class="arrow" aria-hidden="true"> {#if sort.key === column.key}{sort.dir === 'asc' ? '▲' : '▼'}{/if} </span> </a> </th> {/each} </tr> </thead> <tbody> {#each ordered as row (id(row))} <tr> {#each columns as column (column.key)} <td class:num={column.numeric}>{@render cell(row, column)}</td> {/each} </tr> {/each} </tbody></table>