Skip to content

Commit

Permalink
Merge pull request #132 from sinamics/table_sorting
Browse files Browse the repository at this point in the history
Improved member id and ip address sorting
  • Loading branch information
sinamics committed Sep 7, 2023
2 parents b031e6f + acfb706 commit 35e8e3c
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/components/modules/table/memberHeaderColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useModalStore } from "~/utils/store";
import toast from "react-hot-toast";
import { useTranslations } from "next-intl";
import TimeAgo from "react-timeago";
import { type ColumnDef, createColumnHelper } from "@tanstack/react-table";
import { type ColumnDef, createColumnHelper, Row } from "@tanstack/react-table";
import { type NetworkMemberNotation, type MemberEntity } from "~/types/local/member";

enum ConnectionStatus {
Expand All @@ -21,6 +21,41 @@ interface IProp {
central: boolean;
}

const sortingMemberHex = (
rowA: Row<MemberEntity>,
rowB: Row<MemberEntity>,
columnId: string,
): number => {
const a = rowA.original[columnId] as string;
const b = rowB.original[columnId] as string;
const numA = BigInt(`0x${a}`);
const numB = BigInt(`0x${b}`);

if (numA > numB) return 1;
if (numA < numB) return -1;
return 0;
};

const sortIP = (ip: string) => {
return ip
.split(".")
.map(Number)
.reduce((acc, val) => acc * 256 + val);
};

const sortingIpaddress = (
rowA: Row<MemberEntity>,
rowB: Row<MemberEntity>,
columnId: string,
): number => {
const a = rowA.original[columnId] as string[];
const b = rowB.original[columnId] as string[];
const numA = a.length ? sortIP(a[0]) : 0;
const numB = b.length ? sortIP(b[0]) : 0;

return numA - numB;
};

export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => {
const t = useTranslations();
const { callModal } = useModalStore((state) => state);
Expand Down Expand Up @@ -92,6 +127,7 @@ export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => {
columnHelper.accessor("name", {
header: () => <span>{t("networkById.networkMembersTable.column.name")}</span>,
id: "name",
sortingFn: sortingMemberHex,
}),
columnHelper.accessor("id", {
header: () => <span>{t("networkById.networkMembersTable.column.id")}</span>,
Expand All @@ -103,6 +139,7 @@ export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => {
<span>{t("networkById.networkMembersTable.column.ipAssignments.header")}</span>
),
id: "ipAssignments",
sortingFn: sortingIpaddress,
}),
columnHelper.accessor("creationTime", {
header: () => <span>{t("networkById.networkMembersTable.column.created")}</span>,
Expand Down

0 comments on commit 35e8e3c

Please sign in to comment.