Skip to content

Commit

Permalink
fix: update contributors hook to use contributor API endpoint (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Jun 1, 2023
1 parent cbd6b82 commit eff01c0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import useRepoList from "lib/hooks/useRepoList";
import { useFetchUser } from "lib/hooks/useFetchUser";

interface ContributorListTableRow {
contributor: DbRepoPR;
contributor: DbPRContributor;
topic: string;
}

Expand Down Expand Up @@ -50,7 +50,7 @@ const ContributorListTableRow = ({ contributor, topic }: ContributorListTableRow
const totalPrs = data.length;
const last30days = [
{
id: `last30-${contributor.repo_id}`,
id: `last30-${contributor.author_login}`,
color: "hsl(63, 70%, 50%)",
data: days,
},
Expand Down
4 changes: 1 addition & 3 deletions components/organisms/Contributors/contributors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Contributors = ({ repositories }: ContributorProps): JSX.Element => {
const contributors = data.map((pr) => {
return {
host_login: pr.author_login,
first_commit_time: pr.created_at,
first_commit_time: pr.updated_at,
};
});

Expand All @@ -52,8 +52,6 @@ const Contributors = ({ repositories }: ContributorProps): JSX.Element => {
};
});

// console.log(data);

return (
<>
{/* Table section */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ContributorListTableRow from "components/molecules/ContributorListTableRo
import React from "react";

export interface ContributorTableProps {
contributors: DbRepoPR[];
contributors: DbPRContributor[];
topic: string;
loading?: boolean;
}
Expand Down
14 changes: 7 additions & 7 deletions components/organisms/Dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getInsights, useInsights } from "lib/hooks/api/useInsights";
import { calcDaysFromToday } from "lib/utils/date-utils";
import roundedImage from "lib/utils/roundedImages";
import usePullRequests from "lib/hooks/api/usePullRequests";
import getPullRequestsContributors from "lib/utils/get-pr-contributors";
import useContributors from "lib/hooks/api/useContributors";

type ContributorPrMap = { [contributor: string]: DbRepoPR };
export type PrStatusFilter = "open" | "closed" | "all";
Expand All @@ -23,11 +23,11 @@ interface DashboardProps {

const Dashboard = ({ repositories }: DashboardProps): JSX.Element => {
const { data: insightsData, isLoading } = useInsights(repositories);
const { data: prData, meta: prMeta, isError: prError } = usePullRequests(undefined, repositories);
const { data: prData, isError: prError } = usePullRequests(undefined, repositories);
const { data: contributorData, meta: contributorMeta } = useContributors(undefined, repositories);
const [showBots, setShowBots] = useState(false);
const isMobile = useMediaQuery("(max-width:720px)");
const [prStateFilter, setPrStateFilter] = useState<PrStatusFilter>("all");
const contributorData = getPullRequestsContributors(prData);

const handleSetPrFilter = (state: PrStatusFilter) => {
setPrStateFilter(state);
Expand All @@ -37,7 +37,7 @@ const Dashboard = ({ repositories }: DashboardProps): JSX.Element => {
let metadata: ScatterChartMetadata = {
allPrs: prData.length,
openPrs: prData.filter((pr) => pr.state.toLowerCase() === "open").length,
closedPrs: prData.filter((pr) => pr.state.toLowerCase() === "closed" || pr.state.toLowerCase() === "merged").length
closedPrs: prData.filter((pr) => pr.state.toLowerCase() === "closed" || pr.state.toLowerCase() === "merged").length,
};

const uniqueContributors: ContributorPrMap = prData.reduce((prs, curr) => {
Expand Down Expand Up @@ -73,7 +73,7 @@ const Dashboard = ({ repositories }: DashboardProps): JSX.Element => {
x: calcDaysFromToday(new Date(updated_at)),
y: linesCount,
contributor: author_login,
image: roundedImage(`https://www.github.com/${author_image}.png?size=60`, process.env.NEXT_PUBLIC_CLOUD_NAME)
image: roundedImage(`https://www.github.com/${author_image}.png?size=60`, process.env.NEXT_PUBLIC_CLOUD_NAME),
};
return data;
});
Expand All @@ -99,8 +99,8 @@ const Dashboard = ({ repositories }: DashboardProps): JSX.Element => {
metricIncreases={compare1.allPrsTotal - compare2.allPrsTotal >= 0}
increased={compare1.allPrsTotal - compare2.allPrsTotal >= 0}
numChanged={humanizeNumber(Math.abs(compare1.allPrsTotal - compare2.allPrsTotal), "abbreviation")}
value={humanizeNumber(prMeta.itemCount, "comma")}
contributors={contributorData}
value={humanizeNumber(contributorMeta.itemCount, "comma")}
contributors={contributorData.map((contributor) => ({ host_login: contributor.author_login }))}
isLoading={isLoading}
/>
<HighlightCard
Expand Down
17 changes: 8 additions & 9 deletions lib/hooks/api/useContributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import publicApiFetcher from "lib/utils/public-api-fetcher";
import getFilterQuery from "lib/utils/get-filter-query";

interface PaginatedResponse {
readonly data: DbRepoPR[];
readonly data: DbPRContributor[];
readonly meta: Meta;
}

/**
* Fetch contributors based on pull requests.
* Replace with contributors API endpoint when available.
*
* @param intialLimit
* @param repoIds
* @param range
* @returns
*
* @param intialLimit
* @param repoIds
* @param range
* @returns
*/
const useContributors = (intialLimit = 10, repoIds: number[] = [], range = 30) => {
const router = useRouter();
Expand Down Expand Up @@ -51,7 +50,7 @@ const useContributors = (intialLimit = 10, repoIds: number[] = [], range = 30) =

query.set("range", `${range}`);

const baseEndpoint = "prs/search";
const baseEndpoint = "contributors/search";
const endpointString = `${baseEndpoint}?${query.toString()}`;

const { data, error, mutate } = useSWR<PaginatedResponse, Error>(
Expand All @@ -67,7 +66,7 @@ const useContributors = (intialLimit = 10, repoIds: number[] = [], range = 30) =
mutate,
page,
setPage,
setLimit
setLimit,
};
};

Expand Down
5 changes: 5 additions & 0 deletions next-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ interface DbRepoPR {
readonly last_updated_at: string;
}

interface DbPRContributor {
readonly author_login: string;
readonly updated_at: string;
}

interface DbFollowUser {
readonly id: number;
readonly user_id: number;
Expand Down

0 comments on commit eff01c0

Please sign in to comment.