2020-03-16 09:34:32 +00:00
|
|
|
import React, { useState, useRef } from "react";
|
2020-03-15 05:04:30 +00:00
|
|
|
|
2020-03-15 06:50:56 +00:00
|
|
|
import {
|
|
|
|
ThemeProvider,
|
|
|
|
Box,
|
|
|
|
Heading,
|
|
|
|
Text,
|
|
|
|
Label,
|
|
|
|
Input,
|
|
|
|
Button
|
|
|
|
} from "theme-ui";
|
|
|
|
import theme from "./theme.js";
|
2020-03-15 05:04:30 +00:00
|
|
|
|
2020-03-16 08:44:49 +00:00
|
|
|
import useSession from "./useSession";
|
2020-03-15 05:04:30 +00:00
|
|
|
|
2020-03-16 08:44:49 +00:00
|
|
|
function App() {
|
2020-03-15 05:04:30 +00:00
|
|
|
const [connectId, setConnectId] = useState("");
|
|
|
|
const connectToId = () => {
|
2020-03-16 08:44:49 +00:00
|
|
|
const connection = peer.connect(connectId);
|
2020-03-15 05:04:30 +00:00
|
|
|
connection.on("open", () => {
|
|
|
|
connection.on("data", data => {
|
2020-03-15 07:39:21 +00:00
|
|
|
const blob = new Blob([data]);
|
|
|
|
imgRef.current = blob;
|
|
|
|
setImgSrc(URL.createObjectURL(imgRef.current));
|
2020-03-15 05:04:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-15 07:39:21 +00:00
|
|
|
const [imgSrc, setImgSrc] = useState("");
|
|
|
|
const imgRef = useRef(null);
|
|
|
|
const loadImage = e => {
|
|
|
|
imgRef.current = e.target.files[0];
|
|
|
|
setImgSrc(URL.createObjectURL(imgRef.current));
|
2020-03-16 08:44:49 +00:00
|
|
|
for (let connection of Object.values(connections)) {
|
|
|
|
connection.send(imgRef.current);
|
2020-03-15 07:55:43 +00:00
|
|
|
}
|
2020-03-15 07:39:21 +00:00
|
|
|
};
|
|
|
|
|
2020-03-16 08:44:49 +00:00
|
|
|
const [peer, peerId, connections] = useSession(imgRef);
|
|
|
|
|
2020-03-15 05:04:30 +00:00
|
|
|
return (
|
2020-03-15 06:50:56 +00:00
|
|
|
<ThemeProvider theme={theme}>
|
|
|
|
<Box width={256}>
|
|
|
|
<Heading as="h3">Your ID</Heading>
|
2020-03-16 08:44:49 +00:00
|
|
|
<Text>{peerId}</Text>
|
2020-03-15 07:39:21 +00:00
|
|
|
|
|
|
|
<Box>
|
|
|
|
<img src={imgSrc} />
|
|
|
|
<Label htmlFor="image">Image</Label>
|
|
|
|
<Input
|
|
|
|
type="file"
|
|
|
|
accept="image/*"
|
|
|
|
name="image"
|
|
|
|
id="file"
|
|
|
|
onChange={loadImage}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
|
2020-03-15 06:50:56 +00:00
|
|
|
<Box
|
|
|
|
as="form"
|
|
|
|
width={1 / 2}
|
|
|
|
onSubmit={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
connectToId();
|
|
|
|
}}
|
|
|
|
py={3}
|
|
|
|
>
|
|
|
|
<Label htmlFor="connectId">Connect to</Label>
|
|
|
|
<Input
|
|
|
|
id="connectId"
|
|
|
|
name="connectId"
|
|
|
|
value={connectId}
|
|
|
|
onChange={e => setConnectId(e.target.value)}
|
|
|
|
/>
|
|
|
|
<Button>Connect</Button>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
</ThemeProvider>
|
2020-03-15 05:04:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|