Passed
Push — renovate/eslint-7.x ( eb7113 )
by
unknown
05:36
created

src/components/cards.js   A

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 219
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 172
mnd 0
bc 0
fnc 4
dl 0
loc 219
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A SingleImageCard.render 0 31 1
A CardImage.render 0 36 1
A CardVertical.render 0 40 1
B Card.render 0 81 1
1
import React, { Component } from "react"
2
import { Link } from "gatsby"
3
import { GatsbyImage } from "gatsby-plugin-image"
4
5
import Icon from "./Icon"
6
7
import "./cards.scss"
8
9
/**
10
 * Render a card layout (image / tag / title / excerpt).
11
 */
12
export class Card extends Component {
13
  render() {
14
    const {
15
      title,
16
      icon = null,
17
      body = null,
18
      localFile,
19
      link,
20
      metadata = false,
21
      tags = [],
22
      created = null,
23
    } = this.props
24
25
    const image = (
26
      <GatsbyImage
27
        image={{
28
          ...localFile.childImageSharp.gatsbyImageData,
29
          aspectRatio: 3 / 2,
30
        }}
31
        alt={title}
32
      />
33
    )
34
35
    const absoluteUrlRegex = /^https?:\/\/|^\/\//i
36
37
    return (
38
      <article className={"cardItem"}>
39
        {absoluteUrlRegex.test(link) || (
40
          <Link to={link}>
41
            <header>
42
              <figure>{image}</figure>
43
            </header>
44
45
            <main className={"cardItem__summary"}>
46
              <div className={"cardItem__heading"}>
47
                <h3>
48
                  {icon && <Icon icon={icon} />} {title}
49
                </h3>
50
              </div>
51
52
              {body && <div dangerouslySetInnerHTML={{ __html: body }}></div>}
53
            </main>
54
          </Link>
55
        )}
56
57
        {absoluteUrlRegex.test(link) && (
58
          <a href={link} target="_blank" rel="noopener noreferrer">
59
            <header>
60
              <figure>{image}</figure>
61
            </header>
62
63
            <main className={"cardItem__summary"}>
64
              <div className={"cardItem__heading"}>
65
                <h3>
66
                  {icon && <i className={`fa ${icon}`} aria-hidden={true}></i>}{" "}
67
                  {title}
68
                </h3>
69
              </div>
70
71
              {body && <div dangerouslySetInnerHTML={{ __html: body }}></div>}
72
            </main>
73
          </a>
74
        )}
75
76
        {metadata && (
77
          <footer className={"cardItem__footer article__tags"}>
78
            <span className={"datetime"}>
79
              <i className={"fa fa-clock-o"} aria-hidden="true"></i> {created}
80
            </span>
81
            {tags.length > 0 && (
82
              <span className={"tag__wrapper"}>
83
                <i className={"fa fa-tags"} aria-hidden="true"></i>{" "}
84
                {tags.map(({ path, name }, i) => (
85
                  <Link to={path.alias} key={i}>
86
                    <span className={"tag__label"}>#{name}</span>
87
                  </Link>
88
                ))}
89
              </span>
90
            )}
91
          </footer>
92
        )}
93
      </article>
94
    )
95
  }
96
}
97
98
export class CardImage extends Component {
99
  render() {
100
    const { title, localFile, link, body = null } = this.props
101
    const absoluteUrlRegex = /^https?:\/\/|^\/\//i
102
103
    const image = (
104
      <GatsbyImage
105
        image={{
106
          ...localFile.childImageSharp.gatsbyImageData,
107
          aspectRatio: 2 / 1,
108
        }}
109
        alt={title}
110
      />
111
    )
112
113
    const content = (
114
      <header>
115
        <figure>{image}</figure>
116
        <div className={"gradient gradient--70"}></div>
117
        <div className={"cardItem__heading"}>
118
          <h3>{title}</h3>
119
          {body && <div dangerouslySetInnerHTML={{ __html: body }}></div>}
120
        </div>
121
      </header>
122
    )
123
124
    return (
125
      <article className={"cardItem cardItem--image"}>
126
        {link === false && content}
127
        {link !== false &&
128
          (absoluteUrlRegex.test(link) || <Link to={link}>{content}</Link>)}
129
        {link !== false && absoluteUrlRegex.test(link) && (
130
          <a href={link} target="_blank" rel="noopener noreferrer">
131
            {content}
132
          </a>
133
        )}
134
      </article>
135
    )
136
  }
137
}
138
139
export class SingleImageCard extends Component {
140
  render() {
141
    const { localFile, link } = this.props
142
143
    const absoluteUrlRegex = /^https?:\/\/|^\/\//i
144
145
    const image = (
146
      <GatsbyImage
147
        image={{
148
          ...localFile.childImageSharp.gatsbyImageData,
149
          aspectRatio: 2 / 1,
150
        }}
151
      />
152
    )
153
154
    return (
155
      <article className={"cardItem cardItem--vertical"}>
156
        {absoluteUrlRegex.test(link) || (
157
          <Link to={link}>
158
            <header>
159
              <figure>{image}</figure>
160
            </header>
161
          </Link>
162
        )}
163
        {absoluteUrlRegex.test(link) && (
164
          <a href={link} target="_blank" rel="noopener noreferrer">
165
            <header>
166
              <figure>{image}</figure>
167
            </header>
168
          </a>
169
        )}
170
      </article>
171
    )
172
  }
173
}
174
175
export class CardVertical extends Component {
176
  render() {
177
    const { title, localFile, link } = this.props
178
179
    const absoluteUrlRegex = /^https?:\/\/|^\/\//i
180
181
    const image = (
182
      <GatsbyImage
183
        image={{
184
          ...localFile.childImageSharp.gatsbyImageData,
185
          aspectRatio: 6 / 8,
186
        }}
187
        alt={title}
188
      />
189
    )
190
191
    return (
192
      <article className={"cardItem cardItem--vertical"}>
193
        {absoluteUrlRegex.test(link) || (
194
          <Link to={link}>
195
            <header>
196
              <figure>{image}</figure>
197
              <div className={"gradient gradient--70"}></div>
198
              <div className={"cardItem__heading"}>
199
                <h3>{title}</h3>
200
              </div>
201
            </header>
202
          </Link>
203
        )}
204
        {absoluteUrlRegex.test(link) && (
205
          <a href={link} target="_blank" rel="noopener noreferrer">
206
            <header>
207
              <figure>{image}</figure>
208
              <div className={"gradient gradient--70"}></div>
209
              <div className={"cardItem__heading"}>
210
                <h3>{title}</h3>
211
              </div>
212
            </header>
213
          </a>
214
        )}
215
      </article>
216
    )
217
  }
218
}
219