From e9db884fc5c33f43b30c296190183ac7754bb5da Mon Sep 17 00:00:00 2001 From: Sianida26 Date: Tue, 23 Jan 2024 00:08:51 +0700 Subject: [PATCH] Added base user lsit --- .../users/_tables/UsersTable/UsersTable.tsx | 15 ++-- .../users/_tables/UsersTable/columns.ts | 29 ------- .../users/_tables/UsersTable/columns.tsx | 76 +++++++++++++++++++ src/utils/stringToColorHex.ts | 26 +++++++ 4 files changed, 111 insertions(+), 35 deletions(-) delete mode 100644 src/app/dashboard/(auth)/users/_tables/UsersTable/columns.ts create mode 100644 src/app/dashboard/(auth)/users/_tables/UsersTable/columns.tsx create mode 100644 src/utils/stringToColorHex.ts diff --git a/src/app/dashboard/(auth)/users/_tables/UsersTable/UsersTable.tsx b/src/app/dashboard/(auth)/users/_tables/UsersTable/UsersTable.tsx index a3e53f4..98f0184 100644 --- a/src/app/dashboard/(auth)/users/_tables/UsersTable/UsersTable.tsx +++ b/src/app/dashboard/(auth)/users/_tables/UsersTable/UsersTable.tsx @@ -7,7 +7,7 @@ import { useReactTable, } from "@tanstack/react-table"; import columns, { UserRow } from "./columns"; -import { Table, TableThead } from "@mantine/core"; +import { Table, TableThead, Text } from "@mantine/core"; import { showErrorNotification } from "@/utils/notifications"; interface Props { @@ -20,16 +20,19 @@ export default function UsersTable({users}: Props) { data: users, columns, getCoreRowModel: getCoreRowModel(), + defaultColumn: { + cell: (props) => {props.getValue() as React.ReactNode} + } }); return ( - +
{/* Thead */} {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( - + {header.isPlaceholder ? null : flexRender( @@ -47,12 +50,12 @@ export default function UsersTable({users}: Props) { {table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( - + ))} ))} diff --git a/src/app/dashboard/(auth)/users/_tables/UsersTable/columns.ts b/src/app/dashboard/(auth)/users/_tables/UsersTable/columns.ts deleted file mode 100644 index 2ac4aaf..0000000 --- a/src/app/dashboard/(auth)/users/_tables/UsersTable/columns.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { createColumnHelper } from "@tanstack/react-table"; - -export interface UserRow { - id: string, - name: string | null, - email: string | null, - photoUrl: string | null, -} - -const columnHelper = createColumnHelper() - -const columns = [ - columnHelper.display({ - id: "seequence", - header: "#", - cell: props => props.row.index + 1 - }), - - columnHelper.accessor('name', {}), - - columnHelper.accessor('email', {}), - - columnHelper.display({ - id: 'actions', - cell: () => 'actions' - }) -]; - -export default columns; diff --git a/src/app/dashboard/(auth)/users/_tables/UsersTable/columns.tsx b/src/app/dashboard/(auth)/users/_tables/UsersTable/columns.tsx new file mode 100644 index 0000000..20a0292 --- /dev/null +++ b/src/app/dashboard/(auth)/users/_tables/UsersTable/columns.tsx @@ -0,0 +1,76 @@ +import { createColumnHelper } from "@tanstack/react-table"; +import Link from "next/link"; +import { ActionIcon, Anchor, Avatar, Badge, Flex, Group, Text, Tooltip } from "@mantine/core"; +import { TbEye, TbPencil, TbTrash } from "react-icons/tb"; +import stringToColorHex from "@/utils/stringToColorHex"; + +export interface UserRow { + id: string, + name: string | null, + email: string | null, + photoUrl: string | null, +} + +const columnHelper = createColumnHelper() + +const columns = [ + columnHelper.display({ + id: "seequence", + header: "#", + cell: props => props.row.index + 1, + size: 1 + }), + + columnHelper.accessor('name', { + header: "Name", + cell: (props) => + {props.getValue()?.[0].toUpperCase()} + {props.getValue()} + + }), + + columnHelper.accessor('email', { + header: "Email", + cell: (props) => {props.getValue()} + }), + + columnHelper.display({ + id: "status", + header: "Status", + cell: (props) => Active + }), + + columnHelper.display({ + id: 'actions', + header: "Actions", + size: 10, + meta: { + className: "w-fit" + }, + cell: (props) => + + {/* Detail */} + + + + + + + {/* Edit */} + + + + + + + {/* Delete */} + + + + + + + }) +]; + +export default columns; diff --git a/src/utils/stringToColorHex.ts b/src/utils/stringToColorHex.ts new file mode 100644 index 0000000..d5693c2 --- /dev/null +++ b/src/utils/stringToColorHex.ts @@ -0,0 +1,26 @@ +/** + * Generates a consistent hex color code from a given string. + * + * The function uses a hash algorithm to convert the input string into a number, + * which is then converted into a hexadecimal color code. The same input string + * will always produce the same color hex code. + * + * @param inputString - The input string from which the color is generated. + * @returns The generated hex color code. + */ +export default function stringToColorHex(inputString: string): string { + // Hash function to convert string to number + let hash = 0; + for (let i = 0; i < inputString.length; i++) { + hash = inputString.charCodeAt(i) + ((hash << 5) - hash); + } + + // Convert the number to a hex color code + let color = '#'; + for (let i = 0; i < 3; i++) { + const value = (hash >> (i * 8)) & 0xFF; + color += ('00' + value.toString(16)).substr(-2); + } + + return color; +} \ No newline at end of file
+ {flexRender( cell.column.columnDef.cell, - cell.getContext() + cell.getContext(), )} -