Skip to content

Commit 058fb65

Browse files
committed
➕ADD: Twitter api Dashboard, tweets page with fav twts
1 parent 2b820f0 commit 058fb65

File tree

14 files changed

+204
-72
lines changed

14 files changed

+204
-72
lines changed

components/NowPlaying.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import fetcher from "@/lib/fetcher";
44

55
export default function NowPlaying() {
66
const { data } = useSWR("/api/now-playing", fetcher);
7-
console.log("DATA", data);
87

98
return (
109
<div className="flex flex-row-reverse sm:flex-row mb-8 space-x-0 sm:space-x-2 w-full">

components/TopTracks.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Track from "@/components/Track";
55

66
export default function TopTracks() {
77
const { data } = useSWR("/api/top-tracks", fetcher);
8-
console.log("data", data);
98

109
if (!data) {
1110
return null;

components/metrics/TwitterCard.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default function TwitterCard({ header, link, metric }) {
2+
return (
3+
<div className="metric-card border border-gray-200 dark:border-gray-800 rounded p-4 max-w-72 w-full">
4+
<a
5+
aria-label={header}
6+
target="_blank"
7+
rel="noopener noreferrer"
8+
href={link}
9+
>
10+
<div className="flex items-center text-gray-900 dark:text-gray-100">
11+
{header}
12+
<svg
13+
className="h-4 w-4 ml-1"
14+
xmlns="http://www.w3.org/2000/svg"
15+
fill="none"
16+
viewBox="0 0 24 24"
17+
stroke="currentColor"
18+
>
19+
<path
20+
strokeLinecap="round"
21+
strokeLinejoin="round"
22+
strokeWidth={2}
23+
d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
24+
/>
25+
</svg>
26+
</div>
27+
</a>
28+
<p className="mt-2 text-3xl font-bold spacing-sm text-black dark:text-white">
29+
{metric || "-"}
30+
</p>
31+
</div>
32+
);
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import useSWR from "swr";
2+
import format from "comma-number";
3+
4+
import fetcher from "@/lib/fetcher";
5+
import { useEffect } from "react";
6+
import TwitterCard from "@/components/metrics/TwitterCard";
7+
8+
export default function TwitterStatistics({ type }) {
9+
const { data } = useSWR("/api/twitter-user", fetcher);
10+
11+
if (!data) {
12+
return null;
13+
}
14+
console.log(data);
15+
16+
// useEffect(() => {
17+
// return () => {};
18+
// }, [data]);
19+
20+
let followers = data?.user?.public_metrics?.followers_count;
21+
let following = data?.user?.public_metrics?.following_count;
22+
let count = data?.user?.public_metrics?.tweet_count;
23+
let username = data?.user?.username;
24+
console.log(data.user.public_metrics.followers_count);
25+
const link = "/tweets";
26+
27+
return (
28+
<>
29+
{type == "followers" && (
30+
<TwitterCard
31+
header="Twitter Followers"
32+
link={link}
33+
metric={followers}
34+
/>
35+
)}
36+
{type == "following" && (
37+
<TwitterCard
38+
header="Twitter Following"
39+
link={link}
40+
metric={following}
41+
/>
42+
)}
43+
{type == "total_tweets" && (
44+
<TwitterCard header="Tweet Count" link={link} metric={count} />
45+
)}
46+
{type == "username" && (
47+
<TwitterCard header="Twitter username" link={link} metric={username} />
48+
)}
49+
</>
50+
);
51+
}

lib/twitter.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const getTweets = async (ids) => {
1616
`https://api.twitter.com/2/tweets?${queryParams}`,
1717
{
1818
headers: {
19-
Authorization: `Bearer ${process.env.TWITTER_API_KEY}`,
19+
Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
2020
},
2121
}
2222
);
@@ -57,3 +57,17 @@ export const getTweets = async (ids) => {
5757
return [tweetWithAuthor, ...allTweets];
5858
}, []);
5959
};
60+
61+
export const getUser = async (username) => {
62+
const url = `https://api.twitter.com/2/users/by/username/${username}?user.fields=created_at,description,profile_image_url,public_metrics`;
63+
const response = await fetch(url, {
64+
headers: {
65+
Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
66+
"Content-Type": "application/x-www-form-urlencoded",
67+
"Access-Control-Allow-Origin": "*",
68+
"Access-Control-Allow-Methods": "GET",
69+
},
70+
});
71+
const data = await response.json();
72+
return data;
73+
};

next.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
images: {
3+
domains: [
4+
"i.scdn.co", // Spotify Album Art
5+
"pbs.twimg.com", // Twitter Profile Picture
6+
],
7+
},
8+
};

pages/api/github.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default async (_, res) => {
77
const user = await userResponse.json();
88
const repositories = await userReposResponse.json();
99
// If GitHub API limit reached (demo purpose)
10-
// console.log(user);
1110
if (repositories.message) {
1211
return res.status(200).json({
1312
followers: null,

pages/api/now-playing.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// export default async (_, res) => {
44
// const response = await getNowPlaying();
55
// const { item } = await response.json();
6-
// console.log("item - now-playing", item);
76

87
// if (!item) {
98
// const track = {};
@@ -50,7 +49,6 @@ export default async (_, res) => {
5049
albumImageUrl,
5150
songUrl,
5251
};
53-
console.log("track", track);
5452

5553
res.setHeader(
5654
"Cache-Control",

pages/api/top-tracks.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ export default async (_, res) => {
44
const response = await getTopTracks();
55
const { items } = await response.json();
66

7-
console.log("items - top-tracks", items);
8-
97
if (!items) {
108
const track = [];
119

@@ -18,10 +16,6 @@ export default async (_, res) => {
1816
title: track.name,
1917
}));
2018

21-
console.log("TRACKS", tracks);
22-
23-
// console.log(tracks);
24-
2519
res.setHeader(
2620
"Cache-Control",
2721
"public, s-maxage=86400, stale-while-revalidate=43200"

pages/api/twitter-user.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getUser } from "@/lib/twitter";
2+
3+
export default async (_, res) => {
4+
const response = await getUser("mannupaaji");
5+
const { data } = response;
6+
7+
if (!response) {
8+
const user = null;
9+
10+
return res.status(200).json({ user });
11+
}
12+
13+
const user = {
14+
...data,
15+
};
16+
17+
res.setHeader(
18+
"Cache-Control",
19+
"public, s-maxage=86400, stale-while-revalidate=43200"
20+
);
21+
22+
return res.status(200).json({ user });
23+
};

0 commit comments

Comments
 (0)