awesome-uses/src/components/Person.js

153 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-01-22 17:14:25 +00:00
import React, { useState, useEffect, useRef } from 'react';
2020-01-08 16:25:18 +00:00
import PropTypes from 'prop-types';
2020-01-07 20:23:53 +00:00
import { name } from 'country-emoji';
2023-02-20 20:41:29 +00:00
import { useParams } from '@remix-run/react';
2023-04-14 13:51:15 +00:00
import * as icons from '../util/icons';
2020-01-07 20:23:53 +00:00
2023-02-20 20:41:29 +00:00
export default function Person({ person }) {
2020-01-07 20:23:53 +00:00
const url = new URL(person.url);
2023-04-14 13:51:15 +00:00
const twitter = person.twitter
? `https://unavatar.io/${person.twitter.replace('@', '')}`
: null;
2023-01-09 19:05:02 +00:00
const website = `https://unavatar.io/${url.host}`;
2023-04-14 13:51:15 +00:00
const unavatar = person.twitter
? `${twitter}?fallback=${website}&ttl=28d`
: website;
// const img = `https://images.weserv.nl/?url=${unavatar}&w=100&l=9&af&il&n=-1`;
const mastodonArr = person.mastodon
? person.mastodon.replace('@', '').split('@')
: null;
const webfinger =
person.mastodon
? JSON.parse(`https://${mastodonArr[0]}/.well-known/webfinger?resource=https://${mastodonArr[0]}/@${mastodonArr[1]}`)
: null;
const wfIndex =
person.mastodon && webfinger
? webfinger.links.findIndex((link) => link.rel === 'http://webfinger.net/rel/avatar')
: null;
const wfAvatar =
person.mastodon && wfIndex
? webfinger.links[wfIndex].href
: website;
const img = person.twitter ? unavatar : (person.mastodon ? wfAvatar : website);
2023-02-20 20:41:29 +00:00
const { tag: currentTag } = useParams();
2020-01-07 20:23:53 +00:00
return (
2023-04-14 13:51:15 +00:00
<div
className="PersonWrapper"
style={{ contentVisibility: 'auto', containIntrinsicHeight: '560px' }}
>
2023-02-20 20:41:29 +00:00
<div className="PersonInner">
2020-01-07 22:58:15 +00:00
<header>
2020-01-30 20:04:30 +00:00
<img
width="50"
height="50"
src={img}
alt={person.name}
2023-03-25 01:06:57 +00:00
onError={({ currentTarget }) => {
currentTarget.onerror = null; // prevents looping
2023-04-14 13:51:15 +00:00
currentTarget.src = '/default.png';
2023-03-25 01:06:57 +00:00
}}
2020-01-30 20:04:30 +00:00
loading="lazy"
/>
2020-01-07 21:38:31 +00:00
<h3>
<a href={person.url} target="_blank" rel="noopener noreferrer">
{person.name}
</a>{' '}
{person.emoji}
2020-01-07 21:38:31 +00:00
</h3>
<a
2020-01-10 15:12:12 +00:00
target="_blank"
rel="noopener noreferrer"
2020-01-07 21:38:31 +00:00
className="displayLink"
href={person.url}
2020-01-17 20:40:06 +00:00
>
{url.host}
{url.pathname.replace(/\/$/, '')}
</a>
2020-01-07 21:38:31 +00:00
</header>
2020-01-07 20:23:53 +00:00
<p>{person.description}</p>
2023-02-20 20:41:29 +00:00
<ul className="Tags">
2023-04-14 13:51:15 +00:00
{person.tags.map((tag) => (
<li
className={`Tag small ${tag === currentTag ? 'currentTag' : ''}`}
key={tag}
>
2020-01-07 20:23:53 +00:00
{tag}
2023-02-20 20:41:29 +00:00
</li>
2020-01-07 20:23:53 +00:00
))}
2023-02-20 20:41:29 +00:00
</ul>
</div>
<div className="PersonDeets">
2020-01-07 20:23:53 +00:00
<span className="country" title={name(person.country)}>
{person.country}
</span>
{person.computer && (
<span title={`Computer: ${person.computer}`}>
<img
height="40"
src={icons[person.computer]}
alt={person.computer}
/>
</span>
)}
{person.phone && (
<span title={`Uses an ${person.phone}`}>
<img height="50" src={icons[person.phone]} alt={person.phone} />
</span>
)}
2024-05-28 15:35:55 +00:00
{(person.twitter || person.mastodon) && (
2024-05-23 20:23:09 +00:00
<div className="SocialHandle">
2020-01-07 21:07:32 +00:00
<a
2024-05-23 20:23:09 +00:00
href={
person.twitter
? `https://twitter.com/${person.twitter.replace('@', '')}`
: `https://${mastodonArr[1]}/@${mastodonArr[0]}`
}
2020-01-07 21:07:32 +00:00
target="_blank"
rel="noopener noreferrer"
>
2020-01-07 20:23:53 +00:00
<span className="at">@</span>
2024-05-23 20:23:09 +00:00
{
person.twitter
? person.twitter.replace('@', '')
: `${mastodonArr[1]}/@${mastodonArr[0]}`
}
2020-01-07 20:23:53 +00:00
</a>
2023-02-20 20:41:29 +00:00
</div>
2020-01-07 20:23:53 +00:00
)}
2023-02-20 20:41:29 +00:00
</div>
</div>
2020-01-07 20:23:53 +00:00
);
}
2020-01-07 22:58:15 +00:00
2020-01-08 16:25:18 +00:00
Person.propTypes = {
person: PropTypes.shape({
github: PropTypes.string,
name: PropTypes.string,
url: PropTypes.string,
emoji: PropTypes.string,
description: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.string),
country: PropTypes.string,
2020-01-08 16:25:18 +00:00
computer: PropTypes.oneOf(['apple', 'windows', 'linux']),
2023-02-20 20:41:29 +00:00
phone: PropTypes.oneOf(['iphone', 'android', 'windowsphone', 'flipphone']),
2020-01-08 16:25:18 +00:00
twitter(props, propName, componentName) {
if (!/^@?(\w){1,15}$/.test(props[propName])) {
return new Error(
`Invalid prop \`${propName}\` supplied to` +
2024-05-23 20:23:09 +00:00
` \`${componentName}\`. This isn't a legit Twitter handle.`
);
}
},
mastodon(props, propName, componentName) {
if (!/^@(\w){1,30}@(\w)+\.(\w)+$/.test(props[propName])) {
return new Error(
`Invalid prop \`${propName}\` supplied to` +
` \`${componentName}\`. This isn't a legit Mastodon handle.`
2020-01-08 16:25:18 +00:00
);
}
},
}),
};