-
Notifications
You must be signed in to change notification settings - Fork 6
/
channel-header.tsx
93 lines (79 loc) · 2.62 KB
/
channel-header.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// tslint:disable-next-line:no-unused-variable
import * as React from 'react';
import AppBar from 'material-ui/AppBar';
import { Tab } from 'material-ui/Tabs';
import { Action } from './lib/action';
import { Channel, ChannelBase } from './lib/models/api-shapes';
import { ChannelListViewModel } from './channel-list';
import { Model, fromObservable } from './lib/model';
import { SimpleView } from './lib/view';
import { Store } from './store';
import { when } from './lib/when';
export class ChannelHeaderViewModel extends Model {
@fromObservable selectedChannel: ChannelBase;
@fromObservable channelInfo: Channel;
@fromObservable members: Array<string>;
@fromObservable topic: { value: string};
@fromObservable isDrawerOpen: boolean;
toggleDrawer: Action<boolean>;
constructor(public readonly store: Store, listViewModel: ChannelListViewModel) {
super();
let isDrawerOpen = false;
this.toggleDrawer = Action.create(() => isDrawerOpen = !isDrawerOpen, false);
this.toggleDrawer.result.toProperty(this, 'isDrawerOpen');
when(listViewModel, x => x.selectedChannel)
.toProperty(this, 'selectedChannel');
when(this, x => x.selectedChannel)
.filter(c => !!c)
.switchMap(c => this.store.channels.listen(c.id, c.api))
.do(x => console.log(`New ChannelInfo! ${JSON.stringify(x)}`))
.toProperty(this, 'channelInfo');
// NB: This works but it's too damn clever
this.innerDisp.add(when(this, x => x.channelInfo)
.filter(x => x && !x.topic)
.subscribe(x => this.store.channels.listen(x.id, x.api).invalidate()));
when(this, x => x.channelInfo.members)
.startWith([])
.toProperty(this, 'members');
when(this, x => x.channelInfo.topic)
.startWith({value: ''})
.toProperty(this, 'topic');
}
}
export class ChannelHeaderView extends SimpleView<ChannelHeaderViewModel> {
render() {
const channelName = this.viewModel.selectedChannel ?
this.viewModel.selectedChannel.name :
'Trickline';
let tabs = [];
if (this.viewModel.channelInfo) {
const tabStyle = {
paddingLeft: '20px',
paddingRight: '20px'
};
tabs.push(
<Tab
key='members'
label={`Members: ${this.viewModel.members.length}`}
style={tabStyle}
/>
);
tabs.push(
<Tab
key='topic'
label={this.viewModel.topic.value}
style={tabStyle}
/>
);
}
return (
<AppBar
title={channelName}
zDepth={2}
onLeftIconButtonTouchTap={this.viewModel.toggleDrawer.bind()}
>
{tabs}
</AppBar>
);
}
}