Skip to content

Commit

Permalink
Merge pull request #47 from kudos-ink/refactor/avatar
Browse files Browse the repository at this point in the history
[Refactor] get icon url from json instead of notion
  • Loading branch information
leapalazzolo authored Feb 6, 2024
2 parents d351e04 + 9c7422d commit 75d2919
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 40 deletions.
5 changes: 1 addition & 4 deletions app/explore/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ export default async function ExplorePage({ params }: IProps) {
page_size: DEFAULT_PAGE_SIZE,
filter: queryFilter,
});
const contributions = transformNotionDataToContributions(
data,
filterOptions.repositories,
);
const contributions = transformNotionDataToContributions(data);
const items: PaginatedCustomDataResponse<Contribution> = {
data: contributions,
hasMore: data.has_more,
Expand Down
5 changes: 1 addition & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ export default async function Home() {
const data = await queryDatabase({
page_size: DEFAULT_PAGE_SIZE,
});
const contributions = transformNotionDataToContributions(
data,
filterOptions.repositories,
);
const contributions = transformNotionDataToContributions(data);
const items: PaginatedCustomDataResponse<Contribution> = {
data: contributions,
hasMore: data.has_more,
Expand Down
48 changes: 44 additions & 4 deletions components/table/table.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
"use client";
import React, { useCallback, useEffect, useRef, useState } from "react";
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { useMediaQuery } from "react-responsive";
import {
Table as NuiTable,
Expand All @@ -16,6 +22,8 @@ import { ExternalLink, Content, Time, Project } from "./row";
import { useContributions } from "@/hooks/useContributions";
import { KudosQueryParameters } from "@/lib/notion/types";
import dynamic from "next/dynamic";
import { useFilters } from "@/contexts/filters";
import { extractRepositoryUrlFromIssue } from "@/utils/github";
const Labels = dynamic(() => import("./row").then((m) => m.Labels), {
ssr: false,
});
Expand All @@ -29,6 +37,11 @@ interface ITableProps {
items: PaginatedContributions;
queries?: Partial<KudosQueryParameters>;
}

interface RepositoryMap {
[key: string]: string;
}

export const Table = ({ items, queries = {} }: ITableProps) => {
const [columns, setColumns] = useState<IColumn[]>([
{ name: "PROJECT", uid: "project" },
Expand All @@ -38,6 +51,8 @@ export const Table = ({ items, queries = {} }: ITableProps) => {
{ name: "ACTIONS", uid: "actions" },
]);

const { filterOptions } = useFilters();

const {
data: results,
fetchNextPage,
Expand All @@ -54,15 +69,40 @@ export const Table = ({ items, queries = {} }: ITableProps) => {
return results?.pages.flatMap((page) => page.data) || [];
}, [results]);

const repositoryIconMap: RepositoryMap = useMemo(() => {
const map: RepositoryMap = {};
filterOptions.repositories.forEach((repository) => {
map[repository.repository_url] = repository.repository_url
.toLowerCase()
.includes("polkadot")
? "/images/polkadot-logo.png"
: repository.icon;
});
return map;
}, [filterOptions.repositories]);
const useGetIconByRepositoryUrl = (repositoryIconMap: RepositoryMap) => {
const getIconByRepositoryUrl = useCallback(
(repositoryUrl: string): string | null => {
return repositoryIconMap[repositoryUrl];
},
[repositoryIconMap],
);

return getIconByRepositoryUrl;
};

const getIconByRepositoryUrl = useGetIconByRepositoryUrl(repositoryIconMap);

const renderCell = React.useCallback(
(item: Contribution, columnKey: React.Key) => {
const cellValue = item[columnKey as keyof Contribution];

const repository = extractRepositoryUrlFromIssue(item.url);
const avatar = !!repository ? getIconByRepositoryUrl(repository) : null;
switch (columnKey) {
case "project":
return (
<Project
avatarSrc={item.avatar}
avatarSrc={avatar}
name={item.project}
organization={item.organization}
repository={item.repository}
Expand Down Expand Up @@ -109,7 +149,7 @@ export const Table = ({ items, queries = {} }: ITableProps) => {
return cellValue;
}
},
[],
[getIconByRepositoryUrl],
);

const handleScroll = useCallback(() => {
Expand Down
5 changes: 1 addition & 4 deletions hooks/useContributions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export const useContributions = (
start_cursor: next_cursor,
});
return {
data: transformNotionDataToContributions(
response,
filterOptions.repositories,
),
data: transformNotionDataToContributions(response),
hasMore: response.has_more,
nextCursor: response.next_cursor ?? undefined,
};
Expand Down
1 change: 0 additions & 1 deletion types/contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { PaginatedCustomDataResponse } from ".";

export type Contribution = {
id: number;
avatar: string | null;
labels: string[];
languages: string[];
project: string;
Expand Down
2 changes: 2 additions & 0 deletions types/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type FilterOption = {
value: string;
interests?: string[];
id?: string;
icon?: string;
repository_url?: string;
};

export type FilterOptions = {
Expand Down
31 changes: 12 additions & 19 deletions utils/github.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
import { Repository } from "@/types/filters";

export function getImagePath(
githubUrl: string,
repositories: Repository[],
): string | null {
const lowerCaseGithubUrl = githubUrl.toLowerCase();

if (lowerCaseGithubUrl.includes("polkadot")) {
return "/images/polkadot-logo.png";
}

const repository = repositories.find(
({ repository_url }) => repository_url.toLowerCase() === lowerCaseGithubUrl,
);

return repository?.icon ?? null;
}

export const getProjectUrls = (organization: string, repository?: string) => {
return {
organizationUrl: `https://github.com/${organization}`,
Expand All @@ -25,3 +6,15 @@ export const getProjectUrls = (organization: string, repository?: string) => {
: undefined,
};
};
export const extractRepositoryUrlFromIssue = (
issueUrl: string,
): string | null => {
// Regex pattern to match the repository URL in the issue URL
const regex = /(https:\/\/github\.com\/[^/]+\/[^/]+)\/issues\/\d+/;

// Use the regex pattern to extract the repository URL
const match = issueUrl.match(regex);

// If a match is found, return the repository URL, otherwise, return null
return match ? match[1] : null;
};
5 changes: 1 addition & 4 deletions utils/notion.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { QueryDatabaseResponse } from "@notionhq/client/build/src/api-endpoints";
import { Contribution } from "@/types/contribution";
import { getImagePath } from "./github";
import {
GOOD_FIRST_ISSUE_KEY,
GOOD_FIRST_ISSUE_LABELS,
INTEREST_KEY,
LANGUAGES_KEY,
PROJECTS_KEY,
} from "@/data/filters";
import { FilterOption, Filters, Repository } from "@/types/filters";
import { FilterOption, Filters } from "@/types/filters";

export function transformNotionDataToContributions(
notionData: QueryDatabaseResponse,
repositoriesData: Repository[],
): Contribution[] {
return notionData.results.reduce((acc: Contribution[], currentItem: any) => {
const properties = currentItem.properties;
Expand All @@ -23,7 +21,6 @@ export function transformNotionDataToContributions(
const organization = urlElements.pop();
const contribution: Contribution = {
id,
avatar: getImagePath(avatarKey, repositoriesData),
labels: properties["Issue Labels"].multi_select.map(
(label: any) => label.name,
),
Expand Down

0 comments on commit 75d2919

Please sign in to comment.