forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResources.js
More file actions
143 lines (131 loc) · 3.74 KB
/
Copy pathResources.js
File metadata and controls
143 lines (131 loc) · 3.74 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';
import { graphql, Link } from 'gatsby';
import { ExternalLink, Icon, Tag } from '@newrelic/gatsby-theme-newrelic';
import Section from './Section';
import Title from './Title';
const SITE_TAGS = {
developer: 'https://developer.newrelic.com',
'open source': 'https://opensource.newrelic.com',
docs: 'https://docs.newrelic.com',
github: 'https://github.com',
terraform: 'https://terraform.io',
kubernetes: 'https://kubernetes.io',
youtube: 'https://youtube.com',
discuss: 'https://discuss.newrelic.com',
blog: 'https://blog.newrelic.com',
'newrelic.com': 'https://newrelic.com',
'visual studio': 'https://marketplace.visualstudio.com',
};
const findTag = (resource) =>
resource.url.startsWith('/')
? 'developer'
: Object.keys(SITE_TAGS).find((tag) =>
resource.url.startsWith(SITE_TAGS[tag])
);
const normalizeDeveloperUrl = (url) =>
url.replace('https://developer.newrelic.com', '');
const Resources = ({ page }) => {
const { relatedResources, frontmatter } = page;
const resources = (frontmatter.resources || []).concat(relatedResources);
return resources.length > 0 ? (
<Section>
<Title>Related resources</Title>
<nav>
<ul
css={css`
list-style: none;
margin: 0;
padding: 0;
`}
>
{resources.map((resource) => {
const tag = findTag(resource);
const isDeveloperSite = tag === 'developer';
const LinkElement = isDeveloperSite ? Link : ExternalLink;
const props = isDeveloperSite
? { to: normalizeDeveloperUrl(resource.url) }
: { href: resource.url };
return (
<li
key={resource.url}
css={css`
font-size: 0.875rem;
&:not(:last-child) {
margin-bottom: 1rem;
}
`}
>
<LinkElement
{...props}
css={css`
display: block;
margin-bottom: 0.25rem;
`}
>
<span
css={css`
vertical-align: middle;
`}
>
{resource.title}
</span>
{!isDeveloperSite && (
<Icon
name={Icon.TYPE.EXTERNAL_LINK}
css={css`
margin-left: 0.25rem;
vertical-align: middle;
`}
/>
)}
</LinkElement>
<Tag
css={css`
text-transform: uppercase;
font-size: 0.5625rem;
letter-spacing: 0.5px;
`}
>
{tag}
</Tag>
</li>
);
})}
</ul>
</nav>
</Section>
) : null;
};
Resources.propTypes = {
page: PropTypes.shape({
frontmatter: PropTypes.shape({
resources: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string,
url: PropTypes.string,
})
),
}).isRequired,
relatedResources: PropTypes.shape({
title: PropTypes.string,
url: PropTypes.string,
}),
}).isRequired,
};
export const query = graphql`
fragment Resources_page on Mdx {
frontmatter {
resources {
title
url
}
}
relatedResources(limit: $relatedResourceLimit) {
title
url
}
}
`;
export default Resources;