aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/ui/Feeds.js
blob: d68131308552131aac165a25fbb518b4e0bf622d (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
import { useState, useEffect } from 'react';
import { Link, useLocation, useHistory, useParams } from 'react-router-dom';

import qs from 'qs';
import moment from 'moment';

import Message from './Message';
import Spinner from './Spinner';

import UserInfo from './UserInfo';

import { getMessages } from '../api';

/**
 * @typedef {Object} Query
 * @property {string} baseUrl
 * @property {Object=} search
 * @property {string} pageParam
 */

/**
 * @typedef {Object} PageProps
 * @property {string=} search
 * @property {import('../api').SecureUser} visitor
 * @property {import('../api').Message[]=} msgs
 */

/**
 * @param {PageProps} props
 */
export function Discover({ visitor }) {
  const location = useLocation();
  let search = qs.parse(location.search.substring(1));
  const query = {
    baseUrl: '/api/messages',
    search: search,
    pageParam: search.search ? 'page' : 'before_mid'
  };
  return (<Feed authRequired={false} query={query} visitor={visitor} />);
}

/**
 * @param {PageProps} props
 */
export function Discussions({ visitor }) {
  const query = {
    baseUrl: '/api/messages/discussions',
    pageParam: 'to'
  };
  return (<Feed authRequired={false} query={query} visitor={visitor} />);
}

/**
 * @param {PageProps} props
 */
export function Blog({ visitor }) {
  const { user } = useParams();
  const location = useLocation();
  const search = {
    ...qs.parse(location.search.substring(1)),
    uname: user
  };
  const query = {
    baseUrl: '/api/messages',
    search: search,
    pageParam: search.search ? 'page' : 'before_mid'
  };
  return (
    <>
      <div className="msg-cont">
        <UserInfo uname={user} />
      </div>
      <Feed authRequired={false} query={query} visitor={visitor} />
    </>
  );
}

/**
 * @param {PageProps} props
 */
export function Tag({ visitor }) {
  const params = useParams();
  const { tag } = params;
  const query = {
    baseUrl: '/api/messages',
    search: {
      tag: tag
    },
    pageParam: 'before_mid'
  };
  return (<Feed authRequired={false} query={query} visitor={visitor} />);
}

/**
 * @param {PageProps} props
 */
export function Home({ visitor }) {
  const query = {
    baseUrl: '/api/home',
    pageParam: 'before_mid'
  };
  return (<Feed authRequired={true} query={query} visitor={visitor} />);
}

/**
 * @typedef {Object} FeedState
 * @property { boolean } authRequired
 * @property { import('../api').SecureUser } visitor
 * @property { import('../api').Message[]= } msgs
 * @property { Query} query
 */

/**
 * @param {FeedState} props
 */
function Feed({ visitor, query, authRequired }) {
  const location = useLocation();
  const history = useHistory();
  const [state, setState] = useState({
    authRequired: authRequired,
    hash: visitor.hash,
    msgs: [],
    nextpage: null,
    error: false,
    tag: ''
  });
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    setLoading(true);
    const filter = location.search.substring(1);
    let getPageParam = (pageParam, lastMessage, filterParams) => {
      const pageValue = pageParam === 'before_mid' ? lastMessage.mid : pageParam === 'page' ? (Number(filterParams.page) || 0) + 1 : moment.utc(lastMessage.updated).valueOf();
      let newFilter = { ...filterParams };
      newFilter[pageParam] = pageValue;
      return `?${qs.stringify(newFilter)}`;
    };
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
    const filterParams = qs.parse(filter);
    let params = Object.assign({}, filterParams || {}, query.search || {});
    let url = query.baseUrl;
    if (state.hash) {
      params.hash = state.hash;
    }
    if (!params.hash && state.authRequired) {
      history.push('/');
    }
    getMessages(url, params)
      .then(response => {
        const { data } = response;
        const { pageParam } = query;
        const lastMessage = data.slice(-1)[0] || {};
        const nextpage = getPageParam(pageParam, lastMessage, filterParams);
        setState((prevState) => {
          return {
            ...prevState,
            msgs: data,
            nextpage: nextpage,
            tag: qs.parse(location.search.substring(1))['tag'] || ''
          };
        });
        setLoading(false);
      }).catch(ex => {
        setState((prevState) => {
          return {
            ...prevState,
            error: true
          };
        });
      });
  }, [location.search, state.hash, state.authRequired, history, query]);
  return (state.msgs.length > 0 ? (
    <div className="msgs">
      {
        state.tag && (
          <p className="page">
            <Link to={{ pathname: `/tag/${state.tag}` }}>
              <span> All posts with tag&nbsp;</span><b>{state.tag}</b>
            </Link>
          </p>
        )
      }
      {
        state.msgs.map(msg =>
          <Message key={msg.mid} data={msg} visitor={visitor} />)
      }
      {
        state.msgs.length >= 20 && (
          <p className="page">
            <Link to={{ pathname: location.pathname, search: state.nextpage }} rel="prev">Next </Link>
          </p>
        )
      }
    </div>
  ) : state.error ? <div>error</div> : loading ? <div className="msgs"><Spinner /><Spinner /><Spinner /><Spinner /></div> : <div>No more messages</div>
  );
}