Files
swiss-datashare/frontend/src/pages/account/shares.tsx

142 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-05-11 13:50:28 +02:00
import {
ActionIcon,
Box,
2022-05-11 13:50:28 +02:00
Button,
Center,
Group,
LoadingOverlay,
Space,
Stack,
2022-05-11 13:50:28 +02:00
Table,
Text,
Title,
} from "@mantine/core";
import { useClipboard } from "@mantine/hooks";
import { useModals } from "@mantine/modals";
import moment from "moment";
2022-10-31 11:20:54 +01:00
import Link from "next/link";
import { useEffect, useState } from "react";
import { TbLink, TbTrash } from "react-icons/tb";
import showShareLinkModal from "../../components/account/showShareLinkModal";
2022-05-11 13:50:28 +02:00
import Meta from "../../components/Meta";
2023-01-11 13:06:38 +01:00
import useConfig from "../../hooks/config.hook";
2022-05-11 13:50:28 +02:00
import shareService from "../../services/share.service";
import { MyShare } from "../../types/share.type";
2022-05-11 13:50:28 +02:00
import toast from "../../utils/toast.util";
2022-05-11 13:50:28 +02:00
const MyShares = () => {
const modals = useModals();
const clipboard = useClipboard();
2023-01-11 13:06:38 +01:00
const config = useConfig();
const [shares, setShares] = useState<MyShare[]>();
2022-05-11 13:50:28 +02:00
useEffect(() => {
shareService.getMyShares().then((shares) => setShares(shares));
}, []);
2022-05-11 13:50:28 +02:00
if (!shares) return <LoadingOverlay visible />;
return (
<>
<Meta title="My shares" />
<Title mb={30} order={3}>
My shares
</Title>
{shares.length == 0 ? (
<Center style={{ height: "70vh" }}>
<Stack align="center" spacing={10}>
<Title order={3}>It's empty here 👀</Title>
<Text>You don't have any shares.</Text>
<Space h={5} />
<Button component={Link} href="/upload" variant="light">
Create one
</Button>
</Stack>
</Center>
) : (
<Box sx={{ display: "block", overflowX: "auto" }}>
<Table>
<thead>
<tr>
<th>Name</th>
<th>Visitors</th>
<th>Expires at</th>
<th></th>
</tr>
</thead>
<tbody>
{shares.map((share) => (
<tr key={share.id}>
<td>{share.id}</td>
<td>{share.views}</td>
<td>
{moment(share.expiration).unix() === 0
? "Never"
: moment(share.expiration).format("LLL")}
</td>
<td>
<Group position="right">
<ActionIcon
color="victoria"
variant="light"
size={25}
onClick={() => {
if (window.isSecureContext) {
clipboard.copy(
`${config.get("APP_URL")}/share/${share.id}`
);
toast.success(
"Your link was copied to the keyboard."
);
} else {
showShareLinkModal(
modals,
share.id,
config.get("APP_URL")
);
}
}}
>
<TbLink />
</ActionIcon>
<ActionIcon
color="red"
variant="light"
size={25}
onClick={() => {
modals.openConfirmModal({
title: `Delete share ${share.id}`,
children: (
<Text size="sm">
Do you really want to delete this share?
</Text>
),
confirmProps: {
color: "red",
},
labels: { confirm: "Confirm", cancel: "Cancel" },
onConfirm: () => {
shareService.remove(share.id);
setShares(
shares.filter((item) => item.id !== share.id)
);
},
});
}}
>
<TbTrash />
</ActionIcon>
</Group>
</td>
</tr>
))}
</tbody>
</Table>
</Box>
)}
</>
);
2022-05-11 13:50:28 +02:00
};
export default MyShares;