Skip to content

Commit

Permalink
Adding React.js, Redux, revamping dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron committed Aug 24, 2016
1 parent 68c93f8 commit 49520d6
Show file tree
Hide file tree
Showing 34 changed files with 297 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ public/system
public/assets
.env
.env.*
node_modules/
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ gem 'rack-attack'
gem 'sidekiq'
gem 'sinatra', require: nil, github: 'sinatra'

gem 'react-rails'
gem 'browserify-rails'

group :development, :test do
gem 'rspec-rails'
gem 'pry-rails'
Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ GEM
addressable (2.4.0)
arel (7.1.1)
ast (2.3.0)
babel-source (5.8.35)
babel-transpiler (0.7.0)
babel-source (>= 4.0, < 6)
execjs (~> 2.0)
bcrypt (3.1.11)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
browserify-rails (3.1.0)
railties (>= 4.0.0, < 5.1)
sprockets (>= 3.5.2)
builder (3.2.2)
bullet (5.3.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -245,6 +252,13 @@ GEM
rake (11.2.2)
rdoc (4.2.2)
json (~> 1.4)
react-rails (1.8.2)
babel-transpiler (>= 0.7.0)
coffee-script-source (~> 1.8)
connection_pool
execjs
railties (>= 3.2)
tilt
redis (3.3.1)
ref (2.0.0)
responders (2.3.0)
Expand Down Expand Up @@ -348,6 +362,7 @@ DEPENDENCIES
addressable
better_errors
binding_of_caller
browserify-rails
bullet
coffee-rails (~> 4.1.0)
devise
Expand Down Expand Up @@ -380,6 +395,7 @@ DEPENDENCIES
rails (= 5.0.0.1)
rails_12factor
rails_autolink
react-rails
redis (~> 3.2)
rspec-rails
rspec-sidekiq
Expand Down
4 changes: 3 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require components
//= require cable
//= require mastodon-logo
13 changes: 0 additions & 13 deletions app/assets/javascripts/channels/timeline.js

This file was deleted.

9 changes: 9 additions & 0 deletions app/assets/javascripts/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//= require_self
//= require react_ujs

window.React = require('react');
window.ReactDOM = require('react-dom');

//= require_tree ./components

window.Root = require('./components/containers/root');
Empty file.
18 changes: 18 additions & 0 deletions app/assets/javascripts/components/actions/statuses.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const SET_TIMELINE = 'SET_TIMELINE';
export const ADD_STATUS = 'ADD_STATUS';

export function setTimeline(timeline, statuses) {
return {
type: SET_TIMELINE,
timeline: timeline,
statuses: statuses
};
}

export function addStatus(timeline, status) {
return {
type: ADD_STATUS,
timeline: timeline,
status: status
};
}
19 changes: 19 additions & 0 deletions app/assets/javascripts/components/components/column.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import StatusListContainer from '../containers/status_list_container';
import ColumnHeader from './column_header';

const Column = React.createClass({
propTypes: {
type: React.PropTypes.string
},

render: function() {
return (
<div style={{ width: '350px', flex: '0 0 auto', background: '#282c37', margin: '10px', marginRight: '0', display: 'flex', flexDirection: 'column' }}>
<ColumnHeader type={this.props.type} />
<StatusListContainer type={this.props.type} />
</div>
);
}
});

export default Column;
15 changes: 15 additions & 0 deletions app/assets/javascripts/components/components/column_header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const ColumnHeader = React.createClass({
propTypes: {
type: React.PropTypes.string
},

render: function() {
return (
<div style={{ padding: '15px', fontSize: '16px', background: '#2f3441', flex: '0 0 auto' }}>
{this.props.type}
</div>
);
}
});

export default ColumnHeader;
15 changes: 15 additions & 0 deletions app/assets/javascripts/components/components/columns_area.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Column from './column';

const ColumnsArea = React.createClass({

render: function() {
return (
<div style={{ display: 'flex', flexDirection: 'row', flex: '1' }}>
<Column type='home' />
<Column type='mentions' />
</div>
);
}
});

export default ColumnsArea;
16 changes: 16 additions & 0 deletions app/assets/javascripts/components/components/frontend.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import NavBar from './nav_bar';
import ColumnsArea from './columns_area';

const Frontend = React.createClass({

render: function() {
return (
<div style={{ flex: '0 0 auto', display: 'flex', width: '100%', height: '100%', background: '#1a1c23' }}>
<NavBar />
<ColumnsArea />
</div>
);
}
});

export default Frontend;
8 changes: 8 additions & 0 deletions app/assets/javascripts/components/components/nav_bar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const NavBar = React.createClass({

render: function() {
return <div style={{ background: '#2f3441', width: '60px', margin: '10px', marginRight: '0' }} />;
}
});

export default NavBar;
19 changes: 19 additions & 0 deletions app/assets/javascripts/components/components/status.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ImmutablePropTypes from 'react-immutable-proptypes';

const Status = React.createClass({
propTypes: {
status: ImmutablePropTypes.map.isRequired
},

render: function() {
console.log(this.props.status.toJS());

return (
<div style={{ height: '100px' }}>
{this.props.status.getIn(['account', 'username'])}: {this.props.status.get('content')}
</div>
);
}
});

export default Status;
22 changes: 22 additions & 0 deletions app/assets/javascripts/components/components/status_list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Status from './status';
import ImmutablePropTypes from 'react-immutable-proptypes';

const StatusList = React.createClass({
propTypes: {
statuses: ImmutablePropTypes.list.isRequired
},

render: function() {
return (
<div style={{ overflowY: 'scroll', flex: '1 1 auto' }}>
<div>
{this.props.statuses.map((status) => {
return <Status key={status.get('id')} status={status} />;
})}
</div>
</div>
);
}
});

export default StatusList;
40 changes: 40 additions & 0 deletions app/assets/javascripts/components/containers/root.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';
import Frontend from '../components/frontend';
import { setTimeline, addStatus } from '../actions/statuses';

const store = configureStore();

const Root = React.createClass({

componentWillMount() {
for (var timelineType in this.props.timelines) {
if (this.props.timelines.hasOwnProperty(timelineType)) {
store.dispatch(setTimeline(timelineType, JSON.parse(this.props.timelines[timelineType])));
}
}

if (typeof App !== 'undefined') {
App.timeline = App.cable.subscriptions.create("TimelineChannel", {
connected: function() {},

disconnected: function() {},

received: function(data) {
return store.dispatch(addStatus(data.timeline, JSON.parse(data.message)));
}
});
}
},

render() {
return (
<Provider store={store}>
<Frontend />
</Provider>
);
}

});

export default Root;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { connect } from 'react-redux';
import StatusList from '../components/status_list';

const mapStateToProps = function (state, props) {
return {
statuses: state.getIn(['statuses', props.type])
};
};

export default connect(mapStateToProps)(StatusList);
6 changes: 6 additions & 0 deletions app/assets/javascripts/components/reducers/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux-immutable';
import statuses from './statuses';

export default combineReducers({
statuses
});
17 changes: 17 additions & 0 deletions app/assets/javascripts/components/reducers/statuses.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SET_TIMELINE, ADD_STATUS } from '../actions/statuses';
import Immutable from 'immutable';

const initialState = Immutable.Map();

export default function statuses(state = initialState, action) {
switch(action.type) {
case SET_TIMELINE:
return state.set(action.timeline, Immutable.fromJS(action.statuses));
case ADD_STATUS:
return state.update(action.timeline, function (list) {
list.unshift(Immutable.fromJS(action.status));
});
default:
return state;
}
}
6 changes: 6 additions & 0 deletions app/assets/javascripts/components/store/configureStore.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createStore } from 'redux';
import appReducer from '../reducers';

export default function configureStore(initialState) {
return createStore(appReducer, initialState);
}
17 changes: 17 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ body {
font-weight: 400;
color: #fff;
padding-bottom: 140px;
text-rendering: optimizelegibility;
font-feature-settings: "kern";

&.app-body {
position: fixed;
width: 100%;
height: 100%;
padding: 0;
}
}

.app-holder {
display: flex;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}

.container {
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ApplicationController < ActionController::Base
end
end

helper_method :current_account

protected

def current_account
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class HomeController < ApplicationController
layout 'dashboard'

before_action :authenticate_user!

def index
@timeline = Feed.new(:home, current_user.account).get(10, params[:max_id])
@body_classes = 'app-body'
@home = Feed.new(:home, current_user.account).get(20)
@mentions = Feed.new(:mentions, current_user.account).get(20)
end
end
2 changes: 0 additions & 2 deletions app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class SettingsController < ApplicationController
layout 'dashboard'

before_action :authenticate_user!
before_action :set_account

Expand Down
2 changes: 0 additions & 2 deletions app/controllers/statuses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class StatusesController < ApplicationController
layout 'dashboard'

before_action :authenticate_user!

def create
Expand Down
2 changes: 1 addition & 1 deletion app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def initialize(type, account)
@account = account
end

def get(limit, max_id)
def get(limit, max_id = nil)
max_id = '+inf' if max_id.nil?
unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", '-inf', limit: [0, limit])
status_map = Hash.new
Expand Down
2 changes: 1 addition & 1 deletion app/services/fan_out_on_write_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def deliver_to_mentioned(status)
def push(type, receiver, status)
redis.zadd(FeedManager.key(type, receiver.id), status.id, status.id)
trim(type, receiver)
ActionCable.server.broadcast("timeline:#{receiver.id}", message: inline_render(receiver, status))
ActionCable.server.broadcast("timeline:#{receiver.id}", timeline: type, message: inline_render(receiver, status))
end

def trim(type, receiver)
Expand Down
Loading

0 comments on commit 49520d6

Please sign in to comment.