1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/common/ApiTokenTable/useApiTokenTable.tsx
Fredrik Strand Oseberg f381718fd6
fix: icon imports (#6499)
Based on this article:
https://mui.com/material-ui/guides/minimizing-bundle-size/ importing
from `'@mui/icons-material'` instead of specifying the actual path to
the icon like `import Delete from '@mui/icons-material/Delete';` can be
up to six time slower. This change changes all named imports in Unleash
referencing the `@mui/icons-material` to default imports.

This reduced the amount of modules we had to process when building the
frontend from 15206 to 4746

Before:
<img width="1016" alt="Skjermbilde 2024-03-11 kl 14 19 58"
src="https://github.com/Unleash/unleash/assets/16081982/f137d24a-6557-4183-a40f-f62a33524520">

After:
<img width="1237" alt="Skjermbilde 2024-03-11 kl 14 20 32"
src="https://github.com/Unleash/unleash/assets/16081982/05a27d6a-2c3f-4409-9862-7188ab4b9c72">

Build time locally decreased by around 50%

Before:
<img width="1504" alt="Skjermbilde 2024-03-11 kl 14 31 45"
src="https://github.com/Unleash/unleash/assets/16081982/bc931559-b022-47ed-9f8f-c87401578518">


After:
<img width="1219" alt="Skjermbilde 2024-03-11 kl 14 27 00"
src="https://github.com/Unleash/unleash/assets/16081982/3c3a8d6b-576d-45c3-aa40-cc5f95d9df2b">
2024-03-12 10:56:10 +01:00

144 lines
4.1 KiB
TypeScript

import { useMemo } from 'react';
import { IApiToken } from 'hooks/api/getters/useApiTokens/useApiTokens';
import { DateCell } from 'component/common/Table/cells/DateCell/DateCell';
import { HighlightCell } from 'component/common/Table/cells/HighlightCell/HighlightCell';
import { IconCell } from 'component/common/Table/cells/IconCell/IconCell';
import { TimeAgoCell } from 'component/common/Table/cells/TimeAgoCell/TimeAgoCell';
import { useTable, useGlobalFilter, useSortBy } from 'react-table';
import { sortTypes } from 'utils/sortTypes';
import { ProjectsList } from 'component/admin/apiToken/ProjectsList/ProjectsList';
import Key from '@mui/icons-material/Key';
export const useApiTokenTable = (
tokens: IApiToken[],
getActionCell: (props: any) => JSX.Element,
) => {
const initialState = useMemo(
() => ({ sortBy: [{ id: 'createdAt', desc: true }] }),
[],
);
const COLUMNS = useMemo(() => {
return [
{
id: 'Icon',
width: '1%',
Cell: () => <IconCell icon={<Key color='disabled' />} />,
disableSortBy: true,
disableGlobalFilter: true,
},
{
Header: 'Username',
accessor: 'username',
Cell: HighlightCell,
},
{
Header: 'Type',
accessor: 'type',
Cell: ({
value,
}: {
value: 'client' | 'admin' | 'frontend';
}) => (
<HighlightCell
value={tokenDescriptions[value.toLowerCase()].label}
subtitle={tokenDescriptions[value.toLowerCase()].title}
/>
),
minWidth: 280,
},
{
Header: 'Project',
accessor: 'project',
Cell: (props: any) => (
<ProjectsList
project={props.row.original.project}
projects={props.row.original.projects}
/>
),
minWidth: 120,
},
{
Header: 'Environment',
accessor: 'environment',
Cell: HighlightCell,
minWidth: 120,
},
{
Header: 'Created',
accessor: 'createdAt',
Cell: DateCell,
minWidth: 150,
disableGlobalFilter: true,
},
{
Header: 'Last seen',
accessor: 'seenAt',
Cell: TimeAgoCell,
minWidth: 150,
disableGlobalFilter: true,
},
{
Header: 'Actions',
id: 'Actions',
align: 'center',
width: '1%',
disableSortBy: true,
disableGlobalFilter: true,
Cell: getActionCell,
},
];
}, []);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state,
setGlobalFilter,
setHiddenColumns,
} = useTable(
{
columns: COLUMNS as any,
data: tokens as any,
initialState,
sortTypes,
autoResetHiddenColumns: false,
disableSortRemove: true,
},
useGlobalFilter,
useSortBy,
);
return {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state,
setGlobalFilter,
setHiddenColumns,
columns: COLUMNS,
};
};
const tokenDescriptions: {
[index: string]: { label: string; title: string };
} = {
client: {
label: 'CLIENT',
title: 'Connect server-side SDK or Unleash Proxy/Edge',
},
frontend: {
label: 'FRONTEND',
title: 'Connect web and mobile SDK',
},
admin: {
label: 'ADMIN',
title: 'Full access for managing Unleash',
},
};