From f33d9fd8ab66242aecf07be23d961c33b9779727 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Tue, 18 Jul 2023 17:23:07 +0530 Subject: [PATCH] feat: view repos on opensauced (#218) * feat: view repos on opensauced * change var names --- src/hooks/useGetGitHubPageInfo.ts | 12 ++-- src/popup/pages/home.tsx | 103 ++++++++++++++++++---------- src/utils/fetchOpenSaucedApiData.ts | 14 ++++ 3 files changed, 88 insertions(+), 41 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 2d319915..91358133 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"; @@ -28,7 +28,8 @@ 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[]>([]); 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,49 +163,65 @@ const Home = () => { > {GitHubPageType === "REPO" && ( - + + + { + repoOpenSaucedURL && ( + + + View On OpenSauced + + ) + } + )} {GitHubPageType === "PR" && (