Move file sharing into a separate tab

master
Torjus 2018-07-30 14:47:57 +02:00
parent 730c4e23c7
commit 5138673fea
14 changed files with 127 additions and 69 deletions

View File

@ -205,6 +205,22 @@ export default class RoomClient
}); });
} }
sendFile(file)
{
logger.debug('sendFile() [file: %o]', file);
return this._protoo.send('send-file', { file })
.catch((error) =>
{
logger.error('sendFile() | failed: %o', error);
this._dispatch(requestActions.notify({
typ: 'error',
text: 'An error occurred while sharing a file'
}));
});
}
getChatHistory() getChatHistory()
{ {
logger.debug('getChatHistory()'); logger.debug('getChatHistory()');
@ -1146,6 +1162,17 @@ export default class RoomClient
break; break;
} }
case 'file-receive':
{
accept();
const { file } = request.data;
this._dispatch(stateActions.addFile(file));
break;
}
default: default:
{ {
logger.error('unknown protoo method "%s"', request.method); logger.error('unknown protoo method "%s"', request.method);

View File

@ -4,7 +4,6 @@ import PropTypes from 'prop-types';
import * as stateActions from '../../redux/stateActions'; import * as stateActions from '../../redux/stateActions';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import MessageList from './MessageList'; import MessageList from './MessageList';
import FileSharing from './FileSharing';
class Chat extends Component class Chat extends Component
{ {
@ -27,8 +26,6 @@ class Chat extends Component
data-component='Sender' data-component='Sender'
onSubmit={(e) => { onSendMessage(e, displayName, picture); }} onSubmit={(e) => { onSendMessage(e, displayName, picture); }}
> >
<FileSharing />
<input <input
type='text' type='text'
className='new-message' className='new-message'

View File

@ -2,7 +2,6 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import marked from 'marked'; import marked from 'marked';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import FileChatEntry from './FileChatEntry';
const scrollToBottom = () => const scrollToBottom = () =>
{ {
@ -60,7 +59,6 @@ class MessageList extends Component
<img className='message-avatar' src={picture} /> <img className='message-avatar' src={picture} />
<div className='message-content'> <div className='message-content'>
{message.type === 'message' && (
<div <div
className='message-text' className='message-text'
// eslint-disable-next-line react/no-danger // eslint-disable-next-line react/no-danger
@ -69,11 +67,6 @@ class MessageList extends Component
{ sanitize: true, renderer: linkRenderer } { sanitize: true, renderer: linkRenderer }
) }} ) }}
/> />
)}
{message.type === 'file' && (
<FileChatEntry message={message} />
)}
<span className='message-time'> <span className='message-time'>
{message.name} - {this.getTimeString(messageTime)} {message.name} - {this.getTimeString(messageTime)}

View File

@ -3,9 +3,9 @@ import { connect } from 'react-redux';
import magnet from 'magnet-uri'; import magnet from 'magnet-uri';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import { saveAs } from 'file-saver/FileSaver'; import { saveAs } from 'file-saver/FileSaver';
import { client } from './FileSharing'; import { client } from './index';
class FileChatEntry extends Component class FileEntry extends Component
{ {
state = { state = {
active : false, active : false,
@ -75,7 +75,7 @@ class FileChatEntry extends Component
active : true active : true
}); });
const magnet = this.props.message.file.magnet; const magnet = this.props.data.file.magnet;
const existingTorrent = client.get(magnet); const existingTorrent = client.get(magnet);
@ -95,13 +95,13 @@ class FileChatEntry extends Component
<div> <div>
{!this.state.active && !this.state.files && ( {!this.state.active && !this.state.files && (
<Fragment> <Fragment>
{this.props.message.sender === 'client' ? ( {this.props.data.me ? (
<p>You shared a file.</p> <p>You shared a file.</p>
) : ( ) : (
<p>A new file was shared.</p> <p>A new file was shared.</p>
)} )}
<p>{magnet.decode(this.props.message.file.magnet).dn}</p> <p>{magnet.decode(this.props.data.file.magnet).dn}</p>
<button onClick={this.download}> <button onClick={this.download}>
Download Download
@ -147,4 +147,4 @@ const mapDispatchToProps = {
export default connect( export default connect(
undefined, undefined,
mapDispatchToProps mapDispatchToProps
)(FileChatEntry); )(FileEntry);

View File

@ -1,4 +1,5 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { connect } from 'react-redux';
import WebTorrent from 'webtorrent'; import WebTorrent from 'webtorrent';
import createTorrent from 'create-torrent'; import createTorrent from 'create-torrent';
import dragDrop from 'drag-drop'; import dragDrop from 'drag-drop';
@ -7,6 +8,7 @@ import * as stateActions from '../../redux/stateActions';
import * as requestActions from '../../redux/requestActions'; import * as requestActions from '../../redux/requestActions';
import { store } from '../../store'; import { store } from '../../store';
import config from '../../../config'; import config from '../../../config';
import FileEntry from './FileEntry';
export const client = new WebTorrent({ export const client = new WebTorrent({
tracker : { tracker : {
@ -20,11 +22,10 @@ const notifyPeers = (file) =>
{ {
const { displayName, picture } = store.getState().me; const { displayName, picture } = store.getState().me;
store.dispatch(stateActions.addUserFile(file)); store.dispatch(requestActions.sendFile(file, displayName, picture));
store.dispatch(requestActions.sendChatFile(file, displayName, picture));
}; };
const shareFiles = async(files) => const shareFiles = async (files) =>
{ {
const notification = const notification =
{ {
@ -97,6 +98,7 @@ class FileSharing extends Component
render() render()
{ {
return ( return (
<div>
<div> <div>
<input <input
style={{ display: 'none' }} style={{ display: 'none' }}
@ -113,8 +115,24 @@ class FileSharing extends Component
share file share file
</button> </button>
</div> </div>
<div>
{this.props.sharing.map((entry) => (
<FileEntry
data={entry}
/>
))}
</div>
</div>
); );
} }
} }
export default FileSharing; const mapStateToProps = (state) =>
({
sharing: state.sharing
});
export default connect(
mapStateToProps
)(FileSharing);

View File

@ -5,6 +5,7 @@ import * as toolTabActions from '../../redux/stateActions';
import ParticipantList from '../ParticipantList/ParticipantList'; import ParticipantList from '../ParticipantList/ParticipantList';
import Chat from '../Chat/Chat'; import Chat from '../Chat/Chat';
import Settings from '../Settings'; import Settings from '../Settings';
import FileSharing from '../FileSharing';
class ToolArea extends React.Component class ToolArea extends React.Component
{ {
@ -46,6 +47,19 @@ class ToolArea extends React.Component
<Chat /> <Chat />
</div> </div>
<input
type='radio'
name='tabs'
id='tab-files'
onChange={() => setToolTab('files')}
checked={currentToolTab === 'files'}
/>
<label htmlFor='tab-files'>Files</label>
<div className='tab'>
<FileSharing />
</div>
<input <input
type='radio' type='radio'
name='tabs' name='tabs'

View File

@ -17,15 +17,6 @@ const chatmessages = (state = [], action) =>
return [ ...state, message ]; return [ ...state, message ];
} }
case 'ADD_NEW_USER_FILE':
{
const { file } = action.payload;
const message = createNewFile(file, 'client', 'Me', undefined);
return [ ...state, message ];
}
case 'ADD_NEW_RESPONSE_MESSAGE': case 'ADD_NEW_RESPONSE_MESSAGE':
{ {
const { message } = action.payload; const { message } = action.payload;

View File

@ -9,15 +9,3 @@ export function createNewMessage(text, sender, name, picture)
picture picture
}; };
} }
export function createNewFile(file, sender, name, picture)
{
return {
type : 'file',
file,
time : Date.now(),
name,
sender,
picture
};
}

View File

@ -8,6 +8,7 @@ import notifications from './notifications';
import chatmessages from './chatmessages'; import chatmessages from './chatmessages';
import chatbehavior from './chatbehavior'; import chatbehavior from './chatbehavior';
import toolarea from './toolarea'; import toolarea from './toolarea';
import sharing from './sharing';
const reducers = combineReducers( const reducers = combineReducers(
{ {
@ -19,7 +20,8 @@ const reducers = combineReducers(
notifications, notifications,
chatmessages, chatmessages,
chatbehavior, chatbehavior,
toolarea toolarea,
sharing
}); });
export default reducers; export default reducers;

View File

@ -0,0 +1,16 @@
const sharing = (state = [], action) =>
{
switch (action.type)
{
case 'SEND_FILE':
return [ ...state, { ...action.payload, me: true }];
case 'ADD_FILE':
return [ ...state, action.payload ];
default:
return state;
}
};
export default sharing;

View File

@ -214,13 +214,11 @@ export const sendChatMessage = (text, name, picture) =>
}; };
}; };
export const sendChatFile = (magnet, name, picture) => export const sendFile = (file, name, picture) =>
{ {
const message = createNewFile(magnet, 'response', name, picture);
return { return {
type : 'SEND_CHAT_MESSAGE', type: 'SEND_FILE',
payload : { message } payload: { file, name, picture }
}; };
}; };

View File

@ -231,6 +231,12 @@ export default ({ dispatch, getState }) => (next) =>
break; break;
} }
case 'SEND_FILE':
{
client.sendFile(action.payload);
break;
}
} }
return next(action); return next(action);

View File

@ -441,6 +441,14 @@ export const dropMessages = () =>
}; };
}; };
export const addFile = (payload) =>
{
return {
type: 'ADD_FILE',
payload
};
};
export const setPicture = (picture) => export const setPicture = (picture) =>
({ ({
type : 'SET_PICTURE', type : 'SET_PICTURE',

View File

@ -89,7 +89,7 @@
font-weight: bold; font-weight: bold;
transition: background ease 0.2s; transition: background ease 0.2s;
text-align: center; text-align: center;
width: 33.33%; width: 25%;
font-size: 1.3vmin; font-size: 1.3vmin;
height: 3vmin; height: 3vmin;