import React from 'react' import Grid from '@material-ui/core/Grid' import Button from 'components/Button' import { v2 } from 'api' import * as jsonapi from 'utils/json-api-client' import * as models from 'core/store/models' import { useErrorHandler } from 'hooks/useErrorHandler' import { useLoadingPlaceholder } from 'hooks/useLoadingPlaceholder' import Table from '@material-ui/core/Table' import TableBody from '@material-ui/core/TableBody' import TableCell from '@material-ui/core/TableCell' import TableHead from '@material-ui/core/TableHead' import TableRow from '@material-ui/core/TableRow' import Card from '@material-ui/core/Card' import CardContent from '@material-ui/core/CardContent' import CardHeader from '@material-ui/core/CardHeader' import Typography from '@material-ui/core/Typography' import { TimeAgo } from 'components/TimeAgo' import { createStyles, withStyles, WithStyles, Theme, } from '@material-ui/core/styles' import { Delete } from './Delete' import { KeyBundle } from './KeyBundle' import { useDispatch } from 'react-redux' import { deleteNotification, createNotification } from './Notifications' import { Copy } from './Copy' const styles = (theme: Theme) => createStyles({ card: { padding: theme.spacing.unit, marginBottom: theme.spacing.unit * 3, }, avatar: { backgroundColor: theme.palette.grey[800], }, }) const KEY_TYPE = 'P2P' export const P2PKeys = withStyles(styles)( ({ classes }: WithStyles) => { const [p2pKeys, setP2Keys] = React.useState< jsonapi.ApiResponse['data'] >() const { error, ErrorComponent, setError } = useErrorHandler() const { LoadingPlaceholder } = useLoadingPlaceholder(!error && !p2pKeys) const dispatch = useDispatch() const handleFetchIndex = React.useCallback(() => { v2.p2pKeys .getP2PKeys() .then(({ data }) => { setP2Keys(data) }) .catch(setError) }, [setError]) React.useEffect(() => { handleFetchIndex() }, [handleFetchIndex]) function handleDelete(id: string) { v2.p2pKeys .destroyP2PKey(id) .then(() => { handleFetchIndex() dispatch( deleteNotification({ keyType: KEY_TYPE, }), ) }) .catch(setError) } function handleCreate() { v2.p2pKeys .createP2PKey() .then(({ data }) => { handleFetchIndex() dispatch( createNotification({ keyType: KEY_TYPE, keyValue: data.id, }), ) }) .catch(setError) } return ( handleCreate()} > New P2P Key } title={`${KEY_TYPE} Keys`} subheader={`Manage your ${KEY_TYPE} Key Bundles.`} /> Key Bundle Created Delete {p2pKeys?.length === 0 && ( No entries to show. )} {p2pKeys?.map((key) => ( Peer ID: {key.attributes.peerId}{' '} } secondary={[ <>Public Key: {key.attributes.publicKey}, ]} /> {key.attributes.createdAt || ''} ))}
) }, )