umami/components/pages/settings/users/UsersTable.js

111 lines
3.2 KiB
JavaScript
Raw Normal View History

import {
Button,
2023-01-21 02:12:53 +01:00
Text,
Icon,
Table,
TableBody,
TableCell,
TableColumn,
TableHeader,
TableRow,
2023-01-25 16:42:46 +01:00
Flexbox,
Icons,
ModalTrigger,
Modal,
} from 'react-basics';
2023-01-25 16:42:46 +01:00
import { useIntl } from 'react-intl';
2023-01-21 02:12:53 +01:00
import { formatDistance } from 'date-fns';
import Link from 'next/link';
2023-01-25 16:42:46 +01:00
import useUser from 'hooks/useUser';
import UserDeleteForm from './UserDeleteForm';
import { labels } from 'components/messages';
import { ROLES } from 'lib/constants';
2023-01-25 16:42:46 +01:00
export default function UsersTable({ data = [], onDelete }) {
const { formatMessage } = useIntl();
const { user } = useUser();
const columns = [
{ name: 'username', label: formatMessage(labels.username), style: { flex: 2 } },
{ name: 'role', label: formatMessage(labels.role), style: { flex: 1 } },
{ name: 'created', label: formatMessage(labels.created), style: { flex: 1 } },
{ name: 'action', label: ' ', style: { flex: 2 } },
];
return (
2023-01-25 16:42:46 +01:00
<Table columns={columns} rows={data}>
2023-01-21 02:12:53 +01:00
<TableHeader>
{(column, index) => {
return (
2023-01-25 16:42:46 +01:00
<TableColumn key={index} style={{ ...column.style }}>
2023-01-21 02:12:53 +01:00
{column.label}
</TableColumn>
);
}}
</TableHeader>
<TableBody>
{(row, keys, rowIndex) => {
2023-01-25 16:42:46 +01:00
const rowData = {
...row,
created: formatDistance(new Date(row.createdAt), new Date(), {
addSuffix: true,
}),
role: formatMessage(
labels[Object.keys(ROLES).find(key => ROLES[key] === row.role) || labels.unknown],
),
action: (
<>
<Link href={`/settings/users/${row.id}`}>
<Button>
<Icon>
<Icons.Edit />
2023-01-25 16:42:46 +01:00
</Icon>
<Text>{formatMessage(labels.edit)}</Text>
</Button>
</Link>
<ModalTrigger disabled={row.id === user.id}>
<Button disabled={row.id === user.id}>
<Icon>
<Icons.Trash />
2023-01-25 16:42:46 +01:00
</Icon>
<Text>{formatMessage(labels.delete)}</Text>
</Button>
<Modal>
{close => (
<UserDeleteForm
userId={row.id}
username={row.username}
onSave={onDelete}
onClose={close}
/>
)}
</Modal>
2023-01-25 16:42:46 +01:00
</ModalTrigger>
</>
),
};
2023-01-21 02:12:53 +01:00
return (
2023-01-25 16:42:46 +01:00
<TableRow key={rowIndex} data={rowData} keys={keys}>
2023-01-21 02:12:53 +01:00
{(data, key, colIndex) => {
return (
2023-01-25 16:42:46 +01:00
<TableCell key={colIndex} style={{ ...columns[colIndex]?.style }}>
<Flexbox
flex={1}
gap={10}
alignItems="center"
justifyContent={key === 'action' ? 'end' : undefined}
>
{data[key]}
</Flexbox>
2023-01-21 02:12:53 +01:00
</TableCell>
);
}}
</TableRow>
);
}}
</TableBody>
</Table>
);
}