Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-sz committed Jan 7, 2020
0 parents commit 5879825
Show file tree
Hide file tree
Showing 19 changed files with 11,787 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
30 changes: 30 additions & 0 deletions LICENSE
@@ -0,0 +1,30 @@
Copyright (c) 2020, Mat Sz
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# filedrop-web

Easy WebRTC file transfer.
51 changes: 51 additions & 0 deletions package.json
@@ -0,0 +1,51 @@
{
"name": "filedrop-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/qrcode.react": "^1.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-redux": "^7.1.5",
"@types/react-router-dom": "^5.1.3",
"@types/redux": "^3.6.0",
"@types/uuid": "^3.4.6",
"node-sass": "^4.13.0",
"qrcode.react": "^1.0.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.1.3",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"redux": "^4.0.5",
"redux-saga": "^1.1.3",
"typescript": "~3.7.2",
"uuid": "^3.3.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
14 changes: 14 additions & 0 deletions public/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>filedrop</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
Empty file added src/App.scss
Empty file.
37 changes: 37 additions & 0 deletions src/App.tsx
@@ -0,0 +1,37 @@
import React from 'react';
import {
HashRouter as Router,
Switch,
Route
} from 'react-router-dom';

import './App.scss';

import Transfers from './screens/Transfers';
import Home from './screens/Home';
import { useSelector } from 'react-redux';
import { StateType } from './reducers';

const App: React.FC = () => {
const connected = useSelector((state: StateType) => state.connected);

return (
<Router>
<div className="app">
<div>
{ connected ? 'Connected' : 'Connecting...' }
</div>
<Switch>
<Route path="/transfers/:code">
<Transfers />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}

export default App;
108 changes: 108 additions & 0 deletions src/BetterWebSocket.ts
@@ -0,0 +1,108 @@
import { MiddlewareAPI } from "redux";
import { ActionType } from "./types/ActionType";

export class BetterWebSocket {
onConnected?: () => void;
onDisconnected?: () => void;
onMessage?: (message: any) => void;
private socket: WebSocket = null;
private retries = 0;

constructor(private url: string) {
this.connect();
}

connect() {
console.log(this.retries);
this.retries++;

if (this.retries > 5) {
this.disconnected();
return;
}

if (this.socket) {
try {
this.socket.close();
this.disconnected();
} catch { }
}

this.socket = new WebSocket(this.url);

this.socket.onopen = () => {
this.connected();
};

this.socket.onclose = (e) => {
this.disconnected();
this.socket = null;

if (e.code === 1000) {
setTimeout(() => {
this.connect();
}, 500);
}
};

this.socket.onmessage = (e) => {
this.message(e.data);
};

this.socket.onerror = () => {
this.disconnected();
this.socket = null;

setTimeout(() => {
this.connect();
}, 500);
};
}

send(data: any) {
if (!this.socket) return;

this.socket.send(JSON.stringify(data));
}

private connected() {
this.retries = 0;

if (this.onConnected) {
this.onConnected();
}
}

private disconnected() {
if (this.onDisconnected) {
this.onDisconnected();
}
}

private message(data: string) {
try {
const json = JSON.parse(data);
if (json && this.onMessage) {
this.onMessage(json);
}
} catch { }
}
};

export const socketMiddleware = (url: string) => {
return (store: MiddlewareAPI<any, any>) => {
const socket = new BetterWebSocket(url);

socket.onConnected = () => store.dispatch({ type: ActionType.WS_CONNECTED });
socket.onDisconnected = () => store.dispatch({ type: ActionType.WS_DISCONNECTED });
socket.onMessage = (message) => store.dispatch({ type: ActionType.WS_MESSAGE, value: message });

return (next: (action: any) => void) => (action: any) => {
if (action.type && action.type === ActionType.WS_SEND_MESSAGE) {
socket.send(action.value);
}

return next(action);
};
};
};
14 changes: 14 additions & 0 deletions src/index.tsx
@@ -0,0 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';

import App from './App';
import createStore from './store';

const store = createStore();

ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.getElementById('root'));
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
46 changes: 46 additions & 0 deletions src/reducers/index.ts
@@ -0,0 +1,46 @@
import { ActionModel } from '../types/Models';
import { ActionType } from '../types/ActionType';
import { Store } from 'redux';

export interface StateType {
connected: boolean,
error: string,
name: string,
clientId: string,
};

let initialState: StateType = {
connected: false,
error: null,
name: null,
clientId: null,
};

export type StoreType = Store<StateType, ActionModel>;

function applicationState(state = initialState, action: ActionModel) {
const newState = {...state};
switch (action.type) {
case ActionType.SET_ERROR:
newState.error = action.value as string;
break;
case ActionType.DISMISS_ERROR:
newState.error = null;
break;
case ActionType.SET_CONNECTED:
newState.connected = action.value as boolean;
break;
case ActionType.SET_NAME:
newState.name = action.value as string;
break;
case ActionType.SET_CLIENT_ID:
newState.clientId = action.value as string;
break;
default:
return state;
}

return newState;
};

export default applicationState;
31 changes: 31 additions & 0 deletions src/sagas/index.ts
@@ -0,0 +1,31 @@
import { put, takeEvery } from 'redux-saga/effects';
import { ActionModel, MessageModel, WelcomeMessageModel } from '../types/Models';
import { ActionType } from '../types/ActionType';

function* message(action: ActionModel) {
const msg: MessageModel = action.value as MessageModel;

switch (msg.type) {
case 'welcome':
yield put({ type: ActionType.SET_CLIENT_ID, value: (msg as WelcomeMessageModel).clientId });
break;
case 'request':
break;
case 'rtc':
break;
}
}

function* connected() {
yield put({ type: ActionType.SET_CONNECTED, value: true });
}

function* disconnected() {
yield put({ type: ActionType.SET_CONNECTED, value: false });
}

export default function* root() {
yield takeEvery(ActionType.WS_MESSAGE, message);
yield takeEvery(ActionType.WS_CONNECTED, connected);
yield takeEvery(ActionType.WS_DISCONNECTED, disconnected);
};
12 changes: 12 additions & 0 deletions src/screens/Home.tsx
@@ -0,0 +1,12 @@
import React from 'react';
import { Link } from 'react-router-dom';

const Home: React.FC = () => {
return (
<div className="screen">
<Link to="/transfers/123456">Receive files</Link>
</div>
);
}

export default Home;

0 comments on commit 5879825

Please sign in to comment.