Files
co-lite/solvway-admin-portal/app/components/ui/sortable-head.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

92 lines
3.3 KiB
TypeScript

import type { ReactNode } from "react";
import type { SortingState } from "@tanstack/react-table";
import { ArrowDown, ArrowUp, ChevronsUpDown } from "lucide-react";
import { TableHead } from "@/app/components/ui/table";
import { cn } from "@/app/lib/utils";
export type SortDirection = false | "asc" | "desc";
/** The header sort button shared by DataTable and SortableHead. */
export function SortButton({
direction,
numeric,
onClick,
children,
}: {
direction: SortDirection;
numeric?: boolean;
onClick: React.MouseEventHandler<HTMLButtonElement> | undefined;
children: ReactNode;
}) {
return (
<button
type="button"
onClick={onClick}
className={cn(
// Dusty blue like the rest of the gold header row; the sorted column is told apart by
// its arrow and bold weight, not by a colour change. `uppercase` again because Tailwind's
// preflight resets text-transform on buttons, so sortable heads rendered in title case
// beside the uppercase plain heads (feedback 2026-10-26).
"inline-flex cursor-pointer items-center gap-1.5 rounded-md px-1 py-2 uppercase text-table-head-foreground underline-offset-4 hover:underline focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none",
direction && "font-bold",
numeric && "flex-row-reverse",
)}
>
{children}
{direction === "asc" ? (
<ArrowUp aria-hidden="true" className="size-3.5" />
) : direction === "desc" ? (
<ArrowDown aria-hidden="true" className="size-3.5" />
) : (
<ChevronsUpDown aria-hidden="true" className="size-3.5 opacity-50" />
)}
</button>
);
}
export const ariaSort = (d: SortDirection) =>
d === "asc" ? "ascending" : d === "desc" ? "descending" : "none";
/** Sortable header cell for hand-built tables; cycles none -> asc -> desc -> none. */
export function SortableHead({
label,
sortKey,
sorting,
onSortingChange,
className,
}: {
label: ReactNode;
sortKey: string;
sorting: SortingState;
onSortingChange: (sorting: SortingState) => void;
className?: string;
}) {
const current = sorting[0]?.id === sortKey ? sorting[0] : undefined;
const direction: SortDirection = current ? (current.desc ? "desc" : "asc") : false;
const next: SortingState = !current ? [{ id: sortKey, desc: false }] : current.desc ? [] : [{ id: sortKey, desc: true }];
return (
<TableHead aria-sort={ariaSort(direction)} className={className}>
<SortButton direction={direction} onClick={() => onSortingChange(next)}>
{label}
</SortButton>
</TableHead>
);
}
type SortValue = string | number | null | undefined;
/** Sorts rows in the browser by the active sort; numbers numerically, text with localeCompare, empties last. */
export function sortRows<T>(rows: T[], sorting: SortingState, getters: Record<string, (row: T) => SortValue>): T[] {
const s = sorting[0];
const get = s && getters[s.id];
if (!get) return rows;
const dir = s.desc ? -1 : 1;
return [...rows].sort((a, b) => {
const x = get(a);
const y = get(b);
if (x == null || x === "") return y == null || y === "" ? 0 : 1;
if (y == null || y === "") return -1;
return dir * (typeof x === "number" && typeof y === "number" ? x - y : String(x).localeCompare(String(y)));
});
}