aboutsummaryrefslogtreecommitdiff
path: root/src/Home.js
blob: 5685d3bac1c418c55d2b3fdbb561dfb3c2ff57ee (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import React, { Component } from "react";
import { List, Header, Icon, Loader, Grid, Image } from "semantic-ui-react";
import Moment from "react-moment";
import "moment/locale/es";

import { Link } from "@reach/router";

class Home extends Component {
  constructor() {
    super();
    this.state = {
      lastAgeThreads: [],
      newThreadsList: [],
      latestNews: [],
      isLoaded: false,
      autoRefresh: null
    };
    this.lastTime = 0;
    this.refreshCooldown = 15;
    this.cooldownCounter = 0;
    this.lastTimeNoAge = 0;
    this.notificationSound = new Audio(
      "https://bienvenidoainternet.org/msn.ogg"
    );
  }

  componentDidMount() {
    fetch("https://bienvenidoainternet.org/cgi/api/lastage?limit=10")
      .then(response => {
        return response.json();
      })
      .then(resource => {
        this.lastTime = resource.time;
        this.lastTimeNoAge = resource.time;
        this.setState({
          lastAgeThreads: resource["threads"],
          autoRefresh: setInterval(() => this.updateAges(), 1000)
        });
      });

    fetch("https://bienvenidoainternet.org/cgi/api/newThreads?limit=10")
      .then(response => {
        return response.json();
      })
      .then(resource => {
        this.setState({
          newThreadsList: resource["threads"]
        });
      });

    fetch("https://bienvenidoainternet.org/cgi/api/blotter")
      .then(response => {
        return response.json();
      })
      .then(resource => {
        this.setState({
          latestNews: resource["news"],
          isLoaded: true
        });
      });
    window.scrollTo(0, 0);
  }

  componentWillUnmount() {
    clearInterval(this.state.autoRefresh);
  }

  async updateAges() {
    this.cooldownCounter++;
    if (this.cooldownCounter < this.refreshCooldown) {
      return;
    } else {
      this.cooldownCounter = 0;
    }

    let apiURl = `https://bienvenidoainternet.org/cgi/api/lastage?time=${this.lastTime}&limit=10`;
    fetch(apiURl)
      .then(response => {
        return response.json();
      })
      .then(resource => {
        if (resource.state === "success") {
          if (resource.threads.length > 0) {
            this.setState({
              lastAgeThreads: resource.threads
            });
            this.refreshCooldown = 30;
            this.notificationSound.play();
          } else {
            this.refreshCooldown += 15;
            this.lastTimeNoAge = resource.time;
            if (this.refreshCooldown > 60) {
              this.refreshCooldown = 60;
            }
          }
        }
        this.lastTime = resource.time;
      });
  }

  render() {
    if (!this.state.isLoaded) {
      return (
        <Loader active centered="true">
          Cargando ...
        </Loader>
      );
    }

    const { newThreadsList, lastAgeThreads, latestNews } = this.state;
    const { nightMode } = this.props;
    document.title = "B.a.I Home";
    return (
      <React.Fragment>
        <Grid
          columns={2}
          divided
          container
          doubling
          className={nightMode ? "homeContainer inverted" : "homeContainer"}
        >
          <Grid.Row centered columns={1}>
            <Grid.Column centered textAlign="center">
              <Image
                src="https://bienvenidoainternet.org/bai_logo.png"
                style={{ filter: `invert(${nightMode ? 0 : 1})` }}
                centered
              />
              <p>
                Resistiendo desde el 2010
                <br />
                <small>Ahora con 95% extra javascript!</small>
              </p>
            </Grid.Column>
          </Grid.Row>
          <Grid.Row>
            <Grid.Column>
              <Header as="h4" inverted={nightMode}>
                Hilos activos
              </Header>
              <List divided inverted={nightMode}>
                {lastAgeThreads.map(thread => (
                  <List.Item key={thread.id}>
                    <List.Icon
                      name={
                        thread.bumped > this.lastTimeNoAge
                          ? "bullhorn"
                          : "comment alternate outline"
                      }
                      size="large"
                      verticalAlign="middle"
                    />
                    <List.Content>
                      <List.Header
                        as={Link}
                        to={`${thread.dir}/read/${thread.id}`}
                        className="postMessage"
                      >
                        {thread.content}
                      </List.Header>
                      <List.Description>
                        <Moment fromNow unix locale="es" date={thread.last} />{" "}
                        en {thread.board_fulln}{" "}
                      </List.Description>
                    </List.Content>
                  </List.Item>
                ))}
              </List>
            </Grid.Column>
            <Grid.Column>
              <Header as="h4" inverted={nightMode}>
                Nuevos hilos
              </Header>
              <List divided inverted={nightMode}>
                {newThreadsList.map(thread => (
                  <List.Item key={thread.id}>
                    <List.Icon
                      name="comment alternate outline"
                      size="large"
                      verticalAlign="middle"
                    />
                    <List.Content>
                      <List.Header
                        as={Link}
                        to={`${thread.dir}/read/${thread.id}`}
                      >
                        {thread.content}
                      </List.Header>
                      <List.Description>
                        <Moment
                          fromNow
                          unix
                          locale="es"
                          date={thread.timestamp}
                        />{" "}
                        en {thread.board_fulln}{" "}
                      </List.Description>
                    </List.Content>
                  </List.Item>
                ))}
              </List>
            </Grid.Column>
          </Grid.Row>
          <Grid.Row columns={1}>
            <Grid.Column>
              <Header as="h4" inverted={nightMode}>
                Blotter
              </Header>
              <List divided inverted={nightMode}>
                {latestNews.map(n => (
                  <List.Item key={n.timestamp}>
                    <List.Content>
                      <div dangerouslySetInnerHTML={{ __html: n.message }} />
                      <small>
                        <Moment fromNow unix locale="es" date={n.timestamp} />
                      </small>
                    </List.Content>
                  </List.Item>
                ))}
              </List>
            </Grid.Column>
          </Grid.Row>
        </Grid>
      </React.Fragment>
    );
  }
}

export default Home;