umami/components/pages/settings/teams/TeamsTable.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

import Link from 'next/link';
import {
Button,
Flexbox,
Icon,
Icons,
Modal,
ModalTrigger,
Table,
TableBody,
TableCell,
TableColumn,
TableHeader,
TableRow,
2023-01-25 16:42:46 +01:00
Text,
} from 'react-basics';
2023-02-02 20:59:38 +01:00
import TeamDeleteForm from './TeamDeleteForm';
2023-03-22 22:05:55 +01:00
import useMessages from 'hooks/useMessages';
import useUser from 'hooks/useUser';
import { ROLES } from 'lib/constants';
2023-02-02 20:59:38 +01:00
export default function TeamsTable({ data = [], onDelete }) {
2023-03-22 22:05:55 +01:00
const { formatMessage, labels } = useMessages();
const { user } = useUser();
2023-01-25 16:42:46 +01:00
const columns = [
{ name: 'name', label: formatMessage(labels.name), style: { flex: 2 } },
2023-02-02 03:39:54 +01:00
{ name: 'owner', label: formatMessage(labels.owner) },
2023-01-25 16:42:46 +01:00
{ name: 'action', label: ' ' },
];
return (
2023-01-25 16:42:46 +01:00
<Table columns={columns} rows={data}>
<TableHeader>
{(column, index) => {
return (
<TableColumn key={index} style={{ ...column.style }}>
{column.label}
</TableColumn>
);
}}
</TableHeader>
<TableBody>
{(row, keys, rowIndex) => {
const { id } = row;
const owner = row.teamUser.find(({ role }) => role === ROLES.teamOwner);
const showDelete = user.id === owner?.userId;
2023-02-02 03:39:54 +01:00
const rowData = {
...row,
owner: owner?.user?.username,
2023-02-02 03:39:54 +01:00
action: (
2023-02-02 20:59:38 +01:00
<Flexbox flex={1} gap={10} justifyContent="end">
2023-02-02 03:39:54 +01:00
<Link href={`/settings/teams/${id}`}>
<Button>
<Icon>
<Icons.Edit />
</Icon>
<Text>{formatMessage(labels.edit)}</Text>
</Button>
2023-02-02 03:39:54 +01:00
</Link>
{showDelete && (
<ModalTrigger>
<Button>
<Icon>
<Icons.Trash />
</Icon>
<Text>{formatMessage(labels.delete)}</Text>
</Button>
<Modal title={formatMessage(labels.deleteTeam)}>
{close => (
<TeamDeleteForm
teamId={row.id}
teamName={row.name}
onSave={onDelete}
onClose={close}
/>
)}
</Modal>
</ModalTrigger>
)}
2023-02-02 03:39:54 +01:00
</Flexbox>
),
};
return (
2023-02-02 03:39:54 +01:00
<TableRow key={rowIndex} data={rowData} keys={keys}>
{(data, key, colIndex) => {
return (
2023-01-25 16:42:46 +01:00
<TableCell key={colIndex} style={{ ...columns[colIndex]?.style }}>
<Flexbox flex={1} alignItems="center">
{data[key]}
</Flexbox>
</TableCell>
);
}}
</TableRow>
);
}}
</TableBody>
</Table>
);
}