Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved member id and ip address sorting #132

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading