Skip to content

Commit 0ccd0e7

Browse files
BekahHWbdougieBrian 'bdougie' Douglas
authored
feat: latest highlights (#154)
* Add highlights list endpoint * Add getHighlights function * Add comma * Add functionality to fetch and display highlights * Resolve Errors and add Highlight interface * Link title and add styling * Add styling to author link * Refactor to add pagination * Handle links opening in new tab * Add functionality to remove space in link to user who created highlight * Update src/pages/home.tsx * Revert "Update src/pages/home.tsx" This reverts commit 8e8bb0b. --------- Co-authored-by: Brian Douglas <[email protected]> Co-authored-by: Brian 'bdougie' Douglas <[email protected]>
1 parent fd66ce4 commit 0ccd0e7

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const OPEN_SAUCED_USER_INSIGHTS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/u
1717
export const OPEN_SAUCED_AI_PR_DESCRIPTION_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/prs/description/generate`;
1818
export const OPEN_SAUCED_USER_HIGHLIGHTS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/user/highlights`;
1919
export const OPEN_SAUCED_AI_CODE_REFACTOR_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/prs/suggestion/generate`;
20-
20+
export const OPEN_SAUCED_HIGHLIGHTS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/highlights/list`;
2121

2222
// GitHub constants/selectors
2323
export const GITHUB_PROFILE_MENU_SELECTOR = ".p-nickname.vcard-username.d-block";

src/pages/home.tsx

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,71 @@
11
import {
2+
HiArrowLeft,
3+
HiArrowRight,
24
HiArrowTopRightOnSquare,
35
HiOutlineQuestionMarkCircle,
46
HiPencil,
57
HiUserCircle,
68
} from "react-icons/hi2";
9+
import { useEffect, useState } from "react";
710
import OpenSaucedLogo from "../assets/opensauced-logo.svg";
811
import { useAuth } from "../hooks/useAuth";
912
import { useOpensaucedUserCheck } from "../hooks/useOpensaucedUserCheck";
1013
import { Profile } from "./profile";
1114
import { goTo } from "react-chrome-extension-router";
1215
import AIPRDescription from "./aiprdescription";
1316
import PostOnHighlight from "./posthighlight";
14-
17+
import { getHighlights } from "../utils/fetchOpenSaucedApiData";
1518

1619
import Help from "./help";
1720
import { OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants";
21+
interface Highlight {
22+
highlight: string;
23+
title: string;
24+
name: string;
25+
url: string;
26+
}
27+
1828

1929
const Home = () => {
2030
const { user } = useAuth();
2131
const { currentTabIsOpensaucedUser, checkedUser } = useOpensaucedUserCheck();
32+
const [highlights, setHighlights] = useState<Highlight[]>([]);
33+
const [currentPage, setCurrentPage] = useState(0);
34+
const [currentName, setCurrentName] = useState<string>("");
35+
36+
37+
useEffect(() => {
38+
const fetchHighlights = async () => {
39+
try {
40+
const userHighlightsData = await getHighlights();
41+
const highlights = userHighlightsData.data;
42+
43+
setHighlights(highlights);
44+
} catch (error) {
45+
console.log(error);
46+
}
47+
};
48+
49+
void fetchHighlights();
50+
}, []);
51+
52+
const handlePrevious = () => {
53+
setCurrentPage(prevPage => prevPage - 1);
54+
};
55+
56+
const handleNext = () => {
57+
setCurrentPage(prevPage => prevPage + 1);
58+
};
59+
60+
const formatNameForLink = (name: string): string => name.replace(/\s/g, "");
61+
62+
useEffect(() => {
63+
// Update the current name when the highlight changes
64+
if (highlights[currentPage]?.name) {
65+
setCurrentName(formatNameForLink(highlights[currentPage].name));
66+
}
67+
}, [highlights, currentPage]);
68+
2269

2370
return (
2471
<div className="p-4 bg-slate-800">
@@ -50,6 +97,63 @@ const Home = () => {
5097
</header>
5198

5299
<main className="main-content">
100+
<h3 className="text font-medium text-base leading-10">Latest Highlight:</h3>
101+
102+
{highlights.length > 0 && (
103+
104+
<div className="border border-white/40 rounded-sm p-3 mt-2">
105+
<h3 className="text-base font-medium">
106+
<a
107+
className="text-blue-500 hover:text-blue-700 underline cursor-pointer"
108+
href={highlights[currentPage]?.url}
109+
rel="noopener noreferrer"
110+
target="_blank"
111+
>
112+
{highlights[currentPage]?.title}
113+
</a>
114+
</h3>
115+
116+
117+
<div className="flex items-center">
118+
<div className="mr-2">Author:</div>
119+
120+
<a
121+
className="text-blue-500 hover:text-blue-700 underline cursor-pointer"
122+
href={`https://insights.opensauced.pizza/user/${formatNameForLink(highlights[currentPage]?.name)}`}
123+
rel="noopener noreferrer"
124+
target="_blank"
125+
126+
127+
>
128+
{highlights[currentPage]?.name}
129+
</a>
130+
</div>
131+
132+
<p className="py-2">
133+
{highlights[currentPage]?.highlight}
134+
</p>
135+
136+
<div className="flex justify-center">
137+
<div className="flex justify-center mt-4">
138+
<button
139+
className="px-4 py-2 rounded-md bg-blue-500 text-white disabled:opacity-50"
140+
disabled={currentPage === 0}
141+
onClick={handlePrevious}
142+
>
143+
<HiArrowLeft />
144+
</button>
145+
146+
<button
147+
className="px-4 py-2 rounded-md bg-blue-500 text-white disabled:opacity-50 ml-4"
148+
disabled={currentPage === highlights.length - 1}
149+
onClick={handleNext}
150+
>
151+
<HiArrowRight />
152+
</button>
153+
</div>
154+
</div>
155+
</div>)}
156+
53157
<h3 className="text font-medium text-base leading-10">Tools:</h3>
54158

55159
<div className="tools flex flex-col gap-2">

src/utils/fetchOpenSaucedApiData.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
OPEN_SAUCED_REPOS_ENDPOINT,
66
OPEN_SAUCED_USER_INSIGHTS_ENDPOINT,
77
OPEN_SAUCED_USER_HIGHLIGHTS_ENDPOINT,
8+
OPEN_SAUCED_HIGHLIGHTS_ENDPOINT,
89
} from "../constants";
910
import { IInsight } from "../ts/InsightDto";
1011

@@ -177,6 +178,14 @@ export const updateInsight = async (userToken: string, repoId: string, checked:
177178

178179
return response.status === 200;
179180
};
181+
export const getHighlights = async () => {
182+
const response = await fetch(
183+
`${OPEN_SAUCED_HIGHLIGHTS_ENDPOINT}`,
184+
{ method: "GET" },
185+
);
186+
187+
return response.json();
188+
};
180189

181190
export const cerateHighlight = async (userToken: string, url: string, title: string, highlight: string, shipped_at?: string) => fetch(OPEN_SAUCED_USER_HIGHLIGHTS_ENDPOINT, {
182191
headers: {

0 commit comments

Comments
 (0)