summaryrefslogtreecommitdiff
path: root/popup.jsx
blob: 2844f75905108abc7ec1d803fbe3ee2ded5c569b (plain)
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
const {
  React,
  getModule
} = require('powercord/webpack');
const { Modal } = require('powercord/components/modal');
const {
  FormTitle,
  FormNotice
} = require('powercord/components');
const {
  close,
  open
} = require('powercord/modal');
const { SETTINGS_TIMEZONE } = require('./constants');
const {
  lookup,
  timezones
} = require('./tz');
const { SelectInput } = require('powercord/components/settings');

class TimezoneConverterPopup extends React.PureComponent {
  constructor (props) {
    super(props);
    this.state = { timezone: props.toTimezone };
  }

  render () {
    console.log(this.state, this.props);
    return <Modal size={Modal.Sizes.LARGE} className={'vrmodal'}>
      <Modal.Header>
        <FormTitle tag={'h4'}>
          Time converter
        </FormTitle>
        <Modal.CloseButton onClick={close}/>
      </Modal.Header>
      <Modal.Content>
        <p><SelectInput
          searchable={true}
          onChange={(e) => {
            this.setState({ timezone: lookup(e.value) });
          }}
          value={this.state.timezone.code}
          options={[
            ...timezones.map(it => ({
              value: it.code,
              label: `${it.name} (${it.offset})`
            }))
          ]}
        >
          Convert to timezone
        </SelectInput></p>
        <p>{text(this.props.time, this.props.fromTimezone, this.state.timezone)}</p>
        <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      </Modal.Content>
    </Modal>;
  }
}

function a (time) {
  return ((time / 60) | 0) + ':' + (time % 60);
}

function text (time, fromTimezone, toTimezone) {
  let offset = toTimezone.offsetminutes - fromTimezone.offsetminutes;
  let adjusted = (time + offset) % (24 * 60);
  return `${a(time)} ${fromTimezone.code} -> ${a(adjusted)} ${toTimezone.code}`;
}

function getLabel (time, fromTimezone, toTimezone) {
  return <span
    onClick={() => open(() => <TimezoneConverterPopup
      time={time}
      fromTimezone={fromTimezone}
      toTimezone={toTimezone}/>)}>
    {text(time, fromTimezone, toTimezone)}
  </span>;
}

module.exports = {
  TimezoneConverterPopup,
  getLabel
};