From 5e71ad14c711aab3be7607cd78cb7878f8d9b022 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 17 Jul 2023 16:39:30 +0530 Subject: [PATCH 1/2] 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 ""; +}; From 2187203773535c0131b25aa95ab1d45f62e5c702 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Tue, 18 Jul 2023 13:28:07 +0530 Subject: [PATCH 2/2] change var names --- src/hooks/useGetGitHubPageInfo.ts | 12 ++++++------ src/popup/pages/home.tsx | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/hooks/useGetGitHubPageInfo.ts b/src/hooks/useGetGitHubPageInfo.ts index 2864e98d..296e64fc 100644 --- a/src/hooks/useGetGitHubPageInfo.ts +++ b/src/hooks/useGetGitHubPageInfo.ts @@ -2,13 +2,13 @@ import { useEffect, useState } from "react"; import { isGithubPullRequestPage, isGithubRepoPage } from "../utils/urlMatchers"; interface GitHubPageInfo { - prUrl: string; - prTitle: string; + pageUrl: string; + pageTitle: string; type: "unknown" | "PR" | "REPO"; } export const usGetGitHubPageInfo = () => { - const [GithubPRPage, setGithubPRPage] = useState({ prUrl: "", prTitle: "", type: "unknown" }); + const [GithubPage, setGithubPage] = useState({ pageUrl: "", pageTitle: "", type: "unknown" }); useEffect(() => { chrome.tabs.query({ active: true, currentWindow: true }, tabs => { @@ -16,14 +16,14 @@ export const usGetGitHubPageInfo = () => { const tab = tabs[0]; if (isGithubPullRequestPage(tab.url!)) { - setGithubPRPage({ prUrl: tab.url!, prTitle: tab.title!.split("by")[0].trim(), type: "PR" }); + setGithubPage({ pageUrl: tab.url!, pageTitle: tab.title!.split("by")[0].trim(), type: "PR" }); } else if (isGithubRepoPage(tab.url!)) { - setGithubPRPage({ prUrl: tab.url!, prTitle: "", type: "REPO" }); + setGithubPage({ pageUrl: tab.url!, pageTitle: "", type: "REPO" }); } } }); }, []); - return GithubPRPage; + return GithubPage; }; diff --git a/src/popup/pages/home.tsx b/src/popup/pages/home.tsx index 9d0ed3e4..91358133 100644 --- a/src/popup/pages/home.tsx +++ b/src/popup/pages/home.tsx @@ -28,7 +28,7 @@ import { HighlightSlide } from "../components/HighlightSlide"; const Home = () => { const { user } = useAuth(); const { currentTabIsOpensaucedUser, checkedUser } = useOpensaucedUserCheck(); - const { prUrl: pageURL, prTitle, type: GitHubPageType } = usGetGitHubPageInfo(); + const { pageUrl, pageTitle, type: GitHubPageType } = usGetGitHubPageInfo(); const [repoOpenSaucedURL, setRepoOpenSaucedURL] = useState(""); const [highlights, setHighlights] = useState([]); const [emojis, setEmojis] = useState[]>([]); @@ -72,7 +72,7 @@ const Home = () => { useEffect(() => { const fetchRepoOpenSaucedURL = async () => { try { - const openSaucedUrl = await getRepoOpenSaucedURL(pageURL); + const openSaucedUrl = await getRepoOpenSaucedURL(pageUrl); setRepoOpenSaucedURL(openSaucedUrl); } catch (error) { @@ -83,7 +83,7 @@ const Home = () => { if (GitHubPageType === "REPO") { void fetchRepoOpenSaucedURL(); } - }, [pageURL]); + }, [pageUrl]); return (
@@ -181,7 +181,7 @@ const Home = () => { inputFields[0].value = data.name; inputFields[1].value = data.description; } - fetch(getRepoAPIURL(pageURL)).then(async res => res.json()) + fetch(getRepoAPIURL(pageUrl)).then(async res => res.json()) .then(data => chrome.tabs.create( { url: "https://www.linkedin.com/in/me/edit/forms/project/new/", active: true }, tab => { @@ -221,7 +221,7 @@ const Home = () => {