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

Fixed members table sorting icon alignment #501

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
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
111 changes: 19 additions & 92 deletions src/components/networkByIdPage/table/memberHeaderColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { api } from "~/utils/api";
import { useModalStore } from "~/utils/store";
import { useTranslations } from "next-intl";
import TimeAgo from "react-timeago";
import { type ColumnDef, createColumnHelper, Row } from "@tanstack/react-table";
import { type ColumnDef, createColumnHelper } from "@tanstack/react-table";
import { type NetworkMemberNotation, type MemberEntity } from "~/types/local/member";
import {
useTrpcApiErrorHandler,
useTrpcApiSuccessHandler,
} from "~/hooks/useTrpcApiHandler";
import cn from "classnames";
import {
sortingIpAddress,
sortingMemberHex,
sortingPhysicalIpAddress,
} from "~/utils/sorting";

enum ConnectionStatus {
Offline = 0,
Expand All @@ -26,93 +31,6 @@ interface IProp {
organizationId?: string;
}

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 = a ? BigInt(`0x${a}`) : a;
const numB = b ? BigInt(`0x${b}`) : b;

if (numA > numB) return 1;
if (numA < numB) return -1;
return 0;
};
const hexToBigInt = (hex: string) => BigInt(`0x${hex}`);
const sortIP = (ip: string) => {
if (!ip) return BigInt(0);

if (ip.includes(":")) {
const fullAddress = ip
.split(":")
.map((hex) => hex.padStart(4, "0"))
.join("");
return hexToBigInt(fullAddress);
}
return BigInt(
ip
.split(".")
.map(Number)
.reduce((acc, val) => acc * 256 + val),
);
};
const sortingPhysicalIpAddress = (
rowA: Row<MemberEntity>,
rowB: Row<MemberEntity>,
): number => {
const stripPort = (ip: string) => ip.split("/")[0];
const a = rowA.original.peers?.physicalAddress;
const b = rowB.original?.peers?.physicalAddress;

const convertToBigInt = (value: string | string[] | undefined): bigint => {
if (Array.isArray(value)) {
return value.length ? sortIP(stripPort(value[0])) : BigInt(0);
}
return value?.length ? sortIP(stripPort(value)) : BigInt(0);
};

const numA = convertToBigInt(a);
const numB = convertToBigInt(b);

if (numA > numB) return 1;
if (numA < numB) return -1;
return 0;
};
const sortingIpAddress = (
rowA: Row<MemberEntity>,
rowB: Row<MemberEntity>,
columnId?: string,
): number => {
const stripPort = (ip: string) => ip.split("/")[0];
let a: string | string[] | undefined;
let b: string | string[] | undefined;

if (columnId) {
a = rowA.original[columnId] as string | string[];
b = rowB.original[columnId] as string | string[];
} else {
a = rowA.original.peers?.physicalAddress;
b = rowB.original?.peers?.physicalAddress;
}

const convertToBigInt = (value: string | string[] | undefined): bigint => {
if (Array.isArray(value)) {
return value.length ? sortIP(stripPort(value[0])) : BigInt(0);
}
return value?.length ? sortIP(stripPort(value)) : BigInt(0);
};

const numA = convertToBigInt(a);
const numB = convertToBigInt(b);

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

export const MemberHeaderColumns = ({ nwid, central = false, organizationId }: IProp) => {
const b = useTranslations("commonButtons");
const c = useTranslations("commonTable");
Expand Down Expand Up @@ -234,7 +152,12 @@ export const MemberHeaderColumns = ({ nwid, central = false, organizationId }: I
},
}),
columnHelper.accessor("name", {
header: () => <span className="text-left block">{c("header.name")}</span>,
header: () => <span>{c("header.name")}</span>,
meta: {
style: {
textAlign: "left",
},
},
id: "name",
}),
columnHelper.accessor("id", {
Expand All @@ -244,9 +167,12 @@ export const MemberHeaderColumns = ({ nwid, central = false, organizationId }: I
cell: (info) => info.getValue(),
}),
columnHelper.accessor("ipAssignments", {
header: () => (
<span className="text-left block">{c("header.ipAssignments.header")}</span>
),
header: () => <span>{c("header.ipAssignments.header")}</span>,
meta: {
style: {
textAlign: "left",
},
},
id: "ipAssignments",
sortingFn: sortingIpAddress,
}),
Expand Down Expand Up @@ -431,6 +357,7 @@ export const MemberHeaderColumns = ({ nwid, central = false, organizationId }: I
columnHelper.accessor("action", {
header: () => <span>{c("header.actions")}</span>,
id: "action",
enableSorting: false,
cell: ({ row: { original } }) => {
return (
<div className="space-x-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const NetworkMembersTable = ({ nwid, central = false, organizationId }: I
key={header.id}
colSpan={header.colSpan}
className="bg-base-300/50 p-2"
align={header.column.columnDef.meta?.style?.textAlign}
>
{header.isPlaceholder ? null : (
<div
Expand Down
9 changes: 9 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import "@tanstack/react-table";
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/no-empty-interface */
// Use type safe message keys with `next-intl`
type Messages = typeof import("./locales/en/common.json");
declare type IntlMessages = Messages;

declare module "@tanstack/table-core" {
interface ColumnMeta<TData extends RowData, TValue> {
style: {
textAlign: "left" | "center" | "right";
};
}
}
89 changes: 89 additions & 0 deletions src/utils/sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { type Row } from "@tanstack/react-table";
import { MemberEntity } from "~/types/local/member";

export 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 = a ? BigInt(`0x${a}`) : a;
const numB = b ? BigInt(`0x${b}`) : b;

if (numA > numB) return 1;
if (numA < numB) return -1;
return 0;
};
export const hexToBigInt = (hex: string) => BigInt(`0x${hex}`);
export const sortIP = (ip: string) => {
if (!ip) return BigInt(0);

if (ip.includes(":")) {
const fullAddress = ip
.split(":")
.map((hex) => hex.padStart(4, "0"))
.join("");
return hexToBigInt(fullAddress);
}
return BigInt(
ip
.split(".")
.map(Number)
.reduce((acc, val) => acc * 256 + val),
);
};
export const sortingPhysicalIpAddress = (
rowA: Row<MemberEntity>,
rowB: Row<MemberEntity>,
): number => {
const stripPort = (ip: string) => ip.split("/")[0];
const a = rowA.original.peers?.physicalAddress;
const b = rowB.original?.peers?.physicalAddress;

const convertToBigInt = (value: string | string[] | undefined): bigint => {
if (Array.isArray(value)) {
return value.length ? sortIP(stripPort(value[0])) : BigInt(0);
}
return value?.length ? sortIP(stripPort(value)) : BigInt(0);
};

const numA = convertToBigInt(a);
const numB = convertToBigInt(b);

if (numA > numB) return 1;
if (numA < numB) return -1;
return 0;
};
export const sortingIpAddress = (
rowA: Row<MemberEntity>,
rowB: Row<MemberEntity>,
columnId?: string,
): number => {
const stripPort = (ip: string) => ip.split("/")[0];
let a: string | string[] | undefined;
let b: string | string[] | undefined;

if (columnId) {
a = rowA.original[columnId] as string | string[];
b = rowB.original[columnId] as string | string[];
} else {
a = rowA.original.peers?.physicalAddress;
b = rowB.original?.peers?.physicalAddress;
}

const convertToBigInt = (value: string | string[] | undefined): bigint => {
if (Array.isArray(value)) {
return value.length ? sortIP(stripPort(value[0])) : BigInt(0);
}
return value?.length ? sortIP(stripPort(value)) : BigInt(0);
};

const numA = convertToBigInt(a);
const numB = convertToBigInt(b);

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