Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace PRSocialCard with githubs Opengraph image #889

Merged
merged 6 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Title from "components/atoms/Typography/title";
import { generateApiPrUrl } from "lib/utils/github";
import React, { useEffect, useState } from "react";
import PullRequestHighlightCard from "../PullRequestHighlightCard/pull-request-highlight-card";
import { fetchGithubPRInfo } from "lib/hooks/fetchGithubPRInfo";
import {
DropdownMenu,
Expand All @@ -17,6 +16,7 @@ import { FaUserPlus } from "react-icons/fa";
import { GrFlag } from "react-icons/gr";

import { ToastTrigger } from "lib/utils/toast-trigger";
import GhOpenGraphImg from "../GhOpenGraphImg/gh-open-graph-img";

interface ContributorHighlightCardProps {
title?: string;
Expand Down Expand Up @@ -126,7 +126,7 @@ const ContributorHighlightCard = ({ title, desc, prLink, user }: ContributorHigh
</div>

{/* Generated OG card section */}
<PullRequestHighlightCard prLink={prLink} />
<GhOpenGraphImg githubLink={prLink} />
</article>
);
};
Expand Down
24 changes: 24 additions & 0 deletions components/molecules/GhOpenGraphImg/gh-open-graph-img.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { generateGhOgImage } from "lib/utils/github";
import Image from "next/image";
import InvalidImage from "img/404 Image.svg";
import { useEffect, useState } from "react";

interface GhOpenGraphImgProps {
githubLink: string;
}

const GhOpenGraphImg = ({ githubLink }: GhOpenGraphImgProps): JSX.Element => {
const { isValid, url } = generateGhOgImage(githubLink);

return (
<>
{url && (
<picture>
<img src={isValid ? url : InvalidImage} alt={isValid ? "github og image" : "invalid url image"} />
</picture>
)}
</>
);
};

export default GhOpenGraphImg;
4 changes: 2 additions & 2 deletions components/molecules/HighlightInput/highlight-input-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { createHighlights } from "lib/hooks/createHighlights";

import { ToastTrigger } from "lib/utils/toast-trigger";
import { ChangeEvent, useEffect, useRef, useState } from "react";
import PullRequestHighlightCard from "../PullRequestHighlightCard/pull-request-highlight-card";
import { useSWRConfig } from "swr";
import useSupabaseAuth from "lib/hooks/useSupabaseAuth";
import GhOpenGraphImg from "../GhOpenGraphImg/gh-open-graph-img";

const HighlightInputForm = (): JSX.Element => {
const { user } = useSupabaseAuth();
Expand Down Expand Up @@ -116,7 +116,7 @@ const HighlightInputForm = (): JSX.Element => {
/>
</div>

{pullrequestLink && isDivFocused && <PullRequestHighlightCard prLink={pullrequestLink} />}
{pullrequestLink && isDivFocused && <GhOpenGraphImg githubLink={pullrequestLink} />}

{isDivFocused && (
<Button disabled={!bodyText} className="ml-auto" type="primary">
Expand Down

This file was deleted.

18 changes: 17 additions & 1 deletion lib/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ const generateApiPrUrl = (
}
};

export { getAvatarById, getAvatarByUsername, getProfileLink, getRepoIssuesLink, generateApiPrUrl };
const generateGhOgImage = (githubUrl: string): { isValid: boolean; url: string } => {
try {
const trimmedUrl = githubUrl.trim();
const url = new URL(trimmedUrl.includes("https://") ? trimmedUrl : `https://${trimmedUrl}`);
const { pathname } = url;

if (url.hostname !== "github.com") {
return { isValid: false, url: "" };
}

return { isValid: true, url: `https://opengraph.githubassets.com/1${pathname}` };
} catch (err) {
return { isValid: false, url: "" };
}
};

export { getAvatarById, getAvatarByUsername, getProfileLink, getRepoIssuesLink, generateApiPrUrl, generateGhOgImage };
18 changes: 15 additions & 3 deletions tests/lib/utils/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { getAvatarById, getAvatarByUsername, getProfileLink, getRepoIssuesLink } from "lib/utils/github";
import {
getAvatarById,
getAvatarByUsername,
getProfileLink,
getRepoIssuesLink,
generateGhOgImage
} from "lib/utils/github";

describe("[lib] github methods", () => {

it("Should return a valid avatar link", () => {
const result = getAvatarById("Deadreyo", 460);
expect(result).toEqual("https://avatars.githubusercontent.com/u/Deadreyo?size=460&v=4");
Expand All @@ -21,5 +26,12 @@ describe("[lib] github methods", () => {
const result = getRepoIssuesLink("Deadreyo/insights");
expect(result).toEqual("https://github.com/Deadreyo/insights/issues");
});

it("Should return a valid image src link", () => {
const result = generateGhOgImage("https://github.com/open-sauced/hot/pull/448");
expect(result).toEqual({ isValid: true, url: "https://opengraph.githubassets.com/1/open-sauced/hot/pull/448" });
});
it("Should return an object with isValid set to false", () => {
const result = generateGhOgImage("https://gitub.com/open-sauced/hot/pull/448");
expect(result).toEqual({ isValid: false, url: "" });
});
});