aboutsummaryrefslogtreecommitdiff
path: root/src/popup.js
blob: d8e045c08fe5ee98a83589c04129dd2189d1311a (plain) (blame)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React from 'react';
import ReactDOM from 'react-dom';
import * as serviceWorker from './serviceWorker';
import { MuiThemeProvider } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import theme from './theme';
import { PatternEntry } from './pattern';
import { Duration } from './duration';
import { msgType, MsgClient } from './msg';
import { getChartData, StyledPatternPieChart } from './Chart';
import moment from 'moment';

function openOptions() {
    chrome.tabs.create({ url: "index.html" });
}

class Popup extends React.Component {
    state = {
        patternGraphData: [],
    };
    constructor(props) {
        super(props);
        this.msgClient = new MsgClient('main');

        let pm1 = this.msgClient.sendMsg({
            type: msgType.getPatterns,
            data: { id: 'main' }
        }).then(msg => {
            this.patterns = msg.data.map(p => PatternEntry.inflate(p));
        });

        let pm2 = this.msgClient.sendMsg({
            type: msgType.getCalendars,
            data: { enabledOnly: false }
        }).then(msg => {
            this.calendars = msg.data;
        });

        let pm3 = this.msgClient.sendMsg({
            type: msgType.getConfig,
            data: ['trackedPeriods']
        }).then(msg => {
            this.trackedPeriods = msg.data.trackedPeriods.map(p => {
                return {
                    start: Duration.inflate(p.start),
                    end: Duration.inflate(p.end),
                    name: p.name
                };
            });
        });

        // initial update
        Promise.all([pm1, pm2, pm3]).then(() => {
            for (let i = 0; i < this.trackedPeriods.length; i++)
                this.renderChartData(i);
        });
    }

    getCalEvents = (id, start, end) => {
        return this.msgClient.sendMsg({ type: msgType.getCalEvents, data: { id,
            start: start.getTime(),
            end: end.getTime() } })
            .then(({ data }) => data.map(e => {
                return {
                    id: e.id,
                    start: new Date(e.start),
                    end: new Date(e.end) }
            }));
    }

    renderChartData(idx) {
        let p = this.trackedPeriods[idx];
        console.log(this.trackedPeriods);
        let startD = p.start.toMoment();
        let endD = p.end.toMoment();
        if (!(startD && endD)) return;
        let start = moment().endOf('day');
        if (endD.valueOf() == 0) {
            switch (p.start.unit) {
                case 'days': start = moment().endOf('day'); break;
                case 'weeks': start = moment().endOf('week'); break;
                case 'months': start = moment().endOf('month'); break;
                default:
            }
        }
        let end = start.clone();
        start.subtract(startD);
        end.subtract(endD);
        console.log(start, end);
        return getChartData(start.toDate(),
                            end.toDate(),
                            this.patterns, this.calendars, this.getCalEvents).then(results => {
            let patternGraphData = this.state.patternGraphData;
            patternGraphData[idx] = {
                start: moment(results.start),
                end: moment(results.end),
                data: results.patternGraphData
            };
            this.setState({ patternGraphData });
        });
    }

    render() {
        console.log(this.state.patternGraphData);
        return (
            <MuiThemeProvider theme={theme}>
            <Button variant="contained" color="primary" onClick={openOptions}>Dashboard</Button>
            {
                this.state.patternGraphData.map((d, idx) => (
                    <div key={idx}>
                    <Typography variant="subtitle1" align="center" color="textPrimary">
                    {this.trackedPeriods[idx].name}
                    </Typography>
                    <Typography variant="caption" align="center">
                    {`${d.start.format('ddd, MMM Do, YYYY')} -
                    ${d.end.format('ddd, MMM Do, YYYY')}`}
                    </Typography>
                    {(d.data.some(dd => dd.value > 1e-3) &&
                    <StyledPatternPieChart data={d.data} />) ||
                    <Typography variant="subtitle1" align="center" color="textSecondary">
                        No data available
                    </Typography>}
                    </div>
                ))
            }
            </MuiThemeProvider>
        );
    }
}

ReactDOM.render(<Popup />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: http://bit.ly/CRA-PWA
serviceWorker.unregister();