summaryrefslogtreecommitdiffstats
path: root/browser/components/pocket/content/panels/js/components/ArticleList/ArticleList.jsx
blob: 25679bc638386f66d1723f9fecf0d3c88e861170 (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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

import React, { useState } from "react";
import TelemetryLink from "../TelemetryLink/TelemetryLink";

function ArticleUrl(props) {
  // We turn off the link if we're either a saved article, or if the url doesn't exist.
  if (props.savedArticle || !props.url) {
    return (
      <div className="stp_article_list_saved_article">{props.children}</div>
    );
  }
  return (
    <TelemetryLink
      className="stp_article_list_link"
      href={props.url}
      source={props.source}
      position={props.position}
      model={props.model}
    >
      {props.children}
    </TelemetryLink>
  );
}

function Article(props) {
  function encodeThumbnail(rawSource) {
    return rawSource
      ? `https://img-getpocket.cdn.mozilla.net/80x80/filters:format(jpeg):quality(60):no_upscale():strip_exif()/${encodeURIComponent(
          rawSource
        )}`
      : null;
  }

  const [thumbnailLoaded, setThumbnailLoaded] = useState(false);
  const [thumbnailLoadFailed, setThumbnailLoadFailed] = useState(false);

  const {
    article,
    savedArticle,
    position,
    source,
    model,
    utmParams,
    openInPocketReader,
  } = props;

  if (!article.url && !article.resolved_url && !article.given_url) {
    return null;
  }
  const url = new URL(article.url || article.resolved_url || article.given_url);
  const urlSearchParams = new URLSearchParams(utmParams);

  if (
    openInPocketReader &&
    article.item_id &&
    !url.href.match(/getpocket\.com\/read/)
  ) {
    url.href = `https://getpocket.com/read/${article.item_id}`;
  }

  for (let [key, val] of urlSearchParams.entries()) {
    url.searchParams.set(key, val);
  }

  // Using array notation because there is a key titled `1` (`images` is an object)
  const thumbnail =
    article.thumbnail ||
    encodeThumbnail(article?.top_image_url || article?.images?.["1"]?.src);
  const alt = article.alt || "thumbnail image";
  const title = article.title || article.resolved_title || article.given_title;
  // Sometimes domain_metadata is not there, depending on the source.
  const publisher =
    article.publisher ||
    article.domain_metadata?.name ||
    article.resolved_domain;

  return (
    <li className="stp_article_list_item">
      <ArticleUrl
        url={url.href}
        savedArticle={savedArticle}
        position={position}
        source={source}
        model={model}
        utmParams={utmParams}
      >
        <>
          {thumbnail && !thumbnailLoadFailed ? (
            <img
              className="stp_article_list_thumb"
              src={thumbnail}
              alt={alt}
              width="40"
              height="40"
              onLoad={() => {
                setThumbnailLoaded(true);
              }}
              onError={() => {
                setThumbnailLoadFailed(true);
              }}
              style={{
                visibility: thumbnailLoaded ? `visible` : `hidden`,
              }}
            />
          ) : (
            <div className="stp_article_list_thumb_placeholder" />
          )}
          <div className="stp_article_list_meta">
            <header className="stp_article_list_header">{title}</header>
            <p className="stp_article_list_publisher">{publisher}</p>
          </div>
        </>
      </ArticleUrl>
    </li>
  );
}

function ArticleList(props) {
  return (
    <ul className="stp_article_list">
      {props.articles?.map((article, position) => (
        <Article
          article={article}
          savedArticle={props.savedArticle}
          position={position}
          source={props.source}
          model={props.model}
          utmParams={props.utmParams}
          openInPocketReader={props.openInPocketReader}
        />
      ))}
    </ul>
  );
}

export default ArticleList;