From 5e71ad14c711aab3be7607cd78cb7878f8d9b022 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 17 Jul 2023 16:39:30 +0530 Subject: [PATCH] feat: view repos on opensauced --- src/popup/pages/home.tsx | 99 +++++++++++++++++++---------- src/utils/fetchOpenSaucedApiData.ts | 14 ++++ 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/popup/pages/home.tsx b/src/popup/pages/home.tsx index 2d319915..9d0ed3e4 100644 --- a/src/popup/pages/home.tsx +++ b/src/popup/pages/home.tsx @@ -5,7 +5,7 @@ import { HiUserCircle, } from "react-icons/hi2"; import { IoLogoLinkedin } from "react-icons/io5"; -import { FiSettings } from "react-icons/fi"; +import { FiSettings, FiExternalLink } from "react-icons/fi"; import { Navigation, Pagination, A11y } from "swiper"; import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/swiper-bundle.min.css"; @@ -16,7 +16,7 @@ import { Profile } from "./profile"; import { goTo } from "react-chrome-extension-router"; import PostOnHighlight from "./posthighlight"; import { getRepoAPIURL } from "../../utils/urlMatchers"; -import { getEmojis, getHighlights } from "../../utils/fetchOpenSaucedApiData"; +import { getEmojis, getHighlights, getRepoOpenSaucedURL } from "../../utils/fetchOpenSaucedApiData"; import Help from "./help"; import { useEffect, useState, useRef } from "react"; import Settings from "./settings"; @@ -29,6 +29,7 @@ const Home = () => { const { user } = useAuth(); const { currentTabIsOpensaucedUser, checkedUser } = useOpensaucedUserCheck(); const { prUrl: pageURL, prTitle, type: GitHubPageType } = usGetGitHubPageInfo(); + const [repoOpenSaucedURL, setRepoOpenSaucedURL] = useState(""); const [highlights, setHighlights] = useState([]); const [emojis, setEmojis] = useState[]>([]); const toolsRef = useRef(null); @@ -68,6 +69,22 @@ const Home = () => { void fetchEmojis(); }, []); + useEffect(() => { + const fetchRepoOpenSaucedURL = async () => { + try { + const openSaucedUrl = await getRepoOpenSaucedURL(pageURL); + + setRepoOpenSaucedURL(openSaucedUrl); + } catch (error) { + console.log(error); + } + }; + + if (GitHubPageType === "REPO") { + void fetchRepoOpenSaucedURL(); + } + }, [pageURL]); + return (
@@ -146,42 +163,58 @@ const Home = () => { > {GitHubPageType === "REPO" && ( - + + + { + repoOpenSaucedURL && ( + + + View On OpenSauced + + ) + } + )} {GitHubPageType === "PR" && ( diff --git a/src/utils/fetchOpenSaucedApiData.ts b/src/utils/fetchOpenSaucedApiData.ts index 8c9dc027..79d5028c 100644 --- a/src/utils/fetchOpenSaucedApiData.ts +++ b/src/utils/fetchOpenSaucedApiData.ts @@ -6,6 +6,7 @@ import { OPEN_SAUCED_HIGHLIGHTS_LIST_ENDPOINT, OPEN_SAUCED_HIGHLIGHT_ENDPOINT, OPEN_SAUCED_EMOJIS_ENDPOINT, + OPEN_SAUCED_INSIGHTS_DOMAIN, } from "../constants"; import { IInsight } from "../ts/InsightDto"; import { GeneralAPIResponse, Highlights } from "../ts/types"; @@ -230,3 +231,16 @@ export const removeReactionOnHighlight = async (userToken: string, highlightId: headers: { Authorization: `Bearer ${userToken}` }, method: "DELETE", }); + +export const getRepoOpenSaucedURL = async (repoUrl: string) => { + const repoFullName = repoUrl.split("/").slice(-2) + .join("/"); + const response = await fetch(`${OPEN_SAUCED_REPOS_ENDPOINT}/${repoFullName}`, { method: "GET" }); + + if (response.status === 200) { + const data = await response.json(); + + return `https://${OPEN_SAUCED_INSIGHTS_DOMAIN}/${data.language}/repositories/filter/${data.full_name}`; + } + return ""; +};