Skip to content

Commit

Permalink
feat: add location aside to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulieScanlon committed May 24, 2023
1 parent 2845d21 commit 6e4514b
Show file tree
Hide file tree
Showing 10 changed files with 13,198 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"cmdk": "^0.1.18",
"country-flag-icons": "^1.5.5",
"d3-geo": "^3.0.1",
"dotted-map": "^2.2.3",
"faunadb": "^4.6.0",
"gatsby": "^5.0.0",
"gatsby-plugin-gatsby-cloud": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/loading.js
Expand Up @@ -26,7 +26,7 @@ const Loading = ({ className }) => {
};

Loading.defaultProps = {
className: 'fill-salmon'
className: 'fill-muted'
};

Loading.propTypes = {
Expand Down
101 changes: 101 additions & 0 deletions src/components/location-aside.js
@@ -0,0 +1,101 @@
import React, { useState, useEffect } from 'react';

import DottedMap from 'dotted-map/without-countries';

import dottedMapString from '../utils/dotted-string-map';
import Loading from './loading';

const TheMap = ({ location: { lat, lng } }) => {
const map = new DottedMap({ map: JSON.parse(dottedMapString) });
map.addPin({
lat: lat,
lng: lng,
svgOptions: { color: '#ff6090', radius: 1.5 }
});
const svgMap = map.getSVG({
radius: 0.22,
color: '#8b87ea',
shape: 'circle',
backgroundColor: 'transparent'
});

return (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 400 250"
className="relative w-full h-full z-10"
aria-label="US data map"
>
<image href={`data:image/svg+xml;utf8,${encodeURIComponent(svgMap)}`} className="w-auto h-full" />
</svg>
);
};

const LocationAside = () => {
const [location, setLocation] = useState(null);

const setup = async () => {
try {
const twitterUser = await fetch('https://paulieapi.gatsbyjs.io/api/get-twitter-user', {
method: 'POST',
body: JSON.stringify({
username: 'PaulieScanlon'
})
});

if (!twitterUser.ok) {
throw new Error('get-latest-tweets');
}

const userData = await twitterUser.json();
const location = userData.user.location.split(',');
// The location comes from my Twitter profile that needs to be set as a:
// flag, city, lat, lng
// E.g 🇬🇧, London, 51.5072, 0.1276

setLocation({
flag: location[0],
city: location[1],
lat: Number(location[2]),
lng: Number(location[3])
});
} catch (error) {
console.error('Error: [location-aside] - get-twitter-user');
}
};

useEffect(() => {
setup();
}, []);

return (
<div className="flex items-center justify-center rounded border border-outline bg-surface/50 px-4 sm:px-6 py-6 min-h-[158px]">
{location ? (
<div>
<div className="mb-4">
<h5 className="mb-0 text-base text-center leading-6 font-semibold uppercase text-secondary">Location</h5>
<div className="flex items-center justify-center gap-2 text-slate-300 text-sm text-center">
<span className="">I'm currently in</span>
<span className="m-0 flex gap-1 items-center justify-center">{location.city}</span>
<span className="pt-1">{location.flag}</span>
</div>
</div>
<div className="rounded border border-outline bg-surface">
<TheMap location={location} />
</div>
<div className="mt-4 leading-tight">
<small className="text-slate-400 text-xs">Powered by </small>
<a href="https://paulieapi.gatsbyjs.io/" target="_blank" rel="noreferrer" className="text-xs">
Paulie API
</a>
</div>
</div>
) : (
<Loading />
)}
</div>
);
};

export default LocationAside;
4 changes: 2 additions & 2 deletions src/components/recent-github-user-events.js
Expand Up @@ -64,7 +64,7 @@ const RecentGitHubUserEvent = () => {
<div className="lg:h-16 mb-4">
{isLoading ? (
<div className="flex items-center justify-center h-full">
<Loading className="fill-salmon" />
<Loading />
</div>
) : (
<div className="grid sm:grid-cols-auto-1fr gap-2 justify-center text-center sm:text-left items-center">
Expand Down Expand Up @@ -105,7 +105,7 @@ const RecentGitHubUserEvent = () => {
<div className="rounded border border-outline bg-surface p-2 sm:p-4 bg-background h-96 overflow-y-hidden">
{isLoading ? (
<div className="flex items-center justify-center h-full">
<Loading className="fill-salmon" />
<Loading />
</div>
) : (
<ul className="list-none m-0 p-0 overflow-y-auto overflow-x-hidden h-[355px]">
Expand Down
12 changes: 4 additions & 8 deletions src/components/recent-tweets.js
Expand Up @@ -73,9 +73,7 @@ const RecentTweets = () => {
<div className="flex flex-col items-stretch rounded border border-outline bg-surface p-4">
<div className="lg:h-16 mb-4">
{isLoading ? (
<div className="flex items-center justify-center h-full">
{isError ? <ErrorState /> : <Loading className="fill-salmon" />}
</div>
<div className="flex items-center justify-center h-full">{isError ? <ErrorState /> : <Loading />}</div>
) : (
<div className="grid sm:grid-cols-auto-1fr gap-2 justify-center text-center sm:text-left items-center">
<img
Expand Down Expand Up @@ -114,9 +112,7 @@ const RecentTweets = () => {
</div>
<div className="rounded border border-outline bg-surface p-2 sm:p-4 bg-background h-96 overflow-y-hidden">
{isLoading ? (
<div className="flex items-center justify-center h-full">
{isError ? <ErrorState /> : <Loading className="fill-salmon" />}
</div>
<div className="flex items-center justify-center h-full">{isError ? <ErrorState /> : <Loading />}</div>
) : (
<ul className="list-none m-0 p-0 overflow-y-auto overflow-x-hidden h-[355px]">
{response.tweets.map((tweet, index) => {
Expand All @@ -131,12 +127,12 @@ const RecentTweets = () => {
</ul>
)}
</div>
{/* <div className="mt-4 leading-tight">
<div className="mt-4 leading-tight">
<small className="text-slate-400 text-xs">Powered by </small>
<a href="https://paulieapi.gatsbyjs.io/" target="_blank" rel="noreferrer" className="text-xs">
Paulie API
</a>
</div> */}
</div>
</div>
);
};
Expand Down
6 changes: 5 additions & 1 deletion src/pages/dashboard.js
Expand Up @@ -5,6 +5,7 @@ import getUnicodeFlagIcon from 'country-flag-icons/unicode';
import Seo from '../components/seo';
import AsideElement from '../components/aside-element';
import GenericAside from '../components/generic-aside';
import LocationAside from '../components/location-aside';

import Accordion from '../components/accordion';
import AllDaysChart from '../components/all-days-chart';
Expand Down Expand Up @@ -246,7 +247,10 @@ const Page = ({
</div>

<AsideElement>
<GenericAside />
<div className="flex flex-col gap-4">
<GenericAside />
<LocationAside />
</div>
</AsideElement>
</Fragment>
);
Expand Down
6 changes: 5 additions & 1 deletion src/templates/page.js
Expand Up @@ -5,6 +5,7 @@ import MdxParser from '../components/mdx-parser';
import AsideElement from '../components/aside-element';
import Seo from '../components/seo';
import GenericAside from '../components/generic-aside';
import LocationAside from '../components/location-aside';

const Page = ({
data: {
Expand All @@ -20,7 +21,10 @@ const Page = ({
<small className="mb-4 leading-6 font-semibold capitalize text-primary">{title}</small>
<MdxParser>{body}</MdxParser>
<AsideElement>
<GenericAside />
<div className="flex flex-col gap-4">
<GenericAside />
<LocationAside />
</div>
</AsideElement>
</Fragment>
);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/dotted-string-map.js

Large diffs are not rendered by default.

0 comments on commit 6e4514b

Please sign in to comment.