moonstream/frontend/src/components/TokensList.js

102 wiersze
3.0 KiB
JavaScript
Czysty Zwykły widok Historia

2021-09-21 14:58:13 +00:00
import React from "react";
import { Skeleton, IconButton } from "@chakra-ui/react";
import {
Table,
Th,
Td,
Tr,
Thead,
Tbody,
Editable,
EditableInput,
EditablePreview,
} from "@chakra-ui/react";
import { DeleteIcon } from "@chakra-ui/icons";
import moment from "moment";
import CopyButton from "./CopyButton";
2021-09-23 12:58:48 +00:00
const List = ({ data, revoke, isLoading, update }) => {
2021-09-23 13:02:21 +00:00
const userToken = localStorage.getItem("MOONSTREAM_ACCESS_TOKEN");
2021-09-21 14:58:13 +00:00
2021-09-23 12:58:48 +00:00
const cellProps = {
px: ["2px", "6px", "inherit"],
};
2021-09-21 14:58:13 +00:00
if (data) {
return (
<Table
variant="simple"
colorScheme="blue"
2021-09-21 14:58:13 +00:00
justifyContent="center"
alignItems="baseline"
h="auto"
size="sm"
>
<Thead>
<Tr>
2021-09-23 12:58:48 +00:00
<Th>Label</Th>
<Th {...cellProps}>Token</Th>
<Th {...cellProps}>Date Created</Th>
<Th {...cellProps}>Actions</Th>
2021-09-21 14:58:13 +00:00
</Tr>
</Thead>
<Tbody>
{data.token.map((token) => {
if (token.active) {
if (userToken !== token.id) {
return (
<Tr key={`token-row-${token.id}`}>
2021-09-23 12:58:48 +00:00
<Td py={0} {...cellProps}>
2021-09-21 14:58:13 +00:00
<Editable
colorScheme="blue"
2021-09-23 12:58:48 +00:00
placeholder="Click to set up label"
2021-09-21 14:58:13 +00:00
defaultValue={token.note}
2021-09-23 12:58:48 +00:00
isDisabled={update.isLoading}
2021-09-21 14:58:13 +00:00
onSubmit={(nextValue) =>
2021-09-23 12:58:48 +00:00
update.mutate({ token: token.id, note: nextValue })
2021-09-21 14:58:13 +00:00
}
>
<EditablePreview
maxW="40rem"
2021-09-23 12:58:48 +00:00
textColor={token.note ? "inherit" : "gray.900"}
2021-09-21 14:58:13 +00:00
_placeholder={{ color: "black" }}
/>
<EditableInput maxW="40rem" />
</Editable>
</Td>
2021-09-23 12:58:48 +00:00
<Td
mr={4}
py={0}
{...cellProps}
isTruncated
maxW={["100px", "150px", "300px"]}
>
<CopyButton>{token.id}</CopyButton>
</Td>
<Td py={0} {...cellProps}>
{moment(token.created_at).format("L")}
</Td>
<Td py={0} {...cellProps}>
2021-09-21 14:58:13 +00:00
<IconButton
size="sm"
variant="ghost"
colorScheme="blue"
2021-09-21 14:58:13 +00:00
onClick={() => revoke(token.id)}
icon={<DeleteIcon />}
/>
</Td>
</Tr>
);
} else return null;
} else return null;
})}
</Tbody>
</Table>
);
} else if (isLoading) {
return <Skeleton />;
} else {
return "";
}
};
export default List;