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

feat: repository selection & add to insights #693

Merged
merged 22 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/atoms/Checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from "react";
import React, { HTMLAttributes } from "react";
import { Checkbox as SupbaseCheckboxComponent } from "@supabase/ui";

interface CheckboxProps extends React.ComponentProps<typeof SupbaseCheckboxComponent> {
//Add additional prop definitions here
title: string
}

const Checkbox: React.FC<CheckboxProps> = ( props ) => {
return <SupbaseCheckboxComponent {...props} />;
};

export default Checkbox;
export default Checkbox;
19 changes: 15 additions & 4 deletions components/molecules/RepoRow/repo-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import { getRelativeDays } from "lib/utils/date-utils";
import StackedAvatar from "../StackedAvatar/stacked-avatar";
import PullRequestOverview from "../PullRequestOverview/pull-request-overview";
import TableRepositoryName from "../TableRepositoryName/table-repository-name";
import Checkbox from "components/atoms/Checkbox/checkbox";
import useSupabaseAuth from "lib/hooks/useSupabaseAuth";

interface RepoProps {
repo: RepositoriesRows;
topic?: string;
user: string | string[] | undefined;
userPage: string | string[] | undefined;
selected?: boolean;
handleOnSelectRepo: (repo: RepositoriesRows) => void;
}

const getActivity = (total?: number, loading?: boolean) => {
Expand Down Expand Up @@ -84,7 +88,7 @@ const getPrsSpam = (total: number, spam: number): number => {
return result;
};

const RepoRow = ({ repo, topic, user }: RepoProps): JSX.Element => {
const RepoRow = ({ repo, topic, userPage, selected, handleOnSelectRepo }: RepoProps): JSX.Element => {
const {
name,
owner: handle,
Expand All @@ -100,6 +104,7 @@ const RepoRow = ({ repo, topic, user }: RepoProps): JSX.Element => {
prVelocityCount
} = repo;

const { user } = useSupabaseAuth();
const { data: contributorData, meta: contributorMeta } = useContributionsList(repo.id, "", "updated_at");
const { data: commitsData, meta: commitMeta, isLoading: commitLoading } = useRepositoryCommits(repo.id);
const totalPrs = getTotalPrs(openPrsCount, mergedPrsCount, closedPrsCount, draftPrsCount);
Expand All @@ -117,6 +122,11 @@ const RepoRow = ({ repo, topic, user }: RepoProps): JSX.Element => {
];

const [tableOpen, setTableOpen] = useState<boolean>(false);

const handleSelectCheckbox = () => {
handleOnSelectRepo(repo);
};

return (
<>
<div
Expand All @@ -126,7 +136,7 @@ const RepoRow = ({ repo, topic, user }: RepoProps): JSX.Element => {
{/* Row: Repository Name and Pr overview */}
<div className="flex items-center gap-x-3">
<div className="w-[55%]">
<TableRepositoryName topic={topic} avatarURL={ownerAvatar} name={name} handle={handle} user={user} />
<TableRepositoryName topic={topic} avatarURL={ownerAvatar} name={name} handle={handle} user={userPage} />
</div>
<div className="w-[45%]">
{repo.id ? <PullRequestOverview
Expand Down Expand Up @@ -204,9 +214,10 @@ const RepoRow = ({ repo, topic, user }: RepoProps): JSX.Element => {
</div>
</div>
<div className={`${classNames.row} `}>
<Checkbox label="" checked={selected? true : false} onChange={handleSelectCheckbox} disabled={!user} title={!user? "Connect to GitHub" : ""} className={`checked:[&>*]:!bg-orange-500 ${ user? "[&>*]:!border-orange-500 [&>*]:hover:!bg-orange-600": "[&>*]:!border-light-slate-8"}`}/>
{/* Column: Repository Name */}
<div className={classNames.cols.repository}>
<TableRepositoryName topic={topic} avatarURL={ownerAvatar} name={name} handle={handle} user={user}></TableRepositoryName>
<TableRepositoryName topic={topic} avatarURL={ownerAvatar} name={name} handle={handle} user={userPage}></TableRepositoryName>
</div>

{/* Column: Activity */}
Expand Down
12 changes: 10 additions & 2 deletions components/molecules/TableRepositoryName/table-repository-name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Avatar from "components/atoms/Avatar/avatar";
import { truncateString } from "lib/utils/truncate-string";
import { StaticImageData } from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
import React from "react";

interface TableRepositoryNameProps {
Expand All @@ -14,6 +15,13 @@ interface TableRepositoryNameProps {
}

const TableRepositoryName = ({ avatarURL, name, handle, topic, user }: TableRepositoryNameProps): JSX.Element => {
const router = useRouter();

let filterLink = `/${user ? `pages/${user}/` : ""}${topic}/repositories/filter/${handle}/${name}`;
if(router.asPath === filterLink) {
filterLink = `/${user ? `pages/${user}/` : ""}${topic}/repositories/filter/recent`;
}

return (
<div className="flex items-center gap-2.5">
{/* Avatar */}
Expand All @@ -24,12 +32,12 @@ const TableRepositoryName = ({ avatarURL, name, handle, topic, user }: TableRepo
{/* Text */}
<div className="flex flex-col justify-center">
<div title={name} className=" text-base text-light-slate-12 tracking-tight">
<Link href={`/${user ? `pages/${user}/` : ""}${topic}/repositories/filter/${handle}/${name}`}>
<Link href={filterLink}>
<a>{name && name.length > 10 ? truncateString(name, 12) : name}</a>
</Link>
</div>
<div className=" text-sm text-light-slate-11 truncate max-w-[85px] md:max-w-[110px]">
<Link href={`/${user ? `pages/${user}/` : ""}${topic}/repositories/filter/${handle}/${name}`}>
<Link href={filterLink}>
<a>{handle ? `@${handle}` : "handle1234"}</a>
</Link>
</div>
Expand Down
8 changes: 6 additions & 2 deletions components/organisms/InsightPage/InsightPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ interface InsightPageProps {
const InsightPage = ({ edit, insight, pageRepos }: InsightPageProps) => {
const { user, sessionToken } = useSupabaseAuth();
const router = useRouter();
let receivedData = [];
if(router.query.selectedRepos) {
receivedData = JSON.parse(router.query.selectedRepos as string);
}
const username: string = user?.user_metadata.user_name;
const [name, setName] = useState(insight?.name || "");
const [nameError, setNameError] = useState("");
const [submitted, setSubmitted] = useState(false);
const [repoToAdd, setRepoToAdd] = useState("");
const [repos, setRepos] = useState<DbRepo[]>([]);
const [repos, setRepos] = useState<DbRepo[]>(receivedData);
const [repoHistory, setRepoHistory] = useState<DbRepo[]>([]);
const [addRepoError, setAddRepoError] = useState<RepoLookupError>(RepoLookupError.Initial);
const [isPublic, setIsPublic] = useState(!!insight?.is_public);
Expand Down Expand Up @@ -125,7 +129,7 @@ const InsightPage = ({ edit, insight, pageRepos }: InsightPageProps) => {
}

setSubmitted(false);
};
};

const handleOnRepoChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setRepoToAdd(event.target.value);
Expand Down
52 changes: 49 additions & 3 deletions components/organisms/Repositories/repositories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import clsx from "clsx";

Expand All @@ -11,16 +11,18 @@ import TableHeader from "components/molecules/TableHeader/table-header";
import { useRepositoriesList } from "lib/hooks/useRepositoriesList";
import useSupabaseAuth from "lib/hooks/useSupabaseAuth";

import RepositoriesTable, { classNames } from "../RepositoriesTable/repositories-table";
import RepositoriesTable, { classNames, RepositoriesRows } from "../RepositoriesTable/repositories-table";
import RepoNotIndexed from "./repository-not-indexed";
import Checkbox from "components/atoms/Checkbox/checkbox";
import Button from "components/atoms/Button/button";
import useStore from "lib/store";

interface RepositoriesProps {
repositories?: number[];
}

const Repositories = ({ repositories }: RepositoriesProps): JSX.Element => {
const { user } = useSupabaseAuth();
const { user, signIn } = useSupabaseAuth();
const router = useRouter();
const { filterName, toolName, selectedFilter, userOrg } = router.query;
const username = userOrg ? user?.user_metadata.user_name : undefined;
Expand All @@ -37,6 +39,34 @@ const Repositories = ({ repositories }: RepositoriesProps): JSX.Element => {
setLimit
} = useRepositoriesList(false, repositories);
const filteredRepoNotIndexed = selectedFilter && !repoListIsLoading && !repoListIsError && repoListData.length === 0;
const [selectedRepos, setSelectedRepos] = useState<DbRepo[]>([]);

const handleOnSelectAllChecked = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.checked) {
setSelectedRepos(repoListData);
} else {
setSelectedRepos([]);
}
};

const handleOnAddtoInsights = () => {
if(user) {
router.push({pathname: "/hub/insights/new", query: {selectedRepos: JSON.stringify(selectedRepos)}}, "/hub/insights/new" );
} else {
signIn({ provider: "github" });
nightknighto marked this conversation as resolved.
Show resolved Hide resolved
}
};

const handleOnSelectRepo = (repo: RepositoriesRows) => {
const matchingRepo = repoListData.find(iteratedRepo => iteratedRepo.id === repo.id);
if(!matchingRepo) return;

if (selectedRepos.find((r) => r.id === matchingRepo.id)) {
setSelectedRepos(selectedRepos.filter(r => r.id !== repo.id));
} else {
setSelectedRepos([...selectedRepos, matchingRepo]);
}
};

const handleOnSearch = (search?: string) => {
if (search && /^[a-zA-Z0-9\-\.]+\/[a-zA-Z0-9\-\.]+$/.test(search)) {
Expand Down Expand Up @@ -75,6 +105,9 @@ const Repositories = ({ repositories }: RepositoriesProps): JSX.Element => {
</div>
</div>
<div className="hidden md:flex py-4 px-6 bg-light-slate-3 gap-2">
<div className={clsx(classNames.cols.checkbox)} >
<Checkbox label="" onChange={handleOnSelectAllChecked} disabled={!user} title={!user? "Connect to GitHub" : ""} className={`checked:[&>*]:!bg-orange-500 ${ user? "[&>*]:!border-orange-500 [&>*]:hover:!bg-orange-600": "[&>*]:!border-light-slate-8"}`}/>
</div>
<div className={clsx(classNames.cols.repository)}>
<TableTitle text="Repository"></TableTitle>
</div>
Expand All @@ -98,13 +131,26 @@ const Repositories = ({ repositories }: RepositoriesProps): JSX.Element => {
</div>
</div>

{
selectedRepos.length > 0 && (
<div className="p-3 px-6 border-b-2 text-light-slate-11 flex justify-between">
<div>
{selectedRepos.length} Repositories selected
</div>
<Button onClick={handleOnAddtoInsights} type="primary">Add to Insight Page</Button>
</div>
)
}

<RepositoriesTable
topic={topic}
error={repoListIsError}
loading={repoListIsLoading}
listOfRepositories={repoListData}
user={username}
repo={selectedFilter}
selectedRepos={selectedRepos}
handleOnSelectRepo={handleOnSelectRepo}
/>

{/* Table Footer */}
Expand Down
19 changes: 14 additions & 5 deletions components/organisms/RepositoriesTable/repositories-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ interface RepositoriesTableProps {
loading: boolean;
error: boolean;
repo?: string | string[] | undefined[];
selectedRepos: RepositoriesRows[];
handleOnSelectRepo: (repo: RepositoriesRows) => void;
}

export const classNames = {
row: "hidden md:flex gap-4 items-center py-3 px-6 odd:bg-white even:bg-light-slate-2",
cols: {
checkbox: "w-9 mr-0.5",
repository: "w-[30%] lg:flex-1 lg:min-w-[200px] ",
activity: "flex-1 lg:min-w-[100px] flex ",
prOverview: "flex-1 lg:min-w-[170px] ",
Expand All @@ -68,7 +71,9 @@ const RepositoriesTable = ({
error,
topic,
user,
repo
repo,
selectedRepos,
handleOnSelectRepo
}: RepositoriesTableProps): JSX.Element => {
const isLoadedWithRepos = !loading && !error && Array.isArray(listOfRepositories) && listOfRepositories.length > 0;
const isFilteredRepoNotIndexed =
Expand All @@ -81,9 +86,12 @@ const RepositoriesTable = ({
{error && <>An error has occured...</>}

{isLoadedWithRepos &&
listOfRepositories.map((item, index) => (
<RepoRow key={`${item.handle}/${item.name}/${index}`} topic={topic} repo={item} user={user} />
))}
listOfRepositories.map((item, index) => {
const isSelected = selectedRepos.find(iteratedRepo => iteratedRepo.id == item.id) != undefined;
return (
<RepoRow key={`${item.handle}/${item.name}/${index}`} topic={topic} repo={item} userPage={user} selected={isSelected} handleOnSelectRepo={handleOnSelectRepo} />
);
})}
{isFilteredRepoNotIndexed && (
<RepoRow
topic={topic}
Expand All @@ -96,7 +104,8 @@ const RepositoriesTable = ({
// eslint-disable-next-line camelcase
owner_avatar: getAvatarLink(repoOwner as string)
}}
user={user}
userPage={user}
handleOnSelectRepo={handleOnSelectRepo}
/>
)}
</section>
Expand Down